I make my own, to get the class loader right.

package com.basistech.ws.frontend.service;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;

/**
 * A CXF bus, with a correct class loader, as a component.
 */
@Component(configurationPolicy = ConfigurationPolicy.IGNORE)
public class CXFBusImpl implements CXFBus {
    private Bus bus;

    @Activate
    public void activator(ComponentContext context) {
        // Make a bus factory so that the worker and front end, if
co-resident, don't share.
        bus = BusFactory.newInstance().createBus();
        // Set the class loader so we get the correct TCCL.
        
bus.setExtension(context.getBundleContext().getBundle().adapt(BundleWiring.class).getClassLoader(),
ClassLoader.class);
    }

    @Deactivate
    public void deactivator() {
        bus.shutdown(false);
    }

    @Override
    public Bus bus() {
        return bus;
    }
}


On Sun, Mar 6, 2016 at 8:23 PM, James Carman <[email protected]> wrote:
> That worked!  Thanks again.  By the way, how are you determining which Bus
> to use?  I used BusFactory.getDefaultBus(true).
>
>
> On Sun, Mar 6, 2016 at 5:48 PM James Carman <[email protected]>
> wrote:
>
>> This should work very well! Thank you so much! I'll code it up tonight. I
>> will write an open source version of this in the very near future
>> On Sun, Mar 6, 2016 at 5:45 PM Benson Margulies <[email protected]>
>> wrote:
>>
>>> On Sun, Mar 6, 2016 at 5:18 PM, James Carman <[email protected]>
>>> wrote:
>>> > It's karaf. I'm building a service that looks up at runtime the
>>> > configuration of the services and then "exports" them accordingly. I'd
>>> like
>>> > to support exporting via CXF and was looking for inspiration.
>>> > On Sun, Mar 6, 2016 at 5:16 PM Benson Margulies <[email protected]>
>>>
>>> I had built what you describe (DS component that had a
>>> multiple-cardinality reference that collected resources, and then set
>>> up a CXF service), but it could never work out the timing of startup.
>>> So, instead, I allow each service to export itself. The base class of
>>> all the services has the following. The depends on having the latest
>>> SCR from 4.0.4 and the following maven-bundle-plugin options.
>>>
>>> <_dsannotations>*</_dsannotations>
>>> <_dsannotations-options>inherit</_dsannotations-options>
>>>
>>> ......
>>>
>>>
>>> /**
>>>  * Start the service.
>>>  * @param resourcePath the resource path.
>>>  */
>>> protected void startService(String resourcePath) {
>>>
>>>     JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
>>>     // Setting the bus ensures that the TCCL is this bundle when our
>>> resource classes are called.
>>>     sf.setBus(cxfBus.bus());
>>>     sf.setProvider(new
>>> JacksonJaxbJsonProvider(JsonUtils.getObjectMapper(),
>>>             JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS));
>>>     sf.setProvider(new JsonExceptionMapper());
>>>     sf.setProvider(new WebApplicationExceptionMapper());
>>>     sf.setProvider(new GenericExceptionMapper());
>>>     sf.setServiceBeans(Collections.singletonList(this));
>>>
>>>     String url = sharedService.getUrlPathPrefix() + resourcePath;
>>>     LOG.info(String.format("%s at %s", getClass().getName(), url));
>>>     sf.setAddress(url);
>>>     server = sf.create();
>>> }
>>>
>>> @Activate
>>> public void activate(ComponentContext context) {
>>>     LOG.info("Activating " + getClass().getName());
>>>     startService(getPath());
>>> }
>>>
>>> @Deactivate
>>> public void shutdown() {
>>>     if (server != null) {
>>>         server.destroy();
>>>         server = null;
>>>     }
>>> }
>>>
>>

Reply via email to