Search in sources :

Example 11 with DeploymentContext

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

the class CarHandler method getClassLoader.

@Override
public ClassLoader getClassLoader(final ClassLoader parent, DeploymentContext context) {
    ASURLClassLoader cloader = AccessController.doPrivileged(new PrivilegedAction<ASURLClassLoader>() {

        @Override
        public ASURLClassLoader run() {
            return new ASURLClassLoader(parent);
        }
    });
    try {
        cloader.addURL(context.getSource().getURI().toURL());
        // add libraries referenced from manifest
        for (URL url : getManifestLibraries(context)) {
            cloader.addURL(url);
        }
        try {
            final DeploymentContext dc = context;
            final ClassLoader cl = cloader;
            AccessController.doPrivileged(new PermsArchiveDelegate.SetPermissionsAction(SMGlobalPolicyUtil.CommponentType.car, dc, cl));
        } catch (PrivilegedActionException e) {
            throw new SecurityException(e.getException());
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
    return cloader;
}
Also used : PermsArchiveDelegate(com.sun.enterprise.security.perms.PermsArchiveDelegate) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) MalformedURLException(java.net.MalformedURLException) PrivilegedActionException(java.security.PrivilegedActionException) ASURLClassLoader(com.sun.enterprise.loader.ASURLClassLoader) ASURLClassLoader(com.sun.enterprise.loader.ASURLClassLoader) URL(java.net.URL)

Example 12 with DeploymentContext

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

the class ResourcesDeployer method event.

/**
 * Event listener to listen to </code>application undeploy validation</code> and
 * if <i>preserveResources</i> flag is set, cache the &lt;resources&gt;
 * config for persisting it in domain.xml
 * @param event
 */
@Override
public void event(Event event) {
    if (event.is(Deployment.DEPLOYMENT_BEFORE_CLASSLOADER_CREATION)) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        final DeployCommandParameters deployParams = dc.getCommandParameters(DeployCommandParameters.class);
        processResources(dc, deployParams);
    } else if (event.is(Deployment.UNDEPLOYMENT_VALIDATION)) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        final UndeployCommandParameters undeployCommandParameters = dc.getCommandParameters(UndeployCommandParameters.class);
        preserveResources(dc, undeployCommandParameters);
    } else if (Deployment.UNDEPLOYMENT_FAILURE.equals(event.type())) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        cleanupPreservedResources(dc, event);
    } else if (Deployment.DEPLOYMENT_FAILURE.equals(event.type())) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        String appName = getAppNameFromDeployCmdParams(dc);
        cleanupResources(appName, dc.getCommandParameters(DeployCommandParameters.class).origin);
        // TODO ASR call this only when the flag is on ? --properties preserveAppScopedResources=true
        cleanupPreservedResources(dc, event);
    } else if (Deployment.DEPLOYMENT_SUCCESS.equals(event.type())) {
        ApplicationInfo applicationInfo = (ApplicationInfo) event.hook();
        String appName = applicationInfo.getName();
        ResourcesRegistry.remove(appName);
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo)

Example 13 with DeploymentContext

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

the class JPADeployer method event.

@Override
public void event(Event event) {
    if (logger.isLoggable(Level.FINEST)) {
        logger.finest("JpaDeployer.event():" + event.name());
    }
    if (event.is(Deployment.APPLICATION_PREPARED)) {
        ExtendedDeploymentContext context = (ExtendedDeploymentContext) event.hook();
        DeployCommandParameters deployCommandParameters = context.getCommandParameters(DeployCommandParameters.class);
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("JpaDeployer.event(): Handling APPLICATION_PREPARED origin is:" + deployCommandParameters.origin);
        }
        // an application-ref is being created on DAS. Process the app
        if (!deployCommandParameters.origin.isCreateAppRef() || isTargetDas(deployCommandParameters)) {
            Map<String, ExtendedDeploymentContext> deploymentContexts = context.getModuleDeploymentContexts();
            for (DeploymentContext deploymentContext : deploymentContexts.values()) {
                // bundle level pus
                iterateInitializedPUsAtApplicationPrepare(deploymentContext);
            }
            // app level pus
            iterateInitializedPUsAtApplicationPrepare(context);
        }
    } else if (event.is(Deployment.APPLICATION_DISABLED)) {
        logger.fine("JpaDeployer.event(): APPLICATION_DISABLED");
        // APPLICATION_DISABLED will be generated when an app is disabled/undeployed/appserver goes down.
        // close all the emfs created for this app
        ApplicationInfo appInfo = (ApplicationInfo) event.hook();
        closeEMFs(appInfo);
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext)

