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:
018: package java.beans;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: public class PropertyEditorManager {
024:
025: private static String[] path = { "org.apache.harmony.beans.editors" }; //$NON-NLS-1$
026:
027: private static final Map<Class<?>, Class<?>> registeredEditors = new HashMap<Class<?>, Class<?>>();
028:
029: public PropertyEditorManager() {
030: // expected
031: }
032:
033: public static void registerEditor(Class<?> targetType,
034: Class<?> editorClass) {
035: if (targetType == null) {
036: throw new NullPointerException();
037: }
038: SecurityManager sm = System.getSecurityManager();
039:
040: if (sm != null) {
041: sm.checkPropertiesAccess();
042: }
043:
044: if (editorClass != null) {
045: registeredEditors.put(targetType, editorClass);
046: } else {
047: registeredEditors.remove(targetType);
048: }
049: }
050:
051: private static PropertyEditor loadEditor(Class<?> targetType,
052: String className) throws ClassNotFoundException,
053: IllegalAccessException, InstantiationException {
054: ClassLoader loader = targetType.getClassLoader();
055: Class<?> editorClass = null;
056:
057: try {
058: if (loader != null) {
059: editorClass = loader.loadClass(className);
060: } else {
061: loader = ClassLoader.getSystemClassLoader();
062:
063: if (loader != null) {
064: editorClass = loader.loadClass(className);
065: }
066: }
067: } catch (ClassNotFoundException e) {
068: // expected
069: }
070:
071: if (editorClass == null) {
072: loader = Thread.currentThread().getContextClassLoader();
073: editorClass = loader.loadClass(className);
074: }
075: return (PropertyEditor) editorClass.newInstance();
076: }
077:
078: public static synchronized PropertyEditor findEditor(
079: Class<?> targetType) {
080: if (targetType == null) {
081: throw new NullPointerException();
082: }
083: Class<?> editorClass = registeredEditors.get(targetType);
084:
085: if (editorClass != null) {
086: try {
087: return (PropertyEditor) editorClass.newInstance();
088: } catch (Exception e) {
089: // expected
090: }
091: }
092: String editorClassName = targetType.getName() + "Editor"; //$NON-NLS-1$
093:
094: try {
095: return loadEditor(targetType, editorClassName);
096: } catch (Exception exception) {
097: // expected
098: }
099: String shortEditorClassName = (targetType.isPrimitive() ? (editorClassName
100: .substring(0, 1).toUpperCase() + editorClassName
101: .substring(1))
102: : editorClassName.substring(editorClassName
103: .lastIndexOf('.') + 1));
104:
105: for (String element : path) {
106: try {
107: editorClassName = element + '.' + shortEditorClassName;
108: return loadEditor(targetType, editorClassName);
109: } catch (Exception e) {
110: // expected
111: }
112: }
113: return null;
114: }
115:
116: public static void setEditorSearchPath(String[] apath) {
117: SecurityManager sm = System.getSecurityManager();
118: if (sm != null) {
119: sm.checkPropertiesAccess();
120: }
121: synchronized (PropertyEditorManager.class) {
122: path = (apath == null) ? new String[0] : apath;
123: }
124: }
125:
126: public static synchronized String[] getEditorSearchPath() {
127: return path.clone();
128: }
129: }
|