Skip to content

Commit 95e6c70

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

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* @author Kai Kreuzer - introduced bounded thread pool and http service streaming server
8888
* @author Victor Toni - consolidated transport abstraction into one interface
8989
* @author Wouter Born - conditionally enable component based on autoEnable configuration value
90+
* @author Laurent Garnier - do not release the OSGi HttpService
9091
*/
9192
@Component(configurationPid = "org.jupnp", configurationPolicy = ConfigurationPolicy.REQUIRE, enabled = false)
9293
public class OSGiUpnpServiceConfiguration implements UpnpServiceConfiguration {
@@ -122,9 +123,6 @@ public class OSGiUpnpServiceConfiguration implements UpnpServiceConfiguration {
122123

123124
protected BundleContext context;
124125

125-
@SuppressWarnings("rawtypes")
126-
protected ServiceReference httpServiceReference;
127-
128126
@SuppressWarnings("rawtypes")
129127
protected TransportConfiguration transportConfiguration;
130128

@@ -180,18 +178,13 @@ protected void activate(BundleContext context, Map<String, Object> configProps)
180178

181179
namespace = createNamespace();
182180

183-
logger.debug("{} activated", this);
181+
logger.debug("OSGiUpnpServiceConfiguration {} activated", this);
184182
}
185183

186184
@Deactivate
187185
protected void deactivate() {
188-
if (httpServiceReference != null) {
189-
context.ungetService(httpServiceReference);
190-
}
191-
192186
shutdown();
193-
194-
logger.debug("{} deactivated", this);
187+
logger.debug("OSGiUpnpServiceConfiguration {} deactivated", this);
195188
}
196189

197190
@Override
@@ -240,22 +233,18 @@ public StreamServer createStreamServer(NetworkAddressFactory networkAddressFacto
240233
ServiceReference serviceReference = context.getServiceReference(HttpService.class.getName());
241234

242235
if (serviceReference != null) {
243-
244-
if (httpServiceReference != null) {
245-
context.ungetService(httpServiceReference);
246-
}
247-
248-
httpServiceReference = serviceReference;
249-
250236
HttpService httpService = (HttpService) context.getService(serviceReference);
251237

252238
if (httpService != null) {
239+
logger.debug("createStreamServer using OSGi HttpService (port {})",
240+
httpProxyPort != -1 ? httpProxyPort : callbackURI.getBasePath().getPort());
253241
return new ServletStreamServerImpl(new ServletStreamServerConfigurationImpl(
254242
HttpServiceServletContainerAdapter.getInstance(httpService, context),
255243
httpProxyPort != -1 ? httpProxyPort : callbackURI.getBasePath().getPort()));
256244
}
257245
}
258246

247+
logger.debug("createStreamServer (port {})", networkAddressFactory.getStreamListenPort());
259248
return transportConfiguration.createStreamServer(networkAddressFactory.getStreamListenPort());
260249
}
261250

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
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
5858
*/
@@ -85,6 +85,8 @@ public class UpnpServiceImpl implements UpnpService {
8585

8686
protected volatile ScheduledFuture<?> scheduledFuture;
8787

88+
private boolean OSGiHttpServiceInjected;
89+
8890
public UpnpServiceImpl() {
8991
}
9092

@@ -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+
OSGiHttpServiceInjected = true;
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+
OSGiHttpServiceInjected = false;
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
}
@@ -270,6 +277,7 @@ public void startup() {
270277
// Instantiation order is important: Router needs to start its network services after registry is ready
271278

272279
logger.debug("Using configuration: {}", getConfiguration().getClass().getName());
280+
logger.debug("{} OSGi HttpService", OSGiHttpServiceInjected ? "Using" : "Not using");
273281

274282
this.protocolFactory = createProtocolFactory();
275283
this.registry = createRegistry(protocolFactory);

0 commit comments

Comments
 (0)