loadresources.java

来自「一个java 代码生成器」· Java 代码 · 共 121 行

JAVA
121
字号
/**
 * Copyright (c) 2002, Siddhartha P. Chandurkar siddhartha@visioncodified.com
 * All rights reserved.
 * Licensed under the Academic Free License version 1.1
 * See the file LICENSE.TXT for details.
 * LICENSE.txt is located in the directory  <install-directory>\Jenerator
 * of your Jenertaor Installation.
 *
 */
package com.jenerator.util;

//<Imports>

import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

//</Imports>


/**
 * LoadResources
 * NOTE: This class has not ready for the BETA release it is not completed becuase of time constraints.
 * Has to be taken care in the final release.
 *
 * This class has the responsibility to Load all the resources which are needed for
 * the Jenerator. It builds the java classpath dynamically based on the library
 * archives found in $JEN_HOME/lib/ext i.e. .jar and .zip files
 * @author Siddhartha P. Chandurkar
 * @version 0.9.0
 */
public class LoadResources {

    //ATTRIBUTES
    //The log4j logger for this class
    private static Logger log = Logger.getLogger(com.jenerator.util.LoadResources.class.getName());
    private static JenClassLoader jenClassLoader;


    //CONSTRUCTORS
    /**
     * LoadResources
     * Default constructor

     public LoadResources(JenClassLoader _jenClassLoader) {
     jenClassLoader = _jenClassLoader;

     }
     */
    /**
     * LoadResources
     */
    public LoadResources() {
    }

    /**
     * makeClasspath
     * This method reads the library archive files in the $JEN_HOME/lib/ext and
     * appends it to the classpath
     */
    public void makeClasspath() throws ConfigurationException {
        try {
            ClassLoader jvmClassLoader = Thread.currentThread().getContextClassLoader();
            URL urls[] = new URL[]{new URL("file://" + Configurator.getInstance().getJenHome() + File.separator + "lib")};
            JenClassLoader jenClassLoader = new JenClassLoader(urls, jvmClassLoader);

            setJenClassLoader(jenClassLoader);
            String pathSeparator = System.getProperty("path.separator");
            String classPath = System.getProperty("java.class.path");
            String url = "file://" + Configurator.getInstance().getJenHome() + File.separator + "lib" + File.separator + "ext";
            URL u = new URL(getClass().getProtectionDomain().getCodeSource().getLocation(), url);
            File dir = new File(u.getFile());
            String[] files = dir.list();
            for (int i = 0; i < files.length; i++) {
                if (files[i].endsWith(".zip") || files[i].endsWith(".jar")) {
                    URL file = new File(dir, files[i]).getCanonicalFile().toURL();
                    try {
                        jenClassLoader.addURL(file);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    classPath += pathSeparator + file.getFile();

                }
            }
            System.setProperty("java.class.path", classPath);
            log.debug("New java classpath set :- " + classPath);
            try {
                jenClassLoader.findClass("oracle.jdbc.driver.OracleDriver");
            } catch (Exception e) {
                log.fatal("ERROR ", e);
            }
        } catch (MalformedURLException e) {
            log.fatal("Could not set the java classpath :- ", e);
            throw new ConfigurationException(e);
        } catch (IOException e) {
            log.fatal("Could not set the java classpath :- ", e);
            throw new ConfigurationException(e);
        }

    }//makeClasspath

    /**
     * findClass
     */
    public Class findClass(String s) throws ClassNotFoundException {
        return jenClassLoader.findClass(s);
    }//findClass

    /**
     * setJenClassLoader
     *
     */
    private void setJenClassLoader(JenClassLoader _jenClassLoader) {
        jenClassLoader = _jenClassLoader;
    }//setJenClassLoader

}//LoadResources

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?