WebSphere Liberty - How to access OSGi bundle services from a servlet deployed via an EAR file? - websphere-liberty

I have a servlet deployed to the WebSphere Liberty profile via an EAR file. The application entry in server.xml looks like this:
<application id="MyApp" name="MyApp" type="ear" location="MyApp.ear" />
The servlet needs to access a service provided by an OSGi bundle. I followed the example mentioned here. The OSGi bundle exports the package the Counter interface is in. Adding an "Import-Package" statement to the EAR's manifest (like an OSGi bundle would have) to resolve the reference to Counter resulted in a class-def exception. So I added the class directly to the EAR. Now InitialContext.lookup() does return a proxy object but typecasting it to the Counter interface results in a casting exception.
I suspect the casting exception is happening because the EAR has its own Counter.class. Is there some other way I should be resolving that reference? How can an EAR import a package from another OSGi bundle?

Related

JNDI resources not available for Gemini Blueprint running in Tomcat 9 (working in Bundle Activator)

I have an older Web-Application using Eclipse RAP 3.0 with Equinox OSGI-Container and Gemini Blueprint 1.0.2.
I can define my JDNI Values in server.xml
link them in context.xml and use them in my web.xml.
In my launch.ini i use the parent classloader fwk so the JNDI configuration is passed to the OSGI-Container.
My Application-Bundles can read those values for example in Bundle-Activator:
String xxx= InitialContext.doLookup("java:comp/env/xxx"); or
String xxx= new InitialContext().lookup("java:comp/env/xxx");
Some Bundle-Configuration is done via Gemini Blueprint xml files and SpringConfiguration Java classes.
<jee:jndi-lookup id="xxx" jndi-name="java:comp/env/xxx" />
This part is not working and i get following error
Error creating bean with name 'xxx': Invocation of init method failed;
nested exception is javax.naming.NameNotFoundException:
Name [java:comp/env/xxx] is not bound in this Context. Unable to find [java:comp].
I get the same Error when i use Spring-Java-Configuration which is invoked from gemini blueprint xml. Then statements like new InitialContext().lookup("java:comp/env/xxx") don't work and i get the same exception.
I think it has to to with the classloaders that are different for Bundle-Activator-Code and Gemini BlueprintExtender Thread but in can't figure it out how to solve this.
I found a Solution that i can use.
If i Move my JNDI-Lookups from xml into my Spring-Configuration-class then i can change the clasloader in init like this:
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
After that i my Lookups work fine.
i found that solution here
Can Spring Boot be used with OSGi? If not, any plans to have an OSGi Spring Boot?.

Exporting Spring Bean Service in OSGI- New Instance per Injection Call

I am trying to export a spring bean as service using Spring's application context, I need to have them exported as "prototype" scope, but this is not possible due to OSGi Service registry caching the service as singleton.
Doing some research I came upon a post suggesting to use "session" scope to get around this issue. I am following this tutorial to get this working, but I am stuck with an issue that has to do with class loader not finding an interface.
Here's how I am declaring the bean
<osgi:service id="SimulationExporter" ref="simulationService" interface="org.geppetto.core.simulation.ISimulation"/>
<bean id="simulationService" scope="session" class="org.geppetto.simulation.SimulationService">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
When exporting the bean to another bundle, I get the following error
Caused by: java.lang.IllegalArgumentException: interface org.springframework.aop.scope.ScopedObject is not visible from class loader
at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:484)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:713)
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:117)
The bundle importing the service bean has the dependency org.springframework.aop-3.0.0.RELEASE , and this has also being copied to the virgo repository. Any ideas why would the class loader not find that interface that it needs for scope "session"?
Does your MANIFEST.MF include import packages for aop.scope?
If you are using Apache Felix to generate it try adding something like this:
<Import-Package>org.aopalliance.aop,org.springframework.aop,org.springframework.aop.scope,org.springframework.aop.framework,*</Import-Package>

How to load a spring beans which are in jar file in projects lib folder

How can I load a spring beans which are in jar file say for eg. sample.jar with all the beans declared in that jar with sample-applicationContext.xml?
Now I am using some of the beans from this jar in my project so when I deploy my ear file this sample.jar is in lib folder. Now when I deploy this project to server(jboss -5) it is not injecting the bean i have referenced in my main project.
we dont have any web app in this ear so the way we are loading beans are using ClassPathXmlApplicationContext. Can somebody gave me an example of how to load those beans from sample.jar(lib folder) first and then load those are in the project, so when spring creates beans in the main project it will have beans from sample.jar and would inject them.
Thanks
Beans are looked-up on the classpath, so if they are annotated and your have the proper component scan, they should be discovered.
If they are not annotated, but only listed in the sample-applicationContext.xml, then you can <import resource="classpath:sample-applicationContext.xml" />

Can you package an ejb interceptor in a library?

And if so how do you do it?
I have got an ejb #javax.interceptor.AroundInvoke interceptor which I like to move into a library for reuse. I moved the code into a library fixed the dependencies in maven and all compiles well now. Only on deploy I get the following error message:
09.02.2011 14:19:48 com.sun.logging.LogDomains$1 log
SCHWERWIEGEND: Exception while invoking class org.glassfish.ejb.startup.EjbApplication start method
java.lang.RuntimeException: java.lang.ClassNotFoundException:
From JSR 318: Enterprise JavaBeans, Version 3.1 - EJB Core Contract and Requirements:
20.3 Packaging Requirements
The ejb-jar file or .war file must
contain, either by inclusion or by
reference, the class files of each
enterprise bean as follows:
The enterprise bean class.
The enterprise bean business interfaces, web service endpoint
interfaces, and home and com-ponent
interfaces.
Interceptor classes.
The primary key class if the bean is an entity bean.
We say that a .jar file contains a second file “by reference” if the
second file is named in the Class-Path
attribute in the Manifest file of the
referencing .jar file or is contained
(either by inclusion or by reference)
in another .jar file that is named in
the Class-Path attribute in the
Manifest file of the referencing .jar
file.
So I'd say yes, you can package the interceptor class in a library .jar file.
Check that: 1) the library actually got packaged in and deployed with the ejb-jar file; and 2) the library .jar is referenced in the Manifest file as described above.

Classloader problem with Websphere 6.1 version

Hi
I have ear application, inside ear i have 4 web modules. What values i have to set for Classloader mode and WAR classloader policy? Currently it is giving exception ClassNotFound Exception with the values Classloader mode:PARENT_FIRST and WAR classloader policy:module.
Thanks in Advance.
could you elaborate on which class is not found, and on which WAR/JAR it is actually in, and then which one is looking for it.
The solution might end up moving the "missing" class to a separate JAR which the WARs will reference.

Resources