KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > ToolBoxInfo


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the Licenseget.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24
25 //
26
package org.enhydra.tool;
27
28 // ToolBox
29
import org.enhydra.tool.common.FileUtil;
30 import org.enhydra.tool.common.PathHandle;
31 import org.enhydra.tool.common.ResUtil;
32 import org.enhydra.tool.common.ToolException;
33
34 // JDK
35
import java.net.URLClassLoader JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.FileNotFoundException JavaDoc;
41 import java.lang.reflect.Field JavaDoc;
42 import java.util.Properties JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Arrays JavaDoc;
45 import java.util.ResourceBundle JavaDoc;
46
47 //
48
public class ToolBoxInfo {
49     private static boolean rootSettable = false;
50
51     //
52
static ResourceBundle JavaDoc res = ResourceBundle.getBundle("org.enhydra.tool.common.Res"); // nores
53

54     //
55
public static final String JavaDoc FILE_KERNEL_JAR = "Kernel.jar"; // nores
56
public static final String JavaDoc FILE_ENHYDRA_JAR = "eaf.jar"; // nores
57
public static final String JavaDoc WML_FACTORY =
58         "org.enhydra.wireless.wml.WMLDomFactory"; // nores
59
public static final String JavaDoc CHTML_FACTORY =
60         "org.enhydra.wireless.chtml.CHTMLDomFactory"; // nores
61
public static final String JavaDoc XHTML_FACTORY =
62         "org.enhydra.xml.xhtml.XHTMLDomFactory"; // nores
63
public static final String JavaDoc XMLC_VERSION_CLASS =
64         "org.enhydra.xml.xmlc.XMLCVersion"; // nores
65
public static final String JavaDoc ENHYDRA_ROOT = "eas.root"; // nores
66

67     private static final String JavaDoc DIR_LIB = "lib"; // nores
68
private static final String JavaDoc VERSION = "6.5"; // nores
69
private static final String JavaDoc PROPERTY_FILENAME =
70         "toolbox.properties"; // nores
71
private static final String JavaDoc DEFAULT_ENHYDRA_ROOT =
72         "/usr/local/enhydra-6.5";
73
74     private static final String JavaDoc SERVER_RUN_CLASS =
75         "org.objectweb.jonas.server.Bootstrap"; // nores
76

77
78     private ToolBoxInfo() {}
79
80     public static String JavaDoc getCopyright() {
81         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
82
83         buf.append('\n');
84         buf.append("Part of The Enhydra Application Server Suite\n");
85         return buf.toString();
86     }
87
88     public static Properties JavaDoc loadProperties() throws ToolException {
89         String JavaDoc filename = getPropertyFilename();
90         File JavaDoc file = new File JavaDoc(filename);
91         Properties JavaDoc properties = new Properties JavaDoc();
92
93         if (file.isFile() && file.canRead()) {
94             try {
95                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
96
97                 properties.load(in);
98             } catch (FileNotFoundException JavaDoc e) {
99                 throw new ToolException(e,
100                                         ResUtil.format(res.getString("Unable_to_read"), file));
101             } catch (IOException JavaDoc e) {
102                 throw new ToolException(e,
103                                         ResUtil.format(res.getString("Unable_to_read"), file));
104             }
105         }
106         return properties;
107     }
108
109     /**
110      * Store persisted generator options to the properties file. This is
111      * public and static to allow for the creation of a valid properties
112      * file without having an instance of the CodeGen object. Creating
113      * an instance of CodeGen requires that a valid properties file already
114      * exist.
115      *
116      * @param prop
117      * Properties to save into the codegen.properties file.
118      *
119      * @return
120      * A file reference to the properties file.
121      *
122      * @exception java.io.IOException
123      * Thrown if unable to write to the codegen.properties file.
124      */

125     public static File JavaDoc storeProperties(Properties JavaDoc prop) throws ToolException {
126         File JavaDoc file = null;
127         FileOutputStream JavaDoc out = null;
128
129         file = new File JavaDoc(ToolBoxInfo.getPropertyFilename());
130         if (file.getParentFile() != null) {
131             file.getParentFile().mkdirs();
132         }
133         try {
134             out = new FileOutputStream JavaDoc(file);
135             prop.store(out, res.getString("Kelp_ToolBox1"));
136         } catch (IOException JavaDoc e) {
137             throw new ToolException(e,
138                                     ResUtil.format(res.getString("Unable_to_store"), file));
139         }
140         return file;
141     }
142
143     public static String JavaDoc getJavaPath() {
144         final String JavaDoc SYS_JAVA_HOME = "java.home"; // nores
145
String JavaDoc path = null;
146         File JavaDoc file = null;
147
148         path = System.getProperty(SYS_JAVA_HOME, new String JavaDoc());
149         file = new File JavaDoc(path);
150         path = file.getParent().replace('\\', '/');
151         return path;
152     }
153
154     public static String JavaDoc getEnhydraKeyJar() {
155         String JavaDoc jar = ToolBoxInfo.FILE_ENHYDRA_JAR;
156         return jar;
157     }
158
159     public static boolean isClassAvailable(String JavaDoc className) {
160         boolean available = false;
161
162         try {
163             Class.forName(className);
164             available = true;
165         } catch (ClassNotFoundException JavaDoc e) {
166             available = false;
167         }
168         return available;
169     }
170
171     public static void main(String JavaDoc args[]) {
172         System.out.println(res.getString("ToolBox_Info"));
173         System.out.println(ToolBoxInfo.getCopyright());
174         System.out.println(ResUtil.format(res.getString("ToolBox_version_0_"),
175                                           ToolBoxInfo.getToolBoxVersion()));
176         System.out.println(ResUtil.format("Application Server Root: {0}",
177                                           ToolBoxInfo.getEnhydraRoot()));
178         System.out.println(ResUtil.format(res.getString("Enhydra_in_classpath"),
179                                           ToolBoxInfo.isEnhydraInClassPath()));
180         System.out.println(ResUtil.format(res.getString("Enhydra_main_class_0_"),
181                                           ToolBoxInfo.getEnhydraMainClass()));
182         System.out.println(ResUtil.format(res.getString("XMLC_version_0_"),
183                                           ToolBoxInfo.getXMLCVersion()));
184         System.out.println(ResUtil.format(res.getString("XMLC_in_classpath_0_"),
185                                           ToolBoxInfo.isXMLCInClassPath()));
186     }
187
188     public static boolean isXMLCVersion(int v) {
189         boolean is = false;
190         String JavaDoc version = ToolBoxInfo.getXMLCVersion();
191
192         if (version.startsWith(new String JavaDoc() + v)) {
193             is = true;
194         }
195         return is;
196     }
197
198     public static String JavaDoc getXMLCVersion() {
199         final String JavaDoc FIELD_VERSION = "VERSION"; // nores;
200

201         //
202
String JavaDoc version = res.getString("_unknown_");
203         Class JavaDoc verClass = null;
204         Field JavaDoc verField = null;
205
206         try {
207             verClass = Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS);
208             verField = verClass.getDeclaredField(FIELD_VERSION);
209             version = verField.get(null).toString();
210         } catch (Exception JavaDoc e) {
211
212             // no XMLC
213
}
214         return version;
215     }
216
217     /**
218      * Get the class to run to start MultiServer.
219      */

