1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.osgi.context.support;
18
19 import java.beans.PropertyEditor;
20 import java.io.IOException;
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.beans.BeanUtils;
29 import org.springframework.beans.PropertyEditorRegistrar;
30 import org.springframework.beans.PropertyEditorRegistry;
31 import org.springframework.osgi.context.BundleContextAware;
32 import org.springframework.util.Assert;
33
34 /**
35 * Class that registers {@link PropertyEditor}s, useful inside an OSGi
36 * application context.
37 *
38 * As this class is used for bootstrapping and is likely to draw classes from
39 * various packages that already depend on {@link BundleContextAware}, the
40 * configuration has been externalized to avoid package cycles.
41 *
42 * @author Costin Leau
43 */
44 class OsgiPropertyEditorRegistrar implements PropertyEditorRegistrar {
45
46 private static final Log log = LogFactory.getLog(OsgiPropertyEditorRegistrar.class);
47
48 private static final String PROPERTIES_FILE = "/org/springframework/osgi/context/support/internal/default-property-editors.properties";
49
50 private final Map editors;
51
52
53 OsgiPropertyEditorRegistrar() {
54 this(OsgiPropertyEditorRegistrar.class.getClassLoader());
55 }
56
57 OsgiPropertyEditorRegistrar(ClassLoader classLoader) {
58
59 Properties editorsConfig = new Properties();
60 try {
61 editorsConfig.load(getClass().getResourceAsStream(PROPERTIES_FILE));
62 }
63 catch (IOException ex) {
64 throw (RuntimeException) new IllegalStateException(
65 "cannot load default propertiy editorsConfig configuration").initCause(ex);
66 }
67
68 if (log.isTraceEnabled())
69 log.trace("Loaded property editors configuration " + editorsConfig);
70 editors = new LinkedHashMap(editorsConfig.size());
71
72 createEditors(classLoader, editorsConfig);
73 }
74
75 private void createEditors(ClassLoader classLoader, Properties configuration) {
76
77 boolean trace = log.isTraceEnabled();
78
79 for (Iterator iterator = configuration.entrySet().iterator(); iterator.hasNext();) {
80 Map.Entry entry = (Map.Entry) iterator.next();
81
82 Class key;
83
84 Class editorClass;
85 try {
86 key = classLoader.loadClass((String) entry.getKey());
87 editorClass = classLoader.loadClass((String) entry.getValue());
88 }
89 catch (ClassNotFoundException ex) {
90 throw (RuntimeException) new IllegalArgumentException("Cannot load class").initCause(ex);
91 }
92
93 Assert.isAssignable(PropertyEditor.class, editorClass);
94
95 if (trace)
96 log.trace("Adding property editor[" + editorClass + "] for type[" + key + "]");
97 editors.put(key, editorClass);
98 }
99 }
100
101 public void registerCustomEditors(PropertyEditorRegistry registry) {
102 for (Iterator iterator = editors.entrySet().iterator(); iterator.hasNext();) {
103 Map.Entry editor = (Map.Entry) iterator.next();
104 Class type = (Class) editor.getKey();
105 PropertyEditor editorInstance;
106 editorInstance = (PropertyEditor) BeanUtils.instantiateClass(((Class) editor.getValue()));
107 registry.registerCustomEditor(type, editorInstance);
108 }
109 }
110 }