Skip to content

Commit

Permalink
Cleanup configuration objects. Use complex types where appropriate (p…
Browse files Browse the repository at this point in the history
…roxy list, advertise address, excluded contexts, etc.). Avoids unnecessary string translation in AS7.

Cleanup poms.
  • Loading branch information
pferraro committed Mar 27, 2012
1 parent 01322c6 commit 0f1f3e2
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 224 deletions.
3 changes: 1 addition & 2 deletions container-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<parent>
<artifactId>mod_cluster-parent</artifactId>
<groupId>org.jboss.mod_cluster</groupId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-spi</artifactId>
<version>1.2.1.Beta2-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
Expand Down
2 changes: 1 addition & 1 deletion container/catalina-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-container</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-catalina-standalone</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion container/catalina/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-container</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-catalina</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion container/jbossweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-container</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-jbossweb</artifactId>
Expand Down
3 changes: 1 addition & 2 deletions container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<parent>
<artifactId>mod_cluster-parent</artifactId>
<groupId>org.jboss.mod_cluster</groupId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container</artifactId>
<version>1.2.1.Beta2-SNAPSHOT</version>
<packaging>pom</packaging>
<profiles>
<profile>
Expand Down
2 changes: 1 addition & 1 deletion container/tomcat6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-container</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-tomcat6</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion container/tomcat7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-container</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-container-tomcat7</artifactId>
Expand Down
3 changes: 1 addition & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
<parent>
<groupId>org.jboss.mod_cluster</groupId>
<artifactId>mod_cluster-parent</artifactId>
<version> 1.2.1.Beta2-SNAPSHOT</version>
<version>1.2.1.Beta2-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>mod_cluster-core</artifactId>
<version>1.2.1.Beta2-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jboss.mod_cluster</groupId>
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/java/org/jboss/modcluster/ModClusterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -71,7 +70,7 @@

