Bug 52225

Summary: Adding Alias for existing host on running Tomcat causes ClassCastException
Product: Tomcat 6 Reporter: Konstantin Kolinko <knst.kolinko>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Severity: normal    
Priority: P2    
Version: 6.0.33   
Target Milestone: default   
Hardware: PC   
OS: Windows XP   

Description Konstantin Kolinko 2011-11-22 07:14:09 UTC
There is an error in the fix for Bug 42707
("add host alias using jmx doesn't take affect until restart")

In Mapper#addHostAlias(String name, String alias)
the following line:
   newHost.object = realHost;

should be replaced with the following one:
   newHost.object = realHost.object;

That was r712467 (3 years ago). The issue affects current 5.5.x and 6.0.x as well.

The issue manifests itself only if addAlias() is called on an existing Host in embedded scenario or through JMX. The usual workflow of configuring aliases in server.xml is not affected.

The issue was reported on the dev@ list,
"Multiple Aliases Problem"

Reproduction recipe #1, as reported on dev@:

  I created a Host instance with the localhost1 domain name.

  Host host = new StandardHost();

  Then I added the localhost2 as a alias.


  If I entered localhost1 at a browser then it works fine but if I entered localhost2 at the browser then I got an error:

Nov 21, 2011 1:29:26 PM org.apache.coyote.http11.AbstractHttp11Processor process SEVERE: Error processing request java.lang.ClassCastException:
  cannot be cast to org.apache.catalina.Host
at org.apache.catalina.connector.Request.getHost(Request.java:631)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:105) 

A workaround for the embedded scenario should be to reorder the calls as following:

That will call Mapper.addHost(String name, String[] aliases, Object host) which processes aliases correctly.

Reproduction recipe #2, using JMX:

1. In standard server.xml replace
 <Engine defaultHost="localhost" with <Engine defaultHost="foohost"
 <Host name="localhost" with <Host name="foohost"
2. Start Tomcat
3. Open http://localhost:8080/  It opens correctly
4. Start JConsole and connect to Tomcat
5. In Catalina -> Host -> foohost -> Operations
call addAlias(localhost)
6. Refresh http://localhost:8080/  Expected result: It opens correctly
Actual result:
- Blank page
- Exception in the logs,
22.11.2011 10:08:41 org.apache.coyote.http11.AbstractHttp11Processor process
SEVERE: Error processing request
java.lang.ClassCastException: org.apache.tomcat.util.http.mapper.Mapper$Host can
not be cast to org.apache.catalina.Host
        at org.apache.catalina.connector.Request.getHost(Request.java:623)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
Comment 1 Konstantin Kolinko 2011-11-22 08:59:11 UTC
Fixed in trunk and 7.0 with r1204856 and r1204860 and will be in 7.0.24.
Proposed for 6.0 and 5.5.
Comment 2 Mark Thomas 2011-11-22 19:25:49 UTC
Moving to 6.0.x
Comment 3 Mark Thomas 2011-12-20 20:17:40 UTC
Fixed in 5.5.x and will be in 5.5.35 onwards.
Comment 4 Mark Thomas 2011-12-20 20:20:02 UTC
Fixed in 6.0.x and will be included in 6.0.36 onwards.