@Override
public void decorate(AdminCommandContext context, final Server instance) throws TransactionFailure, PropertyVetoException {
Config ourConfig = null;
Cluster ourCluster = null;
Logger logger = LogDomains.getLogger(Cluster.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) {
instance.setNode("localhost"); //remove?
}
else { */
if (node != null){
if (domain.getNodeNamed(node) == null) {
throw new TransactionFailure(localStrings.getLocalString(
"noSuchNode", "Node {0} does not exist.", 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 != null &&
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);
}
}
Cluster cluster = domain.getClusterNamed(clusterName);
final String instanceName = instance.getName();
try {
File configConfigDir = new File(env.getConfigDirPath(), cluster.getConfigRef());
new File(configConfigDir, "docroot").mkdirs();
new File(configConfigDir, "lib/ext").mkdirs();
}
catch (Exception e) {
// no big deal - just ignore
}
if (cluster != null) {
if (tx != null) {
Cluster c = tx.enroll(cluster);
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;
try {
File configConfigDir = new File(env.getConfigDirPath(), specifiedConfig.getName());
new File(configConfigDir, "docroot").mkdirs();
new File(configConfigDir, "lib/ext").mkdirs();
}
catch (Exception e) {
// no big deal - just ignore
}
}
//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);