Example 14 with DeploymentContext

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

the class ConnectorDeployer method event.

public void event(Event event) {
    if (/*env.isDas() && */
    Deployment.UNDEPLOYMENT_VALIDATION.equals(event.type())) {
        // this is an application undeploy event
        DeploymentContext dc = (DeploymentContext) event.hook();
        UndeployCommandParameters dcp = dc.getCommandParameters(UndeployCommandParameters.class);
        String appName = dcp.name;
        Boolean cascade = dcp.cascade;
        Boolean ignoreCascade = dcp._ignoreCascade;
        if (cascade != null && ignoreCascade != null) {
            if (cascade || ignoreCascade) {
                return;
            }
        }
        com.sun.enterprise.config.serverbeans.Application app = domain.getApplications().getApplication(appName);
        boolean isRAR = false;
        if (app != null && Boolean.valueOf(app.getEnabled())) {
            isRAR = app.containsSnifferType(ConnectorConstants.CONNECTOR_MODULE);
        }
        if (!isRAR) {
            return;
        }
        boolean isAppRefEnabled = false;
        Server server = domain.getServers().getServer(env.getInstanceName());
        ApplicationRef appRef = server.getApplicationRef(appName);
        if (appRef != null && Boolean.valueOf(appRef.getEnabled())) {
            isAppRefEnabled = true;
        }
        if (isAppRefEnabled) {
            return;
        }
        boolean isEAR = app.containsSnifferType(EAR);
        String moduleName = appName;
        ResourcesUtil resourcesUtil = ResourcesUtil.createInstance();
        if (isEAR) {
            List<Module> modules = app.getModule();
            for (Module module : modules) {
                moduleName = module.getName();
                if (module.getEngine(ConnectorConstants.CONNECTOR_MODULE) != null) {
                    moduleName = appName + ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER + moduleName;
                    if (moduleName.toLowerCase(Locale.getDefault()).endsWith(".rar")) {
                        int index = moduleName.lastIndexOf(".rar");
                        moduleName = moduleName.substring(0, index);
                        if (resourcesUtil.filterConnectorResources(resourceManager.getAllResources(), moduleName, true).size() > 0) {
                            setFailureStatus(dc, moduleName);
                            return;
                        }
                    }
                }
            }
        } else {
            if (resourcesUtil.filterConnectorResources(resourceManager.getAllResources(), moduleName, true).size() > 0) {
                setFailureStatus(dc, moduleName);
            }
        }
    }
}
Also used : ResourcesUtil(com.sun.enterprise.connectors.util.ResourcesUtil) com.sun.enterprise.config.serverbeans(com.sun.enterprise.config.serverbeans) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters)

Example 15 with DeploymentContext

use of org.glassfish.api.deployment.DeploymentContext 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)

Aggregations

DeploymentContext (org.glassfish.api.deployment.DeploymentContext)27 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)11 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)10 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)6 URL (java.net.URL)5 ParameterMap (org.glassfish.api.admin.ParameterMap)5 PermsArchiveDelegate (com.sun.enterprise.security.perms.PermsArchiveDelegate)4 URI (java.net.URI)4 PrivilegedActionException (java.security.PrivilegedActionException)4 Container (com.sun.ejb.Container)3 AbstractSingletonContainer (com.sun.ejb.containers.AbstractSingletonContainer)3 Application (com.sun.enterprise.deployment.Application)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 ActionReport (org.glassfish.api.ActionReport)3 ApplicationContainer (org.glassfish.api.deployment.ApplicationContainer)3 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)3 ParameterMapExtractor (org.glassfish.common.util.admin.ParameterMapExtractor)3 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)3 VersioningException (org.glassfish.deployment.versioning.VersioningException)3