Getting a weird cast exception : javax.mail.session cannot be cast to javax.mail.session - jboss5.x

I am trying to use apche-commons-email API for sending an email, in my java web application. I have configured the jboss5 mail-service.xml to send emails from Gmail id. But I am getting the error
org.jboss.resteasy.spi.UnhandledException: java.lang.ClassCastException:
javax.mail.Session cannot be cast to javax.mail.Session
when executing the line
Session mailSession = (Session) ictx.lookup("java:/Mail");
Please help.

I can only see this as a custom class loader loading a javax.mail.Session class along with the original javax.mail.Session object !
But when attempting to cast an object of the first class to an instance of the second, an exception occurs because both objects don't match (i.e. method names, member variables..etc)
Check this answer and other answers for the same question.

Related

Why does the Swift interpreter provide memory locations for instances that throw runtime errors, but not class names?

I am trying to debug my Swift application. Below is the error message that I am receiving.
In the image, it is clear that the interpreter knows where the instance of the class is. So why doesn't the error message provide a class name to go with the error in this case? Thank you!
It does. The full error has the form:
objc[9596]: -[main.Foo a_missing_method]: unrecognized selector sent to instance 0x7fa711505390 (no message forward handler is installed)
-[main.Foo a_missing_method] is telling you that the selector a_missing_method was being sent to an instance of class main.Foo (a method being sent to the class itself would use a + instead of a -).

EclipseLink-Moxy Unable to load custom DomHandler class while instantiating JAXB context

I am using Eclipselink Moxy Implementation of JAXB in my project to map complex XML to String Object using XmlAnyElement.
For this I have implemented DomHandler named as LayoutHandler.
I am using JAXB for resteasy web services deployed in JBoss 6.
I am facing Below issue intermittently -
Exception [EclipseLink-50033] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b):
org.eclipse.persistence.exceptions.JAXBException
Exception Description: The DomHandlerConverter for DomHandler
[com.**.LayoutHandler] set on property [layoutXml] could not be
initialized.
Internal Exception: java.lang.ClassNotFoundException:
com.**.LayoutHandler from
BaseClassLoader#5c0b3ad0{vfs:///*/*/jboss-server/server/all/deployers/resteasy.deployer}
While EclipseLink Moxy is instantiating JAXBContext using JAXBContext.newInstance(classes, properties)
After spending some time in debugging and analyzing the issue I could figure out that ClassLoader of resteasy is getting used to load LayoutHandler class instead of my application class loader(vfs://///jboss-server/server/all/deploy/app_name.ear/app_name.war/) which is causing the issue as its unable to find the LayoutHandler class.
When I bounce the server, issue is getting resolved so I am unable to find out the exact root cause. Any help will be appreciated.
Further debugging into org.eclipse.persistence.jaxb.JAXBContextFactory revealed that below two classes are getting passed to createContext() method of JAXBContextFactory -
org.jboss.resteasy.plugins.providers.jaxb.JaxbCollection
com.**.Model_class
public static javax.xml.bind.JAXBContext createContext(Class[] classesToBeBound, Map properties) throws JAXBException {
ClassLoader loader = null;
if (classesToBeBound.length > 0) {
loader = classesToBeBound[0].getClassLoader();
}
return createContext(classesToBeBound, properties, loader);
}
In above method classloader of first class is getting used to load the custom DomHandler later on.
When first element in array is model class at that time code is working fine as application context class loader is getting used but when the first element in array is JaxbCollection rest easy context class loader is getting used and its throwing mentioned exception.
This issue is occurring intermittently as order of elements in array is varying which might be due to the use of HashSet to hold the elements of type Class by caller of this method which is passing the classesToBeBound array
Note: I have replaced actual package names with *.
I'm surprised it works on a bounce... all of your JAXB bits need to line up, you should be using the moxy jaxb provider at all times. If it's failing after initial deploy, then working after a bounce, I suspect that you want to specify the moxy jaxb provider in your system properties ( -Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory ) and ensure that they're available to jboss when your app is not deployed.

Why the default Controller implementation sends crashes with internal error?

I generated controller under rest-api grails app profile. Nothing is changed in controller, just some println calls added.
For call curl -X PUT -d name=petr2 -d phone=338 localhost:8080/TSCell/3 I have {"message":"Internal server error","error":500} response. In debug I can see, that error occured after final respond TSCell, [status: OK, view:"show"] call.
Code for update method:
#Transactional
def update(TSCell tSCell) {
println "in update method"
if (tSCell == null) {
transactionStatus.setRollbackOnly()
render status: NOT_FOUND
return
}
if (tSCell.hasErrors()) {
transactionStatus.setRollbackOnly()
respond tSCell.errors, view:'edit'
return
}
tSCell.save flush:true
respond tSCell, [status: OK, view:"show"]
}
And stack trace
ERROR org.grails.web.errors.GrailsExceptionResolver - IllegalArgumentException occurred when processing request: [PUT] /TSCell/3
Model variable [TSCell] of with value [class zcrm.api.TSCell] type [java.lang.Class] is not of the correct type [zcrm.api.TSCell]. Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:883)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: grails.views.ViewRenderException: Error rendering view: Model variable [TSCell] of with value [class zcrm.api.TSCell] type [java.lang.Class] is not of the correct type [zcrm.api.TSCell]
at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:33)
at grails.views.mvc.GenericGroovyTemplateView.renderMergedOutputModel(GenericGroovyTemplateView.groovy:71)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at grails.views.mvc.renderer.DefaultViewRenderer.render(DefaultViewRenderer.groovy:105)
at grails.artefact.controller.RestResponder$Trait$Helper.internalRespond(RestResponder.groovy:188)
at grails.artefact.controller.RestResponder$Trait$Helper.respond(RestResponder.groovy:98)
at zcrm.api.TSCellController$$EQ0icN2W.$tt__update(TSCellController.groovy:64)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
... 14 common frames omitted
Caused by: java.lang.IllegalArgumentException: Model variable [TSCell] of with value [class zcrm.api.TSCell] type [java.lang.Class] is not of the correct type [zcrm.api.TSCell]
at grails.views.WritableScriptTemplate.make(WritableScriptTemplate.groovy:138)
at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper.prepareWritable(DefaultGrailsJsonViewHelper.groovy:736)
at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper$7.writeTo(DefaultGrailsJsonViewHelper.groovy:713)
at grails.plugin.json.view.JsonViewTemplate.json(JsonViewTemplate.groovy:126)
at grails.plugin.json.view.JsonViewTemplate.json(JsonViewTemplate.groovy:149)
at zcrm_api_TSCell_show_gson.run(zcrm_api_TSCell_show_gson:7)
at grails.plugin.json.view.JsonViewTemplate.doWrite(JsonViewTemplate.groovy:35)
at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:30)
... 26 common frames omitted
Thanks in advance.
That code shouldn't compile, but Groovy is too 'helpful' sometimes and allowed it through. You named an instance variable the same as its class, TSCell, and this turns out to be an interesting block of code to see how Groovy handles disambiguation between instance and static method calls.
For the first line, since you have TSCell TSCell it's possible for the compiler to know that the one on the left is the class name and the one on the right is an instance variable, since there's no other valid interpretation of those tokens.
In the third line it's not as clear whether the class or instance variable is being checked for null, but I tried this out locally and it's the instance variable.
TSCell.hasErrors() could be interpreted as a static method call on the class or a call on the instance, but since that method isn't static, Groovy invokes it on the instance and it succeeds. The same logic has to be applied to the save call, but again since it's not a static method it's call on the instance and succeeds.
And then on the last line of the method, kaboom, your luck ran out after four successful calls. There are a few overloads of the respond method and you end up calling respond(Object, Map), and that's valid for either TSCell the class, or TSCell the instance of the TSCell class. Groovy picked the one that wasn't what you intended and the one that isn't supported inside the respond method.
Groovy shares Java's variable and class naming conventions, i.e. class names start with an uppercase letter and instance variable names start with a lowercase letter. It's one thing to just tell people that this is a good approach, but an example like this makes it a lot more obvious why it's a bad idea to use uppercase instance variable names (you should be able to look at the variable and not need to see its declaration to know if it's a class or var name) and why it's even worse to use the same name as the class.
This error is probably caused because you named parameter with same name as class name.
I would suggest that you change name of the variable to lowercase (this is groovy and java naming convention).
def update(TSCell tsCell) { //you can also just write tsCell without type
println "in update method"
if (tsCell == null) {
transactionStatus.setRollbackOnly()
render status: NOT_FOUND
return
}
if (tsCell.hasErrors()) {
transactionStatus.setRollbackOnly()
respond tsCell.errors, view:'edit'
return
}
tsCell.save flush:true
respond tsCell, [status: OK, view:"show"]
}
The problem was in the call curl. As documentation says Grails has built in support for Content negotiation using either the HTTP Accept header, an explicit format request parameter or the extension of a mapped URI.
So the adding -H "Accept: application/xml" to the above curl call resolved the issue.