220     public static String JavaDoc getEnhydraMainClass() {
221         String JavaDoc runClass = ToolBoxInfo.SERVER_RUN_CLASS;
222         return runClass;
223     }
224
225     public static boolean isXMLCInClassPath() {
226         boolean inPath = true;
227
228         try {
229             Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS);
230         } catch (java.lang.ClassNotFoundException JavaDoc e) {
231             inPath = false;
232         }
233         return inPath;
234     }
235
236     public static boolean isEnhydraInClassPath() {
237         boolean inPath = true;
238         String JavaDoc checkClass = ToolBoxInfo.SERVER_RUN_CLASS;
239         try {
240             Class JavaDoc testClass = Class.forName(checkClass);
241         } catch (java.lang.ClassNotFoundException JavaDoc e) {
242             inPath = false;
243         }
244         return inPath;
245     }
246
247     public static String JavaDoc getToolBoxVersion() {
248         return VERSION;
249     }
250
251     /**
252      * Method declaration
253      *
254      *
255      * @return
256      */

257     public static String JavaDoc getPropertyFilename() {
258         final String JavaDoc SYS_USER_HOME = "user.home"; // nores
259
final String JavaDoc DIR_ENHYDRA = ".enhydra"; // nores
260

261         //
262
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
263
264         buf.append(System.getProperties().getProperty(SYS_USER_HOME));
265         buf.append(File.separator);
266         buf.append(DIR_ENHYDRA);
267         buf.append(File.separator);
268         buf.append(PROPERTY_FILENAME);
269         return buf.toString();
270     }
271
272     public static boolean isRootSettable() {
273         ToolBoxInfo.getEnhydraRoot();
274         return ToolBoxInfo.rootSettable;
275     }
276
277     public static String JavaDoc getEnhydraRoot() {
278         String JavaDoc path = null;
279
280         ToolBoxInfo.rootSettable = false;
281         path = getEnhydraPathByClassLoader();
282         if (path == null) {
283             path = getEnhydraPathByWorkingDirectory();
284         }
285         if (path == null) {
286             ToolBoxInfo.rootSettable = true;
287             path = getEnhydraPathByPropertyFile();
288         }
289         if (path == null) {
290             path = ToolBoxInfo.DEFAULT_ENHYDRA_ROOT;
291         }
292         return PathHandle.createPathString(path);
293     }
294
295     public static String JavaDoc[] getSupportedDocTypes() {
296         final String JavaDoc TYPE_CHTML = "chtml"; // nores
297
final String JavaDoc TYPE_HTML = "html"; // nores
298
final String JavaDoc TYPE_WML = "wml"; // nores
299
final String JavaDoc TYPE_XHTML = "xhtml"; // nores
300

301         //
302
String JavaDoc[] types = new String JavaDoc[0];
303         ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(types));
304
305         list.add(TYPE_HTML);
306         if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.WML_FACTORY)) {
307             list.add(TYPE_WML);
308         }
309         if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.CHTML_FACTORY)) {
310             list.add(TYPE_CHTML);
311         }
312         if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.XHTML_FACTORY)) {
313             list.add(TYPE_XHTML);
314         }
315         list.trimToSize();
316         types = new String JavaDoc[list.size()];
317         types = (String JavaDoc[]) list.toArray(types);
318         return types;
319     }
320
321     private static String JavaDoc getEnhydraPathByWorkingDirectory() {
322         File JavaDoc file = null;
323         String JavaDoc path = null;
324
325         file = new File JavaDoc((new String JavaDoc()) + '.'); // <enhydra home>/lib
326
file = new File JavaDoc(file.getAbsolutePath());
327         file = file.getParentFile();
328         if ((file.getParentFile() != null)
329                 && (file.getParentFile().getParentFile() != null)) {
330             path = file.getParentFile().getParent();
331         }
332         if (!ToolBoxInfo.isEnhydraRoot(path)) {
333             path = null;
334         }
335         return path;
336     }
337
338     public static boolean isEnhydraRoot(String JavaDoc path) {
339         boolean validRoot = false;
340         File JavaDoc[] jars = new File JavaDoc[0];
341         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
342
343         if (path != null) {
344            jars = new File JavaDoc[1];
345
346             // enhydra 3
347
buf.append(path);
348             buf.append(File.separator);
349             buf.append(DIR_LIB);
350             buf.append(File.separator);
351             buf.append(ToolBoxInfo.FILE_ENHYDRA_JAR);
352             jars[0] = new File JavaDoc(buf.toString());
353         }
354
355         //
356
for (int i = 0; i < jars.length; i++) {
357             if (jars[i].isFile()) {
358                 validRoot = true;
359                 break;
360             }
361         }
362         return validRoot;
363     }
364
365     private static String JavaDoc getEnhydraPathByClassLoader() {
366         final String JavaDoc FILE_TOOLBOX_JAR = "toolbox.jar"; // nores
367

368         //
369
boolean found = false;
370         String JavaDoc[] paths = new String JavaDoc[0];
371         String JavaDoc path = null;
372         File JavaDoc file = null;
373         ToolBoxInfo info = new ToolBoxInfo();
374         ClassLoader JavaDoc loader = info.getClass().getClassLoader();
375
376         paths = FileUtil.findJarPaths(FILE_TOOLBOX_JAR, loader);
377         for (int i = 0; i < paths.length; i++) {
378             file = new File JavaDoc(paths[i]);
379             if (file.getParentFile() != null) {
380                 file = file.getParentFile();
381                 if ((file.getParentFile() != null)
382                         && (file.getParentFile().getParentFile() != null)) {
383                     path = file.getParentFile().getParent();
384                     found = ToolBoxInfo.isEnhydraRoot(path);
385                 }
386             }
387             if (found) {
388                 break;
389             } else {
390                 path = null;
391             }
392         }
393         return path;
394     }
395
396     private static String JavaDoc getEnhydraPathByPropertyFile() {
397         String JavaDoc path = null;
398         Properties JavaDoc properties = new Properties JavaDoc();
399
400         try {
401             properties = ToolBoxInfo.loadProperties();
402             path = properties.getProperty(ToolBoxInfo.ENHYDRA_ROOT);
403         } catch (ToolException e) {
404             e.printStackTrace();
405             path = null;
406         }
407         if (!ToolBoxInfo.isEnhydraRoot(path)) {
408             path = null;
409         }
410         return path;
411     }
412
413     private boolean isEnhydraPathValid(String JavaDoc home) {
414         boolean valid = false;
415         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
416         File JavaDoc f = null;
417
418         path.append(home);
419         path.append(File.separator);
420         path.append(ToolBoxInfo.DIR_LIB);
421         path.append(File.separator);
422         path.append(ToolBoxInfo.FILE_ENHYDRA_JAR);
423         f = new File JavaDoc(path.toString());
424         valid = f.isFile();
425         f = null;
426         return valid;
427     }
428
429 }
430
Popular Tags