1 22 package org.jboss.varia.property; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 35 import javax.management.MBeanServer ; 36 import javax.management.MalformedObjectNameException ; 37 import javax.management.ObjectName ; 38 39 import org.jboss.system.ServiceMBeanSupport; 40 41 47 public class PropertyEditorManagerService extends ServiceMBeanSupport 48 implements PropertyEditorManagerServiceMBean 49 { 50 55 private List registeredEditors = Collections.synchronizedList(new ArrayList ()); 56 57 61 69 public PropertyEditor findEditor(final Class type) 70 { 71 return PropertyEditorManager.findEditor(type); 72 } 73 74 82 public PropertyEditor findEditor(final String typeName) 83 throws ClassNotFoundException 84 { 85 Class type = Class.forName(typeName); 86 87 return PropertyEditorManager.findEditor(type); 88 } 89 90 98 public void registerEditor(final Class type, final Class editorType) 99 { 100 registeredEditors.add(type); 101 PropertyEditorManager.registerEditor(type, editorType); 102 } 103 104 112 public void registerEditor(final String typeName, 113 final String editorTypeName) 114 throws ClassNotFoundException 115 { 116 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 117 Class type = cl.loadClass(typeName); 118 Class editorType = cl.loadClass(editorTypeName); 119 registerEditor(type, editorType); 120 } 121 122 123 private String makeString(final String [] array) 124 { 125 StringBuffer buff = new StringBuffer (); 126 127 for (int i=0; i<array.length; i++) { 128 buff.append(array[i]); 129 if ((i + 1) < array.length) { 130 buff.append(","); 131 } 132 } 133 134 return buff.toString(); 135 } 136 137 138 private String [] makeArray(final String listspec) 139 { 140 StringTokenizer stok = new StringTokenizer (listspec, ","); 141 List list = new ArrayList (); 142 143 while (stok.hasMoreTokens()) { 144 String url = stok.nextToken(); 145 list.add(url); 146 } 147 148 return (String [])list.toArray(new String [list.size()]); 149 } 150 151 158 public String getEditorSearchPath() 159 { 160 return makeString(PropertyEditorManager.getEditorSearchPath()); 161 } 162 163 170 public void setEditorSearchPath(final String path) 171 { 172 PropertyEditorManager.setEditorSearchPath(makeArray(path)); 173 } 174 175 176 180 188 public void setBootstrapEditors(final String propsString) 189 throws ClassNotFoundException , IOException 190 { 191 Properties props = new Properties (); 192 ByteArrayInputStream stream = new ByteArrayInputStream (propsString.getBytes()); 193 props.load(stream); 194 setEditors(props); 195 } 196 197 204 public void setEditors(final Properties props) throws ClassNotFoundException 205 { 206 Iterator iter = props.keySet().iterator(); 207 while (iter.hasNext()) { 208 String typeName = (String )iter.next(); 209 String editorTypeName = props.getProperty(typeName); 210 211 registerEditor(typeName, editorTypeName); 212 } 213 } 214 215 219 public Class [] getRegisteredEditors() 220 { 221 return (Class [])registeredEditors.toArray(new Class [registeredEditors.size()]); 222 } 223 224 228 protected ObjectName getObjectName(final MBeanServer server, final ObjectName name) 229 throws MalformedObjectNameException 230 { 231 return name == null ? OBJECT_NAME : name; 232 } 233 234 237 protected void destroyService() throws Exception 238 { 239 Iterator iter = registeredEditors.iterator(); 240 while (iter.hasNext()) 241 { 242 Class type = (Class )iter.next(); 243 PropertyEditorManager.registerEditor(type, null); 244 iter.remove(); 245 } 246 } 247 248 } 249 | Popular Tags |