1 22 package org.jboss.metadata; 23 24 import org.jboss.deployment.DeploymentException; 25 import org.jboss.logging.Logger; 26 import org.jboss.util.xml.JBossEntityResolver; 27 import org.w3c.dom.Document ; 28 import org.xml.sax.ErrorHandler ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.SAXParseException ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.net.URL ; 38 import java.net.URLClassLoader ; 39 40 53 public class XmlFileLoader 54 { 55 57 private static boolean defaultValidateDTDs = false; 59 private static Logger log = Logger.getLogger(XmlFileLoader.class); 60 private URLClassLoader classLoader; 61 private ApplicationMetaData metaData; 62 private boolean validateDTDs; 63 64 public static boolean getDefaultValidateDTDs() 66 { 67 return defaultValidateDTDs; 68 } 69 70 public static void setDefaultValidateDTDs(boolean validate) 71 { 72 defaultValidateDTDs = validate; 73 } 74 75 76 public XmlFileLoader() 78 { 79 this(defaultValidateDTDs); 80 } 81 82 public XmlFileLoader(boolean validateDTDs) 83 { 84 this.validateDTDs = validateDTDs; 85 } 86 87 public ApplicationMetaData getMetaData() 89 { 90 return metaData; 91 } 92 93 96 public void setClassLoader(URLClassLoader cl) 97 { 98 classLoader = cl; 99 } 100 101 106 public ClassLoader getClassLoader() 107 { 108 return classLoader; 109 } 110 111 112 116 public boolean getValidateDTDs() 117 { 118 return validateDTDs; 119 } 120 121 125 public void setValidateDTDs(boolean validate) 126 { 127 this.validateDTDs = validate; 128 } 129 130 140 public ApplicationMetaData load(URL alternativeDD) throws Exception 141 { 142 URL ejbjarUrl = null; 143 if (alternativeDD != null) 144 { 145 log.debug("Using alternativeDD: " + alternativeDD); 146 ejbjarUrl = alternativeDD; 147 } 148 else 149 { 150 ejbjarUrl = getClassLoader().getResource("META-INF/ejb-jar.xml"); 151 } 152 153 if (ejbjarUrl == null) 154 { 155 throw new DeploymentException("no ejb-jar.xml found"); 156 } 157 158 metaData = new ApplicationMetaData(); 160 metaData.setResourceClassLoader(classLoader); 161 162 Document ejbjarDocument = getDocumentFromURL(ejbjarUrl); 163 164 metaData.setUrl(ejbjarUrl); 166 metaData.importEjbJarXml(ejbjarDocument.getDocumentElement()); 167 168 URL defaultJbossUrl = Thread.currentThread().getContextClassLoader().getResource("standardjboss.xml"); 173 if (defaultJbossUrl == null) 174 { 175 throw new DeploymentException("no standardjboss.xml found"); 176 } 177 178 Document defaultJbossDocument = null; 179 try 180 { 181 defaultJbossDocument = getDocumentFromURL(defaultJbossUrl); 182 metaData.setUrl(defaultJbossUrl); 183 metaData.importJbossXml(defaultJbossDocument.getDocumentElement()); 184 } 185 catch (Exception ex) 186 { 187 log.error("failed to load standardjboss.xml. There could be a syntax error.", ex); 188 throw ex; 189 } 190 191 try 194 { 195 URL jbossUrl = getClassLoader().getResource("META-INF/jboss.xml"); 196 if (jbossUrl != null) 197 { 198 Document jbossDocument = getDocumentFromURL(jbossUrl); 199 metaData.setUrl(jbossUrl); 200 metaData.importJbossXml(jbossDocument.getDocumentElement()); 201 } 202 } 203 catch (Exception ex) 204 { 205 log.error("failed to load jboss.xml. There could be a syntax error.", ex); 206 throw ex; 207 } 208 209 return metaData; 210 } 211 212 215 public static Document getDocument(URL url) throws DeploymentException 216 { 217 return getDocument(url, defaultValidateDTDs); 218 } 219 220 225 public static Document getDocument(URL url, boolean validateDTDs) throws DeploymentException 226 { 227 XmlFileLoader loader = new XmlFileLoader(validateDTDs); 228 return loader.getDocumentFromURL(url); 229 } 230 231 238 public Document getDocumentFromURL(URL url) throws DeploymentException 239 { 240 InputStream is = null; 241 try 242 { 243 is = url.openStream(); 244 return getDocument(is, url.toExternalForm()); 245 } 246 catch (IOException e) 247 { 248 throw new DeploymentException("Failed to obtain xml doc from URL", e); 249 } 250 } 251 252 264 public Document getDocument(InputStream is, String inPath) 265 throws DeploymentException 266 { 267 InputSource is2 = new InputSource (is); 268 is2.setSystemId(inPath); 269 Document doc = null; 270 try 271 { 272 doc = getDocument(is2, inPath); 273 } 274 finally 275 { 276 try 279 { 280 if( is != null ) 281 is.close(); 282 } 283 catch (Exception e) 284 { 285 } 287 } 288 return doc; 289 } 290 291 303 public Document getDocument(InputSource is, String inPath) 304 throws DeploymentException 305 { 306 try 307 { 308 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 309 310 docBuilderFactory.setValidating(validateDTDs); 312 docBuilderFactory.setNamespaceAware(true); 316 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 323 JBossEntityResolver lr = new JBossEntityResolver(); 324 LocalErrorHandler eh = new LocalErrorHandler( inPath, lr ); 325 docBuilder.setEntityResolver(lr); 326 docBuilder.setErrorHandler(eh ); 327 328 Document doc = docBuilder.parse(is); 329 if(validateDTDs && eh.hadError()) 330 { 331 throw new DeploymentException("Invalid XML: file=" + inPath, eh.getException()); 332 } 333 return doc; 334 } 335 catch (DeploymentException e) 336 { 337 throw e; 338 } 339 catch (SAXParseException e) 340 { 341 String msg = "Invalid XML: file=" + inPath+"@"+e.getColumnNumber()+":"+e.getLineNumber(); 342 throw new DeploymentException(msg, e); 343 } 344 catch (SAXException e) 345 { 346 throw new DeploymentException("Invalid XML: file=" + inPath, e); 347 } 348 catch (Exception e) 349 { 350 throw new DeploymentException("Invalid XML: file=" + inPath, e); 351 } 352 } 353 354 356 358 360 362 369 private static class LocalErrorHandler implements ErrorHandler 370 { 371 private String theFileName; 373 private JBossEntityResolver localResolver; 374 private boolean error; 375 private SAXParseException exception; 376 377 public LocalErrorHandler( String inFileName, JBossEntityResolver localResolver ) 378 { 379 this.theFileName = inFileName; 380 this.localResolver = localResolver; 381 this.error = false; 382 } 383 384 public void error(SAXParseException exception) 385 { 386 this.exception = exception; 387 if ( localResolver.isEntityResolved() ) 388 { 389 this.error = true; 390 log.error("XmlFileLoader: File " 391 + theFileName 392 + " process error. Line: " 393 + String.valueOf(exception.getLineNumber()) 394 + ". Error message: " 395 + exception.getMessage() 396 ); 397 } } 399 400 public void fatalError(SAXParseException exception) 401 { 402 this.exception = exception; 403 if ( localResolver.isEntityResolved() ) 404 { 405 this.error = true; 406 log.error("XmlFileLoader: File " 407 + theFileName 408 + " process fatal error. Line: " 409 + String.valueOf(exception.getLineNumber()) 410 + ". Error message: " 411 + exception.getMessage() 412 ); 413 } } 415 416 public void warning(SAXParseException exception) 417 { 418 this.exception = exception; 419 if ( localResolver.isEntityResolved() ) 420 { 421 this.error = true; 422 log.error("XmlFileLoader: File " 423 + theFileName 424 + " process warning. Line: " 425 + String.valueOf(exception.getLineNumber()) 426 + ". Error message: " 427 + exception.getMessage() 428 ); 429 } } 431 432 public SAXParseException getException() 433 { 434 return exception; 435 } 436 437 public boolean hadError() 438 { 439 return error; 440 } 441 }} 443 | Popular Tags |