ToolBoxInfo.java :  » J2EE » enhydra » org » enhydra » tool » Java Open Source

Java Open Source » J2EE » enhydra 
enhydra » org » enhydra » tool » ToolBoxInfo.java

/*
 * Enhydra Java Application Server Project
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific terms governing rights and limitations
 * under the Licenseget.
 *
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 *
 * Contributor(s):
 * Paul Mahar
 *
 */

//
package org.enhydra.tool;

// ToolBox
import org.enhydra.tool.common.FileUtil;
import org.enhydra.tool.common.PathHandle;
import org.enhydra.tool.common.ResUtil;
import org.enhydra.tool.common.ToolException;

// JDK
import java.net.URLClassLoader;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;

//
public class ToolBoxInfo {
    private static boolean      rootSettable = false;

    //
    static ResourceBundle res = ResourceBundle.getBundle("org.enhydra.tool.common.Res");     // nores

    //
    public static final String  FILE_KERNEL_JAR = "Kernel.jar";      // nores
    public static final String  FILE_ENHYDRA_JAR = "eaf.jar";    // nores
    public static final String  WML_FACTORY =
        "org.enhydra.wireless.wml.WMLDomFactory";                    // nores
    public static final String  CHTML_FACTORY =
        "org.enhydra.wireless.chtml.CHTMLDomFactory";                // nores
    public static final String  XHTML_FACTORY =
        "org.enhydra.xml.xhtml.XHTMLDomFactory";                     // nores
    public static final String  XMLC_VERSION_CLASS =
        "org.enhydra.xml.xmlc.XMLCVersion";                          // nores
    public static final String  ENHYDRA_ROOT = "eas.root";           // nores

    private static final String DIR_LIB = "lib";                     // nores
    private static final String VERSION = "6.5";                    // nores
    private static final String PROPERTY_FILENAME =
        "toolbox.properties";                                        // nores
    private static final String DEFAULT_ENHYDRA_ROOT =
        "/usr/local/enhydra-enterprise-6.5";

    private static final String SERVER_RUN_CLASS =
        "org.objectweb.jonas.server.Bootstrap"; // nores


    private ToolBoxInfo() {}

    public static String getCopyright() {
        StringBuffer buf = new StringBuffer();

        buf.append('\n');
        buf.append("Part of The Enhydra Application Server Suite\n");
        return buf.toString();
    }

    public static Properties loadProperties() throws ToolException {
        String     filename = getPropertyFilename();
        File       file = new File(filename);
        Properties properties = new Properties();

        if (file.isFile() && file.canRead()) {
            try {
                FileInputStream in = new FileInputStream(file);

                properties.load(in);
            } catch (FileNotFoundException e) {
                throw new ToolException(e,
                                        ResUtil.format(res.getString("Unable_to_read"), file));
            } catch (IOException e) {
                throw new ToolException(e,
                                        ResUtil.format(res.getString("Unable_to_read"), file));
            }
        }
        return properties;
    }

    /**
     * Store persisted generator options to the properties file. This is
     * public and static to allow for the creation of a valid properties
     * file without having an instance of the CodeGen object. Creating
     * an instance of CodeGen requires that a valid properties file already
     * exist.
     *
     * @param prop
     * Properties to save into the codegen.properties file.
     *
     * @return
     * A file reference to the properties file.
     *
     * @exception java.io.IOException
     * Thrown if unable to write to the codegen.properties file.
     */
    public static File storeProperties(Properties prop) throws ToolException {
        File             file = null;
        FileOutputStream out = null;

        file = new File(ToolBoxInfo.getPropertyFilename());
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        try {
            out = new FileOutputStream(file);
            prop.store(out, res.getString("Kelp_ToolBox1"));
        } catch (IOException e) {
            throw new ToolException(e,
                                    ResUtil.format(res.getString("Unable_to_store"), file));
        }
        return file;
    }

    public static String getJavaPath() {
        final String SYS_JAVA_HOME = "java.home";    // nores
        String       path = null;
        File         file = null;

        path = System.getProperty(SYS_JAVA_HOME, new String());
        file = new File(path);
        path = file.getParent().replace('\\', '/');
        return path;
    }

