1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
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 }