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
;
24 public class JettyServiceFactory
implements ManagedServiceFactory
{
25 private final static CmsLog log
= CmsLog
.getLog(JettyServiceFactory
.class);
27 final static String CMS_JETTY_CUSTOMIZER_CLASS
= "org.argeo.equinox.jetty.CmsJettyCustomizer";
29 final static String WEBSOCKET_ENABLED
= "websocket.enabled";
31 private final BundleContext bc
= FrameworkUtil
.getBundle(JettyServiceFactory
.class).getBundleContext();
34 ServiceTracker
<ServerContainer
, ServerContainer
> serverSt
= new ServiceTracker
<ServerContainer
, ServerContainer
>(
35 bc
, ServerContainer
.class, null) {
38 public ServerContainer
addingService(ServiceReference
<ServerContainer
> reference
) {
39 ServerContainer serverContainer
= super.addingService(reference
);
41 BundleContext bc
= reference
.getBundle().getBundleContext();
42 ServiceReference
<ServerEndpointConfig
.Configurator
> srConfigurator
= bc
43 .getServiceReference(ServerEndpointConfig
.Configurator
.class);
44 ServerEndpointConfig
.Configurator endpointConfigurator
= bc
.getService(srConfigurator
);
45 ServerEndpointConfig config
= ServerEndpointConfig
.Builder
46 .create(TestEndpoint
.class, "/ws/test/events/").configurator(endpointConfigurator
).build();
48 serverContainer
.addEndpoint(config
);
49 } catch (DeploymentException e
) {
50 throw new IllegalStateException("Cannot initalise the WebSocket server runtime.", e
);
52 return serverContainer
;
60 public String
getName() {
61 return "Jetty Service Factory";
65 public void updated(String pid
, Dictionary
<String
, ?
> properties
) throws ConfigurationException
{
66 // Explicitly configures Jetty so that the default server is not started by the
67 // activator of the Equinox Jetty bundle.
68 Map
<String
, String
> config
= LangUtils
.dictToStringMap(properties
);
69 if (!config
.isEmpty()) {
70 config
.put("customizer.class", CMS_JETTY_CUSTOMIZER_CLASS
);
72 // TODO centralise with Jetty extender
73 Object webSocketEnabled
= config
.get(WEBSOCKET_ENABLED
);
74 if (webSocketEnabled
!= null && webSocketEnabled
.toString().equals("true")) {
75 bc
.registerService(ServerEndpointConfig
.Configurator
.class, new CmsWebSocketConfigurator(), null);
76 config
.put(WEBSOCKET_ENABLED
, "true");
82 tryGettyJetty
: while (tryCount
> 0) {
84 // FIXME deal with multiple ids
85 JettyConfigurator
.startServer(CmsConstants
.DEFAULT
, new Hashtable
<>(config
));
86 // Explicitly starts Jetty OSGi HTTP bundle, so that it gets triggered if OSGi
87 // configuration is not cleaned
88 FrameworkUtil
.getBundle(JettyConfigurator
.class).start();
90 } catch (IllegalStateException e
) {
91 // Jetty may not be ready
94 } catch (Exception e1
) {
100 } catch (Exception e
) {
101 log
.error("Cannot start default Jetty server with config " + properties
, e
);
107 public void deleted(String pid
) {
112 JettyConfigurator
.stopServer(CmsConstants
.DEFAULT
);
113 } catch (Exception e
) {
114 log
.error("Cannot stop default Jetty server.", e
);