Thanks Ray. See the pseudo code as below and we are also registering
CXFServlet. During bundle activation we want to register the
org.apache.cxf.transport.servlet.CXFServlet servlet, get the cxf bus returned
from the servlet and set it as the default bus of cxf BusFactory. By doing this
we can receive SOAP calls from HP devices and also send SOAP calls to HP
devices.
In the old product we registered the CXFServlet servlet by calling
httpService.registerServlet(…), the cxf bus was not null (an
org.apache.cxf.bus.spring.SpringBus object), and the communication with HP
device was always fine. Due to the OSGI version upgrading for the product we
changed to use the whiteboard pattern to do the registration and we noticed the
returned cxf bus from the CXFServlet is null after calling
bundleContext.registerService(…) , and also, the published endpoints are not
there for listening. We are trying to find a solution. The experimentations
showed the way to make it work is to get the org.osgi.service.http.HttpService
reference ready first, and then call bundleContext.registerService(…) to
register the CXFServlet. This way the returned cxf bus is not null anymore and
the communication between the web server and HP device is fine. As mentioned,
the HttpService instance seems to be lazy started. In order to get the
org.osgi.service.http.HttpService reference ready, there has to be a web
request sent to the web server. This is not we want and why we asked the
question.
... ...
import javax.servlet.Servlet;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.http.osgi.HTTPTransportActivator;
import org.apache.cxf.transport.servlet.CXFServlet;
... ...
@Component(service = { HPOmniWebServicePublishComponent.class })
public class HPOmniWebServicePublishComponent implements IWebComponent {
... ...
private HTTPTransportActivator httpTransportActivator = new
HTTPTransportActivator();
... ...
@Activate
public void activate(ComponentContext ctx)
{
try {
// Set value for keystore and keystore password
... ...
System.setProperty("javax.net.ssl.keyStore", keyStoreLocation);
System.setProperty("javax.net.ssl.keyStorePassword", password);
... ...
httpTransportActivator.start(ctx.getBundleContext());
} catch (NoClassDefFoundError | Exception exception) {
// Log error for starting HTTPTransportActivator
}
… …
ClassLoader currentcl = Thread.currentThread().getContextClassLoader();
try {
// set classloader to CXF bundle class loader to avoid OSGi
classloader problems
ClassLoader bundlecl = BusFactory.class.getClassLoader();
Thread.currentThread().setContextClassLoader(bundlecl);
... ...
System.setProperty("javax.xml.ws.spi.Provider",
"org.apache.cxf.jaxws.spi.ProviderImpl");
… …
CXFServlet servlet = new CXFServlet(); //
org.apache.cxf.transport.servlet.CXFServlet
BundleContext bundleContext = ctx.getBundleContext();
params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN,
ApplicationFactory.SERVICE_BASE_PATH);
cxfServletRegistration =
bundleContext.registerService(Servlet.class, servlet, params); // Register the
org.apache.cxf.transport.servlet.CXFServlet servlet
Bus bus = servlet.getBus(); // ==> The returned org.apache.cxf.Bus
bus is always null if we didn’t get HttpService instance ready when calling
bundleContext.registerService(Servlet.class, servlet, params);
BusFactory.setDefaultBus(bus); //org.apache.cxf.BusFactory
// Publishe endpoints for specified implementors
Endpoint.publish(path, implementor);
... ...
Regards.
Justin Li
Justin Li
Sr. Software Developer
[cid:Kofax_ab6d7362-d88f-454c-a5e0-9fbdacd27b9c.jpg]
Kofax Canada, ULC
460 Phillip Street
Waterloo, ON N2L 5J2
Tel: +1 519 880 7543
[email protected]
[cid:WorkLikeTomorrow_5b748fe4-9e86-42f8-b5af-b79a2d6ee254.jpg]
________________________________
This communication is only for the use of the intended recipient. It may
contain confidential or proprietary information. If you are not the intended
recipient or have received this communication in error, please notify the
sender via phone and destroy this communication immediately.
From: Raymond Auge <[email protected]>
Sent: Thursday, July 23, 2020 10:29 AM
To: felix users <[email protected]>
Cc: Justin Li <[email protected]>
Subject: [EXTERNAL] Re: How to make HttpService reference ready when
bundleContext.registerService() is called
On Thu, Jul 23, 2020 at 8:59 AM Justin Li
<[email protected]<mailto:[email protected]>> wrote:
Our development requirement needs us to get the
org.osgi.service.http.HttpService reference during the activation of our
bundle, but the tests show the HttpService reference is always null if we want
to locate it from the bundle context.
You appear to be mixing HttpService and Http Whiteboard modes. Why do you need
HttpService if you are registering your servlet as a service using
bundleContext.registerService(Servlet.class, myServlet, params) ?
- Ray
The following is an example. "myServlet" is registered by calling
"bundleContext.registerService" with whiteboard pattern. According to tests, we
do get the org.osgi.service.http.HttpService reference by registering a
ServiceListener , but the HttpService reference is not ready unless we REALLY
make a web call to the web service itself. Actually the dumped HttpService is
an org.apache.felix.http.base.internal.service.PerBundleHttpServiceImpl object.
So, it seems the HttpService is lazy started.
We do not want the HttpService to be lazy started. The question is, is there
any config or setting can be used so that when bundleContext.registerService()
is called the HttpService reference will also be ready?
@Activate
public void activate(ComponentContext ctx)
{
... ...
BundleContext bundleContext = ctx.getBundleContext();
Hashtable<String, String> params = new Hashtable<>();
params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN,
"/currentView.*");
... ...
bundleContext.registerService(Servlet.class, myServlet, params);
... ...
}
Thanks.
Justin
--
Raymond
Augé<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.liferay.com_web_raymond.auge_profile&d=DwMFaQ&c=8oAR4sJCO2ADPpMooHT9XjkFhHutOc0lw16D57ldqyU&r=PRQSnbTAits5rOqriUqGVNp3ZSD_Y0883f9OHh2uyH8&m=k0K36SHBAFNcg0jZML1rYDdCQsBQu7DbaWMg20ltAoo&s=7SRY5xx5C_khvRmPPm-WJ8VFmr4zekWRO78LiVoEn3g&e=>
(@rotty3000)
Senior Software Architect Liferay,
Inc.<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.liferay.com&d=DwMFaQ&c=8oAR4sJCO2ADPpMooHT9XjkFhHutOc0lw16D57ldqyU&r=PRQSnbTAits5rOqriUqGVNp3ZSD_Y0883f9OHh2uyH8&m=k0K36SHBAFNcg0jZML1rYDdCQsBQu7DbaWMg20ltAoo&s=X-HD-i42JbovIZROb6ronxj_PnZlQ95mZXaD3YeIXOY&e=>
(@Liferay)