Skip to content

Instantly share code, notes, and snippets.

@anpieber
Created April 15, 2011 10:16
Show Gist options
  • Select an option

  • Save anpieber/921488 to your computer and use it in GitHub Desktop.

Select an option

Save anpieber/921488 to your computer and use it in GitHub Desktop.
package cxf.equinox.failure.producer;
import java.util.Hashtable;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.cxf.Bus;
import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.databinding.DataBinding;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.endpoint.ServerLifeCycleListener;
import org.apache.cxf.endpoint.ServerLifeCycleManager;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;
import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext;
import cxf.equinox.failure.producer.internal.EchoServiceImpl;
public class Activator implements BundleActivator {
private Set<ServiceReference> httpServiceReferences = new CopyOnWriteArraySet<ServiceReference>();
private ServiceTracker st;
@Override
public void start(BundleContext context) throws Exception {
st = new ServiceTracker(context, HttpService.class.getName(), null) {
@Override
public Object addingService(ServiceReference reference) {
httpServiceReferences.add(reference);
return super.addingService(reference);
}
@Override
public void removedService(ServiceReference reference, Object service) {
httpServiceReferences.remove(reference);
super.removedService(reference, service);
}
};
st.open();
final String contextRoot = "/ws";
OsgiBundleXmlApplicationContext ctx = new OsgiBundleXmlApplicationContext(CXF_CONFIG);
ctx.setPublishContextAsService(false);
ctx.setBundleContext(context);
ctx.refresh();
SpringBusFactory fact = new SpringBusFactory(ctx);
Bus createBus = fact.createBus();
CXFNonSpringServlet cxf = new CXFNonSpringServlet();
cxf.setBus(createBus);
final HttpService httpService =
(HttpService) context.getService((ServiceReference) httpServiceReferences.toArray()[0]);
try {
httpService.registerServlet(contextRoot, cxf, new Hashtable<String, String>(),
httpService.createDefaultHttpContext());
} catch (Exception e) {
throw new ServiceException("CXF DOSGi: problem registering CXF HTTP Servlet", e);
}
DataBinding databinding = new AegisDatabinding();
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setBus(createBus);
factory.setServiceClass(EchoService.class);
factory.setAddress("/echo/");
factory.getServiceFactory().setDataBinding(databinding);
factory.setServiceBean(new EchoServiceImpl());
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(ServerFactoryBean.class.getClassLoader());
Server server = factory.create();
server.getEndpoint().put("WS", contextRoot);
Thread.currentThread().setContextClassLoader(ServerFactoryBean.class.getClassLoader());
ServerLifeCycleListener stopHook = new ServerLifeCycleListener() {
@Override
public void stopServer(Server s) {
Object contextProperty = s.getEndpoint().get("WS");
if (contextProperty != null && contextProperty.equals(contextRoot)) {
httpService.unregister(contextRoot);
}
}
@Override
public void startServer(Server server) {
}
};
ServerLifeCycleManager mgr = createBus.getExtension(ServerLifeCycleManager.class);
if (mgr != null) {
mgr.registerListener(stopHook);
}
server.start();
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
private static final String[] CXF_CONFIG = new String[]{
"classpath:META-INF/cxf/cxf.xml",
};
@Override
public void stop(BundleContext context) throws Exception {
st.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment