From 27875646e4620b5c0b78df5b7a8a597c6b471302 Mon Sep 17 00:00:00 2001 From: kaido207 Date: Wed, 29 Nov 2023 17:14:07 +0900 Subject: [PATCH 1/3] Formatting Signed-off-by: kaido207 --- .../deployment/admin/DeployCommand.java | 330 ++++++++---------- 1 file changed, 155 insertions(+), 175 deletions(-) diff --git a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java index d7166002f68..f7353d318ec 100644 --- a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java +++ b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java @@ -1,4 +1,5 @@ /* + * Copyright (c) 2023 Contributors to the Eclipse Foundation * Copyright (c) 2008, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -72,25 +73,24 @@ import org.glassfish.deployment.versioning.VersioningService; - /** * Deploy command * * @author Jerome Dochez */ -@Service(name="deploy") +@Service(name = "deploy") @I18n("deploy.command") @PerLookup -@ExecuteOn(value={RuntimeType.DAS}) -@TargetType(value={CommandTarget.DOMAIN, CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER}) +@ExecuteOn(value = { RuntimeType.DAS }) +@TargetType(value = { CommandTarget.DOMAIN, CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER }) @RestEndpoints({ - @RestEndpoint(configBean=Applications.class,opType=RestEndpoint.OpType.POST, path="deploy"), - @RestEndpoint(configBean=Cluster.class,opType=RestEndpoint.OpType.POST, path="deploy", params={ - @RestParam(name="target", value="$parent") - }), - @RestEndpoint(configBean=Server.class,opType=RestEndpoint.OpType.POST, path="deploy", params={ - @RestParam(name="target", value="$parent") - }) + @RestEndpoint(configBean = Applications.class, opType = RestEndpoint.OpType.POST, path = "deploy"), + @RestEndpoint(configBean = Cluster.class, opType = RestEndpoint.OpType.POST, path = "deploy", params = { + @RestParam(name = "target", value = "$parent") + }), + @RestEndpoint(configBean = Server.class, opType = RestEndpoint.OpType.POST, path = "deploy", params = { + @RestParam(name = "target", value = "$parent") + }) }) public class DeployCommand extends DeployCommandParameters implements AdminCommand, EventListener, AdminCommandSecurity.Preauthorization, AdminCommandSecurity.AccessCheckProvider { @@ -154,42 +154,39 @@ public DeployCommand() { public boolean preAuthorization(AdminCommandContext context) { events.register(this); - suppInfo = - new DeployCommandSupplementalInfo(); - context.getActionReport(). - setResultType(DeployCommandSupplementalInfo.class, suppInfo); + suppInfo = new DeployCommandSupplementalInfo(); + context.getActionReport().setResultType(DeployCommandSupplementalInfo.class, suppInfo); - timing = new DeploymentTracing(); - tracing=null; - if (System.getProperty("org.glassfish.deployment.trace")!=null) { + timing = new DeploymentTracing(); + tracing = null; + if (System.getProperty("org.glassfish.deployment.trace") != null) { tracing = new DeploymentTracing(); - } + } report = context.getActionReport(); logger = context.getLogger(); originalPathValue = path; if (!path.exists()) { - report.setMessage(localStrings.getLocalString("fnf","File not found", path.getAbsolutePath())); + report.setMessage(localStrings.getLocalString("fnf", "File not found", path.getAbsolutePath())); report.setActionExitCode(ActionReport.ExitCode.FAILURE); return false; } if (!path.canRead()) { - report.setMessage(localStrings.getLocalString("fnr","File {0} does not have read permission", path.getAbsolutePath())); + report.setMessage(localStrings.getLocalString("fnr", "File {0} does not have read permission", path.getAbsolutePath())); report.setActionExitCode(ActionReport.ExitCode.FAILURE); return false; } if (snifferManager.hasNoSniffers()) { String msg = localStrings.getLocalString("nocontainer", "No container services registered, done..."); - report.failure(logger,msg); + report.failure(logger, msg); return false; } - try { archive = archiveFactory.openArchive(path, this); - if (tracing!=null) { + if (tracing != null) { tracing.addMark(DeploymentTracing.Mark.ARCHIVE_OPENED); } } catch (IOException e) { @@ -210,45 +207,45 @@ public boolean preAuthorization(AdminCommandContext context) { if (runtimealtdd != null) { archive.addArchiveMetaData(DeploymentProperties.RUNTIME_ALT_DD, - runtimealtdd); + runtimealtdd); } - expansionDir=null; + expansionDir = null; deploymentContext = null; try { deployment.validateSpecifiedTarget(target); archiveHandler = deployment.getArchiveHandler(archive, type); - if (tracing!=null) { + if (tracing != null) { tracing.addMark(DeploymentTracing.Mark.ARCHIVE_HANDLER_OBTAINED); } - if (archiveHandler==null) { - report.failure(logger,localStrings.getLocalString("deploy.unknownarchivetype","Archive type of {0} was not recognized",path)); + if (archiveHandler == null) { + report.failure(logger, localStrings.getLocalString("deploy.unknownarchivetype", "Archive type of {0} was not recognized", path)); return false; } - // create an initial context + // create an initial context initialContext = new DeploymentContextImpl(report, archive, this, env); initialContext.setArchiveHandler(archiveHandler); - if (tracing!=null) { + if (tracing != null) { initialContext.addModuleMetaData(tracing); tracing.addMark(DeploymentTracing.Mark.INITIAL_CONTEXT_CREATED); } events.send(new Event(Deployment.INITIAL_CONTEXT_CREATED, initialContext), false); - if (name==null) { + if (name == null) { name = archiveHandler.getDefaultApplicationName(initialContext.getSource(), initialContext); } else { DeploymentUtils.validateApplicationName(name); } - if (tracing!=null) { + if (tracing != null) { tracing.addMark(DeploymentTracing.Mark.APPNAME_DETERMINED); } boolean isUntagged = VersioningUtils.isUntagged(name); // no GlassFish versioning support for OSGi budles - if ( name != null && !isUntagged && type != null && type.equals("osgi") ) { + if (name != null && !isUntagged && type != null && type.equals("osgi")) { ActionReport.MessagePart msgPart = context.getActionReport().getTopMessagePart(); msgPart.setChildrenType("WARNING"); ActionReport.MessagePart childPart = msgPart.addChild(); @@ -260,14 +257,12 @@ public boolean preAuthorization(AdminCommandContext context) { // if no version information embedded as part of application name // we try to retrieve the version-identifier element's value from DD - if ( isUntagged ){ + if (isUntagged) { String versionIdentifier = archiveHandler.getVersionIdentifier(initialContext.getSource()); - if ( versionIdentifier != null && !versionIdentifier.isEmpty() ) { - StringBuilder sb = new StringBuilder(name). - append(VersioningUtils.EXPRESSION_SEPARATOR). - append(versionIdentifier); - name = sb.toString(); + if (versionIdentifier != null && !versionIdentifier.isEmpty()) { + StringBuilder sb = new StringBuilder(name).append(VersioningUtils.EXPRESSION_SEPARATOR).append(versionIdentifier); + name = sb.toString(); } } @@ -282,7 +277,7 @@ public boolean preAuthorization(AdminCommandContext context) { events.unregister(this); if (initialContext != null && initialContext.getSource() != null) { try { - initialContext.getSource().close(); + initialContext.getSource().close(); } catch (IOException ioex) { throw new RuntimeException(ioex); } @@ -291,7 +286,6 @@ public boolean preAuthorization(AdminCommandContext context) { } } - @Override public Collection getAccessChecks() { final List accessChecks = new ArrayList(); @@ -299,8 +293,7 @@ public Collection getAccessChecks() { accessChecks.add(new AccessCheck(DeploymentCommandUtils.getTargetResourceNameForNewAppRef(domain, target), "create")); /* - * If this app is already deployed then this operation also represents - * an undeployment - a delete - of that app. + * If this app is already deployed then this operation also represents an undeployment - a delete - of that app. */ if (isredeploy) { final String appResource = DeploymentCommandUtils.getResourceNameForNewApp(domain, name); @@ -314,51 +307,50 @@ public Collection getAccessChecks() { /** * Entry point from the framework into the command execution + * * @param context context for the command. */ @Override public void execute(AdminCommandContext context) { try { - // needs to be fixed in hk2, we don't generate the right innerclass index. it should use $ + // needs to be fixed in hk2, we don't generate the right innerclass index. it should use $ Collection interceptors = habitat.getAllServices(Interceptor.class); - if (interceptors!=null) { + if (interceptors != null) { for (Interceptor interceptor : interceptors) { interceptor.intercept(this, initialContext); } } - deployment.validateDeploymentTarget(target, name, isredeploy); - if (tracing!=null) { + if (tracing != null) { tracing.addMark(DeploymentTracing.Mark.TARGET_VALIDATED); } ActionReport.MessagePart part = report.getTopMessagePart(); part.addProperty(DeploymentProperties.NAME, name); - ApplicationConfigInfo savedAppConfig = - new ApplicationConfigInfo(apps.getModule(Application.class, name)); + ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(apps.getModule(Application.class, name)); Properties undeployProps = handleRedeploy(name, report, context); if (enabled == null) { enabled = Boolean.TRUE; } // clean up any left over repository files - if ( ! keepreposdir.booleanValue()) { + if (!keepreposdir.booleanValue()) { final File reposDir = new File(env.getApplicationRepositoryPath(), VersioningUtils.getRepositoryName(name)); if (reposDir.exists()) { - for (int i=0 ; i - * We rename any uploaded files from the temp directory to the permanent - * place, and we copy any archive files that were not uploaded. This - * prevents any confusion that could result if the developer modified the - * archive file - changing its lastModified value - before redeploying it. + * We rename any uploaded files from the temp directory to the permanent place, and we copy any archive files that were + * not uploaded. This prevents any confusion that could result if the developer modified the archive file - changing its + * lastModified value - before redeploying it. * * @param deploymentContext * @param logger logger @@ -602,73 +584,69 @@ private void moveAppFilesToPermanentLocation( final Logger logger) throws IOException { final File finalUploadDir = deploymentContext.getAppInternalDir(); final File finalAltDDDir = deploymentContext.getAppAltDDDir(); - if ( ! finalUploadDir.mkdirs()) { - logger.log(Level.FINE," Attempting to create upload directory {0} was reported as failed; attempting to continue", - new Object[] {finalUploadDir.getAbsolutePath()}); + if (!finalUploadDir.mkdirs()) { + logger.log(Level.FINE, " Attempting to create upload directory {0} was reported as failed; attempting to continue", + new Object[] { finalUploadDir.getAbsolutePath() }); } - safeCopyOfApp = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalUploadDir, originalPathValue, logger, env); - safeCopyOfDeploymentPlan = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalUploadDir, deploymentplan, logger, env); - safeCopyOfAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalAltDDDir, altdd, logger, env); - safeCopyOfRuntimeAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile( finalAltDDDir, runtimealtdd, logger, env); + safeCopyOfApp = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalUploadDir, originalPathValue, logger, env); + safeCopyOfDeploymentPlan = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalUploadDir, deploymentplan, logger, env); + safeCopyOfAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, altdd, logger, env); + safeCopyOfRuntimeAltDD = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(finalAltDDDir, runtimealtdd, logger, env); } private void recordFileLocations( final Properties appProps) throws URISyntaxException { /* - * Setting the properties in the appProps now will cause them to be - * stored in the domain.xml elements along with other properties when - * the entire config is saved. + * Setting the properties in the appProps now will cause them to be stored in the domain.xml elements along with other + * properties when the entire config is saved. */ if (safeCopyOfApp != null) { appProps.setProperty(Application.APP_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible( - safeCopyOfApp.toURI())); + safeCopyOfApp.toURI())); } if (safeCopyOfDeploymentPlan != null) { - appProps.setProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME, - DeploymentUtils.relativizeWithinDomainIfPossible( - safeCopyOfDeploymentPlan.toURI())); + appProps.setProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME, + DeploymentUtils.relativizeWithinDomainIfPossible( + safeCopyOfDeploymentPlan.toURI())); } if (safeCopyOfAltDD != null) { - appProps.setProperty(Application.ALT_DD_LOCATION_PROP_NAME, - DeploymentUtils.relativizeWithinDomainIfPossible( - safeCopyOfAltDD.toURI())); + appProps.setProperty(Application.ALT_DD_LOCATION_PROP_NAME, + DeploymentUtils.relativizeWithinDomainIfPossible( + safeCopyOfAltDD.toURI())); } if (safeCopyOfRuntimeAltDD != null) { - appProps.setProperty( - Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME, - DeploymentUtils.relativizeWithinDomainIfPossible( - safeCopyOfRuntimeAltDD.toURI())); + appProps.setProperty( + Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME, + DeploymentUtils.relativizeWithinDomainIfPossible( + safeCopyOfRuntimeAltDD.toURI())); } } /** - * Check if the application is deployed or not. - * If force option is true and appInfo is not null, then undeploy - * the application and return false. This will force deployment - * if there's already a running application deployed. + * Check if the application is deployed or not. If force option is true and appInfo is not null, then undeploy the + * application and return false. This will force deployment if there's already a running application deployed. * - * @param name application name - * @param report ActionReport, report object to send back to client. - * @return context properties that might have been set by the deployers - * while undeploying the application + * @param name application name + * @param report ActionReport, report object to send back to client. + * @return context properties that might have been set by the deployers while undeploying the application * */ private Properties handleRedeploy(final String name, final ActionReport report, final AdminCommandContext context) - throws Exception { - if (isredeploy) - { - //preserve settings first before undeploy + throws Exception { + if (isredeploy) { + // preserve settings first before undeploy Application app = apps.getModule(Application.class, name); - if (app.isLifecycleModule()){ - throw new IllegalArgumentException(localStrings.getLocalString("lifecyclemodule_withsamename_exists", "Lifecycle module with same name {0} already exists, please pick a different name for the application. ", name)); + if (app.isLifecycleModule()) { + throw new IllegalArgumentException(localStrings.getLocalString("lifecyclemodule_withsamename_exists", + "Lifecycle module with same name {0} already exists, please pick a different name for the application. ", name)); } // we save some of the old registration information in our deployment parameters settingsFromDomainXML(app); - //if application is already deployed and force=true, - //then undeploy the application first. + // if application is already deployed and force=true, + // then undeploy the application first. // Use ParameterMap till we have a better way // to invoke a command on both DAS and instance with the @@ -718,6 +696,7 @@ private void populatePropertiesToParameterMap(ParameterMap parameters, List Date: Wed, 29 Nov 2023 17:28:32 +0900 Subject: [PATCH 2/3] Fix Fail to deploy a app when the app's material files remain and if there is a lifecycle module Signed-off-by: kaido207 --- .../java/org/glassfish/deployment/admin/DeployCommand.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java index f7353d318ec..4467ffa4c31 100644 --- a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java +++ b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java @@ -340,6 +340,9 @@ public void execute(AdminCommandContext context) { final File reposDir = new File(env.getApplicationRepositoryPath(), VersioningUtils.getRepositoryName(name)); if (reposDir.exists()) { for (int i = 0; i < domain.getApplications().getApplications().size(); i++) { + if (domain.getApplications().getApplications().get(i).isLifecycleModule()) { + continue; + } File existrepos = new File(new URI(domain.getApplications().getApplications().get(i).getLocation())); String appname = domain.getApplications().getApplications().get(i).getName(); if (!appname.equals(name) && existrepos.getAbsoluteFile().equals(reposDir.getAbsoluteFile())) { From 00a69d1ddca058fda46881d5928bc0711dc74f7b Mon Sep 17 00:00:00 2001 From: kaido207 Date: Thu, 30 Nov 2023 13:10:15 +0900 Subject: [PATCH 3/3] Refactor near fixes Signed-off-by: kaido207 --- .../org/glassfish/deployment/admin/DeployCommand.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java index 4467ffa4c31..d1e1b341c65 100644 --- a/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java +++ b/nucleus/deployment/admin/src/main/java/org/glassfish/deployment/admin/DeployCommand.java @@ -339,12 +339,13 @@ public void execute(AdminCommandContext context) { if (!keepreposdir.booleanValue()) { final File reposDir = new File(env.getApplicationRepositoryPath(), VersioningUtils.getRepositoryName(name)); if (reposDir.exists()) { - for (int i = 0; i < domain.getApplications().getApplications().size(); i++) { - if (domain.getApplications().getApplications().get(i).isLifecycleModule()) { + List applications = domain.getApplications().getApplications(); + for (Application app : applications) { + if (app.isLifecycleModule()) { continue; } - File existrepos = new File(new URI(domain.getApplications().getApplications().get(i).getLocation())); - String appname = domain.getApplications().getApplications().get(i).getName(); + File existrepos = new File(new URI(app.getLocation())); + String appname = app.getName(); if (!appname.equals(name) && existrepos.getAbsoluteFile().equals(reposDir.getAbsoluteFile())) { report.failure(logger, localStrings.getLocalString("deploy.dupdeployment", "Application {0} is trying to use the same repository directory as application {1}, please choose a different application name to deploy",