Search in sources :

Example 11 with ExtendedDeploymentContext

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

the class EjbDeployer method event.

@Override
public void event(Event event) {
    if (event.is(Deployment.APPLICATION_PREPARED) && isDas()) {
        ExtendedDeploymentContext context = (ExtendedDeploymentContext) event.hook();
        OpsParams opsparams = context.getCommandParameters(OpsParams.class);
        DeployCommandParameters dcp = context.getCommandParameters(DeployCommandParameters.class);
        ApplicationInfo appInfo = appRegistry.get(opsparams.name());
        Application app = appInfo.getMetaData(Application.class);
        if (app == null) {
            // Not a Java EE application
            return;
        }
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "EjbDeployer in APPLICATION_PREPARED for origin: " + opsparams.origin + ", target: " + dcp.target + ", name: " + opsparams.name());
        }
        boolean createTimers = true;
        if (!(opsparams.origin.isDeploy() || opsparams.origin.isCreateAppRef()) || env.getInstanceName().equals(dcp.target)) {
            // Timers will be created by the BaseContainer if it's a single instance deploy
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, "EjbDeployer ... will only set the timeout application flag if any");
            }
            // But is-timed-app needs to be set in AppInfo in any case
            createTimers = false;
        }
        String target = dcp.target;
        if (createTimers && dcp.isredeploy != null && dcp.isredeploy && DeploymentUtils.isDomainTarget(target)) {
            List<String> targets = (List<String>) context.getTransientAppMetaData(DeploymentProperties.PREVIOUS_TARGETS, List.class);
            for (String ref : targets) {
                target = ref;
                if (domain.getClusterNamed(target) != null || domain.getDeploymentGroupNamed(target) != null) {
                    // prefer cluster target
                    break;
                }
            }
        }
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "EjbDeployer using target for event as " + target);
        }
        boolean isTimedApp = false;
        for (EjbBundleDescriptorImpl ejbBundle : app.getBundleDescriptors(EjbBundleDescriptorImpl.class)) {
            if (checkEjbBundleForTimers(ejbBundle, createTimers, target)) {
                isTimedApp = true;
            }
        }
        if (isTimedApp && (opsparams.origin.isDeploy() || opsparams.origin.isLoad())) {
            // Mark application as a timeout application, so that the clean() call removes the timers.
            appInfo.addTransientAppMetaData(IS_TIMEOUT_APP_PROP, Boolean.TRUE);
        }
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) OpsParams(org.glassfish.api.deployment.OpsParams) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) List(java.util.List) ArrayList(java.util.ArrayList) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Application(com.sun.enterprise.deployment.Application) EjbBundleDescriptorImpl(org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl)

Example 12 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext 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 13 with ExtendedDeploymentContext

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

the class MetroContainer method deployWsTxServices.

public void deployWsTxServices(String target) {
    synchronized (lock) {
        if (wstxServicesDeployed.get() || !wstxServicesDeploying.compareAndSet(false, true)) {
            return;
        }
        Deployment deployment = habitat.getService(Deployment.class);
        boolean isRegistered = deployment.isRegistered(WSTX_SERVICES_APP_NAME);
        if (isRegistered) {
            logger.log(Level.WARNING, LogUtils.WSTX_SERVICE_DEPLOYED_EXPLICITLY);
        } else {
            logger.log(Level.INFO, LogUtils.WSTX_SERVICE_LOADING);
            File root = serverContext.getInstallRoot();
            File app = null;
            try {
                app = FileUtils.getManagedFile(WSTX_SERVICES_APP_NAME + ".war", new File(root, METRO_APPS_INSTALL_ROOT));
            } catch (Exception e) {
                logger.log(Level.WARNING, LogUtils.WSTX_SERVICE_UNEXPECTED_EXCEPTION, e);
            }
            if (app == null || !app.exists()) {
                // TODO
                logger.log(Level.WARNING, format(LogUtils.WSTX_SERVICE_CANNOT_DEPLOY, "Required WAR file (" + WSTX_SERVICES_APP_NAME + ".war) is not installed"));
            } else {
                ActionReport report = habitat.getService(ActionReport.class, "plain");
                DeployCommandParameters params = new DeployCommandParameters(app);
                String appName = WSTX_SERVICES_APP_NAME;
                params.name = appName;
                try {
                    File rootScratchDir = env.getApplicationStubPath();
                    File appScratchDir = new File(rootScratchDir, appName);
                    // String resourceName = getTimerResource(target);
                    if (isDas() && appScratchDir.createNewFile()) {
                        params.origin = OpsParams.Origin.deploy;
                        if (target != null) {
                            params.target = target;
                        }
                    } else {
                        params.origin = OpsParams.Origin.load;
                        params.target = env.getInstanceName();
                    }
                    ExtendedDeploymentContext dc = deployment.getBuilder(logger, params, report).source(app).build();
                    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, format(LogUtils.WSTX_SERVICE_CANNOT_DEPLOY, report.getMessage()), report.getFailureCause());
                    }
                    logger.log(Level.INFO, LogUtils.WSTX_SERVICE_STARTED);
                } catch (Exception ex) {
                    logger.log(Level.WARNING, format(LogUtils.WSTX_SERVICE_CANNOT_DEPLOY, ex.getLocalizedMessage()), ex);
                }
            }
        }
        wstxServicesDeployed.set(true);
        wstxServicesDeploying.set(false);
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) 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)

