Search in sources :

Example 1 with Classpath

use of org.apache.ofbiz.base.start.Classpath in project ofbiz-framework by apache.

the class ComponentContainer method loadClassPathForAllComponents.

/**
 * Iterate over all the components and load their classpath URLs into the classloader
 * and set the classloader as the context classloader
 *
 * @param componentsClassPath a list of classpaths for all components
 * @throws ContainerException
 */
private void loadClassPathForAllComponents(List<Classpath> componentsClassPath) throws ContainerException {
    List<URL> allComponentUrls = new ArrayList<>();
    for (Classpath classPath : componentsClassPath) {
        try {
            allComponentUrls.addAll(Arrays.asList(classPath.getUrls()));
        } catch (MalformedURLException e) {
            Debug.logError("Unable to load component classpath" + classPath.toString(), module);
            Debug.logError(e.getMessage(), module);
        }
    }
    URL[] componentURLs = allComponentUrls.toArray(new URL[allComponentUrls.size()]);
    URLClassLoader classLoader = new URLClassLoader(componentURLs, Thread.currentThread().getContextClassLoader());
    Thread.currentThread().setContextClassLoader(classLoader);
}
Also used : MalformedURLException(java.net.MalformedURLException) Classpath(org.apache.ofbiz.base.start.Classpath) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 2 with Classpath

use of org.apache.ofbiz.base.start.Classpath in project ofbiz-framework by apache.

the class ComponentContainer method loadComponent.

/**
 * Load a single component by adding all its classpath entries to
 * the list of classpaths to be loaded
 *
 * @param config the component configuration
 * @throws IOException
 */
private void loadComponent(ComponentConfig config) throws IOException {
    if (config.enabled()) {
        Classpath classpath = buildClasspathFromComponentConfig(config);
        componentsClassPath.add(classpath);
        Debug.logInfo("Added class path for component : [" + config.getComponentName() + "]", module);
    } else {
        Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it is disabled", module);
    }
}
Also used : Classpath(org.apache.ofbiz.base.start.Classpath)

Example 3 with Classpath

use of org.apache.ofbiz.base.start.Classpath in project ofbiz-framework by apache.

the class ComponentContainer method buildClasspathFromComponentConfig.

/**
 * Construct a <code>Classpath</code> object for a certain component based
 * on its configuration defined in <code>ComponentConfig</code>
 *
 * @param config the component configuration
 * @return the constructed classpath
 * @throws IOException
 */
private Classpath buildClasspathFromComponentConfig(ComponentConfig config) throws IOException {
    Classpath classPath = new Classpath();
    String configRoot = config.getRootLocation().replace('\\', '/');
    configRoot = configRoot.endsWith("/") ? configRoot : configRoot + "/";
    List<ComponentConfig.ClasspathInfo> classpathInfos = config.getClasspathInfos();
    for (ComponentConfig.ClasspathInfo cp : classpathInfos) {
        String location = cp.location.replace('\\', '/');
        if (!"jar".equals(cp.type) && !"dir".equals(cp.type)) {
            Debug.logError("Classpath type '" + cp.type + "' is not supported; '" + location + "' not loaded", module);
            continue;
        }
        location = location.startsWith("/") ? location.substring(1) : location;
        String dirLoc = location.endsWith("/*") ? location.substring(0, location.length() - 2) : location;
        File path = FileUtil.getFile(configRoot + dirLoc);
        if (path.exists()) {
            classPath.addComponent(configRoot + location);
            if (path.isDirectory() && "dir".equals(cp.type)) {
                classPath.addFilesFromPath(path);
            }
        } else {
            Debug.logWarning("Location '" + configRoot + dirLoc + "' does not exist", module);
        }
    }
    return classPath;
}
Also used : ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) Classpath(org.apache.ofbiz.base.start.Classpath) File(java.io.File)

Aggregations

Classpath (org.apache.ofbiz.base.start.Classpath)3 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 ComponentConfig (org.apache.ofbiz.base.component.ComponentConfig)1