KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > api > client > DDProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.dd.api.client;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
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 JavaDoc;
41 import org.xml.sax.SAXParseException JavaDoc;
42
43 /**
44  * Provides access to Deployment Descriptor root
45  * ({@link org.netbeans.modules.j2ee.dd.api.client.AppClient} object).
46  *
47  * @author Milan Kuchtiak
48  */

49 public final class DDProvider {
50     
51     private static final DDProvider ddProvider = new DDProvider();
52     private final FCA fileChangeListener;
53     private final Map JavaDoc<URL JavaDoc, WeakReference JavaDoc<AppClientProxy>> ddMap;
54     private final Map JavaDoc<URL JavaDoc, WeakReference JavaDoc<AppClient>> baseBeanMap;
55     private final Map JavaDoc<URL JavaDoc, SAXParseException JavaDoc> errorMap;
56     private Map JavaDoc<MetadataUnit, AppClient> annotationDDMap;
57     
58     private DDProvider() {
59         //ddMap=new WeakHashMap(5);
60
ddMap = new HashMap JavaDoc<URL JavaDoc, WeakReference JavaDoc<AppClientProxy>>(5);
61         annotationDDMap = new HashMap JavaDoc<MetadataUnit, AppClient>(5);
62         baseBeanMap = new HashMap JavaDoc<URL JavaDoc, WeakReference JavaDoc<AppClient>>(5);
63         errorMap = new HashMap JavaDoc<URL JavaDoc, SAXParseException JavaDoc>(5);
64         fileChangeListener = new FCA();
65     }
66     
67     /**
68      * Accessor method for DDProvider singleton
69      * @return DDProvider object
70      */

71     public static DDProvider getDefault() {
72         return ddProvider;
73     }
74     
75     public AppClient getMergedDDRoot(MetadataUnit mu) throws IOException JavaDoc {
76         if (mu == null) {
77             return null;
78         }
79         AppClient xmlRoot = getDDRoot(mu.getDeploymentDescriptor());
80         // for J2ee 1.4 and lower delegate to XML-only method
81
if (xmlRoot != null && !xmlRoot.getVersion().equals(new BigDecimal JavaDoc(AppClient.VERSION_5_0))) {
82             return xmlRoot;
83         }
84         return null;
85     }
86     
87     /**
88      * Returns the root of deployment descriptor bean graph for java.io.File object.
89      *
90      * @param f File representing the web.xml file
91      * @return appClient object - root of the deployment descriptor bean graph
92      */

93     public AppClient getDDRoot(File JavaDoc f) throws IOException JavaDoc, SAXException JavaDoc {
94         return DDUtils.createAppClient(new FileInputStream JavaDoc(f), ClientParseUtils.getVersion(new FileInputStream JavaDoc(f)));
95     }
96     
97     /**
98      * Returns the root of deployment descriptor bean graph for given file object.
99      * The method is useful for clints planning to read only the deployment descriptor
100      * or to listen to the changes.
101      * @param fo FileObject representing the application-client.xml file
102      * @return AppClient object - root of the deployment descriptor bean graph
103      */

104     public synchronized AppClient getDDRoot(FileObject fo) throws IOException JavaDoc {
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 JavaDoc version = null;
120         SAXParseException JavaDoc 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                     // preparsing
128
error = ClientParseUtils.parse(fo);
129                     original = DDUtils.createAppClient(fo.getInputStream(), version);
130                     baseBeanMap.put(fo.getURL(), new WeakReference JavaDoc<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 JavaDoc ex) {
143             appClient = new AppClientProxy(null, version);
144             appClient.setStatus(AppClient.STATE_INVALID_UNPARSABLE);
145             if (ex instanceof SAXParseException JavaDoc) {
146                 appClient.setError((SAXParseException JavaDoc) ex);
147             } else if (ex.getException() instanceof SAXParseException JavaDoc) {
148                 appClient.setError((SAXParseException JavaDoc) ex.getException());
149             }
150         }
151         ddMap.put(fo.getURL(), new WeakReference JavaDoc<AppClientProxy>(appClient));
152         return appClient;
153     }
154     
155     /**
156      * Returns the root of deployment descriptor bean graph for given file object.
157      * The method is useful for clients planning to modify the deployment descriptor.
158      * Finally the {@link org.netbeans.modules.j2ee.dd.api.ejb.EjbJar#write(org.openide.filesystems.FileObject)} should be used
159      * for writing the changes.
160      * @param fo FileObject representing the ejb-jar.xml file
161      * @return EjbJar object - root of the deployment descriptor bean graph
162      */

163     public AppClient getDDRootCopy(FileObject fo) throws IOException JavaDoc {
164         return (AppClient)getDDRoot(fo).clone();
165     }
166     
167     private AppClientProxy getFromCache(FileObject fo) throws IOException JavaDoc {
168         if (fo == null) {
169             return null;
170         }
171         WeakReference JavaDoc<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 JavaDoc {
183         WeakReference JavaDoc<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             /*
192             if (ddMap.get (fo.getURL ()) == null) {
193             }
194              */

195         }
196         return appClient;
197     }
198     
199     /** Convenient method for getting the BaseBean object from CommonDDBean object.
200      * The j2eeserver module needs BaseBean to implement jsr88 API.
201      * This is a temporary workaround until the implementation of jsr88 moves into ddapi
202      * or the implementation in j2eeserver gets changed.
203      * @deprecated do not use - temporary workaround that exposes the schema2beans implementation
204      */

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 JavaDoc version = null;
229                             try {
230                                 version = ClientParseUtils.getVersion(fo.getInputStream());
231                                 // preparsing
232
SAXParseException JavaDoc 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 JavaDoc<AppClient>(original));
242                                 errorMap.put(fo.getURL(), appClient.getError());
243                                 appClient.merge(original, AppClient.MERGE_UPDATE);
244                             } catch (SAXException JavaDoc ex) {
245                                 if (ex instanceof SAXParseException JavaDoc) {
246                                     appClient.setError((SAXParseException JavaDoc)ex);
247                                 } else if ( ex.getException() instanceof SAXParseException JavaDoc) {
248                                     appClient.setError((SAXParseException JavaDoc)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 JavaDoc 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 JavaDoc<AppClient>(original));
263                                 }
264                             } catch (SAXException JavaDoc ex) {
265                                 baseBeanMap.remove(fo.getURL());
266                             }
267                         }
268                     }
269                 }
270             } catch (IOException JavaDoc ex) {
271                 ErrorManager.getDefault().notify(ex);
272             }
273         }
274         
275     }
276     
277 }
278
Popular Tags