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