Search in sources :

Example 1 with ModuleClassLoader

use of org.openmrs.module.ModuleClassLoader in project openmrs-core by openmrs.

the class OpenmrsClassLoader method getResources.

/**
 * Searches all known module classloaders first, then parent classloaders
 *
 * @see java.lang.ClassLoader#getResources(java.lang.String)
 */
@Override
public Enumeration<URL> getResources(String packageName) throws IOException {
    Set<URI> results = new HashSet<>();
    for (ModuleClassLoader classLoader : ModuleFactory.getModuleClassLoaders()) {
        Enumeration<URL> urls = classLoader.getResources(packageName);
        while (urls.hasMoreElements()) {
            URL result = urls.nextElement();
            if (result != null) {
                try {
                    results.add(result.toURI());
                } catch (URISyntaxException e) {
                    throwInvalidURI(result, e);
                }
            }
        }
    }
    for (Enumeration<URL> en = super.getResources(packageName); en.hasMoreElements(); ) {
        URL url = en.nextElement();
        try {
            results.add(url.toURI());
        } catch (URISyntaxException e) {
            throwInvalidURI(url, e);
        }
    }
    List<URL> resources = new ArrayList<>(results.size());
    for (URI result : results) {
        resources.add(result.toURL());
    }
    return Collections.enumeration(resources);
}
Also used : ModuleClassLoader(org.openmrs.module.ModuleClassLoader) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) HashSet(java.util.HashSet)

Example 2 with ModuleClassLoader

use of org.openmrs.module.ModuleClassLoader in project openmrs-core by openmrs.

the class OpenmrsClassLoader method loadClass.

/**
 * It loads classes from the web container class loader first (parent class loader) and then
 * tries module class loaders.
 *
 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
 * @should load class from cache second time
 * @should not load class from cache if class loader has been disposed
 * @should load class from parent first
 * @should load class if two module class loaders have same packages
 */
@Override
public synchronized Class<?> loadClass(String name, final boolean resolve) throws ClassNotFoundException {
    // Check if the class has already been requested from this class loader
    Class<?> c = getCachedClass(name);
    if (c == null) {
        // We do not try to load classes using this.findClass on purpose.
        // All classes are loaded by web container or by module class loaders.
        // First try loading from modules such that we allow modules to load
        // different versions of the same libraries that may already be used
        // by core or the web container. An example is the chartsearch module
        // which uses different versions of lucene and solr from core
        String packageName = StringUtils.substringBeforeLast(name, ".");
        Set<ModuleClassLoader> moduleClassLoaders = ModuleFactory.getModuleClassLoadersForPackage(packageName);
        for (ModuleClassLoader moduleClassLoader : moduleClassLoaders) {
            try {
                c = moduleClassLoader.loadClass(name);
                break;
            } catch (ClassNotFoundException e) {
            // Continue trying...
            }
        }
        if (c == null) {
            // Finally try loading from web container
            c = getParent().loadClass(name);
        }
        cacheClass(name, c);
    }
    if (resolve) {
        resolveClass(c);
    }
    return c;
}
Also used : ModuleClassLoader(org.openmrs.module.ModuleClassLoader)

Example 3 with ModuleClassLoader

use of org.openmrs.module.ModuleClassLoader in project openmrs-core by openmrs.

the class OpenmrsClassLoader method findResource.

/**
 * @see java.net.URLClassLoader#findResource(java.lang.String)
 */
@Override
public URL findResource(final String name) {
    if (log.isTraceEnabled()) {
        log.trace("finding resource: " + name);
    }
    URL result;
    for (ModuleClassLoader classLoader : ModuleFactory.getModuleClassLoaders()) {
        result = classLoader.findResource(name);
        if (result != null) {
            return result;
        }
    }
    // look for the resource in the parent
    result = super.findResource(name);
    // expand the jar url if necessary
    if (result != null && "jar".equals(result.getProtocol()) && name.contains("openmrs")) {
        result = expandURL(result, getLibCacheFolder());
    }
    return result;
}
Also used : ModuleClassLoader(org.openmrs.module.ModuleClassLoader) URL(java.net.URL)

Example 4 with ModuleClassLoader

use of org.openmrs.module.ModuleClassLoader in project openmrs-module-mirebalais by PIH.

the class CustomAppLoaderUtil method determineResourcePath.

public static String determineResourcePath(Config config, String providerName, String prefix, String resource) {
    try {
        // kind of ugly, and I couldn't mock it properly
        ModuleClassLoader mcl = ModuleFactory.getModuleClassLoader(ModuleFactory.getStartedModuleById(providerName));
        // first try full path with country and site
        String resourcePath = prefix + "/" + config.getCountry().name().toLowerCase() + "/" + config.getSite().name().toLowerCase() + "/" + resource;
        if (mcl.findResource(BASE_PREFIX + resourcePath) != null) {
            return providerName + ":" + resourcePath;
        }
        // now try just country path
        resourcePath = prefix + "/" + config.getCountry().name().toLowerCase() + "/" + resource;
        if (mcl.findResource(BASE_PREFIX + resourcePath) != null) {
            return providerName + ":" + resourcePath;
        }
        // now the base path
        resourcePath = prefix + "/" + resource;
        if (mcl.findResource(BASE_PREFIX + resourcePath) != null) {
            return providerName + ":" + resourcePath;
        }
    }// this catch and the return are kind of hacky, I did it this way to get the MirebalaisHospitalActivatorComponentTest to pass, but we should do this btter
     catch (Exception e) {
        log.error("Unable to find resource " + resource + " from provider " + providerName + " with prefix " + prefix + " - This error is expected when running tests.", e);
    }
    return "";
}
Also used : ModuleClassLoader(org.openmrs.module.ModuleClassLoader)

Example 5 with ModuleClassLoader

use of org.openmrs.module.ModuleClassLoader in project openmrs-core by openmrs.

the class OpenmrsClassLoader method findResources.

/**
 * @see java.net.URLClassLoader#findResources(java.lang.String)
 */
@Override
public Enumeration<URL> findResources(final String name) throws IOException {
    Set<URI> results = new HashSet<>();
    for (ModuleClassLoader classLoader : ModuleFactory.getModuleClassLoaders()) {
        Enumeration<URL> urls = classLoader.findResources(name);
        while (urls.hasMoreElements()) {
            URL result = urls.nextElement();
            if (result != null) {
                try {
                    results.add(result.toURI());
                } catch (URISyntaxException e) {
                    throwInvalidURI(result, e);
                }
            }
        }
    }
    for (Enumeration<URL> en = super.findResources(name); en.hasMoreElements(); ) {
        URL url = en.nextElement();
        try {
            results.add(url.toURI());
        } catch (URISyntaxException e) {
            throwInvalidURI(url, e);
        }
    }
    List<URL> resources = new ArrayList<>(results.size());
    for (URI result : results) {
        resources.add(result.toURL());
    }
    return Collections.enumeration(resources);
}
Also used : ModuleClassLoader(org.openmrs.module.ModuleClassLoader) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) HashSet(java.util.HashSet)

Aggregations

ModuleClassLoader (org.openmrs.module.ModuleClassLoader)6 URL (java.net.URL)3 HashSet (java.util.HashSet)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)1