Search in sources :

Example 1 with VersioningException

use of org.glassfish.deployment.versioning.VersioningException 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 2 with VersioningException

use of org.glassfish.deployment.versioning.VersioningException in project Payara by payara.

the class DeleteApplicationRefCommand method preAuthorization.

@Override
public boolean preAuthorization(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // retrieve matched version(s) if exist
    try {
        matchedVersions = versioningService.getMatchedVersions(name, target);
    } catch (VersioningException e) {
        report.failure(logger, e.getMessage());
        return false;
    }
    // this is an unversioned behavior and the given application is not registered
    if (matchedVersions.isEmpty()) {
        if (env.isDas()) {
            // let's only do this check for DAS to be more
            // tolerable of the partial deployment case
            report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name, target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
        return false;
    }
    return true;
}
Also used : VersioningException(org.glassfish.deployment.versioning.VersioningException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger)

Example 3 with VersioningException

use of org.glassfish.deployment.versioning.VersioningException in project Payara by payara.

the class DisableCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    if (origin == Origin.unload && command == Command.disable) {
        // we should only validate this for the disable command
        deployment.validateSpecifiedTarget(target);
    }
    InterceptorNotifier notifier = new InterceptorNotifier(habitat, null);
    final DeployCommandSupplementalInfo suppInfo = new DeployCommandSupplementalInfo();
    suppInfo.setAccessChecks(accessChecks);
    report.setResultType(DeployCommandSupplementalInfo.class, suppInfo);
    if (env.isDas() && DeploymentUtils.isDomainTarget(target)) {
        // for each distinct enabled version in all known targets
        Iterator it = enabledVersionsInTargets.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            appName = (String) entry.getKey();
            List<String> targets = new ArrayList<String>((Set<String>) entry.getValue());
            // replicate command to all referenced targets
            try {
                ParameterMapExtractor extractor = new ParameterMapExtractor(this);
                ParameterMap paramMap = extractor.extract(Collections.EMPTY_LIST);
                paramMap.set("DEFAULT", appName);
                notifier.ensureBeforeReported(ExtendedDeploymentContext.Phase.REPLICATION);
                ClusterOperationUtil.replicateCommand("disable", FailurePolicy.Error, FailurePolicy.Warn, FailurePolicy.Ignore, targets, context, paramMap, habitat);
            } catch (Exception e) {
                report.failure(logger, e.getMessage());
                return;
            }
        }
    } else if (isVersionExpressionWithWildcard) {
        try {
            if (matchedVersions == Collections.EMPTY_LIST) {
                // no version matched by the expression
                // nothing to do : success
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return;
            }
            String enabledVersion = versioningService.getEnabledVersion(appName, target);
            if (matchedVersions.contains(enabledVersion)) {
                // the enabled version is matched by the expression
                appName = enabledVersion;
            } else {
                // the enabled version is not matched by the expression
                // nothing to do : success
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return;
            }
        } catch (VersioningException e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    if (target == null) {
        target = deployment.getDefaultTarget(appName, origin, _classicstyle);
    }
    if (env.isDas() || !isundeploy) {
        // on instance side for partial deployment case
        if (!deployment.isRegistered(appName)) {
            if (env.isDas()) {
                // let's only do this check for DAS to be more
                // tolerable of the partial deployment case
                report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", appName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            }
            return;
        }
        if (!DeploymentUtils.isDomainTarget(target)) {
            ApplicationRef ref = domain.getApplicationRefInTarget(appName, target);
            if (ref == null) {
                if (env.isDas()) {
                    // let's only do this check for DAS to be more
                    // tolerable of the partial deployment case
                    report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", appName, target));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                }
                return;
            }
        }
    }
    /*
         * If the target is a cluster instance, the DAS will broadcast the command
         * to all instances in the cluster so they can all update their configs.
         */
    if (env.isDas()) {
        try {
            notifier.ensureBeforeReported(ExtendedDeploymentContext.Phase.REPLICATION);
            DeploymentCommandUtils.replicateEnableDisableToContainingCluster("disable", domain, target, appName, habitat, context, this);
        } catch (Exception e) {
            report.failure(logger, e.getMessage());
            return;
        }
    }
    try {
        Application app = applications.getApplication(appName);
        this.name = appName;
        // SHOULD CHECK THAT WE ARE THE CORRECT TARGET BEFORE DISABLING
        String serverName = server.getName();
        if (serverName.equals(target) || (server.getCluster() != null && server.getCluster().getName().equals(target))) {
            // wait until all applications are loaded. Otherwise we get "Application not registered"
            startupProvider.get();
            ApplicationInfo appInfo = deployment.get(appName);
            final DeploymentContext basicDC = deployment.disable(this, app, appInfo, report, logger);
            suppInfo.setDeploymentContext((ExtendedDeploymentContext) basicDC);
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error during disabling: ", e);
        if (env.isDas() || !isundeploy) {
            // we should let undeployment go through
            // on instance side for partial deployment case
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(e.getMessage());
        }
    }
    if (enabledVersionsToDisable == Collections.EMPTY_SET) {
        enabledVersionsToDisable = new HashSet<String>();
        enabledVersionsToDisable.add(appName);
    }
    // iterating all the distinct enabled versions in all targets
    Iterator it = enabledVersionsToDisable.iterator();
    while (it.hasNext()) {
        appName = (String) it.next();
        if (!isundeploy && !report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
            try {
                deployment.updateAppEnabledAttributeInDomainXML(appName, target, false);
            } catch (TransactionFailure e) {
                logger.warning("failed to set enable attribute for " + appName);
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ArrayList(java.util.ArrayList) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ParameterMap(org.glassfish.api.admin.ParameterMap) VersioningException(org.glassfish.deployment.versioning.VersioningException) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ParameterMapExtractor(org.glassfish.common.util.admin.ParameterMapExtractor) Iterator(java.util.Iterator) VersioningException(org.glassfish.deployment.versioning.VersioningException) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) HashMap(java.util.HashMap)

Example 4 with VersioningException

use of org.glassfish.deployment.versioning.VersioningException in project Payara by payara.

the class UndeployCommand method preAuthorization.

@Override
public boolean preAuthorization(AdminCommandContext context) {
    report = context.getActionReport();
    logger = context.getLogger();
    deployment.validateSpecifiedTarget(target);
    // so the versioned code could execute as expected
    if (target == null) {
        target = deployment.getDefaultTarget(_classicstyle);
    }
    /**
     * A little bit of dancing around has to be done, in case the
     * user passed the path to the original directory.
     */
    name = (new File(name)).getName();
    /*
        // I should really look if the associated cluster is virtual
        Cluster cluster = domain.getClusterNamed(name);
        if (cluster!=null) {
            target = name;
        }
*/
    // retrieve matched version(s) if exist
    matchedVersions = null;
    try {
        matchedVersions = versioningService.getMatchedVersions(name, target);
    } catch (VersioningException e) {
        if (env.isDas()) {
            report.failure(logger, e.getMessage());
        } else {
            // we should let undeployment go through
            // on instance side for partial deployment case
            logger.fine(e.getMessage());
        }
        return false;
    }
    // this is an unversioned behavior and the given application is not registered
    if (matchedVersions.isEmpty()) {
        if (env.isDas()) {
            report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name, target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        } else {
            // we should let undeployment go through
            // on instance side for partial deployment case
            logger.fine(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name, target));
        }
        return false;
    }
    for (String appName : matchedVersions) {
        if (target == null) {
            target = deployment.getDefaultTarget(appName, origin, _classicstyle);
        }
        Application application = apps.getModule(Application.class, appName);
        if (application == null) {
            report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", appName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return false;
        }
    }
    return true;
}
Also used : VersioningException(org.glassfish.deployment.versioning.VersioningException) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

VersioningException (org.glassfish.deployment.versioning.VersioningException)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 Logger (java.util.logging.Logger)2 ActionReport (org.glassfish.api.ActionReport)2 ParameterMap (org.glassfish.api.admin.ParameterMap)2 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)2 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)2 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)2 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 JarFile (java.util.jar.JarFile)1 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)1 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)1 ParameterMapExtractor (org.glassfish.common.util.admin.ParameterMapExtractor)1 ApplicationConfigInfo (org.glassfish.deployment.common.ApplicationConfigInfo)1