use of org.glassfish.deployment.common.ApplicationConfigInfo 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 applicationInfo = applications.getApplication(appName);
List<DeploymentGroup> deploymentGroups = domain.getDeploymentGroupsForInstance(target);
boolean isAppOnDeploymentGroupInstance = false;
if (deploymentGroups != null && deploymentGroups.isEmpty()) {
List<Application> applicationsInTarget = domain.getApplicationsInTarget(target);
List<String> listOfApplications = new ArrayList<>();
for (Application application : applicationsInTarget) {
listOfApplications.add(application.getName());
}
if (listOfApplications.contains(appName)) {
isAppOnDeploymentGroupInstance = true;
break;
}
}
if (!isAppOnDeploymentGroupInstance) {
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 (applicationInfo.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 = applicationInfo.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 = applicationInfo.archiveType();
contextProps = applicationInfo.getDeployProperties();
modulePropsMap = applicationInfo.getModulePropertiesMap();
savedAppConfig = new ApplicationConfigInfo(applicationInfo);
URI uri = new URI(applicationInfo.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(applicationInfo.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(applicationInfo), 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);
}
}
}
}
}
}
use of org.glassfish.deployment.common.ApplicationConfigInfo in project Payara by payara.
the class VirtualServer method addContext.
/**
* Registers the given <tt>Context</tt> with this <tt>VirtualServer</tt> at the given context root.
*
* <p>
* If this <tt>VirtualServer</tt> has already been started, the given <tt>context</tt> will be started as well.
*
* @throws org.glassfish.embeddable.GlassFishException
*/
@Override
public void addContext(Context context, String contextRoot) throws ConfigException, GlassFishException {
_logger.fine(VS_ADDED_CONTEXT);
if (!(context instanceof ContextFacade)) {
// embedded context should always be created via ContextFacade
return;
}
if (!contextRoot.startsWith("/")) {
contextRoot = "/" + contextRoot;
}
ExtendedDeploymentContext deploymentContext = null;
try {
if (factory == null) {
factory = services.getService(ArchiveFactory.class);
}
ContextFacade facade = (ContextFacade) context;
File docRoot = facade.getDocRoot();
ClassLoader classLoader = facade.getClassLoader();
ReadableArchive archive = factory.openArchive(docRoot);
if (report == null) {
report = new PlainTextActionReporter();
}
ServerEnvironment env = services.getService(ServerEnvironment.class);
DeployCommandParameters params = new DeployCommandParameters();
params.contextroot = contextRoot;
params.enabled = Boolean.FALSE;
params.origin = OpsParams.Origin.deploy;
params.virtualservers = getName();
params.target = "server";
ExtendedDeploymentContext initialContext = new DeploymentContextImpl(report, archive, params, env);
if (deployment == null) {
deployment = services.getService(Deployment.class);
}
ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
if (archiveHandler == null) {
throw new RuntimeException("Cannot find archive handler for source archive");
}
params.name = archiveHandler.getDefaultApplicationName(archive, initialContext);
Applications apps = domain.getApplications();
ApplicationInfo appInfo = deployment.get(params.name);
ApplicationRef appRef = domain.getApplicationRefInServer(params.target, params.name);
if (appInfo != null && appRef != null) {
if (appRef.getVirtualServers().contains(getName())) {
throw new ConfigException("Context with name " + params.name + " is already registered on virtual server " + getName());
} else {
String virtualServers = appRef.getVirtualServers();
virtualServers = virtualServers + "," + getName();
params.virtualservers = virtualServers;
params.force = Boolean.TRUE;
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Virtual server " + getName() + " added to context " + params.name);
}
return;
}
}
deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).archiveHandler(archiveHandler).build(initialContext);
Properties properties = new Properties();
deploymentContext.getAppProps().putAll(properties);
if (classLoader != null) {
ClassLoader parentCL = classLoaderHierarchy.createApplicationParentCL(classLoader, deploymentContext);
ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
deploymentContext.setClassLoader(cl);
}
ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(apps.getModule(com.sun.enterprise.config.serverbeans.Application.class, params.name));
Properties appProps = deploymentContext.getAppProps();
String appLocation = DeploymentUtils.relativizeWithinDomainIfPossible(deploymentContext.getSource().getURI());
appProps.setProperty(ServerTags.LOCATION, appLocation);
appProps.setProperty(ServerTags.OBJECT_TYPE, "user");
appProps.setProperty(ServerTags.CONTEXT_ROOT, contextRoot);
savedAppConfig.store(appProps);
Transaction t = deployment.prepareAppConfigChanges(deploymentContext);
appInfo = deployment.deploy(deploymentContext);
if (appInfo != null) {
facade.setAppName(appInfo.getName());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.VS_ADDED_CONTEXT, new Object[] { getName(), appInfo.getName() });
}
deployment.registerAppInDomainXML(appInfo, deploymentContext, t);
} else {
if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
throw new ConfigException(report.getMessage());
}
}
// Update web.xml with programmatically added servlets, filters, and listeners
File file = null;
boolean delete = true;
com.sun.enterprise.config.serverbeans.Application appBean = apps.getApplication(params.name);
if (appBean != null) {
file = new File(deploymentContext.getSource().getURI().getPath(), "/WEB-INF/web.xml");
if (file.exists()) {
delete = false;
}
updateWebXml(facade, file);
} else {
_logger.log(Level.SEVERE, LogFacade.APP_NOT_FOUND);
}
ReadableArchive source = appInfo.getSource();
UndeployCommandParameters undeployParams = new UndeployCommandParameters(params.name);
undeployParams.origin = UndeployCommandParameters.Origin.undeploy;
undeployParams.target = "server";
ExtendedDeploymentContext undeploymentContext = deployment.getBuilder(_logger, undeployParams, report).source(source).build();
deployment.undeploy(params.name, undeploymentContext);
params.origin = DeployCommandParameters.Origin.load;
params.enabled = Boolean.TRUE;
archive = factory.openArchive(docRoot);
deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).build();
if (classLoader != null) {
ClassLoader parentCL = classLoaderHierarchy.createApplicationParentCL(classLoader, deploymentContext);
archiveHandler = deployment.getArchiveHandler(archive);
ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
deploymentContext.setClassLoader(cl);
}
deployment.deploy(deploymentContext);
// Enable the app using the modified web.xml
// We can't use Deployment.enable since it doesn't take DeploymentContext with custom class loader
deployment.updateAppEnabledAttributeInDomainXML(params.name, params.target, true);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.VS_ENABLED_CONTEXT, new Object[] { getName(), params.name() });
}
if (delete) {
if (file != null) {
if (file.exists() && !file.delete()) {
String path = file.toString();
_logger.log(Level.WARNING, LogFacade.UNABLE_TO_DELETE, path);
}
}
}
if (contextRoot.equals("/")) {
contextRoot = "";
}
WebModule wm = (WebModule) findChild(contextRoot);
if (wm != null) {
facade.setUnwrappedContext(wm);
wm.setEmbedded(true);
if (config != null) {
wm.setDefaultWebXml(config.getDefaultWebXml());
}
} else {
throw new ConfigException("Deployed app not found " + contextRoot);
}
if (deploymentContext != null) {
deploymentContext.postDeployClean(true);
}
} catch (Exception ex) {
if (deployment != null && deploymentContext != null) {
deploymentContext.clean();
}
throw new GlassFishException(ex);
}
}
use of org.glassfish.deployment.common.ApplicationConfigInfo in project Payara by payara.
the class WebDeployer method load.
@Override
public WebApplication load(WebContainer container, DeploymentContext dc) {
super.load(container, dc);
WebBundleDescriptorImpl wbd = dc.getModuleMetaData(WebBundleDescriptorImpl.class);
if (wbd != null) {
wbd.setClassLoader(dc.getClassLoader());
}
Boolean hotDeploy = dc.getCommandParameters(DeployCommandParameters.class).hotDeploy;
WebModuleConfig wmInfo = dc.getModuleMetaData(WebModuleConfig.class);
if (Boolean.FALSE.equals(hotDeploy) || wmInfo == null) {
wmInfo = new WebModuleConfig();
dc.addModuleMetaData(wmInfo);
}
loadWebModuleConfig(wmInfo, dc);
WebApplication webApp = new WebApplication(container, wmInfo, new ApplicationConfigInfo(dc.getAppProps()));
return webApp;
}
use of org.glassfish.deployment.common.ApplicationConfigInfo in project Payara by payara.
the class ApplicationLifecycle method prepareModule.
@Override
public ModuleInfo prepareModule(List<EngineInfo> sortedEngineInfos, String moduleName, DeploymentContext context, ProgressTracker tracker) throws Exception {
List<EngineRef> addedEngines = new ArrayList<>();
StructuredDeploymentTracing tracing = StructuredDeploymentTracing.load(context);
tracing.switchToContext(TraceContext.Level.MODULE, moduleName);
for (EngineInfo engineInfo : sortedEngineInfos) {
// get the deployer
Deployer deployer = engineInfo.getDeployer();
try (DeploymentSpan span = tracing.startSpan(TraceContext.Level.CONTAINER, engineInfo.getSniffer().getModuleType(), DeploymentTracing.AppStage.PREPARE)) {
deployer.prepare(context);
// construct an incomplete EngineRef which will be later
// filled in at loading time
EngineRef engineRef = new EngineRef(engineInfo, null);
addedEngines.add(engineRef);
tracker.add("prepared", EngineRef.class, engineRef);
tracker.add(Deployer.class, deployer);
} catch (Exception e) {
final ActionReport report = context.getActionReport();
report.failure(logger, "Exception while invoking " + deployer.getClass() + " prepare method", e);
throw e;
}
}
if (events != null) {
DeploymentSpan span = tracing.startSpan(TraceContext.Level.MODULE, moduleName, DeploymentTracing.AppStage.PROCESS_EVENTS, Deployment.MODULE_PREPARED.type());
events.send(new Event<>(Deployment.MODULE_PREPARED, context), false);
span.close();
}
// I need to create the application info here from the context, or something like this.
// and return the application info from this method for automatic registration in the caller.
// set isComposite property on module props so we know whether to persist
// module level properties inside ModuleInfo
String isComposite = context.getAppProps().getProperty(ServerTags.IS_COMPOSITE);
if (isComposite != null) {
context.getModuleProps().setProperty(ServerTags.IS_COMPOSITE, isComposite);
}
ModuleInfo mi = new ModuleInfo(events, moduleName, addedEngines, context.getModuleProps());
/*
* Save the application config that is potentially attached to each
* engine in the corresponding EngineRefs that have already created.
*
* Later, in registerAppInDomainXML, the appInfo is saved, which in
* turn saves the moduleInfo children and their engineRef children.
* Saving the engineRef assigns the application config to the Engine
* which corresponds directly to the <engine> element in the XML.
* A long way to get this done.
*/
// Application existingApp = applications.getModule(Application.class, moduleName);
// if (existingApp != null) {
ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(context.getAppProps());
for (EngineRef er : mi.getEngineRefs()) {
ApplicationConfig c = savedAppConfig.get(mi.getName(), er.getContainerInfo().getSniffer().getModuleType());
if (c != null) {
er.setApplicationConfig(c);
}
}
// }
return mi;
}
use of org.glassfish.deployment.common.ApplicationConfigInfo in project Payara by payara.
the class ApplicationLifecycle method enable.
@Override
public ExtendedDeploymentContext enable(String target, Application app, ApplicationRef appRef, ActionReport report, Logger logger) throws Exception {
ReadableArchive archive = null;
try {
DeployCommandParameters commandParams = app.getDeployParameters(appRef);
// if the application is already loaded, do not load again
ApplicationInfo appInfo = appRegistry.get(commandParams.name);
if (appInfo != null && appInfo.isLoaded()) {
return null;
}
commandParams.origin = DeployCommandParameters.Origin.load;
commandParams.command = DeployCommandParameters.Command.enable;
commandParams.target = target;
commandParams.enabled = Boolean.TRUE;
Properties contextProps = app.getDeployProperties();
Map<String, Properties> modulePropsMap = app.getModulePropertiesMap();
ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(app);
URI uri = new URI(app.getLocation());
File file = new File(uri);
if (!file.exists()) {
throw new Exception(localStrings.getLocalString("fnf", "File not found {0}", file.getAbsolutePath()));
}
archive = archiveFactory.openArchive(file);
final ExtendedDeploymentContext deploymentContext = getBuilder(logger, commandParams, report).source(archive).build();
Properties appProps = deploymentContext.getAppProps();
appProps.putAll(contextProps);
savedAppConfig.store(appProps);
if (modulePropsMap != null) {
deploymentContext.setModulePropsMap(modulePropsMap);
}
deploy(getSniffersFromApp(app), deploymentContext);
return deploymentContext;
} finally {
try {
if (archive != null) {
archive.close();
}
} catch (IOException ioe) {
// ignore
}
}
}
Aggregations