Author: pferraro
Date: 2008-09-05 10:33:02 -0400 (Fri, 05 Sep 2008)
New Revision: 1777
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/mcmp/MCMPUtils.java
Log:
Code cleanup.
Fixed misnamed StickySessionRemove parameter in createConfigRequest(...).
Modified: trunk/mod_cluster/src/main/java/org/jboss/modcluster/mcmp/MCMPUtils.java
===================================================================
--- trunk/mod_cluster/src/main/java/org/jboss/modcluster/mcmp/MCMPUtils.java 2008-09-05
12:20:24 UTC (rev 1776)
+++ trunk/mod_cluster/src/main/java/org/jboss/modcluster/mcmp/MCMPUtils.java 2008-09-05
14:33:02 UTC (rev 1777)
@@ -23,7 +23,9 @@
package org.jboss.modcluster.mcmp;
import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -36,12 +38,13 @@
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.util.StringManager;
+import org.apache.coyote.ProtocolHandler;
import org.apache.tomcat.util.IntrospectionUtils;
import org.jboss.logging.Logger;
+import org.jboss.modcluster.Constants;
+import org.jboss.modcluster.Utils;
import org.jboss.modcluster.config.BalancerConfiguration;
import org.jboss.modcluster.config.NodeConfiguration;
-import org.jboss.modcluster.Constants;
-import org.jboss.modcluster.Utils;
/**
* Utility methods related to the Mod-Cluster Management Protocol.
@@ -52,111 +55,141 @@
public class MCMPUtils
{
public static final int DEFAULT_PORT = 8000;
-
+
private static final MCMPRequest INFO = new MCMPRequest(MCMPRequestType.INFO, false,
new HashMap<String, String>());
-
+
private static final Logger log = Logger.getLogger(MCMPUtils.class);
/**
* The string manager for this package.
*/
private static final StringManager sm = StringManager.getManager(Constants.Package);
-
-
+
public static MCMPRequest createConfigRequest(Engine engine, NodeConfiguration
nodeConfig, BalancerConfiguration balancerConfig)
{
- Connector connector =
Utils.findProxyConnector(engine.getService().findConnectors());
- Map<String, String> parameters = new HashMap<String, String>();
- parameters.put("JVMRoute", engine.getJvmRoute());
- boolean reverseConnection =
-
Boolean.TRUE.equals(IntrospectionUtils.getProperty(connector.getProtocolHandler(),
"reverseConnection"));
- boolean ssl =
-
Boolean.TRUE.equals(IntrospectionUtils.getProperty(connector.getProtocolHandler(),
"SSLEnabled"));
- boolean ajp = ((String)
IntrospectionUtils.getProperty(connector.getProtocolHandler(),
"name")).startsWith("ajp-");
+ Connector connector =
Utils.findProxyConnector(engine.getService().findConnectors());
+ Map<String, String> parameters = new HashMap<String, String>();
+ parameters.put("JVMRoute", engine.getJvmRoute());
- if (reverseConnection) {
- parameters.put("Reversed", "true");
- }
- parameters.put("Host", Utils.getAddress(connector));
- parameters.put("Port", "" + connector.getPort());
- if (ajp) {
- parameters.put("Type", "ajp");
- } else if (ssl) {
- parameters.put("Type", "https");
- } else {
- parameters.put("Type", "http");
- }
-
- // Other configuration parameters
- if (nodeConfig.getDomain() != null) {
- parameters.put("Domain", nodeConfig.getDomain());
- }
- if (nodeConfig.getFlushPackets()) {
- parameters.put("flushpackets", "On");
- }
- if (nodeConfig.getFlushWait() != -1) {
- parameters.put("flushwait", "" +
nodeConfig.getFlushWait());
- }
- if (nodeConfig.getPing() != -1) {
- parameters.put("ping", "" + nodeConfig.getPing());
- }
- if (nodeConfig.getSmax() != -1) {
- parameters.put("smax", "" + nodeConfig.getSmax());
- }
- if (nodeConfig.getTtl() != -1) {
- parameters.put("ttl", "" + nodeConfig.getTtl());
- }
- if (nodeConfig.getNodeTimeout() != -1) {
- parameters.put("Timeout", "" +
nodeConfig.getNodeTimeout());
- }
- if (nodeConfig.getBalancer() != null) {
- parameters.put("Balancer", nodeConfig.getBalancer());
- }
- if (!balancerConfig.getStickySession()) {
- parameters.put("StickySession", "No");
- }
- if
(!org.apache.catalina.Globals.SESSION_COOKIE_NAME.equals("JSESSIONID")) {
- parameters.put("StickySessionCookie",
org.apache.catalina.Globals.SESSION_COOKIE_NAME);
- }
- if
(!org.apache.catalina.Globals.SESSION_PARAMETER_NAME.equals("jsessionid")) {
- parameters.put("StickySessionPath",
org.apache.catalina.Globals.SESSION_PARAMETER_NAME);
- }
- if (balancerConfig.getStickySessionRemove()) {
- parameters.put("StickSessionRemove", "Yes");
- }
- if (!balancerConfig.getStickySessionForce()) {
- parameters.put("StickySessionForce", "No");
- }
- if (balancerConfig.getWorkerTimeout() != -1) {
- parameters.put("WaitWorker", "" +
balancerConfig.getWorkerTimeout());
- }
- if (balancerConfig.getMaxAttempts() != -1) {
- parameters.put("Maxattempts", "" +
balancerConfig.getMaxAttempts());
- }
-
- return new MCMPRequest(MCMPRequestType.CONFIG, false, parameters);
+ ProtocolHandler handler = connector.getProtocolHandler();
+
+ boolean reverseConnection =
Boolean.TRUE.equals(IntrospectionUtils.getProperty(handler,
"reverseConnection"));
+ boolean ssl = Boolean.TRUE.equals(IntrospectionUtils.getProperty(handler,
"SSLEnabled"));
+ boolean ajp = ((String) IntrospectionUtils.getProperty(handler,
"name")).startsWith("ajp-");
+
+ if (reverseConnection)
+ {
+ parameters.put("Reversed", "true");
+ }
+ parameters.put("Host", Utils.getAddress(connector));
+ parameters.put("Port", "" + connector.getPort());
+ if (ajp)
+ {
+ parameters.put("Type", "ajp");
+ }
+ else if (ssl)
+ {
+ parameters.put("Type", "https");
+ }
+ else
+ {
+ parameters.put("Type", "http");
+ }
+
+ // Other configuration parameters
+ String domain = nodeConfig.getDomain();
+ if (domain != null)
+ {
+ parameters.put("Domain", domain);
+ }
+ if (nodeConfig.getFlushPackets())
+ {
+ parameters.put("flushpackets", "On");
+ }
+ int flushWait = nodeConfig.getFlushWait();
+ if (flushWait != -1)
+ {
+ parameters.put("flushwait", String.valueOf(flushWait));
+ }
+ int ping = nodeConfig.getPing();
+ if (ping != -1)
+ {
+ parameters.put("ping", String.valueOf(ping));
+ }
+ int smax = nodeConfig.getSmax();
+ if (smax != -1)
+ {
+ parameters.put("smax", String.valueOf(smax));
+ }
+ int ttl = nodeConfig.getTtl();
+ if (ttl != -1)
+ {
+ parameters.put("ttl", String.valueOf(ttl));
+ }
+ int nodeTimeout = nodeConfig.getNodeTimeout();
+ if (nodeTimeout != -1)
+ {
+ parameters.put("Timeout", String.valueOf(nodeTimeout));
+ }
+ String balancer = nodeConfig.getBalancer();
+ if (balancer != null)
+ {
+ parameters.put("Balancer", balancer);
+ }
+ if (!balancerConfig.getStickySession())
+ {
+ parameters.put("StickySession", "No");
+ }
+ if
(!org.apache.catalina.Globals.SESSION_COOKIE_NAME.equals("JSESSIONID"))
+ {
+ parameters.put("StickySessionCookie",
org.apache.catalina.Globals.SESSION_COOKIE_NAME);
+ }
+ if
(!org.apache.catalina.Globals.SESSION_PARAMETER_NAME.equals("jsessionid"))
+ {
+ parameters.put("StickySessionPath",
org.apache.catalina.Globals.SESSION_PARAMETER_NAME);
+ }
+ if (balancerConfig.getStickySessionRemove())
+ {
+ parameters.put("StickySessionRemove", "Yes");
+ }
+ if (!balancerConfig.getStickySessionForce())
+ {
+ parameters.put("StickySessionForce", "No");
+ }
+ int workerTimeout = balancerConfig.getWorkerTimeout();
+ if (workerTimeout != -1)
+ {
+ parameters.put("WaitWorker", "" + workerTimeout);
+ }
+ int maxAttempts = balancerConfig.getMaxAttempts();
+ if (maxAttempts != -1)
+ {
+ parameters.put("Maxattempts", "" + maxAttempts);
+ }
+
+ return new MCMPRequest(MCMPRequestType.CONFIG, false, parameters);
}
-
+
public static MCMPRequest createEnableAppRequest(Context context)
{
return new MCMPRequest(MCMPRequestType.ENABLE_APP, false,
createContextParameters(context));
}
-
+
public static MCMPRequest createDisableAppRequest(Context context)
{
return new MCMPRequest(MCMPRequestType.DISABLE_APP, false,
createContextParameters(context));
}
-
+
public static MCMPRequest createStopAppRequest(Context context)
{
return new MCMPRequest(MCMPRequestType.STOP_APP, false,
createContextParameters(context));
}
-
+
public static MCMPRequest createRemoveAppRequest(Context context)
{
return new MCMPRequest(MCMPRequestType.REMOVE_APP, false,
createContextParameters(context));
}
-
+
public static MCMPRequest createStatusRequest(Engine engine, int lbf)
{
Map<String, String> parameters = new HashMap<String, String>();
@@ -164,7 +197,7 @@
parameters.put("Load", String.valueOf(lbf));
return new MCMPRequest(MCMPRequestType.STATUS, false, parameters);
}
-
+
public static MCMPRequest createStatusRequest(String jvmRoute, int lbf)
{
Map<String, String> parameters = new HashMap<String, String>();
@@ -172,138 +205,134 @@
parameters.put("Load", String.valueOf(lbf));
return new MCMPRequest(MCMPRequestType.STATUS, false, parameters);
}
-
+
public static MCMPRequest createEnableEngineRequest(Engine engine)
{
return new MCMPRequest(MCMPRequestType.ENABLE_APP, true,
createEngineParameters(engine));
}
-
+
public static MCMPRequest createDisableEngineRequest(Engine engine)
{
return new MCMPRequest(MCMPRequestType.DISABLE_APP, true,
createEngineParameters(engine));
}
-
+
public static MCMPRequest createRemoveAllRequest(Engine engine)
{
return new MCMPRequest(MCMPRequestType.REMOVE_APP, true,
createEngineParameters(engine));
}
-
+
/**
* Reset configuration for a particular proxy following an error.
*/
- public static List<MCMPRequest> getResetRequests(Server server,
- NodeConfiguration nodeConfig, BalancerConfiguration balancerConfig) {
-
- List<MCMPRequest> requests = new ArrayList<MCMPRequest>();
- Service[] services = server.findServices();
- for (int i = 0; i < services.length; i++) {
- Engine engine = (Engine) services[i].getContainer();
- if (engine.getJvmRoute() != null)
- {
- requests.add(MCMPUtils.createRemoveAllRequest(engine));
- }
- requests.add(MCMPUtils.createConfigRequest(engine, nodeConfig,
balancerConfig));
- Container[] children = engine.findChildren();
- for (int j = 0; j < children.length; j++) {
- Container[] children2 = children[j].findChildren();
- for (int k = 0; k < children2.length; k++) {
- Context ctx = (Context) children2[k];
- if (ctx.isStarted())
- {
- requests.add(MCMPUtils.createEnableAppRequest(ctx));
- }
+ public static List<MCMPRequest> getResetRequests(Server server,
NodeConfiguration nodeConfig, BalancerConfiguration balancerConfig)
+ {
+ List<MCMPRequest> requests = new ArrayList<MCMPRequest>();
+ Service[] services = server.findServices();
+ for (Service service: services)
+ {
+ Engine engine = (Engine) service.getContainer();
+ if (engine.getJvmRoute() != null)
+ {
+ requests.add(MCMPUtils.createRemoveAllRequest(engine));
+ }
+ requests.add(MCMPUtils.createConfigRequest(engine, nodeConfig,
balancerConfig));
+ Container[] children = engine.findChildren();
+ for (Container element: children)
+ {
+ Container[] children2 = element.findChildren();
+ for (Container element2: children2)
+ {
+ Context ctx = (Context) element2;
+ if (ctx.isStarted())
+ {
+ requests.add(MCMPUtils.createEnableAppRequest(ctx));
}
- }
- }
-
- return requests;
+ }
+ }
+ }
+
+ return requests;
}
-
+
public static MCMPRequest getInfoRequest()
{
return INFO;
}
-
+
private static Map<String, String> createEngineParameters(Engine engine)
{
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("JVMRoute", engine.getJvmRoute());
return parameters;
}
-
+
private static Map<String, String> createContextParameters(Context context)
{
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("JVMRoute", Utils.getJvmRoute(context));
- parameters.put("Context", ("".equals(context.getPath())) ?
"/" : context.getPath());
+
+ String path = context.getPath();
+
+ parameters.put("Context", "".equals(path) ? "/" :
path);
parameters.put("Alias", Utils.getHost(context));
+
return parameters;
}
-
+
public static AddressPort parseAddressPort(String addressPort)
{
- int pos = addressPort.indexOf(':');
- String host = null;
- int port = 0;
- if (pos < 0) {
- host = addressPort;
- } else if (pos == 0) {
- port = Integer.parseInt(addressPort.substring(1));
- } else {
- host = addressPort.substring(0, pos);
- port = Integer.parseInt(addressPort.substring(pos + 1));
- }
-
- InetAddress address = null;
- try {
- address = InetAddress.getByName(host);
- } catch (Exception e) {
+ try
+ {
+ return parseAddressPort(addressPort, 0);
+ }
+ catch (UnknownHostException e)
+ {
throw new IllegalArgumentException(e);
- }
-
- return new AddressPort(address, port);
+ }
}
- public static List<AddressPort> parseProxies(String proxyList)
+ public static List<AddressPort> parseProxies(String proxyList)
{
- List<AddressPort> proxies = new ArrayList<AddressPort>();
- if (proxyList != null)
- {
- StringTokenizer tok = new StringTokenizer(proxyList, ",");
- while (tok.hasMoreTokens()) {
- String token = tok.nextToken().trim();
- int pos = token.indexOf(':');
- String address = null;
- int port = DEFAULT_PORT;
- if (pos < 0) {
- address = token;
- } else if (pos == 0) {
- port = Integer.parseInt(token.substring(1));
- } else {
- address = token.substring(0, pos);
- port = Integer.parseInt(token.substring(pos + 1));
- }
- InetAddress inetAddress = null;
- try {
- if (address != null) {
- inetAddress = InetAddress.getByName(address);
- }
- } catch (Exception e) {
- log.error(sm.getString("modcluster.error.invalidHost", address),
e);
- continue;
- }
- proxies.add(new AddressPort(inetAddress, port));
- }
+ if (proxyList == null) return Collections.emptyList();
+
+ String[] tokens = proxyList.split(",");
+
+ List<AddressPort> proxies = new ArrayList<AddressPort>(tokens.length);
+
+ for (String token: tokens)
+ {
+ try
+ {
+ AddressPort addressPort = parseAddressPort(token.trim(), DEFAULT_PORT);
+
+ proxies.add(addressPort);
+ }
+ catch (UnknownHostException e)
+ {
+ log.error(sm.getString("modcluster.error.invalidHost", token), e);
+ }
}
-
+
return proxies;
}
-
+
+ private static AddressPort parseAddressPort(String addressPort, int defaultPort)
throws UnknownHostException
+ {
+ int pos = addressPort.indexOf(':');
+ boolean colonExists = (pos >= 0);
+
+ String address = colonExists ? addressPort.substring(0, pos) : addressPort;
+ int port = colonExists ? Integer.parseInt(addressPort.substring(pos + 1)) :
defaultPort;
+
+ InetAddress inetAddress = (address != null) && (address.length() > 0) ?
InetAddress.getByName(address) : null;
+
+ return new AddressPort(inetAddress, port);
+ }
+
/**
* Disable external instantiation.
*/
private MCMPUtils()
{
}
-
}