Search in sources :

Example 26 with DeployCommandParameters

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

the class MTProvisionCommand method execute.

public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    if (app == null) {
        report.setMessage("Application " + appname + " needs to be deployed first before provisioned to tenant");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    ReadableArchive archive = null;
    DeployCommandParameters commandParams = app.getDeployParameters(appRef);
    commandParams.contextroot = contextroot;
    commandParams.target = DeploymentUtils.DAS_TARGET_NAME;
    commandParams.name = DeploymentUtils.getInternalNameForTenant(appname, tenant);
    commandParams.enabled = Boolean.TRUE;
    commandParams.origin = DeployCommandParameters.Origin.mt_provision;
    try {
        URI uri = new URI(app.getLocation());
        File file = new File(uri);
        if (!file.exists()) {
            throw new Exception(localStrings.getLocalString("fnf", "File not found", file.getAbsolutePath()));
        }
        archive = archiveFactory.openArchive(file);
        ExtendedDeploymentContext deploymentContext = deployment.getBuilder(logger, commandParams, report).source(archive).build();
        Properties appProps = deploymentContext.getAppProps();
        appProps.putAll(app.getDeployProperties());
        // app props so we also need to override that
        if (contextroot != null) {
            appProps.setProperty(ServerTags.CONTEXT_ROOT, contextroot);
        }
        deploymentContext.setModulePropsMap(app.getModulePropertiesMap());
        deploymentContext.setTenant(tenant, appname);
        expandCustomizationJar(deploymentContext.getTenantDir());
        deployment.deploy(deploymentContext);
        deployment.registerTenantWithAppInDomainXML(appname, deploymentContext);
    } catch (Throwable e) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(e.getMessage());
        report.setFailureCause(e);
    } finally {
        try {
            if (archive != null) {
                archive.close();
            }
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Properties(java.util.Properties) URI(java.net.URI) File(java.io.File) IOException(java.io.IOException)

Example 27 with DeployCommandParameters

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

the class MEJBNamingObjectProxy method deployMEJB.

private void deployMEJB() throws IOException {
    _logger.info("Loading MEJB app on JNDI look up");
    ServerContext serverContext = habitat.getService(ServerContext.class);
    File mejbArchive = new File(serverContext.getInstallRoot(), "lib/install/applications/mejb.jar");
    DeployCommandParameters deployParams = new DeployCommandParameters(mejbArchive);
    String targetName = habitat.<Server>getService(Server.class, ServerEnvironment.DEFAULT_INSTANCE_NAME).getName();
    deployParams.target = targetName;
    deployParams.name = "mejb";
    ActionReport report = habitat.getService(ActionReport.class, "plain");
    Deployment deployment = habitat.getService(Deployment.class);
    ExtendedDeploymentContext dc = deployment.getBuilder(_logger, deployParams, report).source(mejbArchive).build();
    deployment.deploy(dc);
    if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
        throw new RuntimeException("Failed to deploy MEJB app: " + report.getFailureCause());
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ServerContext(org.glassfish.internal.api.ServerContext) Server(com.sun.enterprise.config.serverbeans.Server) Deployment(org.glassfish.internal.deployment.Deployment) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) File(java.io.File)

Example 28 with DeployCommandParameters

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

the class JPADeployer method createEMFs.

/**
 * CreateEMFs and save them in persistence
 * @param context
 */
private void createEMFs(DeploymentContext context) {
    Application application = context.getModuleMetaData(Application.class);
    Set<BundleDescriptor> bundles = application.getBundleDescriptors();
    // Iterate through all the bundles for the app and collect pu references in referencedPus
    boolean hasScopedResource = false;
    final List<PersistenceUnitDescriptor> referencedPus = new ArrayList<PersistenceUnitDescriptor>();
    for (BundleDescriptor bundle : bundles) {
        Collection<? extends PersistenceUnitDescriptor> pusReferencedFromBundle = bundle.findReferencedPUs();
        for (PersistenceUnitDescriptor pud : pusReferencedFromBundle) {
            referencedPus.add(pud);
            if (hasScopedResource(pud)) {
                hasScopedResource = true;
            }
        }
    }
    if (hasScopedResource) {
        // Scoped resources are registered by connector runtime after prepare(). That is too late for JPA
        // This is a hack to initialize connectorRuntime for scoped resources
        connectorRuntime.registerDataSourceDefinitions(application);
    }
    // Iterate through all the PUDs for this bundle and if it is referenced, load the corresponding pu
    PersistenceUnitDescriptorIterator pudIterator = new PersistenceUnitDescriptorIterator() {

        @Override
        void visitPUD(PersistenceUnitDescriptor pud, DeploymentContext context) {
            if (referencedPus.contains(pud)) {
                boolean isDas = isDas();
                if (isDas && !isTargetDas(context.getCommandParameters(DeployCommandParameters.class))) {
                    DeployCommandParameters deployParams = context.getCommandParameters(DeployCommandParameters.class);
                    // If on DAS and not generating schema for remotes then return here
                    String jpaScemaGeneration = pud.getProperties().getProperty("javax.persistence.schema-generation.database.action", "none").toLowerCase();
                    String eclipselinkSchemaGeneration = pud.getProperties().getProperty("eclipselink.ddl-generation", "none").toLowerCase();
                    if ("none".equals(jpaScemaGeneration) && "none".equals(eclipselinkSchemaGeneration)) {
                        return;
                    } else {
                        InternalSystemAdministrator kernelIdentity = Globals.getDefaultHabitat().getService(InternalSystemAdministrator.class);
                        CommandRunner commandRunner = Globals.getDefaultHabitat().getService(CommandRunner.class);
                        CommandRunner.CommandInvocation getTranslatedValueCommand = commandRunner.getCommandInvocation("_get-translated-config-value", new PlainTextActionReporter(), kernelIdentity.getSubject());
                        ParameterMap params = new ParameterMap();
                        params.add("propertyName", pud.getJtaDataSource());
                        params.add("target", deployParams.target);
                        getTranslatedValueCommand.parameters(params);
                        getTranslatedValueCommand.execute();
                        ActionReport report = getTranslatedValueCommand.report();
                        if (report.hasSuccesses() && report.getSubActionsReport().size() == 1) {
                            ActionReport subReport = report.getSubActionsReport().get(0);
                            String value = subReport.getMessage().replace(deployParams.target + ":", "");
                            pud.setJtaDataSource(value.trim());
                        } else {
                            logger.log(Level.SEVERE, report.getMessage(), report.getFailureCause());
                        }
                    }
                }
                // While running in embedded mode, it is not possible to guarantee that entity classes are not loaded by the app classloader before transformers are installed
                // If that happens, weaving will not take place and EclipseLink will throw up. Provide users an option to disable weaving by passing the flag.
                // Note that we enable weaving if not explicitly disabled by user
                boolean weavingEnabled = Boolean.valueOf(sc.getArguments().getProperty("org.glassfish.persistence.embedded.weaving.enabled", "true"));
                ProviderContainerContractInfo providerContainerContractInfo = weavingEnabled ? new ServerProviderContainerContractInfo(context, connectorRuntime, isDas) : new EmbeddedProviderContainerContractInfo(context, connectorRuntime, isDas);
                try {
                    ((ExtendedDeploymentContext) context).prepareScratchDirs();
                } catch (IOException e) {
                    // There is no way to recover if we are not able to create the scratch dirs. Just rethrow the exception.
                    throw new RuntimeException(e);
                }
                try {
                    PersistenceUnitLoader puLoader = new PersistenceUnitLoader(pud, providerContainerContractInfo);
                    // Store the puLoader in context. It is retrieved to execute java2db and to
                    // store the loaded emfs in a JPAApplicationContainer object for cleanup
                    context.addTransientAppMetaData(getUniquePuIdentifier(pud), puLoader);
                } catch (Exception e) {
                    DeployCommandParameters dcp = context.getCommandParameters(DeployCommandParameters.class);
                    if (dcp.isSkipDSFailure() && ExceptionUtil.isDSFailure(e)) {
                        logger.log(Level.WARNING, "Resource communication failure exception skipped while loading the pu " + pud.getName(), e);
                    } else {
                        if (e.getCause() instanceof ConnectorRuntimeException) {
                            logger.log(Level.SEVERE, "{0} is not a valid data source. If you are using variable replacement then" + "ensure that is available on the DAS.");
                        }
                        throw e;
                    }
                }
            }
        }
    };
    pudIterator.iteratePUDs(context);
}
Also used : ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) CommandRunner(org.glassfish.api.admin.CommandRunner) PlainTextActionReporter(com.sun.enterprise.admin.report.PlainTextActionReporter) InternalSystemAdministrator(org.glassfish.internal.api.InternalSystemAdministrator) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) CommandException(org.glassfish.api.admin.CommandException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) DeploymentException(org.glassfish.deployment.common.DeploymentException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext)

