Search in sources :

Example 6 with Deployment

use of org.glassfish.internal.deployment.Deployment in project Payara by payara.

the class PersistentEJBTimerService method deployEJBTimerService.

private static boolean deployEJBTimerService(File root, File appScratchFile, String resourceName, boolean is_upgrade) {
    boolean deployed = false;
    logger.log(Level.INFO, "Loading EJBTimerService. Please wait.");
    File app = null;
    try {
        app = FileUtils.getManagedFile(TIMER_SERVICE_APP_NAME + ".war", new File(root, "lib/install/applications/"));
    } catch (Exception e) {
        logger.log(Level.WARNING, "Caught unexpected exception", e);
    }
    if (app == null || !app.exists()) {
        logger.log(Level.WARNING, "Cannot deploy or load persistent EJBTimerService: " + "required WAR file (" + TIMER_SERVICE_APP_NAME + ".war) is not installed");
    } else {
        DeployCommandParameters params = new DeployCommandParameters(app);
        params.name = TIMER_SERVICE_APP_NAME;
        try {
            EjbContainerUtil _ejbContainerUtil = EjbContainerUtilImpl.getInstance();
            // first access of the Timer Service application
            if (_ejbContainerUtil.isDas() && appScratchFile.createNewFile() && !is_upgrade) {
                params.origin = OpsParams.Origin.deploy;
            } else {
                params.origin = OpsParams.Origin.load;
            }
            params.target = _ejbContainerUtil.getServerEnvironment().getInstanceName();
            ActionReport report = _ejbContainerUtil.getServices().getService(ActionReport.class, "plain");
            Deployment deployment = _ejbContainerUtil.getDeployment();
            ExtendedDeploymentContext dc = deployment.getBuilder(logger, params, report).source(app).build();
            dc.addTransientAppMetaData(DatabaseConstants.JTA_DATASOURCE_JNDI_NAME_OVERRIDE, resourceName);
            Properties appProps = dc.getAppProps();
            appProps.setProperty(ServerTags.OBJECT_TYPE, DeploymentProperties.SYSTEM_ALL);
            deployment.deploy(dc);
            if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
                logger.log(Level.WARNING, "Cannot deploy or load EJBTimerService: ", report.getFailureCause());
            } else {
                deployed = true;
            }
        } catch (Exception e) {
            logger.log(Level.WARNING, "Cannot deploy or load EJBTimerService: ", e);
        } finally {
            if (!deployed && params.origin.isDeploy() && appScratchFile.exists()) {
                // Remove marker file if deploy failed
                if (!appScratchFile.delete()) {
                    logger.log(Level.WARNING, "Failed to remove the marker file " + appScratchFile);
                }
            }
        }
    }
    return deployed;
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) EjbContainerUtil(com.sun.ejb.containers.EjbContainerUtil) Deployment(org.glassfish.internal.deployment.Deployment) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) File(java.io.File) PropertyVetoException(java.beans.PropertyVetoException) EJBException(javax.ejb.EJBException) FinderException(javax.ejb.FinderException) CreateException(javax.ejb.CreateException)

Example 7 with Deployment

use of org.glassfish.internal.deployment.Deployment in project Payara by payara.

the class VirtualServer method removeContext.

/**
 * Stops the given <tt>context</tt> and removes it from this <tt>VirtualServer</tt>.
 *
 * @throws org.glassfish.embeddable.GlassFishException
 */
@Override
public void removeContext(Context context) throws GlassFishException {
    ActionReport report = services.getService(ActionReport.class, "plain");
    Deployment deployment = services.getService(Deployment.class);
    String name;
    if (context instanceof ContextFacade) {
        name = ((ContextFacade) context).getAppName();
    } else {
        name = context.getPath();
    }
    ApplicationInfo appInfo = deployment.get(name);
    if (appInfo == null) {
        report.setMessage("Cannot find deployed application of name " + name);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot find deployed application of name " + name);
    }
    ReadableArchive source = appInfo.getSource();
    if (source == null) {
        report.setMessage("Cannot get source archive for undeployment");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot get source archive for undeployment");
    }
    UndeployCommandParameters params = new UndeployCommandParameters(name);
    params.origin = UndeployCommandParameters.Origin.undeploy;
    params.target = "server";
    ExtendedDeploymentContext deploymentContext = null;
    try {
        deploymentContext = deployment.getBuilder(_logger, params, report).source(source).build();
        deployment.undeploy(name, deploymentContext);
        deployment.unregisterAppFromDomainXML(name, "server");
    } catch (IOException e) {
        _logger.log(Level.SEVERE, LogFacade.REMOVE_CONTEXT_ERROR, e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot create context for undeployment ", e);
    } catch (TransactionFailure e) {
        throw new GlassFishException(e);
    } finally {
        if (deploymentContext != null) {
            deploymentContext.clean();
        }
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, LogFacade.REMOVED_CONTEXT, name);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) Deployment(org.glassfish.internal.deployment.Deployment) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext)