public class ModClusterService implements ModClusterServiceMBean, ContainerEventHandler, LoadBalanceFactorProvider,
MCMPConnectionListener, ContextFilter {
private static final int DEFAULT_PORT = 8000;
public static final int DEFAULT_PORT = 8000;

protected final Logger log = Logger.getLogger(this.getClass());

Expand Down Expand Up @@ -141,14 +140,12 @@ public synchronized void init(Server server) {

this.server = server;

List<InetSocketAddress> initialProxies = Utils.parseSocketAddresses(this.mcmpConfig.getProxyList(), DEFAULT_PORT);

this.mcmpHandler.init(initialProxies, this);
this.mcmpHandler.init(this.mcmpConfig.getProxies(), this);

this.autoEnableContexts = this.mcmpConfig.isAutoEnableContexts();
this.excludedContexts.clear();

Map<String, Set<String>> excludedContextPaths = Utils.parseContexts(this.mcmpConfig.getExcludedContexts());
Map<String, Set<String>> excludedContextPaths = this.mcmpConfig.getExcludedContextsPerHost();

if (!excludedContextPaths.isEmpty()) {
for (Engine engine : server.getEngines()) {
Expand All @@ -168,7 +165,7 @@ public synchronized void init(Server server) {

Boolean advertise = this.mcmpConfig.getAdvertise();

if (Boolean.TRUE.equals(advertise) || (advertise == null && initialProxies.isEmpty())) {
if (Boolean.TRUE.equals(advertise) || (advertise == null && this.mcmpConfig.getProxies().isEmpty())) {
try {
this.advertiseListener = this.listenerFactory.createListener(this.mcmpHandler, this.mcmpConfig);

Expand Down
85 changes: 2 additions & 83 deletions core/src/main/java/org/jboss/modcluster/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,8 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.logging.Logger;

public class Utils {
private static final String ROOT_CONTEXT = "ROOT";
private static final String CONTEXT_DELIMITER = ",";
private static final String HOST_CONTEXT_DELIMITER = ":";
private static final String DEFAULT_HOST = "localhost";

private static final Logger log = Logger.getLogger(Utils.class);

/**
* 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 exception, or throwing it if it is an Error
Expand Down Expand Up @@ -71,8 +55,7 @@ public static InetSocketAddress parseSocketAddress(String addressPort, int defau
// Handle ipv4 address
address = addressPort.substring(0, lastColon);
port = Integer.parseInt(addressPort.substring(lastColon + 1));
} else // handle ipv6 address
{
} else { // handle ipv6 address
// Detect url-style: [ipv6-address]:port
int openBracket = addressPort.indexOf("[");
int closeBracket = addressPort.indexOf("]");
Expand All @@ -87,71 +70,7 @@ public static InetSocketAddress parseSocketAddress(String addressPort, int defau
}
}

return new InetSocketAddress((address != null) && (address.length() > 0) ? InetAddress.getByName(address)
: InetAddress.getLocalHost(), port);
}

public static List<InetSocketAddress> parseSocketAddresses(String addresses, int defaultPort) {
if ((addresses == null) || (addresses.length() == 0))
return Collections.emptyList();

String[] tokens = addresses.split(",");

List<InetSocketAddress> proxies = new ArrayList<InetSocketAddress>(tokens.length);

for (String token : tokens) {
try {
InetSocketAddress addressPort = parseSocketAddress(token.trim(), defaultPort);

proxies.add(addressPort);
} catch (UnknownHostException e) {
log.error(Strings.ERROR_HOST_INVALID.getString(token), e);
}
}

return proxies;
}

public static Map<String, Set<String>> parseContexts(String contexts) {
if (contexts == null)
return Collections.emptyMap();

String trimmedContexts = contexts.trim();

if (trimmedContexts.length() == 0)
return Collections.emptyMap();

Map<String, Set<String>> map = new HashMap<String, Set<String>>();

for (String context : trimmedContexts.split(CONTEXT_DELIMITER)) {
String[] parts = context.trim().split(HOST_CONTEXT_DELIMITER);

if (parts.length > 2) {
throw new IllegalArgumentException(trimmedContexts + " is not a valid value for excludedContexts");
}

String host = DEFAULT_HOST;
String trimmedContext = parts[0].trim();

if (parts.length == 2) {
host = trimmedContext;
trimmedContext = parts[1].trim();
}

String path = trimmedContext.equals(ROOT_CONTEXT) ? "" : "/" + trimmedContext;

Set<String> paths = map.get(host);

if (paths == null) {
paths = new HashSet<String>();

map.put(host, paths);
}

paths.add(path);
}

return map;
return new InetSocketAddress((address != null) && (address.length() > 0) ? InetAddress.getByName(address) : InetAddress.getLocalHost(), port);
}

private Utils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.security.MessageDigest;
import java.security.SecureRandom;
Expand All @@ -33,10 +34,9 @@
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ConcurrentHashMap;

import net.jcip.annotations.GuardedBy;

Expand All @@ -54,28 +54,19 @@
* @author Mladen Turk
*/
public class AdvertiseListenerImpl implements AdvertiseListener {
/** Default port for listening Advertise messages. */
public static final int DEFAULT_PORT = 23364;
/** Default Multicast group address for listening Advertise messages. */
public static final String DEFAULT_GROUP = "224.0.1.105";
public static final String DEFAULT_ENCODING = "8859_1";
public static final String RFC_822_FMT = "EEE, d MMM yyyy HH:mm:ss Z";

static final Logger log = Logger.getLogger(AdvertiseListenerImpl.class);

volatile boolean listening = false;

final int advertisePort;
final InetAddress groupAddress;
private final InetAddress socketInterface;

private final ThreadFactory threadFactory;
private final AdvertiseConfiguration config;
private final MulticastSocketFactory socketFactory;

private final String securityKey;
MessageDigest md = null;
final MessageDigest md;

final Map<String, AdvertisedServer> servers = new HashMap<String, AdvertisedServer>();
final Map<String, AdvertisedServer> servers = new ConcurrentHashMap<String, AdvertisedServer>();
final MCMPHandler handler;

@GuardedBy("this")
Expand Down Expand Up @@ -106,29 +97,19 @@ private static void digestString(MessageDigest md, String s) {
* @param config our configuration
* @param socketFactory a multicast socket factory
*/
public AdvertiseListenerImpl(MCMPHandler commHandler, AdvertiseConfiguration config, MulticastSocketFactory socketFactory)
throws IOException {
public AdvertiseListenerImpl(MCMPHandler commHandler, AdvertiseConfiguration config, MulticastSocketFactory socketFactory) throws IOException {
this.handler = commHandler;
this.socketFactory = socketFactory;
this.threadFactory = config.getAdvertiseThreadFactory();

String groupAddress = config.getAdvertiseGroupAddress();
this.groupAddress = InetAddress.getByName((groupAddress != null) ? groupAddress : DEFAULT_GROUP);

int port = config.getAdvertisePort();
this.advertisePort = (port > 0) ? port : DEFAULT_PORT;
this.config = config;
this.md = (config.getAdvertiseSecurityKey() != null) ? this.getMessageDigest() : null;
}

this.securityKey = config.getAdvertiseSecurityKey();
if (this.securityKey != null && this.md == null) {
try {
this.md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ex) {
throw new IOException("can't create MD: NoSuchAlgorithmException");
}
private MessageDigest getMessageDigest() throws IOException {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ex) {
throw new IOException("can't create MD: NoSuchAlgorithmException");
}

String advertiseInterface = config.getAdvertiseInterface();
this.socketInterface = (advertiseInterface != null) ? InetAddress.getByName(advertiseInterface) : null;
}

/**
Expand Down Expand Up @@ -159,14 +140,16 @@ public void removeServer(AdvertisedServer server) {

private synchronized void init() throws IOException {
if (this.socket == null) {
MulticastSocket socket = this.socketFactory.createMulticastSocket(this.groupAddress, this.advertisePort);
InetSocketAddress socketAddress = this.config.getAdvertiseSocketAddress();
MulticastSocket socket = this.socketFactory.createMulticastSocket(socketAddress.getAddress(), socketAddress.getPort());

// Limit socket send to localhost
socket.setTimeToLive(0);
if (this.socketInterface != null) {
socket.setInterface(this.socketInterface);
InetAddress socketInterface = this.config.getAdvertiseInterface();
if (socketInterface != null) {
socket.setInterface(socketInterface);
}
socket.joinGroup(this.groupAddress);
socket.joinGroup(socketAddress.getAddress());

this.socket = socket;
}
Expand All @@ -182,12 +165,12 @@ public synchronized void start() throws IOException {

if (this.worker == null) {
this.worker = new AdvertiseListenerWorker(this.socket);
this.thread = this.threadFactory.newThread(this.worker);
this.thread = this.config.getAdvertiseThreadFactory().newThread(this.worker);
this.thread.start();

this.listening = true;

log.info(Strings.ADVERTISE_START.getString(this.groupAddress.getHostAddress(), String.valueOf(this.advertisePort)));
log.info(Strings.ADVERTISE_START.getString(this.config.getAdvertiseSocketAddress().getAddress().getHostAddress(), this.config.getAdvertiseSocketAddress().getPort()));
}
}

Expand Down Expand Up @@ -218,7 +201,8 @@ public synchronized void interruptDatagramReader() {
if (this.socket == null)
return;

DatagramPacket packet = this.worker.createInterruptPacket(this.groupAddress, this.advertisePort);
InetSocketAddress socketAddress = this.config.getAdvertiseSocketAddress();
DatagramPacket packet = this.worker.createInterruptPacket(socketAddress.getAddress(), socketAddress.getPort());

try {
this.socket.send(packet);
Expand Down Expand Up @@ -260,7 +244,7 @@ public synchronized void destroy() {

if (this.socket != null) {
try {
this.socket.leaveGroup(this.groupAddress);
this.socket.leaveGroup(this.config.getAdvertiseSocketAddress().getAddress());
} catch (IOException e) {
log.warn(e.getMessage(), e);
}
Expand All @@ -275,11 +259,11 @@ public synchronized void destroy() {
boolean verifyDigest(String digest, String server, String date, String sequence) {
if (this.md == null)
return true;
if (this.securityKey == null)
return true; // Not set: No used
String securityKey = this.config.getAdvertiseSecurityKey();
if (securityKey == null) return true; // Not set: No used

this.md.reset();
digestString(this.md, this.securityKey);
digestString(this.md, securityKey);
byte[] ssalt = this.md.digest();
this.md.update(ssalt);
digestString(this.md, date);
Expand Down
Loading

0 comments on commit 0f1f3e2

Please sign in to comment.