1 22 package org.jboss.mx.metadata; 23 24 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.Map ; 29 30 import javax.management.MBeanInfo ; 31 import javax.management.NotCompliantMBeanException ; 32 33 import org.dom4j.Document; 34 import org.dom4j.DocumentException; 35 import org.dom4j.DocumentType; 36 import org.dom4j.Element; 37 import org.dom4j.io.SAXReader; 38 import org.jboss.mx.modelmbean.XMBeanConstants; 39 import org.jboss.mx.service.ServiceConstants; 40 import org.jboss.mx.util.JBossNotCompliantMBeanException; 41 import org.jboss.util.xml.JBossEntityResolver; 42 import org.xml.sax.SAXException ; 43 44 53 public class XMLMetaData 54 extends AbstractBuilder 55 implements ServiceConstants, XMBeanConstants 56 { 57 58 private static final int NO_VERSION = -1; 60 private static final int JBOSS_XMBEAN_1_0 = 0; 61 private static final int JBOSS_XMBEAN_1_1 = 1; 62 private static final int JBOSS_XMBEAN_1_2 = 2; 63 64 67 private URL url = null; 68 69 private Element element; 70 71 private String versionString; 72 73 76 private String resourceClassName = null; 77 78 81 private String mmbClassName = null; 82 83 84 86 96 public XMLMetaData(String mmbClassName, String resourceClassName, URL url) 97 { 98 super(); 99 100 this.url = url; 101 this.mmbClassName = mmbClassName; 102 this.resourceClassName = resourceClassName; 103 } 104 105 117 public XMLMetaData(String mmbClassName, String resourceClassName, String url) 118 throws MalformedURLException 119 { 120 this(mmbClassName, resourceClassName, new URL (url)); 121 } 122 123 137 public XMLMetaData(String mmbClassName, String resourceClassName, URL url, Map properties) 138 { 139 this(mmbClassName, resourceClassName, url); 140 setProperties(properties); 141 } 142 143 159 public XMLMetaData(String mmbClassName, String resourceClassName, 160 String url, Map properties) throws MalformedURLException 161 { 162 this(mmbClassName, resourceClassName, new URL (url), properties); 163 } 164 165 175 public XMLMetaData(String mmbClassName, String resourceClassName, Element element, String version) 176 { 177 super(); 178 this.mmbClassName = mmbClassName; 179 this.resourceClassName = resourceClassName; 180 this.element = element; 181 versionString = version; 182 } 183 184 185 211 public MBeanInfo build() throws NotCompliantMBeanException 212 { 213 try 214 { 215 int version = NO_VERSION; 216 217 if (versionString == null) 218 { 219 SAXReader reader = new SAXReader(); 221 222 if (properties.get(SAX_PARSER) != null) 224 { 225 try 226 { 227 reader.setXMLReaderClassName(getStringProperty(SAX_PARSER)); 228 } 229 230 catch (SAXException e) 231 { 232 } } 235 reader.setValidation(true); 237 238 try 240 { 241 boolean validate = getBooleanProperty(XML_VALIDATION); 242 reader.setValidation(validate); 243 } 244 catch (IllegalPropertyException e) 245 { 246 248 } 250 251 reader.setEntityResolver(new JBossEntityResolver()); 253 254 Document doc = reader.read(url); 256 element = doc.getRootElement(); 257 DocumentType type = doc.getDocType(); 258 259 version = validateVersionString(type.getPublicID()); 260 if (version == NO_VERSION) 261 { 262 version = validateVersionString(type.getSystemID()); 263 } 265 } 266 else 267 { 268 version = validateVersionString(versionString); 269 } 271 if (element == null) 272 { 273 throw new IllegalStateException ("No element supplied with explict version!"); 274 } 275 if (version == JBOSS_XMBEAN_1_0 || 278 version == JBOSS_XMBEAN_1_1 || 279 version == JBOSS_XMBEAN_1_2) 280 { 281 return new JBossXMBean10(mmbClassName, resourceClassName, element, properties).build(); 283 } 284 else 285 { 286 throw new NotCompliantMBeanException ("Unknown xmbean type " + versionString); 287 } 289 } 290 catch (DocumentException e) 291 { 292 throw new JBossNotCompliantMBeanException("Error parsing the XML file, from XMLMetaData: ", e); 293 } 294 } 295 296 private int validateVersionString(String versionString) 297 { 298 if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_0.equals(versionString)) 299 { 300 return JBOSS_XMBEAN_1_0; 301 } if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_0)) 303 { 304 return JBOSS_XMBEAN_1_0; 305 } if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_1.equals(versionString)) 307 { 308 return JBOSS_XMBEAN_1_1; 309 } if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_1)) 311 { 312 return JBOSS_XMBEAN_1_1; 313 } if (PUBLIC_JBOSSMX_XMBEAN_DTD_1_2.equals(versionString)) 315 { 316 return JBOSS_XMBEAN_1_2; 317 } if (versionString != null && versionString.endsWith(JBOSSMX_XMBEAN_DTD_1_2)) 319 { 320 return JBOSS_XMBEAN_1_2; 321 } 323 return NO_VERSION; 325 } 326 327 } 328 | Popular Tags |