Example 8 with Deployment

use of org.glassfish.internal.deployment.Deployment in project Payara by payara.

the class EventsTest method badUndeployTest.

@Test
public void badUndeployTest() throws Exception {
    Deployment deployment = habitat.getService(Deployment.class);
    UndeployCommandParameters params = new UndeployCommandParameters("notavalidname");
    params.target = "server";
    ActionReport report = habitat.getService(ActionReport.class, "hk2-agent");
    ExtendedDeploymentContext dc = deployment.getBuilder(Logger.getAnonymousLogger(), params, report).source(application).build();
    deployment.undeploy("notavalidname", dc);
    Assert.assertEquals(report.getActionExitCode(), ActionReport.ExitCode.FAILURE);
}
Also used : UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Deployment(org.glassfish.internal.deployment.Deployment) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 9 with Deployment

use of org.glassfish.internal.deployment.Deployment in project Payara by payara.

the class EventsTest method deployUndeployTest.

@Test
public void deployUndeployTest() throws Exception {
    final List<EventTypes> myTestEvents = getSingletonModuleSuccessfullDeploymentEvents();
    Events events = habitat.getService(Events.class);
    EventListener listener = new EventListener() {

        public void event(Event event) {
            if (myTestEvents.contains(event.type())) {
                myTestEvents.remove(event.type());
            }
        }
    };
    events.register(listener);
    Deployment deployment = habitat.getService(Deployment.class);
    DeployCommandParameters params = new DeployCommandParameters(application);
    params.name = "fakeApplication";
    params.target = "server";
    ActionReport report = habitat.getService(ActionReport.class, "hk2-agent");
    ExtendedDeploymentContext dc = deployment.getBuilder(Logger.getAnonymousLogger(), params, report).source(application).build();
    deployment.deploy(dc);
    events.unregister(listener);
    for (EventTypes et : myTestEvents) {
        System.out.println("An expected event of type " + et.type() + " was not received");
    }
    try {
        final List<EventTypes> myTestEvents2 = getSingletonModuleSuccessfullUndeploymentEvents();
        EventListener listener2 = new EventListener() {

            public void event(Event event) {
                if (myTestEvents2.contains(event.type())) {
                    myTestEvents2.remove(event.type());
                }
            }
        };
        events.register(listener2);
        UndeployCommandParameters params2 = new UndeployCommandParameters("fakeApplication");
        params2.target = "server";
        ActionReport report2 = habitat.getService(ActionReport.class, "hk2-agent");
        ExtendedDeploymentContext dc2 = deployment.getBuilder(Logger.getAnonymousLogger(), params2, report2).source(application).build();
        deployment.undeploy("fakeApplication", dc2);
        events.unregister(listener2);
        for (EventTypes et : myTestEvents2) {
            System.out.println("An expected event of type " + et.type() + " was not received");
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Events(org.glassfish.api.event.Events) Deployment(org.glassfish.internal.deployment.Deployment) EventTypes(org.glassfish.api.event.EventTypes) EventListener(org.glassfish.api.event.EventListener) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 10 with Deployment

use of org.glassfish.internal.deployment.Deployment 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

Deployment (org.glassfish.internal.deployment.Deployment)12 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)9 ActionReport (org.glassfish.api.ActionReport)7 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)7 File (java.io.File)6 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)6 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)5 IOException (java.io.IOException)4 Properties (java.util.Properties)3 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)3 ApplicationRegistry (org.glassfish.internal.data.ApplicationRegistry)3 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)2 ArchiveFactory (com.sun.enterprise.deploy.shared.ArchiveFactory)2 ApplicationLoaderService (com.sun.enterprise.v3.server.ApplicationLoaderService)2 PropertyVetoException (java.beans.PropertyVetoException)2 ServerEnvironment (org.glassfish.api.admin.ServerEnvironment)2 ApplicationMetaDataProvider (org.glassfish.api.deployment.ApplicationMetaDataProvider)2 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)2 EventListener (org.glassfish.api.event.EventListener)2 Events (org.glassfish.api.event.Events)2