1 19 20 package org.netbeans.modules.j2ee.dd.api.client; 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.math.BigDecimal ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean; 31 import org.netbeans.modules.j2ee.dd.impl.client.ClientParseUtils; 32 import org.netbeans.modules.j2ee.dd.impl.client.AppClientProxy; 33 import org.netbeans.modules.j2ee.dd.impl.common.DDUtils; 34 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 35 import org.netbeans.modules.schema2beans.BaseBean; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileChangeAdapter; 38 import org.openide.filesystems.FileEvent; 39 import org.openide.filesystems.FileObject; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXParseException ; 42 43 49 public final class DDProvider { 50 51 private static final DDProvider ddProvider = new DDProvider(); 52 private final FCA fileChangeListener; 53 private final Map <URL , WeakReference <AppClientProxy>> ddMap; 54 private final Map <URL , WeakReference <AppClient>> baseBeanMap; 55 private final Map <URL , SAXParseException > errorMap; 56 private Map <MetadataUnit, AppClient> annotationDDMap; 57 58 private DDProvider() { 59 ddMap = new HashMap <URL , WeakReference <AppClientProxy>>(5); 61 annotationDDMap = new HashMap <MetadataUnit, AppClient>(5); 62 baseBeanMap = new HashMap <URL , WeakReference <AppClient>>(5); 63 errorMap = new HashMap <URL , SAXParseException >(5); 64 fileChangeListener = new FCA(); 65 } 66 67 71 public static DDProvider getDefault() { 72 return ddProvider; 73 } 74 75 public AppClient getMergedDDRoot(MetadataUnit mu) throws IOException { 76 if (mu == null) { 77 return null; 78 } 79 AppClient xmlRoot = getDDRoot(mu.getDeploymentDescriptor()); 80 if (xmlRoot != null && !xmlRoot.getVersion().equals(new BigDecimal (AppClient.VERSION_5_0))) { 82 return xmlRoot; 83 } 84 return null; 85 } 86 87 93 public AppClient getDDRoot(File f) throws IOException , SAXException { 94 return DDUtils.createAppClient(new FileInputStream (f), ClientParseUtils.getVersion(new FileInputStream (f))); 95 } 96 97 104 public synchronized AppClient getDDRoot(FileObject fo) throws IOException { 105 if (fo == null) { 106 return null; 107 } 108 AppClientProxy appClient = null; 109 110 synchronized (ddMap) { 111 appClient = getFromCache(fo); 112 if (appClient!=null) { 113 return appClient; 114 } 115 } 116 117 fo.addFileChangeListener(fileChangeListener); 118 119 String version = null; 120 SAXParseException error = null; 121 try { 122 AppClient original = null; 123 synchronized (baseBeanMap) { 124 original = getOriginalFromCache(fo); 125 if (original == null) { 126 version = ClientParseUtils.getVersion(fo.getInputStream()); 127 error = ClientParseUtils.parse(fo); 129 original = DDUtils.createAppClient(fo.getInputStream(), version); 130 baseBeanMap.put(fo.getURL(), new WeakReference <AppClient>(original)); 131 errorMap.put(fo.getURL(), error); 132 } else { 133 version = original.getVersion().toPlainString(); 134 error = errorMap.get(fo.getURL()); 135 } 136 } 137 appClient = new AppClientProxy(original, version); 138 if (error != null) { 139 appClient.setStatus(AppClient.STATE_INVALID_PARSABLE); 140 appClient.setError(error); 141 } 142 } catch (SAXException ex) { 143 appClient = new AppClientProxy(null, version); 144 appClient.setStatus(AppClient.STATE_INVALID_UNPARSABLE); 145 if (ex instanceof SAXParseException ) { 146 appClient.setError((SAXParseException ) ex); 147 } else if (ex.getException() instanceof SAXParseException ) { 148 appClient.setError((SAXParseException ) ex.getException()); 149 } 150 } 151 ddMap.put(fo.getURL(), new WeakReference <AppClientProxy>(appClient)); 152 return appClient; 153 } 154 155 163 public AppClient getDDRootCopy(FileObject fo) throws IOException { 164 return (AppClient)getDDRoot(fo).clone(); 165 } 166 167 private AppClientProxy getFromCache(FileObject fo) throws IOException { 168 if (fo == null) { 169 return null; 170 } 171 WeakReference <AppClientProxy> wr = ddMap.get(fo.getURL()); 172 if (wr == null) { 173 return null; 174 } 175 AppClientProxy appClient = wr.get(); 176 if (appClient == null) { 177 ddMap.remove(fo.getURL()); 178 } 179 return appClient; 180 } 181 182 private AppClient getOriginalFromCache(FileObject fo) throws IOException { 183 WeakReference <AppClient> wr = baseBeanMap.get(fo.getURL()); 184 if (wr == null) { 185 return null; 186 } 187 AppClient appClient = wr.get(); 188 if (appClient == null) { 189 baseBeanMap.remove(fo.getURL()); 190 errorMap.remove(fo.getURL()); 191 195 } 196 return appClient; 197 } 198 199 205 public BaseBean getBaseBean(CommonDDBean bean) { 206 BaseBean result; 207 if (bean instanceof BaseBean) { 208 result = (BaseBean) bean; 209 } else if (bean instanceof AppClientProxy) { 210 result = (BaseBean) ((AppClientProxy)bean).getOriginal(); 211 } else { 212 result = null; 213 } 214 return result; 215 } 216 217 218 private class FCA extends FileChangeAdapter { 219 220 public void fileChanged(FileEvent evt) { 221 FileObject fo=evt.getFile(); 222 try { 223 synchronized (ddMap) { 224 synchronized (baseBeanMap) { 225 AppClientProxy appClient = getFromCache(fo); 226 AppClient orig = getOriginalFromCache(fo); 227 if (appClient!=null) { 228 String version = null; 229 try { 230 version = ClientParseUtils.getVersion(fo.getInputStream()); 231 SAXParseException error = ClientParseUtils.parse(fo); 233 if (error!=null) { 234 appClient.setError(error); 235 appClient.setStatus(AppClient.STATE_INVALID_PARSABLE); 236 } else { 237 appClient.setError(null); 238 appClient.setStatus(AppClient.STATE_VALID); 239 } 240 AppClient original = DDUtils.createAppClient(fo.getInputStream(), version); 241 baseBeanMap.put(fo.getURL(), new WeakReference <AppClient>(original)); 242 errorMap.put(fo.getURL(), appClient.getError()); 243 appClient.merge(original, AppClient.MERGE_UPDATE); 244 } catch (SAXException ex) { 245 if (ex instanceof SAXParseException ) { 246 appClient.setError((SAXParseException )ex); 247 } else if ( ex.getException() instanceof SAXParseException ) { 248 appClient.setError((SAXParseException )ex.getException()); 249 } 250 appClient.setStatus(AppClient.STATE_INVALID_UNPARSABLE); 251 appClient.setOriginal(null); 252 appClient.setProxyVersion(version); 253 } 254 } else if (orig != null) { 255 String version = null; 256 try { 257 version = ClientParseUtils.getVersion(fo.getInputStream()); 258 AppClient original = DDUtils.createAppClient(fo.getInputStream(), version); 259 if (original.getClass().equals(orig.getClass())) { 260 orig.merge(original,AppClient.MERGE_UPDATE); 261 } else { 262 baseBeanMap.put(fo.getURL(), new WeakReference <AppClient>(original)); 263 } 264 } catch (SAXException ex) { 265 baseBeanMap.remove(fo.getURL()); 266 } 267 } 268 } 269 } 270 } catch (IOException ex) { 271 ErrorManager.getDefault().notify(ex); 272 } 273 } 274 275 } 276 277 } 278 | Popular Tags |