Skip to content

Commit 4702d3e

Browse files
committed
Do not release OSGi HttpService
Fix openhab/openhab-core#4140 Signed-off-by: Laurent Garnier <[email protected]>
1 parent 261787b commit 4702d3e

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

bundles/org.jupnp/src/main/java/org/jupnp/OSGiUpnpServiceConfiguration.java

+13-26
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.jupnp.transport.spi.StreamClientConfiguration;
5656
import org.jupnp.transport.spi.StreamServer;
5757
import org.osgi.framework.BundleContext;
58-
import org.osgi.framework.ServiceReference;
5958
import org.osgi.service.component.annotations.Activate;
6059
import org.osgi.service.component.annotations.Component;
6160
import org.osgi.service.component.annotations.ConfigurationPolicy;
@@ -87,6 +86,7 @@
8786
* @author Kai Kreuzer - introduced bounded thread pool and http service streaming server
8887
* @author Victor Toni - consolidated transport abstraction into one interface
8988
* @author Wouter Born - conditionally enable component based on autoEnable configuration value
89+
* @author Laurent Garnier - do not release the OSGi HttpService
9090
*/
9191
@Component(configurationPid = "org.jupnp", configurationPolicy = ConfigurationPolicy.REQUIRE, enabled = false)
9292
public class OSGiUpnpServiceConfiguration implements UpnpServiceConfiguration {
@@ -122,16 +122,15 @@ public class OSGiUpnpServiceConfiguration implements UpnpServiceConfiguration {
122122

123123
protected BundleContext context;
124124

125-
@SuppressWarnings("rawtypes")
126-
protected ServiceReference httpServiceReference;
127-
128125
@SuppressWarnings("rawtypes")
129126
protected TransportConfiguration transportConfiguration;
130127

131128
protected Integer timeoutSeconds = 10;
132129
protected Integer retryIterations = 5;
133130
protected Integer retryAfterSeconds = (int) TimeUnit.MINUTES.toSeconds(10);
134131

132+
protected HttpService httpService;
133+
135134
/**
136135
* Defaults to port '0', ephemeral.
137136
*/
@@ -185,15 +184,14 @@ protected void activate(BundleContext context, Map<String, Object> configProps)
185184

186185
@Deactivate
187186
protected void deactivate() {
188-
if (httpServiceReference != null) {
189-
context.ungetService(httpServiceReference);
190-
}
191-
192187
shutdown();
193-
194188
logger.debug("{} deactivated", this);
195189
}
196190

191+
protected void setHttpService(HttpService httpService) {
192+
this.httpService = httpService;
193+
}
194+
197195
@Override
198196
public DatagramProcessor getDatagramProcessor() {
199197
return datagramProcessor;
@@ -237,25 +235,14 @@ public DatagramIO createDatagramIO(NetworkAddressFactory networkAddressFactory)
237235
@Override
238236
@SuppressWarnings({ "rawtypes", "unchecked" })
239237
public StreamServer createStreamServer(NetworkAddressFactory networkAddressFactory) {
240-
ServiceReference serviceReference = context.getServiceReference(HttpService.class.getName());
241-
242-
if (serviceReference != null) {
243-
244-
if (httpServiceReference != null) {
245-
context.ungetService(httpServiceReference);
246-
}
247-
248-
httpServiceReference = serviceReference;
249-
250-
HttpService httpService = (HttpService) context.getService(serviceReference);
251-
252-
if (httpService != null) {
253-
return new ServletStreamServerImpl(new ServletStreamServerConfigurationImpl(
254-
HttpServiceServletContainerAdapter.getInstance(httpService, context),
255-
httpProxyPort != -1 ? httpProxyPort : callbackURI.getBasePath().getPort()));
256-
}
238+
if (httpService != null) {
239+
logger.debug("createStreamServer using OSGi HttpService");
240+
return new ServletStreamServerImpl(new ServletStreamServerConfigurationImpl(
241+
HttpServiceServletContainerAdapter.getInstance(httpService, context),
242+
httpProxyPort != -1 ? httpProxyPort : callbackURI.getBasePath().getPort()));
257243
}
258244

245+
logger.debug("createStreamServer without OSGi HttpService");
259246
return transportConfiguration.createStreamServer(networkAddressFactory.getStreamListenPort());
260247
}
261248

bundles/org.jupnp/src/main/java/org/jupnp/UpnpServiceImpl.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@
5252
* <p>
5353
* Override the various <tt>create...()</tt> methods to customize instantiation of protocol factory, router, etc.
5454
* </p>
55-
*
55+
*
5656
* @author Christian Bauer
5757
* @author Kai Kreuzer - OSGiified the service
58+
* @author Laurent Garnier - OSGi HttpService provided to OSGiUpnpServiceConfiguration
5859
*/
5960
@Component(configurationPid = "org.jupnp.upnpservice")
6061
@Designate(ocd = UpnpServiceImpl.Config.class)
@@ -77,6 +78,7 @@ public class UpnpServiceImpl implements UpnpService {
7778
protected UpnpServiceConfiguration configuration;
7879
protected ProtocolFactory protocolFactory;
7980
protected Registry registry;
81+
protected HttpService httpService;
8082

8183
protected ControlPoint controlPoint;
8284
protected Router router;
@@ -124,6 +126,7 @@ private static ScheduledExecutorService createExecutor() {
124126
public void setUpnpServiceConfiguration(UpnpServiceConfiguration configuration) {
125127
this.configuration = configuration;
126128
if (isRunning) {
129+
logger.debug("New OSGi UpnpServiceConfiguration is bound while UPnP service is running; restart needed");
127130
restart(true);
128131
}
129132
}
@@ -134,16 +137,20 @@ public void unsetUpnpServiceConfiguration(UpnpServiceConfiguration configuration
134137

135138
@Reference
136139
public void setHttpService(HttpService httpService) {
140+
this.httpService = httpService;
137141
// Only need to restart jupnp after/if HttpService appears
138142
if (isRunning) {
143+
logger.debug("New OSGi HttpService is bound while UPnP service is running; restart needed");
139144
shutdown(false);
140145
delayedStartup(1500);
141146
}
142147
}
143148

144149
public void unsetHttpService(HttpService httpService) {
150+
this.httpService = null;
145151
// Only need to restart jupnp after/if HttpService disappears
146152
if (isRunning) {
153+
logger.debug("OSGi HttpService is unbound while UPnP service is running; restart needed");
147154
shutdown(false);
148155
delayedStartup(1500);
149156
}
@@ -269,7 +276,12 @@ public void startup() {
269276

270277
// Instantiation order is important: Router needs to start its network services after registry is ready
271278

272-
logger.debug("Using configuration: {}", getConfiguration().getClass().getName());
279+
logger.debug("{} OSGi HttpService; using configuration: {}",
280+
httpService != null ? "Using" : "Not using", getConfiguration().getClass().getName());
281+
282+
if (getConfiguration() instanceof OSGiUpnpServiceConfiguration) {
283+
((OSGiUpnpServiceConfiguration) getConfiguration()).setHttpService(httpService);
284+
}
273285

274286
this.protocolFactory = createProtocolFactory();
275287
this.registry = createRegistry(protocolFactory);

0 commit comments

Comments
 (0)