1 7 package org.jboss.cache; 8 9 import org.jboss.logging.Logger; 10 import org.jboss.util.propertyeditor.ElementEditor; 11 import org.jboss.util.propertyeditor.PropertyEditors; 12 import org.w3c.dom.*; 13 import org.w3c.dom.Node ; 14 import org.xml.sax.InputSource ; 15 import org.xml.sax.SAXException ; 16 import org.xml.sax.SAXParseException ; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import java.beans.PropertyEditor ; 21 import java.beans.PropertyEditorManager ; 22 import java.io.InputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 29 50 public class PropertyConfigurator { 51 private static Logger logger_=Logger.getLogger(PropertyConfigurator.class); 52 private Object objToConfigure_; 53 private static final String ROOT="mbean"; 54 private static final String ATTR="attribute"; 55 private static final String SUB_ATTR="config"; 56 private static final String NAME="name"; 57 58 public PropertyConfigurator() { 59 try { 61 Class clasz=Class.forName("org.w3c.dom.Element"); 62 Class elementClasz=Class.forName("org.jboss.util.propertyeditor.ElementEditor"); 63 try { 64 PropertyEditor editor = PropertyEditors.getEditor(clasz); logger_.info("Found existing property editor for org.w3c.dom.Element: " +editor); 66 } catch (RuntimeException ex) { 67 logger_.info("Property editor for org.w3c.dom.Element does not exist." + 68 " Will register org.jboss.util.propertyeditor.ElementEditor"); 69 PropertyEditors.registerEditor(clasz, elementClasz); 70 } 71 } 72 catch(ClassNotFoundException ex) { 73 logger_.error("Class not found for either org.w3c.dom.Element or " + 74 "org.jboss.util.propertyeditor.ElementEditor"); 75 } 76 } 77 78 protected Element loadDocument(String location) throws ConfigureException { 79 URL url=null; 80 try { 81 url=new URL ("file", "", 80, location); 83 logger_.debug("URL location is " + url.toString()); 84 return loadDocument(url.openStream()); 85 } 86 catch(java.net.MalformedURLException mfx) { 87 logger_.error("Configurator error: " + mfx); 88 mfx.printStackTrace(); 89 } 90 catch(java.io.IOException e) { 91 logger_.error("Configurator error: " + e); 92 e.printStackTrace(); 93 } 94 return null; 95 } 96 97 protected Element loadDocument(InputStream is) throws ConfigureException { 98 Document doc=null; 99 try { 100 InputSource xmlInp=new InputSource (is); 101 102 DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance(); 103 DocumentBuilder parser=docBuilderFactory.newDocumentBuilder(); 104 doc=parser.parse(xmlInp); 105 Element root=doc.getDocumentElement(); 106 root.normalize(); 107 return root; 108 } 109 catch(SAXParseException err) { 110 logger_.error("Configurator SAXParse error: " + err.getMessage()); 111 err.printStackTrace(); 112 } 113 catch(SAXException e) { 114 logger_.error("Configurator SAX error: " + e); 115 e.printStackTrace(); 116 } 117 catch(Exception pce) { 118 logger_.error("Configurator general error: " + pce); 119 pce.printStackTrace(); 120 } 121 return null; 122 } 123 124 protected Element getMBeanElement(Element root) throws ConfigureException { 125 NodeList list=root.getElementsByTagName(ROOT); 127 if(list == null) 128 throw new ConfigureException("Can't find " + ROOT + " tag"); 129 130 if(list.getLength() > 1) 131 throw new ConfigureException("Has multiple " + ROOT + " tag"); 132 133 Node node=list.item(0); 134 Element element=null; 135 if(node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 136 element=(Element)node; 137 } 138 else { 139 throw new ConfigureException("Can't find " + ROOT + " element"); 140 } 141 return element; 142 } 143 144 151 public void configure(Object objToConfigure, String configFile) throws ConfigureException { 152 ClassLoader cl=Thread.currentThread().getContextClassLoader(); 154 InputStream is=cl.getResourceAsStream(configFile); 155 if(is == null) { 156 try { 157 is=new FileInputStream (configFile); 158 } 159 catch(FileNotFoundException e) { 160 e.printStackTrace(); 161 } 162 } 163 if(is == null) 164 throw new ConfigureException("could not find resource " + configFile); 165 configure(objToConfigure, is); 166 } 167 168 175 public void configure(Object objToConfigure, InputStream is) throws ConfigureException { 176 objToConfigure_=objToConfigure; 177 178 if(is == null) 179 throw new ConfigureException("configure(): input stream is null for property xml"); 180 181 Element root=loadDocument(is); 182 Element mbeanElement=getMBeanElement(root); 183 NodeList list=mbeanElement.getElementsByTagName(ATTR); 184 logger_.info("configure(): attribute size: " + list.getLength()); 185 186 Class objClass=objToConfigure_.getClass(); 187 Method [] methods=objClass.getMethods(); 188 Class [] string_sig=new Class []{String .class}; 189 190 for(int loop=0; loop < list.getLength(); loop++) { 192 Node node=list.item(loop); 193 if(node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) 194 continue; 195 196 Element element=(Element)node; 197 String name=element.getAttribute(NAME); 198 String valueStr=getElementContent(element, true); 199 Element valueObj=null; 200 if(valueStr.length() == 0) { valueObj=getSubElementObject(element); 202 } 203 String methodName="set" + name; 204 Object value=null; 205 Method method=null; 206 Object [] args; 207 208 209 try { 211 method=objClass.getMethod(methodName, string_sig); 212 } 213 catch(Exception e) { 214 ; 215 } 216 217 if(method != null) { 218 try { 219 args=new Object []{valueStr}; 220 logger_.debug("setting attribute " + name + " to " + valueStr); 221 method.invoke(objToConfigure_, new Object []{valueStr}); 222 continue; 223 } 224 catch(Exception ex) { 225 ex.printStackTrace(); 226 throw new ConfigureException("configure(): can't invoke " + methodName + " to configure " + 227 "TreeCache properties. Exception: " + ex); 228 } 229 } 230 231 232 for(int i=0; i < methods.length; i++) { 235 237 if(methodName.equals(methods[i].getName())) { 238 method=methods[i]; 239 Class [] clz=method.getParameterTypes(); if(clz.length != 1) 241 throw new ConfigureException("Parameter size of " + methodName + 242 " is not 1 but " + clz.length); 243 244 Class classParam=clz[0]; 245 PropertyEditor editor=PropertyEditorManager.findEditor(classParam); 248 if(editor == null) { 249 String str="Could not find PropertyEditor for type class " + 250 classParam; 251 throw new ConfigureException(str); 252 } 253 254 if(valueObj != null) { editor.setValue(valueObj); 256 } 257 else { 258 editor.setAsText(valueStr); 259 } 260 261 value=editor.getValue(); 262 logger_.debug("Invoking setter method: " + method + 263 " with parameter \"" + value + "\" of type " + value.getClass()); 264 265 try { 267 method.invoke(objToConfigure_, new Object []{value}); 268 } 269 catch(Exception ex) { 270 ex.printStackTrace(); 271 throw new ConfigureException("configure(): Can't invoke set method to configure " + 272 "TreeCache properties. Method name: " + methodName + " exception: " + ex); 273 } 274 } 275 } 276 } 277 } 278 279 private Element getSubElementObject(Element element) 280 throws ConfigureException { 281 282 NodeList nl=element.getChildNodes(); 284 for(int i=0; i < nl.getLength(); i++) { 285 Node node=nl.item(i); 286 if(node.getNodeType() == Node.ELEMENT_NODE && 287 SUB_ATTR.equals( ((Element)node).getTagName() ) ) { 288 return (Element)node; 289 } 290 } 291 292 logger_.debug("getSubElementObject(): " + 293 "element object. Does not exist for " + SUB_ATTR); 294 295 return null; 296 } 297 298 private String getElementContent(Element element, boolean trim) 299 throws ConfigureException { 300 NodeList nl=element.getChildNodes(); 301 String attributeText=""; 302 for(int i=0; i < nl.getLength(); i++) { 303 Node n=nl.item(i); 304 if(n instanceof Text) { 305 attributeText+=((Text)n).getData(); 306 } 307 } if(trim) 309 attributeText=attributeText.trim(); 310 return attributeText; 311 } 312 313 public static void main(String [] args) { 314 try { 315 TreeCache cache=new TreeCache(); 316 PropertyConfigurator config=new PropertyConfigurator(); 317 config.configure(cache, "tree-cache.xml"); 318 } 319 catch(Exception ex) { 320 ex.printStackTrace(); 321 } 322 } 323 } 324 | Popular Tags |