RE: ClassCastException while sharing objects accross applications
The type in Java is a combination of a fully qualified class name and its defining classloader. So the two objects with the same class name are considered different types in your two web applications because each web application is loaded by the dedicated webapp classloader. To solve your problem you can use object serialization or if your objects are the java beans you can use the java.beans encoding. The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. --Seva From: Surya Mishra [mailto:[EMAIL PROTECTED] Sent: Mon 10/3/2005 4:12 PM To: Tomcat Users List Subject: ClassCastException while sharing objects accross applications Hi, I am trying to share an object between 2 applications deployed on the same tomcat server. I have put the object in the ServletContext in my first application. I access the object using ServletContext.getContext(firstApp).getAttribute(object);. The object comes in fine but it won't let me cast it to the actual Object Type. I get a ClassCastException. I tried printing the name of the class. That also came fine (same class name). Thanks in advance. -Surya
Re: ClassCastException while sharing objects accross applications
What about using JNDI to share objects between webapps? I think that: init(){ ... String context = java:comp/env/; InitialContext ic = new InitialContext(); ClassX c = (ClassX) ic.lookup(sContexto); ... } should return the same object to different servlets/webapps, if you define the jndi-resource globally, i.e., in conf/server.xml - Original Message - From: Seva Popov [EMAIL PROTECTED] To: Tomcat Users List Surya Mishra [EMAIL PROTECTED]@gmail.com; Tomcat Users List tomcat-user@jakarta.apache.org Sent: Tuesday, October 04, 2005 8:41 AM Subject: RE: ClassCastException while sharing objects accross applications The type in Java is a combination of a fully qualified class name and its defining classloader. So the two objects with the same class name are considered different types in your two web applications because each web application is loaded by the dedicated webapp classloader. To solve your problem you can use object serialization or if your objects are the java beans you can use the java.beans encoding. The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. --Seva From: Surya Mishra [mailto:[EMAIL PROTECTED] Sent: Mon 10/3/2005 4:12 PM To: Tomcat Users List Subject: ClassCastException while sharing objects accross applications Hi, I am trying to share an object between 2 applications deployed on the same tomcat server. I have put the object in the ServletContext in my first application. I access the object using ServletContext.getContext(firstApp).getAttribute(object);. The object comes in fine but it won't let me cast it to the actual Object Type. I get a ClassCastException. I tried printing the name of the class. That also came fine (same class name). Thanks in advance. -Surya - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: ClassCastException while sharing objects accross applications
From: Andrés Glez. [mailto:[EMAIL PROTECTED] Subject: Re: ClassCastException while sharing objects accross applications What about using JNDI to share objects between webapps? Won't change anything, due to the previously noted classloader-specific casting issue. What should work (haven't tried it) is to put the defining class for the object of interest in shared/classes or shared/lib (if packaged in a jar), and remove it from each webapp. This will create the class under a classloader visible to both webapps. See: http://jakarta.apache.org/tomcat/tomcat-5.5-doc/class-loader-howto.html for more info. - Chuck THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ClassCastException while sharing objects accross applications
What Chuck says is right. This approach has a few gotchas though, especially if ClassX is complex and references many other objects loaded from the same (WebApp) classloader. You can end up with a lot of classes up in the common repository. -o Make ClassX an interface, if you can. That way only the interface (and any referenced types) need go in common. Refactoring here we go :) HTH, Jon Caldarale, Charles R wrote: From: Andrés Glez. [mailto:[EMAIL PROTECTED] Subject: Re: ClassCastException while sharing objects accross applications What about using JNDI to share objects between webapps? Won't change anything, due to the previously noted classloader-specific casting issue. What should work (haven't tried it) is to put the defining class for the object of interest in shared/classes or shared/lib (if packaged in a jar), and remove it from each webapp. This will create the class under a classloader visible to both webapps. See: http://jakarta.apache.org/tomcat/tomcat-5.5-doc/class-loader-howto.html for more info. - Chuck THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: ClassCastException while sharing objects accross applications
Yes, using the shared classloader seems like an obvious and easy option for sharing the objects between the web applications. However, like the previous author notes relying on the classloader can bring up some issues like introducing new dependencies and reducing the web application incapsulation. That is why the alternative way of sharing objects between the web applications is worth considering I guess. As I noted before one can achive this using the java object serialization or the java.beans xml encoding. This technique allows one to effectively eliminate the class loader information from the type and thus share the objects between the web applications without using the shared classloader. BTW, one can not use the Tomcat JNDI tree as a place to share the objects between the web apps, because Tomcat does not allow one to put an arbitrary object into the JNDI tree. One possible way is to utilize the system MBeanServer. --Seva -Original Message- From: Jon Wingfield [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 04, 2005 9:47 AM To: Tomcat Users List Subject: Re: ClassCastException while sharing objects accross applications What Chuck says is right. This approach has a few gotchas though, especially if ClassX is complex and references many other objects loaded from the same (WebApp) classloader. You can end up with a lot of classes up in the common repository. -o Make ClassX an interface, if you can. That way only the interface (and any referenced types) need go in common. Refactoring here we go :) HTH, Jon Caldarale, Charles R wrote: From: Andrés Glez. [mailto:[EMAIL PROTECTED] Subject: Re: ClassCastException while sharing objects accross applications What about using JNDI to share objects between webapps? Won't change anything, due to the previously noted classloader-specific casting issue. What should work (haven't tried it) is to put the defining class for the object of interest in shared/classes or shared/lib (if packaged in a jar), and remove it from each webapp. This will create the class under a classloader visible to both webapps. See: http://jakarta.apache.org/tomcat/tomcat-5.5-doc/class-loader-howto.html for more info. - Chuck THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
ClassCastException while sharing objects accross applications
Hi, I am trying to share an object between 2 applications deployed on the same tomcat server. I have put the object in the ServletContext in my first application. I access the object using ServletContext.getContext(firstApp).getAttribute(object);. The object comes in fine but it won't let me cast it to the actual Object Type. I get a ClassCastException. I tried printing the name of the class. That also came fine (same class name). Thanks in advance. -Surya