use of com.sun.enterprise.config.serverbeans.ApplicationRef 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 {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.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 = clh.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 = clh.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 com.sun.enterprise.config.serverbeans.ApplicationRef in project Payara by payara.
the class RestMonitoringLoader method createAndRegisterApplication.
/**
* Create the system application entry and register the application
* @throws Exception
*/
private void createAndRegisterApplication() throws Exception {
LOGGER.log(Level.FINE, "Registering the Rest Monitoring Application...");
// Create the system application entry and application-ref in the config
ConfigCode code = new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
// Create the system application
SystemApplications systemApplications = (SystemApplications) proxies[0];
Application application = systemApplications.createChild(Application.class);
// Check if the application name is valid, generating a new one if it isn't
checkAndResolveApplicationName(systemApplications);
systemApplications.getModules().add(application);
application.setName(applicationName);
application.setEnabled(Boolean.TRUE.toString());
application.setObjectType("system-admin");
application.setDirectoryDeployed("true");
application.setContextRoot(contextRoot);
try {
application.setLocation("${com.sun.aas.installRootURI}/lib/install/applications/" + RestMonitoringService.DEFAULT_REST_MONITORING_APP_NAME);
} catch (Exception me) {
throw new RuntimeException(me);
}
// Set the engine types
Module singleModule = application.createChild(Module.class);
application.getModule().add(singleModule);
singleModule.setName(applicationName);
Engine webEngine = singleModule.createChild(Engine.class);
webEngine.setSniffer("web");
Engine weldEngine = singleModule.createChild(Engine.class);
weldEngine.setSniffer("weld");
Engine securityEngine = singleModule.createChild(Engine.class);
securityEngine.setSniffer("security");
singleModule.getEngines().add(webEngine);
singleModule.getEngines().add(weldEngine);
singleModule.getEngines().add(securityEngine);
// Create the application-ref
Server s = (Server) proxies[1];
List<ApplicationRef> arefs = s.getApplicationRef();
ApplicationRef aref = s.createChild(ApplicationRef.class);
aref.setRef(application.getName());
aref.setEnabled(Boolean.TRUE.toString());
aref.setVirtualServers(getVirtualServerListAsString());
arefs.add(aref);
return true;
}
};
Server server = domain.getServerNamed(serverEnv.getInstanceName());
ConfigSupport.apply(code, domain.getSystemApplications(), server);
LOGGER.log(Level.FINE, "Rest Monitoring Application Registered.");
}
use of com.sun.enterprise.config.serverbeans.ApplicationRef in project Payara by payara.
the class RestMonitoringLoader method registerApplication.
private void registerApplication() throws Exception {
LOGGER.log(Level.FINE, "Registering the Rest Monitoring Application...");
// Create the application-ref entry in the domain.xml
ConfigCode code = new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
// Get the system application config
SystemApplications systemApplications = (SystemApplications) proxies[0];
Application application = null;
for (Application systemApplication : systemApplications.getApplications()) {
if (systemApplication.getName().equals(applicationName)) {
application = systemApplication;
break;
}
}
if (application == null) {
throw new IllegalStateException("The Rest Monitoring application has no system app entry!");
}
// Create the application-ref
Server s = (Server) proxies[1];
List<ApplicationRef> arefs = s.getApplicationRef();
ApplicationRef aref = s.createChild(ApplicationRef.class);
aref.setRef(application.getName());
aref.setEnabled(Boolean.TRUE.toString());
aref.setVirtualServers(getVirtualServerListAsString());
arefs.add(aref);
return true;
}
};
Server server = domain.getServerNamed(serverEnv.getInstanceName());
ConfigSupport.apply(code, domain.getSystemApplications(), server);
// Update the adapter state
LOGGER.log(Level.FINE, "Rest Monitoring Application Registered.");
}
use of com.sun.enterprise.config.serverbeans.ApplicationRef in project Payara by payara.
the class RestMonitoringLoader method loadApplication.
/**
* Loads the application
*/
private void loadApplication() {
ApplicationRegistry appRegistry = habitat.getService(ApplicationRegistry.class);
ApplicationInfo appInfo = appRegistry.get(applicationName);
if (appInfo != null && appInfo.isLoaded()) {
LOGGER.log(Level.FINE, "Rest Monitoring Application already loaded.");
return;
}
Application config = null;
if (dynamicStart) {
config = restMonitoringAdapter.getSystemApplicationConfig(contextRoot);
} else {
config = restMonitoringAdapter.getSystemApplicationConfig();
}
if (config == null) {
throw new IllegalStateException("The Rest Monitoring application has no system app entry!");
}
// Load the Rest Monitoring Application
String instanceName = serverEnv.getInstanceName();
ApplicationRef ref = domain.getApplicationRefInServer(instanceName, applicationName);
habitat.getService(ApplicationLoaderService.class).processApplication(config, ref);
// Mark as registered
restMonitoringAdapter.setAppRegistered(true);
LOGGER.log(Level.FINE, "Rest Monitoring Application Loaded.");
}
use of com.sun.enterprise.config.serverbeans.ApplicationRef in project Payara by payara.
the class CreateHTTPLBRefCommand method execute.
@Override
public void execute(AdminCommandContext context) {
report = context.getActionReport();
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
boolean isCluster = (target != null) ? tgt.isCluster(target) : false;
if (config != null && lbname != null) {
String msg = localStrings.getLocalString("EitherConfigOrLBName", "Either LB name or LB config name, not both");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
if (config == null && lbname == null) {
String msg = localStrings.getLocalString("SpecifyConfigOrLBName", "Please specify either LB name or LB config name.");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
LbConfigs lbconfigs = domain.getExtensionByType(LbConfigs.class);
if (lbconfigs == null) {
String msg = localStrings.getLocalString("NoLbConfigsElement", "Empty lb-configs");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
if (config != null) {
if (lbconfigs.getLbConfig(config) == null) {
String msg = localStrings.getLocalString("LbConfigDoesNotExist", "Specified LB config {0} does not exist", config);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
} else if (lbname != null) {
LoadBalancers lbs = domain.getExtensionByType(LoadBalancers.class);
if (lbs == null) {
String msg = localStrings.getLocalString("NoLoadBalancersElement", "No Load balancers defined.");
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
LoadBalancer lb = lbs.getLoadBalancer(lbname);
if (lb == null) {
String msg = localStrings.getLocalString("LoadBalancerNotDefined", "Load balancer [{0}] not found.", lbname);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
config = lb.getLbConfigName();
}
if ((lbpolicy != null) || (lbpolicymodule != null)) {
if (!isCluster) {
String msg = localStrings.getLocalString("NotCluster", "{0} not a cluster", target);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
}
Cluster c = null;
Server s = null;
if (isCluster) {
c = domain.getClusterNamed(target);
if (c == null) {
String msg = localStrings.getLocalString("ClusterNotDefined", "Cluster {0} cannot be used as target", target);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
} else {
s = domain.getServerNamed(target);
if (s == null) {
String msg = localStrings.getLocalString("ServerNotDefined", "Server {0} cannot be used as target", target);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
}
// create lb ref
createLBRef(lbconfigs, target, config);
if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
return;
}
if (healthcheckerurl != null) {
try {
final CreateHTTPHealthCheckerCommand command = (CreateHTTPHealthCheckerCommand) runner.getCommand("create-http-health-checker", report, context.getLogger());
command.url = healthcheckerurl;
command.interval = healthcheckerinterval;
command.timeout = healthcheckertimeout;
command.config = config;
command.target = target;
command.execute(context);
checkCommandStatus(context);
} catch (CommandException e) {
String msg = e.getLocalizedMessage();
logger.warning(msg);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(msg);
return;
}
}
if (Boolean.parseBoolean(lbenableallinstances)) {
try {
final EnableHTTPLBServerCommand command = (EnableHTTPLBServerCommand) runner.getCommand("enable-http-lb-server", report, context.getLogger());
command.target = target;
command.execute(context);
checkCommandStatus(context);
} catch (CommandException e) {
String msg = e.getLocalizedMessage();
logger.warning(msg);
// report.setActionExitCode(ExitCode.FAILURE);
// report.setMessage(msg);
// return;
}
}
if (Boolean.parseBoolean(lbenableallapplications)) {
List<ApplicationRef> appRefs = domain.getApplicationRefsInTarget(target);
if ((appRefs.size() > 0) && Boolean.parseBoolean(lbenableallapplications)) {
for (ApplicationRef ref : appRefs) {
// enable only user applications
if (isUserApp(ref.getRef())) {
enableApp(context, ref.getRef());
}
}
}
}
}
Aggregations