Example 29 with DeployCommandParameters

use of org.glassfish.api.deployment.DeployCommandParameters 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 30 with DeployCommandParameters

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

the class JPADeployer method isEMFCreationRequired.

/**
 * @param context
 * @return true if emf creation is required false otherwise
 */
private boolean isEMFCreationRequired(DeploymentContext context) {
    /*
  Here are various use cases that needs to be handled.
  This method handles EMF creation part, APPLICATION_PREPARED event handle handles java2db and closing of emf

  To summarize,
  -Unconditionally create EMFs on DAS for java2db if it is deploy. We will close this EMF in APPLICATION_PREPARED after java2db if (target!= DAS || enable=false)
  -We will not create EMFs on instance if application is not enabled

        ------------------------------------------------------------------------------------
            Scenario                                       Expected Behavior
        ------------------------------------------------------------------------------------
        deploy --target=server   --enabled=true.   DAS(EMF created, java2db, EMF remains open)
           -restart                                DAS(EMF created, EMF remains open)
           -undeploy                               DAS(EMF closed. Drop tables)
           -create-application-ref instance1       DAS(No action)
                                                   INSTANCE1(EMF created)

        deploy --target=server   --enabled=false.  DAS(EMF created,java2db, EMF closed in APPLICATION_PREPARED)
           -restart                                DAS(No EMF created)
           -undeploy                               DAS(No EMF to close, Drop tables)

           -enable                                 DAS(EMF created)
           -undelpoy                               DAS(EMF closed, Drop tables)

           -create-application-ref instance1       DAS(No action)
                                                   INSTANCE1(EMF created)

        deploy --target=instance1 --enabled=true   DAS(EMF created, java2db, EMF closed in APPLICATION_PREPARED)
                                                   INSTANCE1(EMF created)
            -create-application-ref instance2      INSTANCE2(EMF created)
            -restart                               DAS(No EMF created)
                                                   INSTANCE1(EMF created)
                                                   INSTANCE2(EMF created)
            -undeploy                              DAS(No EMF to close, Drop tables)
                                                   INSTANCE1(EMF closed)

            -create-application-ref server         DAS(EMF created)
            -delete-application-ref server         DAS(EMF closed)
            undeploy                               INSTANCE1(EMF closed)


        deploy --target=instance --enabled=false.  DAS(EMF created, java2db, EMF closed in APPLICATION_PREPARED)
                                                   INSTANCE1(No EMF created)
            -create-application-ref instance2      DAS(No action)
                                                   INSTANCE2(No Action)
            -restart                               DAS(No EMF created)
                                                   INSTANCE1(No EMF created)
                                                   INSTANCE2(No EMF created)
            -undeploy                              DAS(No EMF to close, Drop tables)
                                                   INSTANCE1(No EMF to close)
                                                   INSTANCE2(No EMF to close)

            -enable --target=instance1             DAS(No EMF created)
                                                   INSTANCE1(EMF created)

*/
    boolean createEMFs = false;
    DeployCommandParameters deployCommandParameters = context.getCommandParameters(DeployCommandParameters.class);
    boolean deploy = deployCommandParameters.origin.isDeploy();
    boolean enabled = deployCommandParameters.enabled;
    boolean isDas = isDas();
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("isEMFCreationRequired(): deploy: " + deploy + " enabled: " + enabled + " isDas: " + isDas);
    }
    if (isDas) {
        if (deploy) {
            // Always create emfs on DAS while deploying to take care of java2db and PU validation on deploy
            createEMFs = true;
        } else {
            // We reach here for (!deploy && das) => server restart or enabling a disabled app on DAS
            boolean isTargetDas = isTargetDas(deployCommandParameters);
            if (logger.isLoggable(Level.FINER)) {
                logger.finer("isEMFCreationRequired(): isTargetDas: " + isTargetDas);
            }
            if (enabled && isTargetDas) {
                createEMFs = true;
            }
        }
    } else {
        // !das => on an instance
        if (enabled) {
            createEMFs = true;
        }
    }
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("isEMFCreationRequired(): returning createEMFs:" + createEMFs);
    }
    return createEMFs;
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters)

Aggregations

DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)69 IOException (java.io.IOException)25 File (java.io.File)24 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)24 ActionReport (org.glassfish.api.ActionReport)23 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)16 Application (com.sun.enterprise.deployment.Application)14 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)13 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)13 Logger (java.util.logging.Logger)12 Properties (java.util.Properties)10 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)10 DeploymentException (org.glassfish.deployment.common.DeploymentException)10 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)10 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)9 VersioningSyntaxException (org.glassfish.deployment.versioning.VersioningSyntaxException)9 Deployment (org.glassfish.internal.deployment.Deployment)9 PropertyVetoException (java.beans.PropertyVetoException)8 URI (java.net.URI)8 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)8