View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  		// load properties
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  			// key represents type
82  			Class key;
83  			// value represents property editor
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 }