001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the Licenseget.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023:
024: //
025: package org.enhydra.tool;
026:
027: // ToolBox
028: import org.enhydra.tool.common.FileUtil;
029: import org.enhydra.tool.common.PathHandle;
030: import org.enhydra.tool.common.ResUtil;
031: import org.enhydra.tool.common.ToolException;
032:
033: // JDK
034: import java.net.URLClassLoader;
035: import java.io.IOException;
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.FileOutputStream;
039: import java.io.FileNotFoundException;
040: import java.lang.reflect.Field;
041: import java.util.Properties;
042: import java.util.ArrayList;
043: import java.util.Arrays;
044: import java.util.ResourceBundle;
045:
046: //
047: public class ToolBoxInfo {
048: private static boolean rootSettable = false;
049:
050: //
051: static ResourceBundle res = ResourceBundle
052: .getBundle("org.enhydra.tool.common.Res"); // nores
053:
054: //
055: public static final String FILE_KERNEL_JAR = "Kernel.jar"; // nores
056: public static final String FILE_ENHYDRA_JAR = "eaf.jar"; // nores
057: public static final String WML_FACTORY = "org.enhydra.wireless.wml.WMLDomFactory"; // nores
058: public static final String CHTML_FACTORY = "org.enhydra.wireless.chtml.CHTMLDomFactory"; // nores
059: public static final String XHTML_FACTORY = "org.enhydra.xml.xhtml.XHTMLDomFactory"; // nores
060: public static final String XMLC_VERSION_CLASS = "org.enhydra.xml.xmlc.XMLCVersion"; // nores
061: public static final String ENHYDRA_ROOT = "eas.root"; // nores
062:
063: private static final String DIR_LIB = "lib"; // nores
064: private static final String VERSION = "6.5"; // nores
065: private static final String PROPERTY_FILENAME = "toolbox.properties"; // nores
066: private static final String DEFAULT_ENHYDRA_ROOT = "/usr/local/enhydra-enterprise-6.5";
067:
068: private static final String SERVER_RUN_CLASS = "org.objectweb.jonas.server.Bootstrap"; // nores
069:
070: private ToolBoxInfo() {
071: }
072:
073: public static String getCopyright() {
074: StringBuffer buf = new StringBuffer();
075:
076: buf.append('\n');
077: buf.append("Part of The Enhydra Application Server Suite\n");
078: return buf.toString();
079: }
080:
081: public static Properties loadProperties() throws ToolException {
082: String filename = getPropertyFilename();
083: File file = new File(filename);
084: Properties properties = new Properties();
085:
086: if (file.isFile() && file.canRead()) {
087: try {
088: FileInputStream in = new FileInputStream(file);
089:
090: properties.load(in);
091: } catch (FileNotFoundException e) {
092: throw new ToolException(e, ResUtil.format(res
093: .getString("Unable_to_read"), file));
094: } catch (IOException e) {
095: throw new ToolException(e, ResUtil.format(res
096: .getString("Unable_to_read"), file));
097: }
098: }
099: return properties;
100: }
101:
102: /**
103: * Store persisted generator options to the properties file. This is
104: * public and static to allow for the creation of a valid properties
105: * file without having an instance of the CodeGen object. Creating
106: * an instance of CodeGen requires that a valid properties file already
107: * exist.
108: *
109: * @param prop
110: * Properties to save into the codegen.properties file.
111: *
112: * @return
113: * A file reference to the properties file.
114: *
115: * @exception java.io.IOException
116: * Thrown if unable to write to the codegen.properties file.
117: */
118: public static File storeProperties(Properties prop)
119: throws ToolException {
120: File file = null;
121: FileOutputStream out = null;
122:
123: file = new File(ToolBoxInfo.getPropertyFilename());
124: if (file.getParentFile() != null) {
125: file.getParentFile().mkdirs();
126: }
127: try {
128: out = new FileOutputStream(file);
129: prop.store(out, res.getString("Kelp_ToolBox1"));
130: } catch (IOException e) {
131: throw new ToolException(e, ResUtil.format(res
132: .getString("Unable_to_store"), file));
133: }
134: return file;
135: }
136:
137: public static String getJavaPath() {
138: final String SYS_JAVA_HOME = "java.home"; // nores
139: String path = null;
140: File file = null;
141:
142: path = System.getProperty(SYS_JAVA_HOME, new String());
143: file = new File(path);
144: path = file.getParent().replace('\\', '/');
145: return path;
146: }
147:
148: public static String getEnhydraKeyJar() {
149: String jar = ToolBoxInfo.FILE_ENHYDRA_JAR;
150: return jar;
151: }
152:
153: public static boolean isClassAvailable(String className) {
154: boolean available = false;
155:
156: try {
157: Class.forName(className);
158: available = true;
159: } catch (ClassNotFoundException e) {
160: available = false;
161: }
162: return available;
163: }
164:
165: public static void main(String args[]) {
166: System.out.println(res.getString("ToolBox_Info"));
167: System.out.println(ToolBoxInfo.getCopyright());
168: System.out.println(ResUtil.format(res
169: .getString("ToolBox_version_0_"), ToolBoxInfo
170: .getToolBoxVersion()));
171: System.out.println(ResUtil.format(
172: "Application Server Root: {0}", ToolBoxInfo
173: .getEnhydraRoot()));
174: System.out.println(ResUtil.format(res
175: .getString("Enhydra_in_classpath"), ToolBoxInfo
176: .isEnhydraInClassPath()));
177: System.out.println(ResUtil.format(res
178: .getString("Enhydra_main_class_0_"), ToolBoxInfo
179: .getEnhydraMainClass()));
180: System.out.println(ResUtil.format(res
181: .getString("XMLC_version_0_"), ToolBoxInfo
182: .getXMLCVersion()));
183: System.out.println(ResUtil.format(res
184: .getString("XMLC_in_classpath_0_"), ToolBoxInfo
185: .isXMLCInClassPath()));
186: }
187:
188: public static boolean isXMLCVersion(int v) {
189: boolean is = false;
190: String version = ToolBoxInfo.getXMLCVersion();
191:
192: if (version.startsWith(new String() + v)) {
193: is = true;
194: }
195: return is;
196: }
197:
198: public static String getXMLCVersion() {
199: final String FIELD_VERSION = "VERSION"; // nores;
200:
201: //
202: String version = res.getString("_unknown_");
203: Class verClass = null;
204: Field 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 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 getEnhydraMainClass() {
221: String 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 e) {
231: inPath = false;
232: }
233: return inPath;
234: }
235:
236: public static boolean isEnhydraInClassPath() {
237: boolean inPath = true;
238: String checkClass = ToolBoxInfo.SERVER_RUN_CLASS;
239: try {
240: Class testClass = Class.forName(checkClass);
241: } catch (java.lang.ClassNotFoundException e) {
242: inPath = false;
243: }
244: return inPath;
245: }
246:
247: public static String getToolBoxVersion() {
248: return VERSION;
249: }
250:
251: /**
252: * Method declaration
253: *
254: *
255: * @return
256: */
257: public static String getPropertyFilename() {
258: final String SYS_USER_HOME = "user.home"; // nores
259: final String DIR_ENHYDRA = ".enhydra"; // nores
260:
261: //
262: StringBuffer buf = new StringBuffer();
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 getEnhydraRoot() {
278: String 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[] getSupportedDocTypes() {
296: final String TYPE_CHTML = "chtml"; // nores
297: final String TYPE_HTML = "html"; // nores
298: final String TYPE_WML = "wml"; // nores
299: final String TYPE_XHTML = "xhtml"; // nores
300:
301: //
302: String[] types = new String[0];
303: ArrayList list = new ArrayList(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[list.size()];
317: types = (String[]) list.toArray(types);
318: return types;
319: }
320:
321: private static String getEnhydraPathByWorkingDirectory() {
322: File file = null;
323: String path = null;
324:
325: file = new File((new String()) + '.'); // <enhydra home>/lib
326: file = new File(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 path) {
339: boolean validRoot = false;
340: File[] jars = new File[0];
341: StringBuffer buf = new StringBuffer();
342:
343: if (path != null) {
344: jars = new File[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(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 getEnhydraPathByClassLoader() {
366: final String FILE_TOOLBOX_JAR = "toolbox.jar"; // nores
367:
368: //
369: boolean found = false;
370: String[] paths = new String[0];
371: String path = null;
372: File file = null;
373: ToolBoxInfo info = new ToolBoxInfo();
374: ClassLoader 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(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 getEnhydraPathByPropertyFile() {
397: String path = null;
398: Properties properties = new Properties();
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 home) {
414: boolean valid = false;
415: StringBuffer path = new StringBuffer();
416: File 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(path.toString());
424: valid = f.isFile();
425: f = null;
426: return valid;
427: }
428:
429: }
|