1 17 18 package org.apache.geronimo.common.propertyeditor; 19 20 import java.beans.PropertyEditor ; 21 import java.beans.PropertyEditorManager ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 import org.apache.geronimo.kernel.ClassLoading; 27 28 35 public class PropertyEditors { 36 40 static { 41 appendEditorSearchPath("org.apache.geronimo.common.propertyeditor"); 43 PropertyEditorManager.registerEditor(Boolean .class, BooleanEditor.class); 45 PropertyEditorManager.registerEditor(Integer .class, IntegerEditor.class); 46 } 47 48 55 public static PropertyEditor findEditor(Class type) { 56 if (type == null) { 58 throw new IllegalArgumentException ("type is null"); 59 } 60 61 62 PropertyEditor editor = PropertyEditorManager.findEditor(type); 64 65 if (editor != null) { 67 return editor; 68 } 69 70 if (type.isArray()) { 74 editor = findEditor(type.getComponentType()); 76 if (editor != null) { 79 return new ArrayPropertyEditorAdapter(type.getComponentType(), editor); 80 } 81 } 82 return null; 84 } 85 86 96 public static PropertyEditor findEditor(String typeName, ClassLoader loader) throws ClassNotFoundException { 97 if (typeName == null) { 99 throw new IllegalArgumentException ("typeName is null"); 100 } 101 102 Class type = null; 103 try { 105 type = ClassLoading.loadClass(typeName, loader); 106 } catch (ClassNotFoundException e) { 107 type = ClassLoading.loadClass(typeName + "$PropertyEditor", loader); 111 } 112 113 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); 117 try { 118 Thread.currentThread().setContextClassLoader(loader); 119 return findEditor(type); 121 } finally { 122 Thread.currentThread().setContextClassLoader(oldLoader); 125 } 126 } 127 128 137 public static PropertyEditor getEditor(Class type) { 138 PropertyEditor editor = findEditor(type); 140 if (editor == null) { 142 throw new PropertyEditorException("No property editor for type: " + type); 143 } 144 return editor; 145 } 146 147 153 public static void registerEditor(Class type, Class editorType) { 154 if (type == null) { 156 throw new IllegalArgumentException ("type is null"); 157 } 158 159 if (editorType == null) { 161 throw new IllegalArgumentException ("editorType is null"); 162 } 163 164 PropertyEditorManager.registerEditor(type, editorType); 165 } 166 167 174 public static void registerEditor(String typeName, String editorName) throws ClassNotFoundException { 175 if (typeName == null) { 177 throw new IllegalArgumentException ("typeName is null"); 178 } 179 180 if (editorName == null) { 182 throw new IllegalArgumentException ("editorTypeName is null"); 183 } 184 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 186 187 Class type = ClassLoading.loadClass(typeName, loader); 189 Class editor = ClassLoading.loadClass(editorName, loader); 190 191 registerEditor(type, editor); 193 } 194 195 200 public static List getEditorSearchPath() { 201 204 String [] paths = PropertyEditorManager.getEditorSearchPath(); 207 208 List pathList = new ArrayList (paths.length); 210 211 for (int i = 0; i < paths.length; i++) { 213 pathList.add(paths[i]); 214 } 215 216 return pathList; 217 } 218 219 224 public static void setEditorSearchPath(List path) { 225 if (path == null) { 227 throw new IllegalArgumentException ("path is null"); 228 } 229 230 String [] elements = (String []) path.toArray(new String [path.size()]); 233 PropertyEditorManager.setEditorSearchPath(elements); 234 } 235 236 241 public static void appendEditorSearchPath(List newNames) { 242 if (newNames == null) { 244 throw new IllegalArgumentException ("names is null"); 245 } 246 247 if (newNames.isEmpty()) { 249 return; 250 } 251 252 List currentPath = getEditorSearchPath(); 255 currentPath.addAll(newNames); 256 257 setEditorSearchPath(currentPath); 258 } 259 260 265 public static void appendEditorSearchPath(String [] newNames) { 266 if (newNames == null) { 268 throw new IllegalArgumentException ("names is null"); 269 } 270 271 if (newNames.length != 0) { 273 appendEditorSearchPath(Arrays.asList(newNames)); 275 } 276 } 277 278 283 public static void appendEditorSearchPath(String newName) { 284 if (newName == null) { 286 throw new IllegalArgumentException ("name is null"); 287 } 288 289 List currentPath = getEditorSearchPath(); 292 currentPath.add(newName); 293 294 setEditorSearchPath(currentPath); 295 } 296 } 297 | Popular Tags |