Author: pferraro
Date: 2008-09-05 10:42:32 -0400 (Fri, 05 Sep 2008)
New Revision: 1778
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/Utils.java
Log:
Code cleanup.
Modified: trunk/mod_cluster/src/main/java/org/jboss/modcluster/Utils.java
===================================================================
--- trunk/mod_cluster/src/main/java/org/jboss/modcluster/Utils.java 2008-09-05 14:33:02
UTC (rev 1777)
+++ trunk/mod_cluster/src/main/java/org/jboss/modcluster/Utils.java 2008-09-05 14:42:32
UTC (rev 1778)
@@ -8,6 +8,7 @@
import org.apache.catalina.Host;
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.mcmp.MCMPHandler;
@@ -15,8 +16,9 @@
public class Utils
{
private static final Logger log = Logger.getLogger(Utils.class);
+
private static final StringManager sm = StringManager.getManager(Constants.Package);
-
+
/**
* Find the most likely connector the proxy server should connect to, or
* accept connections from.
@@ -26,24 +28,34 @@
*/
public static Connector findProxyConnector(Connector[] connectors)
{
- int pos = 0;
- int maxThreads = 0;
- for (int i = 0; i < connectors.length; i++) {
- if (connectors[i].getProtocol().startsWith("AJP")) {
- // Return any AJP connector found
- return connectors[i];
- }
- if
(Boolean.TRUE.equals(IntrospectionUtils.getProperty(connectors[i].getProtocolHandler(),
"reverseConnection"))) {
- return connectors[i];
- }
- Integer mt = (Integer)
IntrospectionUtils.getProperty(connectors[i].getProtocolHandler(),
"maxThreads");
- if (mt.intValue() > maxThreads) {
- maxThreads = mt.intValue();
- pos = i;
- }
- }
- // If no AJP connector and no reverse, return the connector with the most threads
- return connectors[pos];
+ int highestMaxThreads = 0;
+ Connector bestConnector = connectors[0];
+
+ for (Connector connector: connectors)
+ {
+ if (connector.getProtocol().startsWith("AJP")) // Return any AJP
connector found
+ {
+ return connector;
+ }
+
+ ProtocolHandler handler = connector.getProtocolHandler();
+
+ if (Boolean.TRUE.equals(IntrospectionUtils.getProperty(handler,
"reverseConnection")))
+ {
+ return connector;
+ }
+
+ int maxThreads = ((Integer) IntrospectionUtils.getProperty(handler,
"maxThreads")).intValue();
+
+ if (maxThreads > highestMaxThreads)
+ {
+ highestMaxThreads = maxThreads;
+ bestConnector = connector;
+ }
+ }
+
+ // If no AJP connector and no reverse, return the connector with the most threads
+ return bestConnector;
}
/**
@@ -54,14 +66,9 @@
*/
public static String getAddress(Connector connector)
{
- InetAddress inetAddress =
- (InetAddress) IntrospectionUtils.getProperty(connector.getProtocolHandler(),
"address");
- if (inetAddress == null) {
- // Should not happen
- return "127.0.0.1";
- } else {
- return inetAddress.getHostAddress();
- }
+ InetAddress inetAddress = (InetAddress)
IntrospectionUtils.getProperty(connector.getProtocolHandler(), "address");
+
+ return (inetAddress != null) ? inetAddress.getHostAddress() :
"172.0.0.1";
}
/**
@@ -72,7 +79,7 @@
*/
public static String getJvmRoute(Context context)
{
- return ((Engine) context.getParent().getParent()).getJvmRoute();
+ return ((Engine) context.getParent().getParent()).getJvmRoute();
}
/**
@@ -83,49 +90,43 @@
*/
public static String getHost(Context context)
{
- StringBuffer result = new StringBuffer();
- Host host = (Host) context.getParent();
- result.append(host.getName());
- String[] aliases = host.findAliases();
- for (int i = 0; i < aliases.length; i++) {
- result.append(',');
- result.append(aliases[i]);
- }
- return result.toString();
+ StringBuilder result = new StringBuilder();
+ Host host = (Host) context.getParent();
+ result.append(host.getName());
+ String[] aliases = host.findAliases();
+ for (String alias: aliases)
+ {
+ result.append(',');
+ result.append(alias);
+ }
+ return result.toString();
}
public static void establishJvmRouteAndConnectorAddress(Engine engine, MCMPHandler
mcmpHandler) throws IOException
{
Connector connector = findProxyConnector(engine.getService().findConnectors());
- InetAddress localAddress =
- (InetAddress) IntrospectionUtils.getProperty(connector.getProtocolHandler(),
"address");
- if ((engine.getJvmRoute() == null || localAddress == null) &&
mcmpHandler.getProxyStates().size() > 0) {
- // Automagical JVM route (address + port + engineName)
- if (localAddress == null) {
- localAddress = mcmpHandler.getLocalAddress();
- if (localAddress != null) {
- IntrospectionUtils.setProperty(connector.getProtocolHandler(),
"address", localAddress.getHostAddress());
- } else {
- // Should not happen
- IntrospectionUtils.setProperty(connector.getProtocolHandler(),
"address", "127.0.0.1");
- }
- log.info(sm.getString("modcluster.util.address",
localAddress.getHostAddress()));
- }
- if (engine.getJvmRoute() == null) {
- String hostName = null;
- if (localAddress != null) {
- hostName = localAddress.getHostName();
- } else {
- // Fallback
- hostName = "127.0.0.1";
- }
- String jvmRoute = hostName + ":" + connector.getPort() +
":" + engine.getName();
- engine.setJvmRoute(jvmRoute);
- log.info(sm.getString("modcluster.util.jvmRoute",
engine.getName(), jvmRoute));
- }
+ InetAddress localAddress = (InetAddress) IntrospectionUtils
+ .getProperty(connector.getProtocolHandler(), "address");
+ if ((engine.getJvmRoute() == null || localAddress == null) &&
!mcmpHandler.getProxyStates().isEmpty())
+ {
+ // Automagical JVM route (address + port + engineName)
+ if (localAddress == null)
+ {
+ localAddress = mcmpHandler.getLocalAddress();
+ String hostAddress = (localAddress != null) ? localAddress.getHostAddress() :
"127.0.0.1";
+ IntrospectionUtils.setProperty(connector.getProtocolHandler(),
"address", hostAddress);
+ log.info(sm.getString("modcluster.util.address",
localAddress.getHostAddress()));
+ }
+ if (engine.getJvmRoute() == null)
+ {
+ String hostName = (localAddress != null) ? localAddress.getHostName() :
"127.0.0.1";
+ String jvmRoute = hostName + ":" + connector.getPort() +
":" + engine.getName();
+ engine.setJvmRoute(jvmRoute);
+ log.info(sm.getString("modcluster.util.jvmRoute", engine.getName(),
jvmRoute));
+ }
}
}
-
+
/**
* Analyzes the type of the given Throwable, handing it back if it is a
* RuntimeException, wrapping it in a RuntimeException if it is a checked
@@ -138,15 +139,20 @@
public static RuntimeException convertToUnchecked(Throwable t) throws Error
{
if (t instanceof Error)
+ {
throw (Error) t;
+ }
else if (t instanceof RuntimeException)
+ {
return (RuntimeException) t;
+ }
else
+ {
return new RuntimeException(t.getMessage(), t);
+ }
}
-
+
private Utils()
{
}
-
}
\ No newline at end of file
Show replies by date