]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jetty/JettyServiceFactory.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jetty / JettyServiceFactory.java
1 package org.argeo.cms.servlet.internal.jetty;
2
3 import java.util.Dictionary;
4 import java.util.Hashtable;
5 import java.util.Map;
6
7 import javax.websocket.DeploymentException;
8 import javax.websocket.server.ServerContainer;
9 import javax.websocket.server.ServerEndpointConfig;
10
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;
23
24 public class JettyServiceFactory implements ManagedServiceFactory {
25 private final static CmsLog log = CmsLog.getLog(JettyServiceFactory.class);
26
27 final static String CMS_JETTY_CUSTOMIZER_CLASS = "org.argeo.equinox.jetty.CmsJettyCustomizer";
28 // Argeo specific
29 final static String WEBSOCKET_ENABLED = "websocket.enabled";
30
31 private final BundleContext bc = FrameworkUtil.getBundle(JettyServiceFactory.class).getBundleContext();
32
33 public void start() {
34 ServiceTracker<ServerContainer, ServerContainer> serverSt = new ServiceTracker<ServerContainer, ServerContainer>(
35 bc, ServerContainer.class, null) {
36
37 @Override
38 public ServerContainer addingService(ServiceReference<ServerContainer> reference) {
39 ServerContainer serverContainer = super.addingService(reference);
40
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();
47 try {
48 serverContainer.addEndpoint(config);
49 } catch (DeploymentException e) {
50 throw new IllegalStateException("Cannot initalise the WebSocket server runtime.", e);
51 }
52 return serverContainer;
53 }
54
55 };
56 serverSt.open();
57 }
58
59 @Override
60 public String getName() {
61 return "Jetty Service Factory";
62 }
63
64 @Override
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);
71
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");
77 }
78 }
79
80 int tryCount = 60;
81 try {
82 tryGettyJetty: while (tryCount > 0) {
83 try {
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();
89 break tryGettyJetty;
90 } catch (IllegalStateException e) {
91 // Jetty may not be ready
92 try {
93 Thread.sleep(1000);
94 } catch (Exception e1) {
95 // silent
96 }
97 tryCount--;
98 }
99 }
100 } catch (Exception e) {
101 log.error("Cannot start default Jetty server with config " + properties, e);
102 }
103
104 }
105
106 @Override
107 public void deleted(String pid) {
108 }
109
110 public void stop() {
111 try {
112 JettyConfigurator.stopServer(CmsConstants.DEFAULT);
113 } catch (Exception e) {
114 log.error("Cannot stop default Jetty server.", e);
115 }
116
117 }
118
119 }