1 package org
.argeo
.cms
.servlet
.internal
.jetty
;
3 import java
.util
.Dictionary
;
4 import java
.util
.Hashtable
;
7 import javax
.websocket
.DeploymentException
;
8 import javax
.websocket
.server
.ServerContainer
;
9 import javax
.websocket
.server
.ServerEndpointConfig
;
11 import org
.argeo
.api
.cms
.CmsConstants
;
12 import org
.argeo
.api
.cms
.CmsLog
;
13 import org
.argeo
.cms
.websocket
.javax
.server
.CmsWebSocketConfigurator
;
14 import org
.argeo
.cms
.websocket
.javax
.server
.TestEndpoint
;
15 import org
.argeo
.util
.LangUtils
;
16 import org
.eclipse
.equinox
.http
.jetty
.JettyConfigurator
;
17 import org
.osgi
.framework
.BundleContext
;
18 import org
.osgi
.framework
.FrameworkUtil
;
19 import org
.osgi
.framework
.ServiceReference
;
20 import org
.osgi
.service
.cm
.ConfigurationException
;
21 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
22 import org
.osgi
.util
.tracker
.ServiceTracker
;
25 public class JettyServiceFactory
implements ManagedServiceFactory
{
26 private final static CmsLog log
= CmsLog
.getLog(JettyServiceFactory
.class);
28 final static String CMS_JETTY_CUSTOMIZER_CLASS
= "org.argeo.equinox.jetty.CmsJettyCustomizer";
30 final static String WEBSOCKET_ENABLED
= "websocket.enabled";
32 private final BundleContext bc
= FrameworkUtil
.getBundle(JettyServiceFactory
.class).getBundleContext();
35 ServiceTracker
<ServerContainer
, ServerContainer
> serverSt
= new ServiceTracker
<ServerContainer
, ServerContainer
>(
36 bc
, ServerContainer
.class, null) {
39 public ServerContainer
addingService(ServiceReference
<ServerContainer
> reference
) {
40 ServerContainer serverContainer
= super.addingService(reference
);
42 BundleContext bc
= reference
.getBundle().getBundleContext();
43 ServiceReference
<ServerEndpointConfig
.Configurator
> srConfigurator
= bc
44 .getServiceReference(ServerEndpointConfig
.Configurator
.class);
45 ServerEndpointConfig
.Configurator endpointConfigurator
= bc
.getService(srConfigurator
);
46 ServerEndpointConfig config
= ServerEndpointConfig
.Builder
47 .create(TestEndpoint
.class, "/ws/test/events/").configurator(endpointConfigurator
).build();
49 serverContainer
.addEndpoint(config
);
50 } catch (DeploymentException e
) {
51 throw new IllegalStateException("Cannot initalise the WebSocket server runtime.", e
);
53 return serverContainer
;
61 public String
getName() {
62 return "Jetty Service Factory";
66 public void updated(String pid
, Dictionary
<String
, ?
> properties
) throws ConfigurationException
{
67 // Explicitly configures Jetty so that the default server is not started by the
68 // activator of the Equinox Jetty bundle.
69 Map
<String
, String
> config
= LangUtils
.dictToStringMap(properties
);
70 if (!config
.isEmpty()) {
71 config
.put("customizer.class", CMS_JETTY_CUSTOMIZER_CLASS
);
73 // TODO centralise with Jetty extender
74 Object webSocketEnabled
= config
.get(WEBSOCKET_ENABLED
);
75 if (webSocketEnabled
!= null && webSocketEnabled
.toString().equals("true")) {
76 bc
.registerService(ServerEndpointConfig
.Configurator
.class, new CmsWebSocketConfigurator(), null);
77 config
.put(WEBSOCKET_ENABLED
, "true");
83 tryGettyJetty
: while (tryCount
> 0) {
85 // FIXME deal with multiple ids
86 JettyConfigurator
.startServer(CmsConstants
.DEFAULT
, new Hashtable
<>(config
));
87 // Explicitly starts Jetty OSGi HTTP bundle, so that it gets triggered if OSGi
88 // configuration is not cleaned
89 FrameworkUtil
.getBundle(JettyConfigurator
.class).start();
91 } catch (IllegalStateException e
) {
92 // Jetty may not be ready
95 } catch (Exception e1
) {
101 } catch (Exception e
) {
102 log
.error("Cannot start default Jetty server with config " + properties
, e
);
108 public void deleted(String pid
) {
113 JettyConfigurator
.stopServer(CmsConstants
.DEFAULT
);
114 } catch (Exception e
) {
115 log
.error("Cannot stop default Jetty server.", e
);