Skip to content

Commit

Permalink
Move console config code to separate class
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Feb 22, 2024
1 parent cd24d58 commit 7309d0f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package com.sun.enterprise.v3.admin.adapter;

import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.ApplicationRef;
import com.sun.enterprise.config.serverbeans.Engine;
import com.sun.enterprise.config.serverbeans.Module;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.SystemApplications;

import java.beans.PropertyVetoException;
import java.util.Arrays;
import java.util.List;

import org.glassfish.deployment.common.DeploymentProperties;
import org.glassfish.server.ServerEnvironmentImpl;
import org.jvnet.hk2.config.ConfigBeanProxy;
import org.jvnet.hk2.config.ConfigCode;
import org.jvnet.hk2.config.TransactionFailure;

/**
* Create the Administration Console web application entry in {@code domain.xml}.
*/
class ConsoleConfigCode implements ConfigCode {

private final List<String> virtualServers;
private final String contextRoot;

ConsoleConfigCode(List<String> virtualServers, String contextRoot) {
this.virtualServers = virtualServers;
this.contextRoot = contextRoot;
}

@Override
public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
SystemApplications systemApplications = (SystemApplications) proxies[0];

Application application = systemApplications.createChild(Application.class);
systemApplications.getModules().add(application);

application.setName(ServerEnvironmentImpl.DEFAULT_ADMIN_CONSOLE_APP_NAME);
application.setEnabled(Boolean.TRUE.toString());
application.setObjectType(DeploymentProperties.SYSTEM_ADMIN);
application.setDirectoryDeployed("true");
application.setContextRoot(contextRoot);
try {
application.setLocation("${com.sun.aas.installRootURI}/lib/install/applications/"
+ ServerEnvironmentImpl.DEFAULT_ADMIN_CONSOLE_APP_NAME);
} catch (Exception e) {
// Can't do anything
throw new RuntimeException(e);
}

Module consoleModule = application.createChild(Module.class);
application.getModule().add(consoleModule);
consoleModule.setName(application.getName());

Engine webEngine = consoleModule.createChild(Engine.class);
webEngine.setSniffer("web");

Engine weldEngine = consoleModule.createChild(Engine.class);
weldEngine.setSniffer("weld");

Engine securityEngine = consoleModule.createChild(Engine.class);
securityEngine.setSniffer("security");

consoleModule.getEngines().add(webEngine);
consoleModule.getEngines().add(weldEngine);
consoleModule.getEngines().add(securityEngine);

Server server = (Server) proxies[1];
ApplicationRef applicationRef = server.createChild(ApplicationRef.class);
applicationRef.setRef(application.getName());
applicationRef.setEnabled(Boolean.TRUE.toString());
applicationRef.setVirtualServers(getVirtualServerList());
server.getApplicationRef().add(applicationRef);

return true;
}

private String getVirtualServerList() {
if (virtualServers == null) {
return "";
}

String servers = Arrays.toString(virtualServers.toArray(String[]::new));
// Remove [] if present
if (servers.startsWith("[") && servers.endsWith("]")) {
servers = servers.substring(1, servers.length() - 1);
}
return servers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,18 @@
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.ApplicationRef;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Engine;
import com.sun.enterprise.config.serverbeans.Module;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.SystemApplications;
import com.sun.enterprise.v3.server.ApplicationLoaderService;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.deployment.common.DeploymentProperties;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.kernel.KernelLoggerInfo;
import org.glassfish.server.ServerEnvironmentImpl;
import org.jvnet.hk2.config.ConfigCode;
import org.jvnet.hk2.config.ConfigSupport;

/**
Expand Down Expand Up @@ -96,7 +90,7 @@ public void run() {


/**
* Install the admingui.war file
* Install the Admin Console web application.
*/
private void install() throws Exception {
if (domain.getSystemApplicationReferencedFrom(serverEnvironment.getInstanceName(),
Expand All @@ -111,54 +105,9 @@ private void install() throws Exception {

logger.log(Level.FINE, "Installing the Admin Console Application...");

// Create the application entry in domain.xml
ConfigCode code = proxies -> {
SystemApplications systemApplications = (SystemApplications) proxies[0];

Application application = systemApplications.createChild(Application.class);
systemApplications.getModules().add(application);

application.setName(ServerEnvironmentImpl.DEFAULT_ADMIN_CONSOLE_APP_NAME);
application.setEnabled(Boolean.TRUE.toString());
application.setObjectType(DeploymentProperties.SYSTEM_ADMIN);
application.setDirectoryDeployed("true");
application.setContextRoot(contextRoot);
try {
application.setLocation("${com.sun.aas.installRootURI}/lib/install/applications/"
+ ServerEnvironmentImpl.DEFAULT_ADMIN_CONSOLE_APP_NAME);
} catch (Exception e) {
// Can't do anything
throw new RuntimeException(e);
}

Module singleModule = application.createChild(Module.class);
application.getModule().add(singleModule);
singleModule.setName(application.getName());

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);

Server server = (Server) proxies[1];
ApplicationRef applicationRef = server.createChild(ApplicationRef.class);
applicationRef.setRef(application.getName());
applicationRef.setEnabled(Boolean.TRUE.toString());
applicationRef.setVirtualServers(getVirtualServerList());
server.getApplicationRef().add(applicationRef);

return true;
};

ConsoleConfigCode code = new ConsoleConfigCode(virtualServers, contextRoot);
Server instance = domain.getServerNamed(serverEnvironment.getInstanceName());

ConfigSupport.apply(code, domain.getSystemApplications(), instance);

// Set the adapter state
Expand All @@ -167,19 +116,6 @@ private void install() throws Exception {
logger.log(Level.FINE, "Admin Console Application Installed.");
}

private String getVirtualServerList() {
if (virtualServers == null) {
return "";
}

String servers = Arrays.toString(virtualServers.toArray(String[]::new));
// Remove [] if present
if (servers.startsWith("[") && servers.endsWith("]")) {
servers = servers.substring(1, servers.length() - 1);
}
return servers;
}

/**
* Load the Admin Console web application.
*/
Expand Down

0 comments on commit 7309d0f

Please sign in to comment.