From 0ca89e26b6adabd9897247b2f822f9c26e6006bf Mon Sep 17 00:00:00 2001 From: markt Date: Sun, 9 Nov 2008 01:57:34 +0000 Subject: [PATCH] Fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=42707 I debated adding the addContainerListener() meethod to JMX. I'll leave it a little while for people to comment on this (or anything else) before I propose this one for 6.0.x. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@712467 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/connector/MapperListener.java | 46 +++++++++++++++++----- .../org/apache/tomcat/util/http/mapper/Mapper.java | 43 +++++++++++++++++++- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/java/org/apache/catalina/connector/MapperListener.java b/java/org/apache/catalina/connector/MapperListener.java index c1e6285b1..2ed95faf6 100644 --- a/java/org/apache/catalina/connector/MapperListener.java +++ b/java/org/apache/catalina/connector/MapperListener.java @@ -26,6 +26,10 @@ import javax.management.NotificationListener; import javax.management.ObjectInstance; import javax.management.ObjectName; +import org.apache.catalina.ContainerEvent; +import org.apache.catalina.ContainerListener; +import org.apache.catalina.Host; +import org.apache.catalina.ServerFactory; import org.apache.catalina.core.StandardContext; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; @@ -46,8 +50,10 @@ import org.apache.tomcat.util.res.StringManager; * @author Costin Manolache */ public class MapperListener - implements NotificationListener + implements NotificationListener, ContainerListener { + + private static Log log = LogFactory.getLog(MapperListener.class); @@ -254,6 +260,19 @@ public class MapperListener } + // --------------------------------------------- Container Listener methods + + public void containerEvent(ContainerEvent event) { + + if (event.getType() == Host.ADD_ALIAS_EVENT) { + mapper.addHostAlias(((Host) event.getSource()).getName(), + event.getData().toString()); + } else if (event.getType() == Host.REMOVE_ALIAS_EVENT) { + mapper.removeHostAlias(event.getData().toString()); + } + } + + // ------------------------------------------------------ Protected Methods private void registerEngine() @@ -296,7 +315,7 @@ public class MapperListener if (!isRegisteredWithAlias && log.isWarnEnabled()) log.warn(sm.getString("mapperListener.unknownDefaultHost", defaultHost)); } - // This should probablt be called later + // This should probably be called later if( defaultHost != null ) { mapper.setDefaultHostName(defaultHost); } @@ -309,13 +328,16 @@ public class MapperListener throws Exception { String name=objectName.getKeyProperty("host"); if( name != null ) { - String[] aliases = (String[]) - mBeanServer.invoke(objectName, "findAliases", null, null); + + Host host = (Host) ServerFactory.getServer().findService( + domain).getContainer().findChild(name); + + String[] aliases = host.findAliases(); mapper.addHost(name, aliases, objectName); + host.addContainerListener(this); if(log.isDebugEnabled()) log.debug(sm.getString ("mapperListener.registerHost", name, domain)); - } } @@ -326,10 +348,16 @@ public class MapperListener private void unregisterHost(ObjectName objectName) throws Exception { String name=objectName.getKeyProperty("host"); - mapper.removeHost(name); - if(log.isDebugEnabled()) - log.debug(sm.getString - ("mapperListener.unregisterHost", name, domain)); + if( name != null ) { + Host host = (Host) ServerFactory.getServer().findService( + domain).getContainer().findChild(name); + + mapper.removeHost(name); + host.removeContainerListener(this); + if(log.isDebugEnabled()) + log.debug(sm.getString + ("mapperListener.unregisterHost", name, domain)); + } } diff --git a/java/org/apache/tomcat/util/http/mapper/Mapper.java b/java/org/apache/tomcat/util/http/mapper/Mapper.java index 6bd3c7df6..4b34a98ae 100644 --- a/java/org/apache/tomcat/util/http/mapper/Mapper.java +++ b/java/org/apache/tomcat/util/http/mapper/Mapper.java @@ -136,6 +136,47 @@ public final class Mapper { } } + /** + * Add an alias to an existing host. + * @param name The name of the host + * @param alias The alias to add + */ + public synchronized void addHostAlias(String name, String alias) { + int pos = find(hosts, name); + if (pos < 0) { + // Should not be adding an alias for a host that doesn't exist but + // just in case... + return; + } + Host realHost = hosts[pos]; + + Host[] newHosts = new Host[hosts.length + 1]; + Host newHost = new Host(); + newHost.name = alias; + newHost.contextList = realHost.contextList; + newHost.object = realHost; + if (insertMap(hosts, newHosts, newHost)) { + hosts = newHosts; + } + } + + /** + * Remove a host alias + * @param alias The alias to remove + */ + public synchronized void removeHostAlias(String alias) { + // Find and remove the alias + int pos = find(hosts, alias); + if (pos < 0) { + return; + } + Host[] newHosts = new Host[hosts.length - 1]; + if (removeMap(hosts, newHosts, alias)) { + hosts = newHosts; + } + + } + public String[] getHosts() { String hostN[] = new String[hosts.length]; for( int i = 0; i < hosts.length; i++ ) { @@ -1025,7 +1066,7 @@ public final class Mapper { /** - * Find a map elemnt given its name in a sorted array of map elements. + * Find a map element given its name in a sorted array of map elements. * This will return the index for the closest inferior or equal item in the * given array. */ -- 2.11.0