KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > util > PreloadXML


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.admingui.util;
23
24 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
25 import com.sun.enterprise.tools.admingui.AdminGUIConstants;
26 import com.sun.enterprise.tools.guiframework.view.ViewXMLEntityResolver;
27
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.net.URL JavaDoc;
33 import javax.servlet.ServletConfig JavaDoc;
34
35
36 public class PreloadXML extends Thread JavaDoc {
37
38     /**
39      * Constructor.
40      *
41      * @param name The Thread name.
42      * @param xmlFileName The name of the XML file to load.
43      * @param config The ServletConfig.
44      */

45     public PreloadXML(String JavaDoc name, String JavaDoc xmlFileName, ServletConfig JavaDoc config) {
46     super(name);
47     setXMLFileName(xmlFileName);
48     _config = config;
49     }
50
51     /**
52      * <p> This constructor attempt to figure out the XML file that should be
53      * used. It will be "xml/eeViewDescriptor.xml" or if not found:
54      * "xml/viewDescriptor.xml". The ThreadName will be "PreloadXML".</p>
55      *
56      * @param config The ServletConfig.
57      */

58     public PreloadXML(ServletConfig JavaDoc config) {
59     // Init what we know...
60
this("PreloadXML", null, config);
61
62     // Calculate xmlFile name
63
String JavaDoc xmlFile = "xml/eeViewDescriptor.xml";
64     URL JavaDoc xmlURL = null;
65     try {
66         xmlURL = getClass().getClassLoader().getResource(xmlFile);
67         if (xmlURL == null) {
68         // Next try file system...
69
xmlURL = new URL JavaDoc("file:///" + _config.getServletContext().getRealPath(xmlFile));
70         xmlURL.openConnection().connect(); // Exception if not found
71
}
72     } catch (Exception JavaDoc ex) {
73         // Fall back...
74
xmlFile = "xml/viewDescriptor.xml";
75     }
76
77     // We should now have the correct xmlFile...
78
setXMLFileName(xmlFile);
79     }
80
81     /**
82      * <p> This method returns true if the ViewXML file appears to be loaded
83      * already.</p>
84      */

85     public static boolean isAlreadyLoaded() {
86     ViewDescriptorManager vdm = ViewDescriptorManager.getInstance();
87     return (vdm.getViewDescriptorURL() != null);
88     }
89
90     /**
91      * <p> This method populates the VDM by deserializing its state.</p>
92      */

93     public void deserialize() {
94     ViewDescriptorManager vdm = ViewDescriptorManager.getInstance();
95     try {
96         vdm.deserialize(
97         new ObjectInputStream JavaDoc(getClass().getClassLoader().
98             getResourceAsStream("viewXML.ser")));
99     } catch (Exception JavaDoc ex) {
100         // This is an optimization, ignore if it fails
101
ex.printStackTrace();
102         return;
103     }
104     }
105
106     /**
107      * <p> This method pre-loads the ViewXML structure.</p>
108      */

109     public void run() {
110     init();
111     deserialize();
112     }
113
114     /**
115      * Initialize stuff.
116      */

117     protected void init() {
118     // The following is to pre-load the ViewXML descriptor stuff...
119
URL JavaDoc xmlURL = null;
120     try {
121         xmlURL = getClass().getClassLoader().getResource(_xmlFileName);
122     } catch (Exception JavaDoc ex) {
123         // Ignore...
124
}
125     try {
126         ViewDescriptorManager vdm = ViewDescriptorManager.getInstance();
127         if (PreloadXML.isAlreadyLoaded()) {
128         // Although this is not synchronizing, this thread is invoked
129
// from the login page... and the LOGIN page must be accessed
130
// before a page w/i the application. So the chances of a
131
// Threading issue are next to none. (Also, the login page
132
// calls isAlreadyLoaded(), so it won't execute this Thread
133
// more than once.)
134
return;
135         }
136         if (xmlURL == null) {
137         xmlURL = new URL JavaDoc("file:///" + _config.getServletContext().getRealPath(_xmlFileName));
138         }
139         vdm.setViewDescriptorURL(xmlURL);
140         vdm.setDTDURLBase("/xml/"); // Different servlet, hard code
141
vdm.setJSPRoot(AdminGUIConstants.DEFAULT_DISPLAY_URL_DIR);
142         vdm.setViewXMLEntityResolver(new ViewXMLEntityResolver());
143     } catch (Exception JavaDoc ex) {
144         ex.printStackTrace();
145     }
146     }
147
148     /**
149      * <p> This displays usage information for using this class from the
150      * Commandline to generate a Serialization file.</p>
151      */

152     public static void printUsage() {
153     System.out.println("You must specify the xml file name:\n");
154     System.out.println("Usage:\n");
155     System.out.println("java " + PreloadXML.class.getName()
156         + " <xml file name>\n");
157     }
158
159     /**
160      * <p> This <code>main</code> method allows this class to be used as a
161      * utility for generating a Serialized version of the ViewDescriptor
162      * tree. The XML/DTD files must be in the ClassPath. The XML file
163      * name must be supplied via the command line.</p>
164      */

165     public static void main(String JavaDoc args[]) {
166     if (args.length < 1) {
167         printUsage();
168         return;
169     }
170
171     // Initialize stuff
172
PreloadXML pre = new PreloadXML("na", args[0], (ServletConfig JavaDoc) null);
173     pre.init();
174
175     // Get the VDM and ensure it is populated
176
ViewDescriptorManager vdm = ViewDescriptorManager.getInstance();
177     vdm.getViewDescriptor("TopFrameset");
178
179     // Serialize the VDM state
180
try {
181         vdm.serialize(
182         new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc("viewXML.ser")));
183     } catch (Exception JavaDoc ex) {
184         ex.printStackTrace();
185         return;
186     }
187
188     // Let the user know that it worked!
189
System.out.println("Success!");
190     }
191
192     public String JavaDoc getXMLFileName() {
193     return _xmlFileName;
194     }
195
196     public void setXMLFileName(String JavaDoc name) {
197     _xmlFileName = name;
198     }
199
200     private String JavaDoc _xmlFileName = null;
201     private ServletConfig JavaDoc _config = null;
202 }
203
Popular Tags