@Override
public void decorate(AdminCommandContext context, final Server instance) throws TransactionFailure, PropertyVetoException {
Config ourConfig = null;
Cluster ourCluster = null;
Logger logger = LogDomains.getLogger(Server.class, LogDomains.ADMIN_LOGGER);
LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Server.class);
Transaction tx = Transaction.getTransaction(instance);
String configRef = instance.getConfigRef();
Clusters clusters = domain.getClusters();
if (tx == null) {
throw new TransactionFailure(localStrings.getLocalString(
"noTransaction", "Internal Error - Cannot obtain transaction object"));
}
if (node != null){
Node theNode = domain.getNodeNamed(node);
if (theNode == null) {
throw new TransactionFailure(localStrings.getLocalString(
"noSuchNode", "Node {0} does not exist.", node));
}
/* 16034: see if instance creation is turned off on node */
if (! theNode.instanceCreationAllowed()) {
throw new TransactionFailure(localStrings.getLocalString(
"instanceCreationNotAllowed",
"Instance creation is disabled on node {0}.",
node));
}
}
if (portBase != null) {
PortBaseHelper pbh = new PortBaseHelper(instance, portBase, false, logger);
pbh.verifyPortBase();
pbh.setPorts();
}
// cluster instance using cluster config
if (clusterName != null) {
if (configRef != null) {
throw new TransactionFailure(localStrings.getLocalString(
"Server.cannotSpecifyBothConfigAndCluster",
"A configuration name and cluster name cannot both be specified."));
}
boolean clusterExists = false;
if (clusters != null) {
for (Cluster cluster : clusters.getCluster()) {
if (cluster != null && clusterName.equals(cluster.getName())) {
ourCluster = cluster;
String configName = cluster.getConfigRef();
instance.setConfigRef(configName);
clusterExists = true;
ourConfig = domain.getConfigNamed(configName);
break;
}
}
}
if (ourCluster == null) {
throw new TransactionFailure(localStrings.getLocalString(
"noSuchCluster", "Cluster {0} does not exist.", clusterName));
}
/*
* We are only setting this when the discovery uri list
* is set to "generate." Otherwise the user must set this
* properly to match the discovery uri list.
*/
if (ourCluster.getProperty("GMS_DISCOVERY_URI_LIST") != null &&
"generate".equals(
ourCluster.getProperty("GMS_DISCOVERY_URI_LIST").getValue())) {
final String propName = "GMS_LISTENER_PORT-" +
ourCluster.getName();
/*
* Currently all the instances will use the same port
* as the DAS. When/if we move to allow more than one
* instance/machine, the value here will need to be
* calculated differently.
*/
Config serverConf = domain.getConfigNamed("server-config");
SystemProperty dasGmsPortProp =
serverConf.getSystemProperty(propName);
if (dasGmsPortProp != null) {
SystemProperty gmsListenerPortProp =
instance.createChild(SystemProperty.class);
gmsListenerPortProp.setName(propName);
gmsListenerPortProp.setValue(dasGmsPortProp.getValue());
instance.getSystemProperty().add(gmsListenerPortProp);
}
}
final String instanceName = instance.getName();
File configConfigDir = new File(env.getConfigDirPath(), ourCluster.getConfigRef());
File docroot = new File(configConfigDir, "docroot");
if (!docroot.exists() && !docroot.mkdirs()) {
throw new TransactionFailure(localStrings.getLocalString(
"noMkdir", "Cannot create configuration specific directory {0}", "docroot"));
}
File lib = new File(configConfigDir, "lib/ext");
if (!lib.exists() && !lib.mkdirs()) {
throw new TransactionFailure(localStrings.getLocalString(
"noMkdir", "Cannot create configuration specific directory {0}", "lib/ext"));
}
Cluster c = tx.enroll(ourCluster);
ServerRef newServerRef = c.createChild(ServerRef.class);
newServerRef.setRef(instanceName);
if (lbEnabled != null) {
newServerRef.setLbEnabled(lbEnabled);
} else {
//check whether all instances in cluster had lb-enabled set to false
List<ServerRef> serverRefs = c.getServerRef();
Iterator<ServerRef> serverRefIter = serverRefs.iterator();
boolean allLBEnabled = false;
while (!allLBEnabled && serverRefIter.hasNext()) {
ServerRef serverRef = serverRefIter.next();
allLBEnabled = allLBEnabled
|| Boolean.parseBoolean(serverRef.getLbEnabled());
}
//if there are existing instances in cluster
//and they all have lb-enabled to false, set it
//false for new instance as well
if (!allLBEnabled && serverRefs.size() > 0) {
newServerRef.setLbEnabled("false");
} else {
//check if system property exists and use that
String lbEnabledDefault =
System.getProperty(lbEnabledSystemProperty);
if (lbEnabledDefault != null) {
newServerRef.setLbEnabled(lbEnabledDefault);
}
}
}
c.getServerRef().add(newServerRef);
}
// instance using specified config
if (configRef != null) {
Config specifiedConfig = domain.getConfigs().getConfigByName(configRef);
if (specifiedConfig == null) {
throw new TransactionFailure(localStrings.getLocalString(
"noSuchConfig", "Configuration {0} does not exist.", configRef));
}
ourConfig = specifiedConfig;
File configConfigDir = new File(env.getConfigDirPath(), specifiedConfig.getName());
File docroot = new File(configConfigDir, "docroot");
if (!docroot.exists() && !docroot.mkdirs()) {
throw new TransactionFailure(localStrings.getLocalString(
"noMkdir", "Cannot create configuration specific directory {0}", "docroot"));
}
File lib = new File(configConfigDir, "lib/ext");
if (!lib.exists() && !lib.mkdirs()) {
throw new TransactionFailure(localStrings.getLocalString(
"noMkdir", "Cannot create configuration specific directory {0}", "lib/ext"));
}
}
//stand-alone instance using default-config if config not specified
if (configRef == null && clusterName == null) {
Config defaultConfig = domain.getConfigs().getConfigByName("default-config");
if (defaultConfig == null) {
final String msg = localStrings.getLocalString(Server.class,
"Cluster.noDefaultConfig",
"Can''t find the default config (an element named \"default-config\") "
+ "in domain.xml. You may specify the name of an existing config element next time.");
logger.log(Level.SEVERE, msg);