Reputation: 685
With the environment:
I have already exposed a service via CXF and now I'm trying to expose the same service as a Hessian Service.
There is no war or web.xml, just plain beans + pax-http and I have tried the following:
<bean name="/hessian" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="promocionalOnLineWebServiceBean"/>
<property name="serviceInterface" value="org.fideliapos.promos.webservice.PromocionalOnLineFacade"/>
</bean>
...
<bean id="hessianServlet" class="org.springframework.web.context.support.HttpRequestHandlerServlet"/>
...
<osgi:service ref="hessianServlet" interface="javax.servlet.http.HttpServlet">
<service-properties>
<entry key="alias" value="/hessian"/>
</service-properties>
</osgi:service>
The idea is to register a servlet (a HttpRequestHandlerServlet) whose target is a HessianServiceExporter but I'm getting a No WebApplicationContext found: no ContextLoaderListener registered?.
I have traced the spring code and the internal jetty is recognizing the servlet and calling its init method:
@Override
public void init() throws ServletException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.target = wac.getBean(getServletName(), HttpRequestHandler.class);
}
and here lies the problem, since there is no spring WebApplicationContext and the target property cannot be inyected.
Am I missing something? or it is not possible to make it work like this.
As a workaround I'm considering extending the Servlet with my own implementation (setTarget and so) but I'd rather not to do it.
After trying to create and add my own HttpContext there is still something missing:
I implemented my own HttpContext:
public class HessianContext implements HttpContext{
...
}
added the bean
<bean id="hessianContext" class="org.fideliapos.promos.hessian.HessianContext"/>
the service:
<osgi:service id="hessianContextService" ref="hessianContext" interface="org.osgi.service.http.HttpContext">
<service-properties>
<entry key="httpContext.id" value="hessian"/> <!-- also tried with contextId-->
</service-properties>
</osgi:service>
and finally the servlet as a service:
<osgi:service ref="hessianServlet" interface="javax.servlet.http.HttpServlet">
<service-properties>
<entry key="alias" value="/hessian"/>
<entry key="httpContext.id" value="hessian"/> <!-- also tried with contextId-->
</service-properties>
</osgi:service>
Since the init method is looking for a WebApplicationContext it looks like I should declare and explicit GenericWebApplicationContext bean but I dont know how to 'join' this bean with the required HttpContext for OSGi.
Upvotes: 1
Views: 146
Reputation: 5285
Looks like you need to add the Spring WebApplicationContext to the HttpContext used for your servlet. Right now you use the DefaultHttpContext of Pax Web. In your case you'll need to register a custom HttpContext that is aware of the Spring stuff so the WebApplicationContextUtils.getRequireWebApplicationContext is capable of extracting this information. For this you'll need to register your custom HttpContext as Service and reference it in your Servlet, a complete Example of this using Blueprint (similar to spring) can be found here
Following is an excerpt of it:
<service id="forbiddenCtxtService" ref="forbiddenContext" interface="org.osgi.service.http.HttpContext">
<service-properties>
<entry key="httpContext.id" value="forbidden"/>
</service-properties>
</service>
The Important part is the httpContext.id
<bean id="forbiddenServlet" class="org.ops4j.pax.web.extender.samples.whiteboard.internal.WhiteboardServlet">
<argument type="java.lang.String" value="/forbidden"/>
</bean>
<service id="forbiddenServletService" ref="forbiddenServlet" interface="javax.servlet.Servlet">
<service-properties>
<entry key="alias" value="/forbidden"/>
<entry key="httpContext.id" value="forbidden"/>
</service-properties>
</service>
Again here the registered Servlet does have a configuration for the corresponding httpContext.id, this binds this Servlet to the previously registered HttpContext.
Upvotes: 2