    public static String getEnhydraKeyJar() {
        String jar = ToolBoxInfo.FILE_ENHYDRA_JAR;
        return jar;
    }

    public static boolean isClassAvailable(String className) {
        boolean available = false;

        try {
            Class.forName(className);
            available = true;
        } catch (ClassNotFoundException e) {
            available = false;
        }
        return available;
    }

    public static void main(String args[]) {
        System.out.println(res.getString("ToolBox_Info"));
        System.out.println(ToolBoxInfo.getCopyright());
        System.out.println(ResUtil.format(res.getString("ToolBox_version_0_"),
                                          ToolBoxInfo.getToolBoxVersion()));
        System.out.println(ResUtil.format("Application Server Root: {0}",
                                          ToolBoxInfo.getEnhydraRoot()));
        System.out.println(ResUtil.format(res.getString("Enhydra_in_classpath"),
                                          ToolBoxInfo.isEnhydraInClassPath()));
        System.out.println(ResUtil.format(res.getString("Enhydra_main_class_0_"),
                                          ToolBoxInfo.getEnhydraMainClass()));
        System.out.println(ResUtil.format(res.getString("XMLC_version_0_"),
                                          ToolBoxInfo.getXMLCVersion()));
        System.out.println(ResUtil.format(res.getString("XMLC_in_classpath_0_"),
                                          ToolBoxInfo.isXMLCInClassPath()));
    }

    public static boolean isXMLCVersion(int v) {
        boolean is = false;
        String  version = ToolBoxInfo.getXMLCVersion();

        if (version.startsWith(new String() + v)) {
            is = true;
        }
        return is;
    }

