View Javadoc

1   /***
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.xbean.spring.context.impl;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.beans.PropertyEditorManager;
23  
24  /***
25   * A helper method to register some custom editors
26   * 
27   * @version $Revision: 1.1 $
28   */
29  public class PropertyEditorHelper {
30      private static final Log log = LogFactory.getLog(PropertyEditorHelper.class);
31  
32      public static void registerCustomEditors() {
33          registerEditor("java.io.File", "org.apache.xbean.spring.context.impl.FileEditor");
34          registerEditor("java.net.URI", "org.apache.xbean.spring.context.impl.URIEditor");
35          registerEditor("java.util.Date", "org.apache.xbean.spring.context.impl.DateEditor");
36          registerEditor("javax.management.ObjectName", "org.apache.xbean.spring.context.impl.ObjectNameEditor");
37      }
38  
39      protected static void registerEditor(String typeName, String editorName) {
40          Class type = loadClass(typeName);
41          Class editor = loadClass(editorName);
42          if (type != null && editor != null) {
43              PropertyEditorManager.registerEditor(type, editor);
44          }
45      }
46  
47      public static void unregisterCustomEditors() {
48          unregisterEditor("java.io.File");
49          unregisterEditor("java.net.URI");
50          unregisterEditor("java.util.Date");
51          unregisterEditor("javax.management.ObjectName");
52      }
53  
54      protected static void unregisterEditor(String typeName) {
55          Class type = loadClass(typeName);
56          if (type != null) {
57              PropertyEditorManager.registerEditor(type, null);
58          }
59      }
60      public static Class loadClass(String name) {
61          ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
62          if (contextClassLoader != null) {
63              try {
64                  return contextClassLoader.loadClass(name);
65              }
66              catch (ClassNotFoundException e) {
67              }
68              catch (NoClassDefFoundError e) {
69              }
70          }
71          try {
72              return PropertyEditorHelper.class.getClassLoader().loadClass(name);
73          }
74          catch (ClassNotFoundException e) {
75              log.debug("Could not find class: " + name + " on the classpath");
76              return null;
77          }
78          catch (NoClassDefFoundError e) {
79              log.debug("Could not load class: " + name + " on the classpath. " + e.getMessage());
80              return null;
81          }
82      }
83  
84  }