KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > ViewDescriptorManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 package com.sun.enterprise.tools.guiframework.view;
23
24 import com.iplanet.jato.RequestManager;
25
26 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
27 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
28
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.servlet.ServletContext JavaDoc;
38
39 import org.xml.sax.EntityResolver JavaDoc;
40
41 /**
42  * For now we're limiting this class to 1 instance per JVM. The
43  * ViewDescriptorURL and the cache are stored staticly.
44  */

45 public class ViewDescriptorManager implements ViewDescriptorManagerInterface {
46
47     /**
48      *
49      */

50     private ViewDescriptorManager() {
51     }
52
53
54     /**
55      *
56      */

57     public static ViewDescriptorManager getInstance() {
58     return _instance;
59     }
60
61
62     /**
63      * <P>This sets the base URL that relative DTD documents can be resolved
64      * against (namely the view.dtd).</P>
65      *
66      * <P>An example might be: "file://something/something/blah/blah"</P>
67      */

68     public void setDTDURLBase(String JavaDoc base) {
69     _dtdBase = base;
70     }
71
72
73     /**
74      * This returns the base URL that relative DTD documents can be resolved
75      * against (namely the view.dtd).
76      */

77     public String JavaDoc getDTDURLBase() {
78         // Provide a best guess...
79
if (_dtdBase == null) {
80         ServletContext JavaDoc servletContext =
81         RequestManager.getRequestContext().getServletContext();
82         String JavaDoc path = "file:///"+servletContext.getRealPath("/")+"xml/";
83         setDTDURLBase(path);
84     }
85     return _dtdBase;
86     }
87
88
89     /**
90      *
91      */

92     public void setViewDescriptorURL(String JavaDoc url) {
93     try {
94         setViewDescriptorURL(new URL JavaDoc(url));
95     } catch (MalformedURLException JavaDoc ex) {
96         throw new FrameworkException(ex);
97     }
98     }
99
100
101     /**
102      *
103      */

104     public void setViewDescriptorURL(URL JavaDoc url) {
105     _descURL = url;
106     }
107
108
109     /**
110      *
111      */

112     public URL JavaDoc getViewDescriptorURL() {
113     return _descURL;
114     }
115
116
117     public void setViewXMLEntityResolver(EntityResolver JavaDoc entityResolver) {
118         _viewXMLEntityResolver = entityResolver;
119     }
120     
121     public EntityResolver JavaDoc getViewXMLEntityResolver() {
122         return _viewXMLEntityResolver;
123     }
124     
125     /**
126      * This method sets a flag to indicate that the cache should be dumped and
127      * the XML file should be re-read.
128      */

129     public void clearCache() {
130     _clearCache = true;
131     }
132
133
134     public ViewDescriptor getViewDescriptor(String JavaDoc name) {
135     ViewDescriptor viewDesc = null;
136     if (_clearCache) {
137         // This enable re-reading after flushing
138
synchronized (this) {
139         if (_clearCache) {
140             _descriptors = new HashMap JavaDoc();
141             _clearCache = false;
142                     _processAllDescriptors = true;
143         }
144         }
145     } else {
146         // Try getting from the ViewDescriptor cache
147
viewDesc = (ViewDescriptor) _descriptors.get(name);
148         if (viewDesc != null) {
149         return viewDesc;
150         }
151     }
152
153     // We have not yet found the ViewDescriptor, look in the XML file
154
try {
155                         
156         // Get the view xml url
157
URL JavaDoc url = getViewDescriptorURL();
158         if (url == null) {
159         throw new FrameworkException(
160             "View Descriptor XML file not specified!!!");
161         }
162
163         // Get a ViewXMLReader
164
ViewXMLReader reader = null;
165         try {
166                 //System.out.println("reading: "+url.toString()+
167
// "\n base: "+getDTDURLBase());
168
reader = new ViewXMLReader(
169             url.openStream(), getDTDURLBase(), getJSPRoot(),
170                     getViewXMLEntityResolver());
171         } catch (IOException JavaDoc ex) {
172         throw new FrameworkException("Unable to open URL: '"+
173             url+"'.", ex);
174         } catch (Exception JavaDoc ex) {
175         throw new FrameworkException(ex);
176         }
177
178             if (_processAllDescriptors == true) {
179                 synchronized(this) {
180                     if (_descriptors.size() == 0) {
181                         _descriptors = reader.getAllViewDescriptors();
182                     }
183                     _processAllDescriptors = false;
184                 }
185                 viewDesc = (ViewDescriptor)_descriptors.get(name);
186             }
187             
188             if (viewDesc == null) {
189                 // Read the ViewDescriptor
190
viewDesc = reader.getViewDescriptor(name);
191                 // Save the ViewDescriptor for later
192
synchronized (this) {
193                     _descriptors.put(name, viewDesc);
194                 }
195             }
196
197         // Return the ViewDescriptor
198
return viewDesc;
199         } catch (Exception JavaDoc ex) {
200         throw new FrameworkException("Unable to locate ViewDescriptor '"+
201             name+"'.", ex);
202         }
203     }
204
205     /**
206      *
207      */

208     public void deserialize(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
209     _descriptors = (Map JavaDoc) stream.readObject();
210     }
211
212     /**
213      *
214      */

215     public void serialize(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
216     stream.writeObject(_descriptors);
217     }
218
219
220     /**
221      * This method is not part of the Interface, consider removing.
222      */

223     public void setJSPRoot(String JavaDoc root) {
224     _jspRoot = root;
225     }
226
227
228     /**
229      * This method is not part of the Interface, consider removing.
230      */

231     public String JavaDoc getJSPRoot() {
232     return _jspRoot;
233     }
234
235
236     /**
237      * The 1 shared instance of ViewDescriptorManager
238      */

239     private static ViewDescriptorManager _instance =
240     new ViewDescriptorManager();
241
242
243     /**
244      *
245      */

246     protected String JavaDoc _jspRoot = "";
247
248
249     /**
250      * Cache of ViewDescriptors
251      */

252     protected boolean _clearCache = false;
253
254     /**
255      * Cache of ViewDescriptors
256      */

257     protected Map JavaDoc _descriptors = new HashMap JavaDoc();
258
259
260     /**
261      * Location of the View Descriptor
262      */

263     protected URL JavaDoc _descURL = null;
264
265
266     /**
267      * Base URL used to resolve relative paths to DTD's
268      */

269     protected String JavaDoc _dtdBase = null;
270     
271     protected EntityResolver JavaDoc _viewXMLEntityResolver = null;
272     
273     /**
274      * whether all descriptors should be processed together
275      **/

276     protected boolean _processAllDescriptors = true;
277     
278 }
279
Popular Tags