1 19 20 package org.netbeans.modules.j2ee.dd.api.ejb; 21 22 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import org.netbeans.modules.j2ee.dd.impl.ejb.EjbJarProxy; 27 import org.netbeans.modules.j2ee.dd.impl.common.DDProviderDataObject; 28 import org.netbeans.modules.j2ee.dd.impl.common.DDUtils; 29 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 30 import org.netbeans.modules.schema2beans.BaseBean; 31 import org.netbeans.modules.schema2beans.Common; 32 import org.netbeans.modules.xml.api.EncodingUtil; 33 import org.openide.filesystems.FileChangeAdapter; 34 import org.openide.filesystems.FileEvent; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 import org.openide.ErrorManager; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.DocumentType ; 41 import org.xml.sax.EntityResolver ; 42 import org.xml.sax.InputSource ; 43 import org.xml.sax.SAXException ; 44 import org.xml.sax.SAXParseException ; 45 46 import java.io.IOException ; 47 import java.io.InputStreamReader ; 48 import java.io.BufferedInputStream ; 49 import java.math.BigDecimal ; 50 import java.util.Map ; 51 import java.util.HashMap ; 52 import java.net.URL ; 53 import java.util.ArrayList ; 54 import java.util.List ; 55 import java.util.WeakHashMap ; 56 57 62 63 public final class DDProvider { 64 private static final String EJB_21_DOCTYPE = "http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"; private static final String EJB_20_DOCTYPE = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"; private static final String EJB_11_DOCTYPE = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"; private static final DDProvider ddProvider = new DDProvider(); 68 private Map ddMap; 69 private Map <MetadataUnit, EjbJar> annotationDDMap; 70 71 72 private DDProvider() { 73 ddMap = new HashMap (5); 75 annotationDDMap = new WeakHashMap <MetadataUnit, EjbJar>(5); 76 } 77 78 82 public static DDProvider getDefault() { 83 return ddProvider; 84 } 85 86 public EjbJar getMergedDDRoot(MetadataUnit mu) throws IOException { 87 if (mu == null) { 88 return null; 89 } 90 EjbJar xmlRoot = getDDRoot(mu.getDeploymentDescriptor()); 91 if (xmlRoot != null && !xmlRoot.getVersion().equals(new BigDecimal (EjbJar.VERSION_3_0))) { 93 return xmlRoot; 94 } 95 return null; 96 } 97 98 public List <EjbJar> getRoots() { 99 synchronized (this) { 100 return new ArrayList (annotationDDMap.values()); 101 } 102 } 103 104 112 public synchronized EjbJar getDDRoot(FileObject fo) throws java.io.IOException { 113 if (fo == null) { 114 return null; 115 } 116 try { 117 DataObject dataObject = DataObject.find(fo); 118 if(dataObject instanceof DDProviderDataObject){ 119 return getDDRoot0((DDProviderDataObject) dataObject); 120 } 121 } catch (DataObjectNotFoundException e) { 122 return null; } 124 EjbJarProxy ejbJarProxy = null; 125 synchronized (ddMap) { 126 ejbJarProxy = getFromCache(fo); 127 if (ejbJarProxy != null) { 128 return ejbJarProxy; 129 } 130 } 131 132 fo.addFileChangeListener(new DDFileChangeListener()); 133 134 ejbJarProxy = DDUtils.createEjbJarProxy(fo.getInputStream()); 135 putToCache(fo, ejbJarProxy); 136 137 return ejbJarProxy; 138 } 139 140 private synchronized EjbJar getDDRoot0(final DDProviderDataObject ddProviderDataObject) throws java.io.IOException { 141 EjbJarProxy ejbJarProxy = null; 142 synchronized (ddMap) { 143 ejbJarProxy = getFromCache(ddProviderDataObject) ; 144 if (ejbJarProxy == null) { 145 ejbJarProxy = DDUtils.createEjbJarProxy(ddProviderDataObject.createReader()); 146 putToCache(ddProviderDataObject, ejbJarProxy); 147 } 148 } 149 return ejbJarProxy; 150 } 151 152 160 public EjbJar getDDRootCopy(FileObject fo) throws java.io.IOException { 161 return (EjbJar)getDDRoot(fo).clone(); 162 } 163 164 private EjbJarProxy getFromCache (Object o) { 165 174 return (EjbJarProxy) ddMap.get(o); 175 } 176 177 private void putToCache(Object o, EjbJarProxy ejbJarProxy) { 178 ddMap.put(o, (ejbJarProxy)); 179 } 180 181 187 public EjbJar getDDRoot(InputSource inputSource) throws IOException , SAXException { 188 ErrorHandler errorHandler = new ErrorHandler(); 189 DocumentBuilder parser = createParser(errorHandler); 190 parser.setEntityResolver(DDResolver.getInstance()); 191 Document document = parser.parse(inputSource); 192 SAXParseException error = errorHandler.getError(); 193 String version = extractVersion(document); 194 EjbJar original = createEjbJar(version, document); 195 EjbJarProxy ejbJarProxy = new EjbJarProxy(original, version); 196 ejbJarProxy.setError(error); 197 if (error != null) { 198 ejbJarProxy.setStatus(EjbJar.STATE_INVALID_PARSABLE); 199 } else { 200 ejbJarProxy.setStatus(EjbJar.STATE_VALID); 201 } 202 return ejbJarProxy; 203 } 204 205 209 public BaseBean getBaseBean(CommonDDBean bean) { 210 if (bean instanceof BaseBean) { 211 return (BaseBean) bean; 212 } else if (bean instanceof EjbJarProxy) { 213 return (BaseBean) ((EjbJarProxy) bean).getOriginal(); 214 } 215 return null; 216 } 217 218 private static EjbJar createEjbJar(String version, Document document) { 219 if (EjbJar.VERSION_3_0.equals(version)) { 220 return new org.netbeans.modules.j2ee.dd.impl.ejb.model_3_0.EjbJar(document, Common.USE_DEFAULT_VALUES); 221 } else if (EjbJar.VERSION_2_1.equals(version)) { 222 return new org.netbeans.modules.j2ee.dd.impl.ejb.model_2_1.EjbJar(document, Common.USE_DEFAULT_VALUES); 223 } else if (EjbJar.VERSION_2_0.equals(version)) { 224 return new org.netbeans.modules.j2ee.dd.impl.ejb.model_2_0.EjbJar(document, Common.USE_DEFAULT_VALUES); 225 } else { 226 return null; 227 } 228 } 229 230 233 private static String extractVersion(Document document) { 234 String id = null; 236 DocumentType dt = document.getDoctype(); 237 if (dt != null) { 238 id = dt.getPublicId(); 240 } else { 241 String schemaLocation = document.getDocumentElement().getAttribute("xsi:schemaLocation"); 243 if (schemaLocation != null) { 244 id = schemaLocation.substring(schemaLocation.lastIndexOf(" ") + 1); 245 } 246 } 247 if (id != null) { 249 if (EJB_21_DOCTYPE.equals(id)) { 250 return EjbJar.VERSION_2_1; 251 } 252 if (EJB_20_DOCTYPE.equals(id)) { 253 return EjbJar.VERSION_2_0; 254 } 255 if (EJB_11_DOCTYPE.equals(id)){ 256 return EjbJar.VERSION_1_1; 257 } 258 } 259 return EjbJar.VERSION_3_0; 260 261 } 262 263 private static DocumentBuilder createParser(ErrorHandler errorHandler) 264 throws SAXException { 265 DocumentBuilder parser=null; 266 try { 267 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 268 parser = fact.newDocumentBuilder(); 269 } catch (ParserConfigurationException ex) { 270 throw new SAXException (ex.getMessage()); 271 } 272 parser.setErrorHandler(errorHandler); 273 return parser; 274 } 275 276 private static class DDResolver implements EntityResolver { 277 static DDResolver resolver; 278 static synchronized DDResolver getInstance() { 279 if (resolver==null) { 280 resolver=new DDResolver(); 281 } 282 return resolver; 283 } 284 285 public InputSource resolveEntity(String publicId, String systemId) { 286 String resource; 288 if (EJB_11_DOCTYPE.equals(publicId)) { 289 resource = "/org/netbeans/modules/j2ee/dd/impl/resources/ejb-jar_1_1.dtd"; } else if (EJB_20_DOCTYPE.equals(publicId)) { 291 resource = "/org/netbeans/modules/j2ee/dd/impl/resources/ejb-jar_2_0.dtd"; } else if ("http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd".equals(systemId)) { 293 resource = "/org/netbeans/modules/j2ee/dd/impl/resources/ejb-jar_2_1.xsd"; } else if ("http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd".equals(systemId)) { 295 resource = "/org/netbeans/modules/j2ee/dd/impl/resources/ejb-jar_3_0.xsd"; } else { 297 return null; 298 } 299 URL url = this.getClass().getResource(resource); 300 return new InputSource (url.toString()); 301 } 302 } 303 304 private static class ErrorHandler implements org.xml.sax.ErrorHandler { 305 private int errorType=-1; 306 SAXParseException error; 307 308 public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 309 if (errorType<0) { 310 errorType=0; 311 error=sAXParseException; 312 } 313 } 315 public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 316 if (errorType<1) { 317 errorType=1; 318 error=sAXParseException; 319 } 320 } 322 public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException { 323 errorType=2; 324 throw sAXParseException; 325 } 326 327 public int getErrorType() { 328 return errorType; 329 } 330 public SAXParseException getError() { 331 return error; 332 } 333 } 334 335 private class DDFileChangeListener extends FileChangeAdapter { 336 public void fileChanged(FileEvent evt) { 337 FileObject fo = evt.getFile(); 338 try { 339 synchronized (ddMap) { 340 EjbJarProxy ejbJarProxy = getFromCache(fo); 341 if (ejbJarProxy != null) { 342 String encoding = EncodingUtil.detectEncoding(new BufferedInputStream (fo.getInputStream())); 343 if (encoding == null) { 344 encoding = "UTF8"; 345 } 346 DDUtils.merge(ejbJarProxy, new InputStreamReader (fo.getInputStream(), encoding)); 347 } 348 } 349 } catch (IOException ex) { 350 ErrorManager.getDefault().notify(ex); 351 } 352 } 353 } 354 } 355 | Popular Tags |