use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.
the class WebContainerImpl method bind.
private void bind(Port port, WebListener webListener, String vsId) {
String protocol = Port.HTTP_PROTOCOL;
final int portNumber = port.getPortNumber();
final String defaultVS = vsId;
final WebListener listener = webListener;
if (webListener == null) {
listenerName = getListenerName();
webListener = new HttpListener();
webListener.setId(listenerName);
webListener.setPort(portNumber);
} else {
listenerName = webListener.getId();
protocol = webListener.getProtocol();
}
listeners.add(webListener);
if (protocol.equals(Port.HTTP_PROTOCOL)) {
securityEnabled = "false";
} else if (protocol.equals(Port.HTTPS_PROTOCOL)) {
securityEnabled = "true";
}
try {
ConfigSupport.apply(new SingleConfigCode<Protocols>() {
public Object run(Protocols param) throws TransactionFailure {
final Protocol protocol = param.createChild(Protocol.class);
protocol.setName(listenerName);
protocol.setSecurityEnabled(securityEnabled);
param.getProtocol().add(protocol);
final Http http = protocol.createChild(Http.class);
http.setDefaultVirtualServer(defaultVS);
http.setFileCache(http.createChild(FileCache.class));
protocol.setHttp(http);
return protocol;
}
}, networkConfig.getProtocols());
ConfigSupport.apply(new ConfigCode() {
public Object run(ConfigBeanProxy... params) throws TransactionFailure {
NetworkListeners nls = (NetworkListeners) params[0];
Transports transports = (Transports) params[1];
final NetworkListener listener = nls.createChild(NetworkListener.class);
listener.setName(listenerName);
listener.setPort(Integer.toString(portNumber));
listener.setProtocol(listenerName);
listener.setThreadPool("http-thread-pool");
if (listener.findThreadPool() == null) {
final ThreadPool pool = nls.createChild(ThreadPool.class);
pool.setName(listenerName);
listener.setThreadPool(listenerName);
}
listener.setTransport("tcp");
if (listener.findTransport() == null) {
final Transport transport = transports.createChild(Transport.class);
transport.setName(listenerName);
listener.setTransport(listenerName);
}
nls.getNetworkListener().add(listener);
return listener;
}
}, networkConfig.getNetworkListeners(), networkConfig.getTransports());
if (webListener.getProtocol().equals("https")) {
NetworkListener networkListener = networkConfig.getNetworkListener(listenerName);
Protocol httpProtocol = networkListener.findHttpProtocol();
ConfigSupport.apply(new SingleConfigCode<Protocol>() {
public Object run(Protocol param) throws TransactionFailure {
Ssl newSsl = param.createChild(Ssl.class);
populateSslElement(newSsl, listener);
System.out.println("SSL " + newSsl.getKeyStore() + " " + newSsl.getKeyStorePassword() + " " + newSsl.getTrustStore() + " " + newSsl.getTrustStorePassword());
param.setSsl(newSsl);
return newSsl;
}
}, httpProtocol);
}
com.sun.enterprise.config.serverbeans.VirtualServer vs = httpService.getVirtualServerByName(config.getVirtualServerId());
ConfigSupport.apply(new SingleConfigCode<com.sun.enterprise.config.serverbeans.VirtualServer>() {
public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs) throws PropertyVetoException {
avs.addNetworkListener(listenerName);
return avs;
}
}, vs);
} catch (Exception e) {
if (listeners.contains(webListener)) {
listeners.remove(webListener);
}
e.printStackTrace();
}
}
use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.
the class UpdateResourceRef method execute.
/**
* Execution method for updating the configuration of a ResourceRef. Will be
* replicated if the target is a cluster.
*
* @param context context for the command.
*/
@Override
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
final Logger logger = context.getLogger();
// Make a list of all ResourceRefs that need to change
List<ResourceRef> resourceRefsToChange = new ArrayList<>();
// Add the ResourceRef from a named server if the target is a server
Server server = domain.getServerNamed(target);
// if the target is a server
if (server != null) {
ResourceRef serverResourceRef = server.getResourceRef(name);
// if the ResourceRef doesn't exist
if (serverResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(serverResourceRef);
}
// Add the ResourceRef from a named config if the target is a config
Config config = domain.getConfigNamed(target);
// if the target is a config
if (config != null) {
ResourceRef configResourceRef = config.getResourceRef(name);
// if the ResourceRef doesn't exist
if (configResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(configResourceRef);
}
// Add the ResourceRefs from a named cluster if the target is a cluster
Cluster cluster = domain.getClusterNamed(target);
// if the target is a cluster
if (cluster != null) {
ResourceRef clusterResourceRef = cluster.getResourceRef(name);
// if the ResourceRef doesn't exist
if (clusterResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(clusterResourceRef);
for (Server instance : cluster.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the cluster contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
if (dg != null) {
ResourceRef ref = dg.getResourceRef(name);
if (ref == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(ref);
for (Server instance : dg.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the dg contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Apply the configuration to the listed ResourceRefs
try {
ConfigSupport.apply(new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
for (ConfigBeanProxy proxy : params) {
if (proxy instanceof ResourceRef) {
ResourceRef resourceRefProxy = (ResourceRef) proxy;
if (enabled != null) {
resourceRefProxy.setEnabled(enabled.toString());
}
}
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
return true;
}
}, resourceRefsToChange.toArray(new ResourceRef[] {}));
} catch (TransactionFailure ex) {
report.failure(logger, ex.getLocalizedMessage());
}
}
use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.
the class CreateNetworkListener method execute.
/**
* Executes the command with the command parameters passed as Properties where the keys are the paramter names and
* the values the parameter values
*
* @param context information
*/
public void execute(AdminCommandContext context) {
Target targetUtil = services.getService(Target.class);
Config newConfig = targetUtil.getConfig(target);
if (newConfig != null) {
config = newConfig;
}
final ActionReport report = context.getActionReport();
NetworkConfig networkConfig = config.getNetworkConfig();
NetworkListeners nls = networkConfig.getNetworkListeners();
// ensure we don't have one of this name already
for (NetworkListener networkListener : nls.getNetworkListener()) {
if (networkListener.getName().equals(listenerName)) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL_DUPLICATE), listenerName));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
if (!verifyUniquePort(networkConfig)) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.PORT_IN_USE), port, address));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
Protocol prot = networkConfig.findProtocol(protocol);
if (prot == null) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_HTTP_FAIL_PROTOCOL_NOT_FOUND), protocol));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
if (prot.getHttp() == null && prot.getPortUnification() == null) {
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL_BAD_PROTOCOL), protocol));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
try {
ConfigSupport.apply(new ConfigCode() {
public Object run(ConfigBeanProxy... params) throws TransactionFailure, PropertyVetoException {
NetworkListeners listeners = (NetworkListeners) params[0];
NetworkListener newNetworkListener = listeners.createChild(NetworkListener.class);
newNetworkListener.setProtocol(protocol);
newNetworkListener.setTransport(transport);
newNetworkListener.setEnabled(enabled.toString());
newNetworkListener.setJkEnabled(jkEnabled.toString());
newNetworkListener.setPort(port);
if (portRange != null) {
newNetworkListener.setPortRange(portRange);
}
newNetworkListener.setThreadPool(threadPool);
newNetworkListener.setName(listenerName);
newNetworkListener.setAddress(address);
listeners.getNetworkListener().add(newNetworkListener);
((VirtualServer) params[1]).addNetworkListener(listenerName);
return newNetworkListener;
}
}, nls, findVirtualServer(prot));
} catch (TransactionFailure e) {
e.printStackTrace();
report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL), listenerName) + (e.getMessage() == null ? "No reason given" : e.getMessage()));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setFailureCause(e);
return;
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
use of org.jvnet.hk2.config.ConfigCode 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 org.jvnet.hk2.config.ConfigCode in project Payara by payara.
the class RestMonitoringLoader method reconfigureSystemApplication.
private void reconfigureSystemApplication() throws Exception {
Application systemApplication = restMonitoringAdapter.getSystemApplicationConfig();
LOGGER.log(Level.FINE, "Reconfiguring the Rest Monitoring Application...");
// Reconfigure the system-application entry in the domain.xml
ConfigCode code = new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
Application systemApplication = (Application) proxies[0];
systemApplication.setContextRoot(contextRoot);
return true;
}
};
ConfigSupport.apply(code, systemApplication);
LOGGER.log(Level.FINE, "Rest Monitoring Application Reconfigured.");
}
Aggregations