Search in sources :

Example 1 with ResourceClassLoader

use of org.glassfish.api.deployment.ResourceClassLoader in project Payara by payara.

the class ApplicationState method start.

/**
 * Starts the Application state for new deployment by copying the cached
 * metadata and properties to the new {@code DeploymentContext} instance.
 *
 * @param commandParams
 * @param newContext
 * @param events
 * @return
 */
public boolean start(DeployCommandParameters commandParams, ExtendedDeploymentContext newContext, Events events) {
    validateInactiveState();
    this.active = true;
    if (commandParams.sourcesChanged != null) {
        this.classesChanged = new HashMap<>();
        for (String sourcePath : commandParams.sourcesChanged) {
            String className = getClassName(sourcePath);
            if (className != null) {
                this.classesChanged.put(className, sourcePath);
            }
        }
    }
    if (hotswap) {
        final Map<Class<?>, byte[]> reloadMap = new HashMap<>();
        ResourceClassLoader previousResourceClassLoader = ResourceClassLoader.class.cast(applicationClassLoader);
        ConcurrentHashMap<String, ResourceEntry> previousResourceEntries = previousResourceClassLoader.getResourceEntries();
        previousResourceEntries.entrySet().stream().filter(e -> classesChanged.containsKey(e.getKey())).forEach(e -> {
            Class clazz = previousResourceClassLoader.reloadResourceEntry(e.getKey(), classesChanged.get(e.getKey()), e.getValue());
            reloadMap.put(clazz, e.getValue().binaryContent);
        });
        // Update application classloader
        HotSwapHelper.hotswap(reloadMap);
        newContext.setClassLoader(applicationClassLoader);
        ProgressTracker tracker = newContext.getTransientAppMetaData(ExtendedDeploymentContext.TRACKER, ProgressTracker.class);
        try {
            // Reload application metadata
            reloadApplicationMetaData(newContext);
            // Reload application engines
            applicationInfo.reload(newContext, tracker);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return false;
    } else {
        if (this.applicationInfo != null) {
            this.previousClassLoaders = getClassLoaders(this.applicationInfo);
        }
        newContext.getAppProps().putAll(this.deploymentContext.getAppProps());
        newContext.getModulePropsMap().putAll(this.deploymentContext.getModulePropsMap());
        this.deploymentContext = newContext;
        Set<Class> requiredMetaDataClasses = requiredMetaDataClasses();
        this.modulesMetaData.values().stream().filter(md -> !requiredMetaDataClasses.contains(md.getClass())).forEach(newContext::addModuleMetaData);
        this.getDescriptorMetadata().entrySet().forEach(e -> newContext.addTransientAppMetaData(e.getKey(), e.getValue()));
        if (applicationClassLoader != null && applicationClassLoader instanceof ResourceClassLoader) {
            ClassLoader newClassLoader = newContext.getArchiveHandler().getClassLoader(applicationClassLoader.getParent(), newContext);
            ResourceClassLoader newResourceClassLoader = ResourceClassLoader.class.cast(newClassLoader);
            ResourceClassLoader previousResourceClassLoader = ResourceClassLoader.class.cast(applicationClassLoader);
            ConcurrentHashMap<String, ResourceEntry> previousResourceEntries = previousResourceClassLoader.getResourceEntries();
            previousResourceEntries.entrySet().stream().filter(e -> !classesChanged.containsKey(e.getKey())).forEach(e -> newResourceClassLoader.addResourceEntry(e.getKey(), classesChanged.get(e.getKey()), e.getValue()));
            newContext.setClassLoader(newClassLoader);
            if (this.applicationInfo != null) {
                // unload previous app
                events.send(new EventListener.Event<>(Deployment.APPLICATION_UNLOADED, this.applicationInfo), false);
            }
        }
    }
    return true;
}
Also used : Arrays(java.util.Arrays) ApplicationMetaDataProvider(org.glassfish.api.deployment.ApplicationMetaDataProvider) ProgressTracker(org.glassfish.internal.data.ProgressTracker) Globals(org.glassfish.internal.api.Globals) Events(org.glassfish.api.event.Events) HashMap(java.util.HashMap) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) Function(java.util.function.Function) HashSet(java.util.HashSet) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) Sniffer(org.glassfish.api.container.Sniffer) EventListener(org.glassfish.api.event.EventListener) PreDestroy(org.glassfish.hk2.api.PreDestroy) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) Collectors.toSet(java.util.stream.Collectors.toSet) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) File(java.io.File) ModuleInfo(org.glassfish.internal.data.ModuleInfo) ResourceClassLoader(org.glassfish.api.deployment.ResourceClassLoader) List(java.util.List) EngineInfo(org.glassfish.internal.data.EngineInfo) Entry(java.util.Map.Entry) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) Optional(java.util.Optional) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HotSwapHelper(com.sun.enterprise.glassfish.bootstrap.MainHelper.HotSwapHelper) Collections(java.util.Collections) Deployment(org.glassfish.internal.deployment.Deployment) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProgressTracker(org.glassfish.internal.data.ProgressTracker) IOException(java.io.IOException) ResourceClassLoader(org.glassfish.api.deployment.ResourceClassLoader) ResourceEntry(org.glassfish.api.deployment.ResourceEntry) ResourceClassLoader(org.glassfish.api.deployment.ResourceClassLoader) EventListener(org.glassfish.api.event.EventListener)

Aggregations

HotSwapHelper (com.sun.enterprise.glassfish.bootstrap.MainHelper.HotSwapHelper)1 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Logger (java.util.logging.Logger)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Sniffer (org.glassfish.api.container.Sniffer)1 ApplicationMetaDataProvider (org.glassfish.api.deployment.ApplicationMetaDataProvider)1