Example 14 with ExtendedDeploymentContext

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

the class CreateApplicationRefCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // retrieve matched version(s) if exist
    List<String> matchedVersions = null;
    if (enabled) {
        try {
            // warn users that they can use version expressions
            VersioningUtils.checkIdentifier(name);
            matchedVersions = new ArrayList<String>(1);
            matchedVersions.add(name);
        } catch (VersioningWildcardException ex) {
            // a version expression is supplied with enabled == true
            report.setMessage(localStrings.getLocalString("wildcard.not.allowed", "WARNING : version expression are available only with --enabled=false"));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        } catch (VersioningSyntaxException ex) {
            report.setMessage(ex.getLocalizedMessage());
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        if (!deployment.isRegistered(name)) {
            report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", name));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    } else {
        // retrieve matched version(s) if exist
        try {
            matchedVersions = versioningService.getMatchedVersions(name, null);
        } catch (VersioningException e) {
            report.failure(logger, e.getMessage());
            return;
        }
        // this is an unversioned behavior and the given application is not registered
        if (matchedVersions.isEmpty()) {
            report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name, target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    ActionReport.MessagePart part = report.getTopMessagePart();
    boolean isVersionExpression = VersioningUtils.isVersionExpression(name);
    // for each matched version
    Iterator it = matchedVersions.iterator();
    while (it.hasNext()) {
        String appName = (String) it.next();
        Application app = applications.getApplication(appName);
        ApplicationRef applicationRef = domain.getApplicationRefInTarget(appName, target);
        if (applicationRef != null) {
            // if a versioned name has been provided to the command
            if (isVersionExpression) {
                ActionReport.MessagePart childPart = part.addChild();
                childPart.setMessage(localStrings.getLocalString("appref.already.exists", "Application reference {0} already exists in target {1}.", appName, target));
            } else {
                // returns failure if an untagged name has been provided to the command
                report.setMessage(localStrings.getLocalString("appref.already.exists", "Application reference {0} already exists in target {1}.", name, target));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        } else {
            Transaction t = new Transaction();
            if (app.isLifecycleModule()) {
                handleLifecycleModule(context, t);
                return;
            }
            ReadableArchive archive;
            File file = null;
            DeployCommandParameters commandParams = null;
            Properties contextProps;
            Map<String, Properties> modulePropsMap = null;
            ApplicationConfigInfo savedAppConfig = null;
            try {
                commandParams = app.getDeployParameters(null);
                commandParams.origin = Origin.create_application_ref;
                commandParams.command = Command.create_application_ref;
                commandParams.target = target;
                commandParams.virtualservers = virtualservers;
                commandParams.enabled = enabled;
                if (lbenabled != null) {
                    commandParams.lbenabled = lbenabled;
                }
                commandParams.type = app.archiveType();
                contextProps = app.getDeployProperties();
                modulePropsMap = app.getModulePropertiesMap();
                savedAppConfig = new ApplicationConfigInfo(app);
                URI uri = new URI(app.getLocation());
                file = new File(uri);
                if (!file.exists()) {
                    report.setMessage(localStrings.getLocalString("fnf", "File not found", file.getAbsolutePath()));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
                archive = archiveFactory.openArchive(file);
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Error opening deployable artifact : " + file.getAbsolutePath(), e);
                report.setMessage(localStrings.getLocalString("unknownarchiveformat", "Archive format not recognized"));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
            try {
                final ExtendedDeploymentContext deploymentContext = deployment.getBuilder(logger, commandParams, report).source(archive).build();
                Properties appProps = deploymentContext.getAppProps();
                appProps.putAll(contextProps);
                // relativize the location so it could be set properly in
                // domain.xml
                String location = DeploymentUtils.relativizeWithinDomainIfPossible(new URI(app.getLocation()));
                appProps.setProperty(ServerTags.LOCATION, location);
                // relativize the URI properties so they could store in the
                // domain.xml properly on the instances
                String appLocation = appProps.getProperty(Application.APP_LOCATION_PROP_NAME);
                appProps.setProperty(Application.APP_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(appLocation)));
                String planLocation = appProps.getProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME);
                if (planLocation != null) {
                    appProps.setProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(planLocation)));
                }
                String altDDLocation = appProps.getProperty(Application.ALT_DD_LOCATION_PROP_NAME);
                if (altDDLocation != null) {
                    appProps.setProperty(Application.ALT_DD_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(altDDLocation)));
                }
                String runtimeAltDDLocation = appProps.getProperty(Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME);
                if (runtimeAltDDLocation != null) {
                    appProps.setProperty(Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(runtimeAltDDLocation)));
                }
                savedAppConfig.store(appProps);
                if (modulePropsMap != null) {
                    deploymentContext.setModulePropsMap(modulePropsMap);
                }
                if (enabled) {
                    versioningService.handleDisable(appName, target, deploymentContext.getActionReport(), context.getSubject());
                }
                if (domain.isCurrentInstanceMatchingTarget(target, appName, server.getName(), null)) {
                    deployment.deploy(deployment.getSniffersFromApp(app), deploymentContext);
                } else {
                    // send the APPLICATION_PREPARED event for DAS
                    events.send(new Event<DeploymentContext>(Deployment.APPLICATION_PREPARED, deploymentContext), false);
                }
                final List<String> targets = new ArrayList<String>(Arrays.asList(commandParams.target.split(",")));
                List<String> deploymentTarget = new ArrayList<>();
                // If targets contains Deployment Group, check if the application is already deployed to instances in it.
                for (String target : targets) {
                    if (isDeploymentGroup(target)) {
                        List<Server> instances = domain.getDeploymentGroupNamed(target).getInstances();
                        for (Server instance : instances) {
                            List<Application> applications = domain.getApplicationsInTarget(instance.getName());
                            List<String> listOfApplications = new ArrayList<>();
                            for (Application application : applications) {
                                listOfApplications.add(application.getName());
                            }
                            if (!listOfApplications.contains(appName)) {
                                deploymentTarget.add(instance.getName());
                            }
                        }
                    }
                }
                if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
                    try {
                        deployment.registerAppInDomainXML(null, deploymentContext, t, true);
                    } catch (TransactionFailure e) {
                        logger.warning("failed to create application ref for " + appName);
                    }
                }
                // if the target is DAS, we do not need to do anything more
                if (!isVersionExpression && DeploymentUtils.isDASTarget(target)) {
                    return;
                }
                final ParameterMap paramMap = deployment.prepareInstanceDeployParamMap(deploymentContext);
                if (!deploymentTarget.isEmpty()) {
                    replicateCommand(deploymentTarget, context, paramMap);
                } else {
                    replicateCommand(targets, context, paramMap);
                }
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Error during creating application ref ", e);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            } finally {
                try {
                    archive.close();
                } catch (IOException e) {
                    logger.log(Level.INFO, "Error while closing deployable artifact : " + file.getAbsolutePath(), e);
                }
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) VersioningWildcardException(org.glassfish.deployment.versioning.VersioningWildcardException) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) URI(java.net.URI) Iterator(java.util.Iterator) VersioningException(org.glassfish.deployment.versioning.VersioningException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) VersioningException(org.glassfish.deployment.versioning.VersioningException) VersioningWildcardException(org.glassfish.deployment.versioning.VersioningWildcardException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Transaction(org.jvnet.hk2.config.Transaction) ApplicationConfigInfo(org.glassfish.deployment.common.ApplicationConfigInfo) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 15 with ExtendedDeploymentContext

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

the class CreateApplicationRefCommand method handleLifecycleModule.

private void handleLifecycleModule(AdminCommandContext context, Transaction t) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    Application app = applications.getApplication(name);
    // create a dummy context to hold params and props
    DeployCommandParameters commandParams = new DeployCommandParameters();
    commandParams.name = name;
    commandParams.target = target;
    commandParams.virtualservers = virtualservers;
    commandParams.enabled = enabled;
    ExtendedDeploymentContext lifecycleContext = new DeploymentContextImpl(report, null, commandParams, null);
    try {
        deployment.registerAppInDomainXML(null, lifecycleContext, t, true);
    } catch (Exception e) {
        report.failure(logger, e.getMessage());
    }
    if (!DeploymentUtils.isDASTarget(target)) {
        final ParameterMap paramMap = new ParameterMap();
        paramMap.add("DEFAULT", name);
        paramMap.add(DeploymentProperties.TARGET, target);
        paramMap.add(DeploymentProperties.ENABLED, enabled.toString());
        if (virtualservers != null) {
            paramMap.add(DeploymentProperties.VIRTUAL_SERVERS, virtualservers);
        }
        // pass the app props so we have the information to persist in the
        // domain.xml
        Properties appProps = app.getDeployProperties();
        paramMap.set(DeploymentProperties.APP_PROPS, DeploymentUtils.propertiesValue(appProps, ':'));
        final List<String> targets = new ArrayList<String>();
        targets.add(target);
        ClusterOperationUtil.replicateCommand("_lifecycle", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, paramMap, habitat);
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ArrayList(java.util.ArrayList) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) VersioningException(org.glassfish.deployment.versioning.VersioningException) VersioningWildcardException(org.glassfish.deployment.versioning.VersioningWildcardException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl)

Aggregations

ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)39 ActionReport (org.glassfish.api.ActionReport)23 IOException (java.io.IOException)17 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)17 File (java.io.File)16 Logger (java.util.logging.Logger)11 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)11 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)10 Properties (java.util.Properties)9 URI (java.net.URI)7 Deployment (org.glassfish.internal.deployment.Deployment)7 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)6 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)6 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)6 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)6 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)6 Application (com.sun.enterprise.deployment.Application)5 ArrayList (java.util.ArrayList)5 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)5 VersioningException (org.glassfish.deployment.versioning.VersioningException)5