Bug 52225 - Adding Alias for existing host on running Tomcat causes ClassCastException
Summary: Adding Alias for existing host on running Tomcat causes ClassCastException
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 6.0.33
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-22 07:14 UTC by Konstantin Kolinko
Modified: 2011-12-20 20:20 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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,
http://tomcat.markmail.org/thread/sskxor4a3xtrjmk3
"Multiple Aliases Problem"

Reproduction recipe #1, as reported on dev@:

<quote>
  I created a Host instance with the localhost1 domain name.

  Host host = new StandardHost();
  host.setAppBase(CATALINA_HOSTS_HOME);
  host.setName("localhost1");
  host.setDeployOnStartup(false);
  host.setBackgroundProcessorDelay(5);
  host.setAutoDeploy(false);
  host.setRealm(engine.getRealm());
  engine.addChild(host);

  Then I added the localhost2 as a alias.

  host.addAlias("localhost2");

  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:
  org.apache.tomcat.util.http.mapper.Mapper$Host
  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) 
</quote>

A workaround for the embedded scenario should be to reorder the calls as following:
  host.addAlias("localhost2");
  engine.addChild(host);

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
ve.java:77)
]]]
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.