• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cross context access to objects

 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


You makesure , set value of "dataObject" in context object is have type DataObject or not ?
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is a DataObject. The log from o.toString() confirms this.
 
gayathri sridhar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the result of System.out.println(o instanceof DataObject);?
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic