if(cacheProperties==null) {
cacheProperties = new HashMap<String, String>();
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ServerAPI serverAPI = APILocator.getServerAPI();
String cacheProtocol, bindAddr, bindPort, cacheTCPInitialHosts, mCastAddr, mCastPort, preferIPv4;
if(Config.getBooleanProperty("CLUSTER_AUTOWIRE",true)) {
Logger.info(this, "Using automatic port placement as CLUSTER_AUTOWIRE is ON");
cacheProtocol = UtilMethods.isSet(cacheProperties.get("CACHE_PROTOCOL"))?cacheProperties.get("CACHE_PROTOCOL")
:Config.getStringProperty("CACHE_PROTOCOL", "tcp");
String storedBindAddr = (UtilMethods.isSet(localServer.getHost()) && !localServer.getHost().equals("localhost"))
?localServer.getHost():localServer.getIpAddress();
bindAddr = UtilMethods.isSet(cacheProperties.get("BIND_ADDRESS"))?cacheProperties.get("BIND_ADDRESS")
:Config.getStringProperty("CACHE_BINDADDRESS", storedBindAddr );
bindPort = UtilMethods.isSet(cacheProperties.get("CACHE_BINDPORT"))?cacheProperties.get("CACHE_BINDPORT")
:localServer!=null&&UtilMethods.isSet(localServer.getCachePort())?Long.toString(localServer.getCachePort())
:ClusterFactory.getNextAvailablePort(localServer.getServerId(), ServerPort.CACHE_PORT);
localServer.setCachePort(Integer.parseInt(bindPort));
localServer.setHost(Config.getStringProperty("CACHE_BINDADDRESS", null));
List<String> myself = new ArrayList<String>();
myself.add(localServer.getServerId());
List<Server> aliveServers = serverAPI.getAliveServers(myself);
aliveServers.add(localServer);
StringBuilder initialHosts = new StringBuilder();
int i=0;
for (Server server : aliveServers) {
if(i>0) {
initialHosts.append(",");
}
if(UtilMethods.isSet(server.getHost()) && !server.getHost().equals("localhost")) {
initialHosts.append(server.getHost()).append("[").append(server.getCachePort()).append("]");
} else {
initialHosts.append(server.getIpAddress()).append("[").append(server.getCachePort()).append("]");
}
i++;
}
if(initialHosts.length()==0) {
if(bindAddr.equals("localhost")) {
initialHosts.append(localServer.getIpAddress()).append("[").append(bindPort).append("]");
} else {
initialHosts.append(bindAddr).append("[").append(bindPort).append("]");
}
}
cacheTCPInitialHosts = UtilMethods.isSet(cacheProperties.get("CACHE_TCP_INITIAL_HOSTS"))?cacheProperties.get("CACHE_TCP_INITIAL_HOSTS")
:Config.getStringProperty("CACHE_TCP_INITIAL_HOSTS", initialHosts.toString());
mCastAddr = UtilMethods.isSet(cacheProperties.get("CACHE_MULTICAST_ADDRESS"))?cacheProperties.get("CACHE_MULTICAST_ADDRESS")
:Config.getStringProperty("CACHE_MULTICAST_ADDRESS", "228.10.10.10");
mCastPort = UtilMethods.isSet(cacheProperties.get("CACHE_MULTICAST_PORT"))?cacheProperties.get("CACHE_MULTICAST_PORT")
:Config.getStringProperty("CACHE_MULTICAST_PORT", "45588");
preferIPv4 = UtilMethods.isSet(cacheProperties.get("CACHE_FORCE_IPV4"))?cacheProperties.get("CACHE_FORCE_IPV4")
:Config.getStringProperty("CACHE_FORCE_IPV4", "true");
}
else {
Logger.info(this, "Using manual port placement as CLUSTER_AUTOWIRE is OFF");
cacheProtocol = Config.getStringProperty("CACHE_PROTOCOL", "tcp");
bindAddr = Config.getStringProperty("CACHE_BINDADDRESS", null);
bindPort = Config.getStringProperty("CACHE_BINDPORT", null);
cacheTCPInitialHosts = Config.getStringProperty("CACHE_TCP_INITIAL_HOSTS", "localhost[7800]");
mCastAddr = Config.getStringProperty("CACHE_MULTICAST_ADDRESS", "228.10.10.10");
mCastPort = Config.getStringProperty("CACHE_MULTICAST_PORT", "45588");
preferIPv4 = Config.getStringProperty("CACHE_FORCE_IPV4", "true");
}
String cacheFile = "cache-jgroups-" + cacheProtocol + ".xml";
Logger.info(this, "***\t Going to load JGroups with this Classpath file " + cacheFile);
if (UtilMethods.isSet(bindAddr)) {
Logger.info(this, "***\t Using " + bindAddr + " as the bindaddress");
System.setProperty("jgroups.bind_addr", bindAddr);
}
else {
Logger.info(this, "***\t bindaddress is not set");
}
if (UtilMethods.isSet(bindPort)) {
Logger.info(this, "***\t Using " + bindPort + " as the bindport");
System.setProperty("jgroups.bind_port", bindPort);
}
else {
Logger.info(this, "***\t bindport is not set");
}
if (cacheProtocol.equals("tcp")) {
Logger.info(this, "***\t Setting up TCP initial hosts: "+cacheTCPInitialHosts);
System.setProperty("jgroups.tcpping.initial_hosts", cacheTCPInitialHosts);
} else if (cacheProtocol.equals("udp")) {
Logger.info(this, "***\t Setting up UDP address and port: "+mCastAddr+":"+mCastPort);
System.setProperty("jgroups.udp.mcast_port", mCastPort);
System.setProperty("jgroups.udp.mcast_addr", mCastAddr);
} else {
Logger.info(this, "Not Setting up any Properties as no protocal was found");
}
Logger.info(this, "***\t Prefer IPv4: "+(preferIPv4.equals("true") ? "enabled" : "disabled"));
System.setProperty("java.net.preferIPv4Stack", preferIPv4);
Logger.info(this, "***\t Setting up JChannel");
if(channel!=null) {
channel.disconnect();
}
channel = new JChannel(classLoader.getResource(cacheFile));
channel.setReceiver(this);
channel.connect(Config.getStringProperty("CACHE_JGROUPS_GROUP_NAME","dotCMSCluster"));
channel.setOpt(JChannel.LOCAL, false);
useJgroups = true;
channel.send(new Message(null, null, TEST_MESSAGE));
Address channelAddress = channel.getAddress();
PhysicalAddress physicalAddr = (PhysicalAddress)channel.downcall(new Event(Event.GET_PHYSICAL_ADDRESS, channelAddress));
String[] addrParts = physicalAddr.toString().split(":");
String usedPort = addrParts[addrParts.length-1];
localServer.setCachePort(Integer.parseInt(usedPort));
serverAPI.updateServer(localServer);
Logger.info(this, "***\t " + channel.toString(true));
Logger.info(this, "***\t Ending JGroups Cluster Setup");
}