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 void unregisterCustomEditors() {
048 unregisterEditor("java.io.File");
049 unregisterEditor("java.net.URI");
050 unregisterEditor("java.util.Date");
051 unregisterEditor("javax.management.ObjectName");
052 }
053
054 protected static void unregisterEditor(String typeName) {
055 Class type = loadClass(typeName);
056 if (type != null) {
057 PropertyEditorManager.registerEditor(type, null);
058 }
059 }
060 public static Class loadClass(String name) {
061 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
062 if (contextClassLoader != null) {
063 try {
064 return contextClassLoader.loadClass(name);
065 }
066 catch (ClassNotFoundException e) {
067 }
068 catch (NoClassDefFoundError e) {
069 }
070 }
071 try {
072 return PropertyEditorHelper.class.getClassLoader().loadClass(name);
073 }
074 catch (ClassNotFoundException e) {
075 log.debug("Could not find class: " + name + " on the classpath");
076 return null;
077 }
078 catch (NoClassDefFoundError e) {
079 log.debug("Could not load class: " + name + " on the classpath. " + e.getMessage());
080 return null;
081 }
082 }
083
084 }