    public static String getXMLCVersion() {
        final String FIELD_VERSION = "VERSION";    // nores;

        //
        String       version = res.getString("_unknown_");
        Class        verClass = null;
        Field        verField = null;

        try {
            verClass = Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS);
            verField = verClass.getDeclaredField(FIELD_VERSION);
            version = verField.get(null).toString();
        } catch (Exception e) {

            // no XMLC
        }
        return version;
    }

    /**
     * Get the class to run to start MultiServer.
     */
    public static String getEnhydraMainClass() {
        String   runClass = ToolBoxInfo.SERVER_RUN_CLASS;
        return runClass;
    }

    public static boolean isXMLCInClassPath() {
        boolean inPath = true;

        try {
            Class.forName(ToolBoxInfo.XMLC_VERSION_CLASS);
        } catch (java.lang.ClassNotFoundException e) {
            inPath = false;
        }
        return inPath;
    }

    public static boolean isEnhydraInClassPath() {
        boolean inPath = true;
        String checkClass = ToolBoxInfo.SERVER_RUN_CLASS;
        try {
            Class testClass = Class.forName(checkClass);
        } catch (java.lang.ClassNotFoundException e) {
            inPath = false;
        }
        return inPath;
    }

    public static String getToolBoxVersion() {
        return VERSION;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    public static String getPropertyFilename() {
        final String SYS_USER_HOME = "user.home";    // nores
        final String DIR_ENHYDRA = ".enhydra";       // nores

        //
        StringBuffer buf = new StringBuffer();

        buf.append(System.getProperties().getProperty(SYS_USER_HOME));
        buf.append(File.separator);
        buf.append(DIR_ENHYDRA);
        buf.append(File.separator);
        buf.append(PROPERTY_FILENAME);
        return buf.toString();
    }

    public static boolean isRootSettable() {
        ToolBoxInfo.getEnhydraRoot();
        return ToolBoxInfo.rootSettable;
    }

    public static String getEnhydraRoot() {
        String path = null;

        ToolBoxInfo.rootSettable = false;
        path = getEnhydraPathByClassLoader();
        if (path == null) {
            path = getEnhydraPathByWorkingDirectory();
        }
        if (path == null) {
            ToolBoxInfo.rootSettable = true;
            path = getEnhydraPathByPropertyFile();
        }
        if (path == null) {
            path = ToolBoxInfo.DEFAULT_ENHYDRA_ROOT;
        }
        return PathHandle.createPathString(path);
    }

    public static String[] getSupportedDocTypes() {
        final String TYPE_CHTML = "chtml";    // nores
        final String TYPE_HTML = "html";      // nores
        final String TYPE_WML = "wml";        // nores
        final String TYPE_XHTML = "xhtml";    // nores

        //
        String[]     types = new String[0];
        ArrayList    list = new ArrayList(Arrays.asList(types));

        list.add(TYPE_HTML);
        if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.WML_FACTORY)) {
            list.add(TYPE_WML);
        }
        if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.CHTML_FACTORY)) {
            list.add(TYPE_CHTML);
        }
        if (ToolBoxInfo.isClassAvailable(ToolBoxInfo.XHTML_FACTORY)) {
            list.add(TYPE_XHTML);
        }
        list.trimToSize();
        types = new String[list.size()];
        types = (String[]) list.toArray(types);
        return types;
    }

    private static String getEnhydraPathByWorkingDirectory() {
        File   file = null;
        String path = null;

        file = new File((new String()) + '.');    // <enhydra home>/lib
        file = new File(file.getAbsolutePath());
        file = file.getParentFile();
        if ((file.getParentFile() != null)
                && (file.getParentFile().getParentFile() != null)) {
            path = file.getParentFile().getParent();
        }
        if (!ToolBoxInfo.isEnhydraRoot(path)) {
            path = null;
        }
        return path;
    }

    public static boolean isEnhydraRoot(String path) {
        boolean      validRoot = false;
        File[]       jars = new File[0];
        StringBuffer buf = new StringBuffer();

        if (path != null) {
           jars = new File[1];

            // enhydra 3
            buf.append(path);
            buf.append(File.separator);
            buf.append(DIR_LIB);
            buf.append(File.separator);
            buf.append(ToolBoxInfo.FILE_ENHYDRA_JAR);
            jars[0] = new File(buf.toString());
        }

        //
        for (int i = 0; i < jars.length; i++) {
            if (jars[i].isFile()) {
                validRoot = true;
                break;
            }
        }
        return validRoot;
    }

    private static String getEnhydraPathByClassLoader() {
        final String FILE_TOOLBOX_JAR = "toolbox.jar";    // nores

        //
        boolean      found = false;
        String[]     paths = new String[0];
        String       path = null;
        File         file = null;
        ToolBoxInfo  info = new ToolBoxInfo();
        ClassLoader  loader = info.getClass().getClassLoader();

        paths = FileUtil.findJarPaths(FILE_TOOLBOX_JAR, loader);
        for (int i = 0; i < paths.length; i++) {
            file = new File(paths[i]);
            if (file.getParentFile() != null) {
                file = file.getParentFile();
                if ((file.getParentFile() != null)
                        && (file.getParentFile().getParentFile() != null)) {
                    path = file.getParentFile().getParent();
                    found = ToolBoxInfo.isEnhydraRoot(path);
                }
            }
            if (found) {
                break;
            } else {
                path = null;
            }
        }
        return path;
    }

    private static String getEnhydraPathByPropertyFile() {
        String     path = null;
        Properties properties = new Properties();

        try {
            properties = ToolBoxInfo.loadProperties();
            path = properties.getProperty(ToolBoxInfo.ENHYDRA_ROOT);
        } catch (ToolException e) {
            e.printStackTrace();
            path = null;
        }
        if (!ToolBoxInfo.isEnhydraRoot(path)) {
            path = null;
        }
        return path;
    }

    private boolean isEnhydraPathValid(String home) {
        boolean      valid = false;
        StringBuffer path = new StringBuffer();
        File         f = null;

        path.append(home);
        path.append(File.separator);
        path.append(ToolBoxInfo.DIR_LIB);
        path.append(File.separator);
        path.append(ToolBoxInfo.FILE_ENHYDRA_JAR);
        f = new File(path.toString());
        valid = f.isFile();
        f = null;
        return valid;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.