Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Tomcat and the fly likes cross context access to objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "cross context access to objects" Watch "cross context access to objects" New topic
Author

cross context access to objects

Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
I am currently working on a web application where I want to be able to access an object in another context. I am able to get the object and use its toString method, but I get a ClassCastException when I try to actually cast it. I'm thinking it's a classloader issue. Does anyone know a workaround for this? I'm using Tomcat 5.0.25 and Windows XP. Here is a small piece of my code in a ServletContextListener.


[ September 17, 2004: Message edited by: Angel Dobbs-Sciortino ]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312

Object o = otherContext.getAttribute("dataObject");


You makesure , set value of "dataObject" in context object is have type DataObject or not ?


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Yes, it is a DataObject. The log from o.toString() confirms this.
gayathri sridhar
Greenhorn

Joined: Sep 15, 2004
Posts: 13
Originally posted by Angel Dobbs-Sciortino:
I'm thinking it's a classloader issue. Does anyone know a workaround for this?

uhhm could be..
Where is the DataObject class located? Both the contexts have their copies of DataObject? Should you try putting the DataObject class in the tomcat shared/lib folder? or may be tomcat/common/classes folder? instead of letting the contexts have their own copies...this is just a guess.
Al Korov
Greenhorn

Joined: Sep 11, 2004
Posts: 21
What's the result of System.out.println(o instanceof DataObject);?
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
It logs what I have in the toString method in the DataObject from the other context.

"This is a simple string from the a DataObject in the other context."
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Where is the DataObject class located? Both the contexts have their copies of DataObject? Should you try putting the DataObject class in the tomcat shared/lib folder? or may be tomcat/common/classes folder? instead of letting the contexts have their own copies...this is just a guess.


They each have their own copy right now. I will try putting it in a jar in the shared folder.
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
I tried moving my DataObject into a jar file. First I put the jar in the WEB-INF/lib directory. When I do this, I am able to access the DataObject. But when I moved the jar file into shared/lib for Tomcat, I get a NoClassDefFoundError. Does anyone know why it would be doing this? According to the Tomcat docs, I should be able to access objects in the shared/lib directory.
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Just an update. I didn't exactly solve my problem with sharing my object cross-context. I eventually decided there were too many dependencies to make it worth it to get it working. It was all a lot more complicated than my post showed, since the object I wanted to share was connected to all sorts of other objects.

However, I was able to share a small HelloWorld class that only had one method that I got working, and perhaps it would help you. Each application can *not* have the object in its own classpath.

What I did was take my HelloWorld class and package it into a jar. You must put this jar into tomcat/shared/lib or tomcat/common/lib. After doing that, I put the jar into my lib directory of my applications so that they would compile. When I deployed the applications, I did not include the jar file in the build. This is important, because the classloader will check each individual applications classpath before checking shared or common (tomcat documentation gives the order the paths are checked).

It is necessary to do all this because each application has its own classloader.
 
jQuery in Action, 2nd edition
 
subject: cross context access to objects
 
Similar Threads
FBN: Closing the database in Network mode.
Hibernate problem - not sure why not working....
headfirst ejb AdviceClient problem
Using for..each loop on Object[]
Need help on design of Swing application