Search in sources :

Example 1 with ResolveError

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

the class OSGiModuleImpl method start.

@Override
public synchronized void start() throws ResolveError {
    int state = bundle.getState();
    if (((Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING) & state) != 0) {
        if (logger.isLoggable(Level.FINER)) {
            logger.logp(Level.FINER, "OSGiModuleImpl", "start", "Ignoring start of bundle {0} as it is in {1} state", new Object[] { bundle, toString(bundle.getState()) });
        }
        return;
    }
    if (registry.getPackageAdmin().getBundleType(bundle) == PackageAdmin.BUNDLE_TYPE_FRAGMENT) {
        if (logger.isLoggable(Level.FINER)) {
            logger.logp(Level.FINER, "OSGiModuleImpl", "start", "Ignoring start of bundle {0} as it is a fragment bundle", new Object[] { bundle });
        }
        return;
    }
    try {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction() {

                    public Object run() throws BundleException {
                        startBundle();
                        return null;
                    }
                });
            } catch (PrivilegedActionException e) {
                throw (BundleException) e.getException();
            }
        } else {
            startBundle();
        }
        isTransientlyActive = true;
        if (logger.isLoggable(Level.FINE)) {
            logger.logp(Level.FINE, "OSGiModuleImpl", "start", "Started bundle {0}", bundle);
        }
    } catch (BundleException e) {
        throw new ResolveError("Failed to start " + this, e);
    }
    // if there is a LifecyclePolicy, then instantiate and invoke.
    if (md.getLifecyclePolicyClassName() != null) {
        try {
            Class<LifecyclePolicy> lifecyclePolicyClass = (Class<LifecyclePolicy>) bundle.loadClass(md.getLifecyclePolicyClassName());
            lifecyclePolicy = lifecyclePolicyClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new ResolveError("ClassNotFound : " + e.getMessage(), e);
        } catch (java.lang.InstantiationException | IllegalAccessException e) {
            throw new ResolveError(e);
        }
    }
    if (lifecyclePolicy != null) {
        lifecyclePolicy.start(this);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) LifecyclePolicy(com.sun.enterprise.module.LifecyclePolicy) ResolveError(com.sun.enterprise.module.ResolveError) BundleException(org.osgi.framework.BundleException)

Example 2 with ResolveError

use of com.sun.enterprise.module.ResolveError 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)

Example 3 with ResolveError

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

the class ModuleImpl method start.

/**
 * Forces module startup. In most cases, the runtime will take care
 * of starting modules when they are first used. There could be cases where
 * code need to manually start a sub module. Invoking this method will
 * move the module to the {@link ModuleState#READY ModuleState.READY}, the
 * {@link LifecyclePolicy#start Lifecycle.start} method will be invoked.
 */
public void start() throws ResolveError {
    if (state == ModuleState.READY)
        return;
    // ensure RESOLVED state
    resolve();
    for (HK2Module subModules : dependencies) {
        subModules.start();
    }
    // time to initialize the lifecycle instance
    if (moduleDef.getLifecyclePolicyClassName() != null) {
        try {
            Class<LifecyclePolicy> lifecyclePolicyClass = (Class<LifecyclePolicy>) getPrivateClassLoader().loadClass(moduleDef.getLifecyclePolicyClassName());
            lifecyclePolicy = lifecyclePolicyClass.newInstance();
        } catch (ClassNotFoundException e) {
            state = ModuleState.ERROR;
            throw new ResolveError("ClassNotFound : " + e.getMessage(), e);
        } catch (java.lang.InstantiationException e) {
            state = ModuleState.ERROR;
            throw new ResolveError(e);
        } catch (IllegalAccessException e) {
            state = ModuleState.ERROR;
            throw new ResolveError(e);
        }
    }
    if (lifecyclePolicy != null) {
        lifecyclePolicy.start(this);
    }
    state = ModuleState.READY;
    // module started. notify listeners
    for (ModuleLifecycleListener listener : registry.getLifecycleListeners()) listener.moduleStarted(this);
}
Also used : ModuleLifecycleListener(com.sun.enterprise.module.ModuleLifecycleListener) HK2Module(com.sun.enterprise.module.HK2Module) LifecyclePolicy(com.sun.enterprise.module.LifecyclePolicy) ResolveError(com.sun.enterprise.module.ResolveError)

Example 4 with ResolveError

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

the class AbstractModulesRegistryImpl method makeModuleFor.

public HK2Module makeModuleFor(String name, String version, boolean resolve) throws ResolveError {
    HK2Module module;
    if (parent != null) {
        module = parent.makeModuleFor(name, version, resolve);
        if (module != null)
            return module;
    }
    module = modules.get(AbstractFactory.getInstance().createModuleId(name, version));
    if (module == null && version == null) {
        Collection<HK2Module> matchingModules = getModules(name);
        if (!matchingModules.isEmpty()) {
            module = matchingModules.iterator().next();
        }
    }
    if (module == null) {
        module = loadFromRepository(name, version);
        if (module != null) {
            add(module);
        }
    }
    if (module != null && resolve) {
        try {
            module.resolve();
        } catch (Throwable e) {
            module.uninstall();
            throw new ResolveError(e);
        }
    }
    return module;
}
Also used : HK2Module(com.sun.enterprise.module.HK2Module) ResolveError(com.sun.enterprise.module.ResolveError)

Aggregations

ResolveError (com.sun.enterprise.module.ResolveError)4 HK2Module (com.sun.enterprise.module.HK2Module)2 LifecyclePolicy (com.sun.enterprise.module.LifecyclePolicy)2 ModuleLifecycleListener (com.sun.enterprise.module.ModuleLifecycleListener)2 ImportPolicy (com.sun.enterprise.module.ImportPolicy)1 ModuleDependency (com.sun.enterprise.module.ModuleDependency)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 BundleException (org.osgi.framework.BundleException)1