1 19 20 package org.netbeans.modules.j2ee.dd.api.web; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.net.URL ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import org.netbeans.api.java.classpath.ClassPath; 30 import org.netbeans.modules.j2ee.metadata.ClassPathSupport; 31 import org.netbeans.modules.j2ee.dd.impl.web.WebAppProxy; 32 import org.netbeans.modules.j2ee.dd.impl.web.WebParseUtils; 33 import org.netbeans.modules.j2ee.dd.impl.common.DDUtils; 34 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 35 import org.netbeans.modules.web.api.webmodule.WebModule; 36 import org.netbeans.spi.java.classpath.PathResourceImplementation; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.*; 39 import org.xml.sax.*; 40 import java.util.Map ; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 44 49 50 public final class DDProvider { 51 private static DDProvider ddProvider; 52 private Map ddMap; 53 private Map <MetadataUnit, WebApp> annotationDDMap; 54 private Map baseBeanMap; 55 private Map errorMap; 56 private FCA fileChangeListener; 57 private Map musMap; 58 59 60 private DDProvider() { 61 ddMap=new java.util.HashMap (5); 62 annotationDDMap = new HashMap <MetadataUnit, WebApp>(5); 63 baseBeanMap=new java.util.HashMap (5); 64 errorMap=new java.util.HashMap (5); 65 musMap=new HashMap (5); 66 fileChangeListener = new FCA(); 67 } 68 69 73 public static synchronized DDProvider getDefault() { 74 if (ddProvider==null) ddProvider = new DDProvider(); 75 return ddProvider; 76 } 77 78 85 public WebApp getMergedDDRoot(FileObject fo) throws IOException { 86 if (fo == null) { 87 throw new IllegalArgumentException ("FileObject is null"); } 89 90 WebModule wm = WebModule.getWebModule(fo); 91 if(wm != null) { 92 MetadataUnit mu = (MetadataUnit)musMap.get(wm.getDeploymentDescriptor()); 94 if(mu == null) { 95 mu = new SimpleMetadataUnit(wm.getDeploymentDescriptor(), wm.getJavaSources()); 96 musMap.put(wm.getDeploymentDescriptor(), mu); 97 } 98 return getMergedDDRoot(mu); 99 } else { 100 return getDDRoot(fo); 101 } 102 } 103 104 public WebApp getMergedDDRoot(MetadataUnit mu) throws IOException { 105 WebApp xmlRoot = getDDRoot(mu.getDeploymentDescriptor()); 106 if (xmlRoot != null) { return xmlRoot; 109 } 110 return null; 111 } 112 113 public WebApp getDDRoot(FileObject fo) throws java.io.IOException { 114 WebAppProxy webApp = null; 115 116 synchronized (ddMap) { 117 webApp = getFromCache(fo); 118 if (webApp!=null) { 119 return webApp; 120 } 121 } 122 123 fo.addFileChangeListener(fileChangeListener); 124 125 String version = null; 126 SAXParseException error = null; 127 try { 128 WebApp original = null; 129 synchronized (baseBeanMap) { 130 original = getOriginalFromCache(fo); 131 if (original == null) { 132 version = WebParseUtils.getVersion(fo.getInputStream()); 133 error = parse(fo); 135 original = DDUtils.createWebApp(fo.getInputStream(), version); 136 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 137 errorMap.put(fo.getURL(), error); 138 } else { 139 version = original.getVersion(); 140 error = (SAXParseException) errorMap.get(fo.getURL()); 141 } 142 } 143 webApp = new WebAppProxy(original, version); 144 if (error != null) { 145 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE); 146 webApp.setError(error); 147 } 148 } catch (SAXException ex) { 149 webApp = new WebAppProxy(null, version); 150 webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE); 151 if (ex instanceof SAXParseException) { 152 webApp.setError((SAXParseException) ex); 153 } else if (ex.getException() instanceof SAXParseException) { 154 webApp.setError((SAXParseException) ex.getException()); 155 } 156 } 157 ddMap.put(fo.getURL(), new WeakReference (webApp)); 158 return webApp; 159 } 160 161 169 public WebApp getDDRootCopy(FileObject fo) throws java.io.IOException { 170 return (WebApp)getDDRoot(fo).clone(); 171 } 172 173 private WebAppProxy getFromCache(FileObject fo) throws java.io.IOException { 174 if (fo == null) { 175 return null; 176 } 177 WeakReference wr = (WeakReference ) ddMap.get(fo.getURL()); 178 if (wr == null) { 179 return null; 180 } 181 WebAppProxy webApp = (WebAppProxy) wr.get(); 182 if (webApp == null) { 183 ddMap.remove(fo.getURL()); 184 } 185 return webApp; 186 } 187 188 private WebApp getOriginalFromCache(FileObject fo) throws java.io.IOException { 189 WeakReference wr = (WeakReference ) baseBeanMap.get(fo.getURL()); 190 if (wr == null) { 191 return null; 192 } 193 WebApp webApp = (WebApp) wr.get(); 194 if (webApp == null) { 195 baseBeanMap.remove(fo.getURL()); 196 errorMap.remove(fo.getURL()); 197 if (ddMap.get(fo.getURL()) == null) { 198 } 199 } 200 return webApp; 201 } 202 203 209 public WebApp getDDRoot(File f) throws IOException , SAXException { 210 return DDUtils.createWebApp(new FileInputStream (f), WebParseUtils.getVersion(new FileInputStream (f))); 211 } 212 213 219 public org.netbeans.modules.schema2beans.BaseBean getBaseBean(org.netbeans.modules.j2ee.dd.api.common.CommonDDBean bean) { 220 if (bean instanceof org.netbeans.modules.schema2beans.BaseBean) return (org.netbeans.modules.schema2beans.BaseBean)bean; 221 else if (bean instanceof WebAppProxy) return (org.netbeans.modules.schema2beans.BaseBean) ((WebAppProxy)bean).getOriginal(); 222 return null; 223 } 224 225 public SAXParseException parse(FileObject fo) 226 throws org.xml.sax.SAXException , java.io.IOException { 227 return WebParseUtils.parse(fo); 228 } 229 230 231 236 private void removeFromCache(FileObject fo){ 237 try{ 238 URL foUrl = fo.getURL(); 239 ddMap.remove(foUrl); 240 baseBeanMap.remove(foUrl); 241 errorMap.remove(foUrl); 242 musMap.remove(fo); 243 } catch (FileStateInvalidException ex) { 244 ErrorManager.getDefault().notify(ex); 245 } 246 } 247 248 private class FCA extends FileChangeAdapter { 249 public void fileChanged(FileEvent evt) { 250 FileObject fo=evt.getFile(); 251 try { 252 if (DataObject.find(fo) != null) { 253 return; 254 } 255 } catch (DataObjectNotFoundException e) { 256 } 257 try { 258 synchronized (ddMap) { 259 synchronized (baseBeanMap) { 260 WebAppProxy webApp = getFromCache(fo); 261 WebApp orig = getOriginalFromCache(fo); 262 if (webApp!=null) { 263 String version = null; 264 try { 265 version = WebParseUtils.getVersion(fo.getInputStream()); 266 SAXParseException error = parse(fo); 268 if (error!=null) { 269 webApp.setError(error); 270 webApp.setStatus(WebApp.STATE_INVALID_PARSABLE); 271 } else { 272 webApp.setError(null); 273 webApp.setStatus(WebApp.STATE_VALID); 274 } 275 WebApp original = DDUtils.createWebApp(fo.getInputStream(), version); 276 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 277 errorMap.put(fo.getURL(), webApp.getError()); 278 webApp.merge(original, WebApp.MERGE_UPDATE); 279 } catch (SAXException ex) { 280 if (ex instanceof SAXParseException) { 281 webApp.setError((SAXParseException)ex); 282 } else if ( ex.getException() instanceof SAXParseException) { 283 webApp.setError((SAXParseException)ex.getException()); 284 } 285 webApp.setStatus(WebApp.STATE_INVALID_UNPARSABLE); 286 webApp.setOriginal(null); 287 webApp.setProxyVersion(version); 288 } 289 } else if (orig != null) { 290 String version = null; 291 try { 292 version = WebParseUtils.getVersion(fo.getInputStream()); 293 WebApp original = DDUtils.createWebApp(fo.getInputStream(), version); 294 if (original.getClass().equals(orig.getClass())) { 295 orig.merge(original,WebApp.MERGE_UPDATE); 296 } else { 297 baseBeanMap.put(fo.getURL(), new WeakReference (original)); 298 } 299 } catch (SAXException ex) { 300 baseBeanMap.remove(fo.getURL()); 301 } 302 } 303 } 304 } 305 } catch (java.io.IOException ex){} 306 } 307 308 public void fileDeleted(FileEvent fe) { 309 removeFromCache(fe.getFile()); 311 } 312 313 314 } 315 316 private class SimpleMetadataUnit implements MetadataUnit { 317 318 private FileObject dd; 319 private FileObject[] roots; 320 321 public SimpleMetadataUnit(FileObject dd, FileObject[] javaSources) { 322 this.dd = dd; 323 this.roots = javaSources; 324 } 325 326 public FileObject getDeploymentDescriptor() { 327 return dd; 328 } 329 330 public ClassPath getClassPath() { 331 if (roots.length > 0) { 332 FileObject fo = roots[0]; 333 return ClassPathSupport.createWeakProxyClassPath(new ClassPath[] { 334 ClassPath.getClassPath(fo, ClassPath.SOURCE), 335 ClassPath.getClassPath(fo, ClassPath.COMPILE) 336 }); 337 } else { 338 return org.netbeans.spi.java.classpath.support.ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList()); 339 } 340 } 341 342 343 344 } 345 } 346 | Popular Tags |