001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.xbean.spring.context.impl; 018 019 import org.apache.commons.logging.Log; 020 import org.apache.commons.logging.LogFactory; 021 022 import java.beans.PropertyEditorManager; 023 024 /** 025 * A helper method to register some custom editors 026 * 027 * @version $Revision: 1.1 $ 028 */ 029 public class PropertyEditorHelper { 030 private static final Log log = LogFactory.getLog(PropertyEditorHelper.class); 031 032 public static void registerCustomEditors() { 033 registerEditor("java.io.File", "org.apache.xbean.spring.context.impl.FileEditor"); 034 registerEditor("java.net.URI", "org.apache.xbean.spring.context.impl.URIEditor"); 035 registerEditor("java.util.Date", "org.apache.xbean.spring.context.impl.DateEditor"); 036 registerEditor("javax.management.ObjectName", "org.apache.xbean.spring.context.impl.ObjectNameEditor"); 037 } 038 039 protected static void registerEditor(String typeName, String editorName) { 040 Class type = loadClass(typeName); 041 Class editor = loadClass(editorName); 042 if (type != null && editor != null) { 043 PropertyEditorManager.registerEditor(type, editor); 044 } 045 } 046 047 public static Class loadClass(String name) { 048 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 049 if (contextClassLoader != null) { 050 try { 051 return contextClassLoader.loadClass(name); 052 } 053 catch (ClassNotFoundException e) { 054 } 055 catch (NoClassDefFoundError e) { 056 } 057 } 058 try { 059 return PropertyEditorHelper.class.getClassLoader().loadClass(name); 060 } 061 catch (ClassNotFoundException e) { 062 log.debug("Could not find class: " + name + " on the classpath"); 063 return null; 064 } 065 catch (NoClassDefFoundError e) { 066 log.debug("Could not load class: " + name + " on the classpath. " + e.getMessage()); 067 return null; 068 } 069 } 070 071 }