Skip to content

Package: Activator

Activator

nameinstructionbranchcomplexitylinemethod
Activator()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
readProperty(BundleContext, String, Object)
M: 34 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
start(BundleContext)
M: 97 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
startGrizzly(int, boolean, boolean)
M: 49 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
stop(BundleContext)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly.osgi.httpservice;
18:
19: import java.io.IOException;
20: import java.util.Hashtable;
21:
22: import org.glassfish.grizzly.comet.CometAddOn;
23: import org.glassfish.grizzly.http.server.HttpServer;
24: import org.glassfish.grizzly.http.server.NetworkListener;
25: import org.glassfish.grizzly.osgi.httpservice.util.Logger;
26: import org.glassfish.grizzly.websockets.WebSocketAddOn;
27: import org.osgi.framework.BundleActivator;
28: import org.osgi.framework.BundleContext;
29: import org.osgi.framework.ServiceRegistration;
30: import org.osgi.service.http.HttpService;
31: import org.osgi.service.log.LogService;
32: import org.osgi.util.tracker.ServiceTracker;
33:
34: /**
35: * OSGi HttpService Activator.
36: *
37: * @author Hubert Iwaniuk
38: * @since Jan 20, 2009
39: */
40: public class Activator implements BundleActivator {
41:
42: private static final String ORG_OSGI_SERVICE_HTTP_PORT = "org.osgi.service.http.port";
43: private static final String ORG_OSGI_SERVICE_HTTPS_PORT = "org.osgi.service.http.port.secure";
44: private static final String GRIZZLY_COMET_SUPPORT = "org.glassfish.grizzly.cometSupport";
45: private static final String GRIZZLY_WEBSOCKETS_SUPPORT = "org.glassfish.grizzly.websocketsSupport";
46:
47: private ServiceTracker<?, ?> logTracker;
48: private Logger logger;
49:
50: private HttpServiceFactory serviceFactory;
51: private HttpServer httpServer;
52:
53: private ServiceRegistration<?> httpServiceRegistration;
54: private ServiceRegistration<?> extServiceRegistration;
55:
56: /**
57: * {@inheritDoc}
58: */
59: @Override
60: public void start(final BundleContext bundleContext) throws Exception {
61: logTracker = new ServiceTracker<>(bundleContext, LogService.class.getName(), null);
62: logTracker.open();
63: logger = new Logger(logTracker);
64: logger.info("Starting Grizzly OSGi HttpService");
65:
66: int port = readProperty(bundleContext, ORG_OSGI_SERVICE_HTTP_PORT, 80);
67:• if (bundleContext.getProperty(ORG_OSGI_SERVICE_HTTPS_PORT) != null) {
68: logger.warn("HTTPS not supported yet.");
69: }
70:
71: boolean cometEnabled = readProperty(bundleContext, GRIZZLY_COMET_SUPPORT, false);
72: boolean websocketsEnabled = readProperty(bundleContext, GRIZZLY_WEBSOCKETS_SUPPORT, false);
73:
74: startGrizzly(port, cometEnabled, websocketsEnabled);
75: serviceFactory = new HttpServiceFactory(httpServer, logger, bundleContext.getBundle());
76:
77: // Register our HttpService/HttpServiceExtension implementation so that it may be looked up by the OSGi runtime.
78: // We do it once per interface type so it can be looked up by either.
79: httpServiceRegistration = bundleContext.registerService(HttpService.class.getName(), serviceFactory, new Hashtable<>());
80: extServiceRegistration = bundleContext.registerService(HttpServiceExtension.class.getName(), serviceFactory, new Hashtable<>());
81: }
82:
83: /**
84: * Reads property from {@link BundleContext}. If property not present or invalid value for type <code>T</code> return
85: * <code>defValue</code>.
86: *
87: * @param ctx Bundle context to query.
88: * @param name Property name to query for.
89: * @param defValue Default value if property not present or invalid value for type <code>T</code>.
90: * @param <T> Property type.
91: * @return Property value or default as described above.
92: */
93: @SuppressWarnings("unchecked")
94: private <T> T readProperty(BundleContext ctx, String name, T defValue) {
95: String value = ctx.getProperty(name);
96:• if (value != null) {
97:• if (defValue instanceof Integer) {
98: try {
99: return (T) (Integer) Integer.parseInt(value);
100: } catch (NumberFormatException e) {
101: logger.info("Couldn't parse '" + name + "' property, going to use default (" + defValue + "). " + e.getMessage());
102: }
103:• } else if (defValue instanceof Boolean) {
104: return (T) (Boolean) Boolean.parseBoolean(value);
105: }
106:
107: return (T) value;
108: }
109:
110: return defValue;
111: }
112:
113: /**
114: * Starts the {@link HttpServer}.
115: *
116: * @param port Port to listen on.
117: * @param cometEnabled If comet should be enabled.
118: * @param cometEnabled If websockets should be enabled.
119: * @throws IOException Couldn't start the {@link HttpServer}.
120: */
121: private void startGrizzly(int port, boolean cometEnabled, boolean websocketsEnabled) throws IOException {
122: httpServer = new HttpServer();
123: NetworkListener networkListener = new NetworkListener("osgi-listener", "0.0.0.0", port);
124:
125: logger.info("HttpServer will listen on " + port);
126:
127:• if (cometEnabled) {
128: logger.info("Enabling Comet.");
129: networkListener.registerAddOn(new CometAddOn());
130: }
131:• if (websocketsEnabled) {
132: logger.info("Enabling WebSockets.");
133: networkListener.registerAddOn(new WebSocketAddOn());
134: }
135:
136: httpServer.addListener(networkListener);
137: httpServer.start();
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: @Override
144: public void stop(final BundleContext bundleContext) throws Exception {
145: logger.info("Stopping Grizzly OSGi HttpService");
146:
147: serviceFactory.stop();
148:• if (httpServiceRegistration != null) {
149: httpServiceRegistration.unregister();
150: }
151:• if (extServiceRegistration != null) {
152: extServiceRegistration.unregister();
153: }
154:
155: httpServer.shutdownNow();
156: logTracker.close();
157: }
158: }