Search in sources :

Example 1 with ImportPolicy

use of com.sun.enterprise.module.ImportPolicy in project glassfish-hk2 by eclipse-ee4j.

the class ModuleImpl method resolve.

/**
 * Ensure that this module is {@link ModuleState#RESOLVED resolved}.
 *
 * <p>
 * If the module is already resolved, this method does nothing.
 * Otherwise, iterate over all declared ModuleDependency instances and use the
 * associated <code>ModuleRegistry</code> to resolve it. After successful
 * completion of this method, the module state is
 * {@link ModuleState#RESOLVED}.
 *
 * @throws com.sun.enterprise.module.ResolveError if any of the declared dependency of this module
 * cannot be satisfied
 */
public synchronized void resolve() throws ResolveError {
    // already resolved ?
    if (state == ModuleState.ERROR)
        throw new ResolveError("Module " + getName() + " is in ERROR state");
    if (state.compareTo(ModuleState.RESOLVED) >= 0)
        return;
    if (state == ModuleState.PREPARING) {
        Utils.identifyCyclicDependency(this, Logger.getAnonymousLogger());
        throw new ResolveError("Cyclic dependency with " + getName());
    }
    state = ModuleState.PREPARING;
    if (moduleDef.getImportPolicyClassName() != null) {
        try {
            Class<ImportPolicy> importPolicyClass = (Class<ImportPolicy>) getPrivateClassLoader().loadClass(moduleDef.getImportPolicyClassName());
            ImportPolicy importPolicy = importPolicyClass.newInstance();
            importPolicy.prepare(this);
        } catch (ClassNotFoundException e) {
            state = ModuleState.ERROR;
            throw new ResolveError(e);
        } catch (java.lang.InstantiationException e) {
            state = ModuleState.ERROR;
            throw new ResolveError(e);
        } catch (IllegalAccessException e) {
            state = ModuleState.ERROR;
            throw new ResolveError(e);
        }
    }
    for (ModuleDependency dependency : moduleDef.getDependencies()) {
        ModuleImpl depModule = (ModuleImpl) registry.makeModuleFor(dependency.getName(), dependency.getVersion());
        if (depModule == null) {
            state = ModuleState.ERROR;
            throw new ResolveError(dependency + " referenced from " + moduleDef.getName() + " is not resolved");
        }
        // if (Utils.isLoggable(Level.INFO)) {
        // Utils.getDefaultLogger().info("For module" + getName() + " adding new dependent " + module.getName());
        // }
        dependencies.add(depModule);
    }
    // once we have proper import/export filtering for modules, we can
    // build a look-up table to improve performance
    // build up the complete list of transitive dependency modules, without any duplication,
    // in a breadth-first fashion. The reason we do this in breadth-first is to reduce
    // the search time based on the assumption that classes tend to be discovered in close dependencies.
    List<ModuleImpl> transitiveDependencies = new ArrayList<ModuleImpl>();
    Set<ModuleImpl> transitiveDependenciesSet = new HashSet<ModuleImpl>();
    LinkedList<ModuleImpl> q = new LinkedList<ModuleImpl>();
    q.addAll(dependencies);
    while (!q.isEmpty()) {
        ModuleImpl m = q.removeFirst();
        if (transitiveDependenciesSet.add(m)) {
            // first time visited
            transitiveDependencies.add(m);
            m.resolve();
            q.addAll(m.dependencies);
        }
    }
    for (ModuleImpl m : transitiveDependencies) {
        getPrivateClassLoader().addDelegate(m.getClassLoader());
    }
    // Logger.global.info("HK2Module " + getName() + " resolved");
    state = ModuleState.RESOLVED;
    for (ModuleLifecycleListener l : registry.getLifecycleListeners()) {
        l.moduleResolved(this);
    }
}
Also used : ModuleDependency(com.sun.enterprise.module.ModuleDependency) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ModuleLifecycleListener(com.sun.enterprise.module.ModuleLifecycleListener) ImportPolicy(com.sun.enterprise.module.ImportPolicy) ResolveError(com.sun.enterprise.module.ResolveError) HashSet(java.util.HashSet)

Aggregations

ImportPolicy (com.sun.enterprise.module.ImportPolicy)1 ModuleDependency (com.sun.enterprise.module.ModuleDependency)1 ModuleLifecycleListener (com.sun.enterprise.module.ModuleLifecycleListener)1 ResolveError (com.sun.enterprise.module.ResolveError)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1