Jackson (jersey) deserialize exception - (for id type 'Id.class'): no such class found

I have a REST web application based on Jersey and running in an OSGi container - Geronimo3. The service is returning results fine with Json POST data and able to marshal json output to java object. I have written a REST client and that too works fine with main method getting back the response object.
However, Rest client fails in one scenario - when called from Bundle Activator class in a OSGi web application. It gives below error in this case.
Also when the web application is fully initialized, the Rest client works fine in request scope. I am suspecting some issue with classloaders since OSGi classloading is different regular webapps (tomcat). Also the response class uses #JsonTypeInfo annotation to map Interface type to concrete class.
Caused by: java.lang.IllegalArgumentException: Invalid type id 'com.nnn.IContentValue$ContentText' (for id type 'Id.class'): no such class found
at org.codehaus.jackson.map.jsontype.impl.ClassNameIdResolver.typeFromId(ClassNameIdResolver.java:57)
at org.codehaus.jackson.map.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:113)
at org.codehaus.jackson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:82)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:52)
at org.codehaus.jackson.map.deser.std.MapDeserializer._readAndBind(MapDeserializer.java:321)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:249)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:33)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
I found a workaround that I'm documenting.
It is caused due to org.codehaus.jackson.map.util.ClassUtil.findClass loading typeid class from org.eclipse.core.runtime.internal.adaptor.ContextFinder that is different from OSGi bundle classloader during bundle init phase.
I replaced the contextClassLoader using Thread.currentThread().setContextClassLoader(getClass().getClassLoader()) before making the jersey client call and reverting it to original one in finally block.

GWT RequestContext ENum in the request

If we use enum as one of the attribute in the Request invocation , it throws an UnsupportedOpeationException and does not even invoke the service method on the server.
#Service(value = DesignService.class, locator = DesignServiceLocator.class)
public interface DesignRequest extends RequestContext {
Request<List<DesignProxy>> findDesign(SortEnum sortorder);
}
when we invoke the designRequest.findDesign(sortorderEnum).fire() the UnsupportOperationException is thrown on the javascript console on chrome dev tools/Firebug console.
Looks like it is related to Issue 6504, which will throw an UnsupportedOperationException if it fails to find the type you are using - consider trying to change to class methods in your enum, or wait until 2.4 is released.
If you are not using anonymous enum instances, can you post more info about this error, such as where the exception is thrown from?