Skip to content

Commit

Permalink
Removed access to 'BundleContext' from 'BaseThingHandler' (openhab#1214)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored and kaikreuzer committed Nov 17, 2019
1 parent 0abf994 commit 931b200
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,9 +66,6 @@ public abstract class BaseThingHandler implements ThingHandler {
protected final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(THING_HANDLER_THREADPOOL_NAME);

@Deprecated // this must not be used by bindings!
protected @NonNullByDefault({}) BundleContext bundleContext;

protected Thing thing;

private @Nullable ThingHandlerCallback callback;
Expand All @@ -83,14 +79,6 @@ public BaseThingHandler(Thing thing) {
this.thing = thing;
}

public void setBundleContext(final BundleContext bundleContext) {
this.bundleContext = bundleContext;
}

public void unsetBundleContext(final BundleContext bundleContext) {
this.bundleContext = null;
}

@Override
public void handleRemoval() {
// can be overridden by subclasses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public abstract class BaseThingHandlerFactory implements ThingHandlerFactory {
* @param componentContext component context (must not be null)
*/
protected void activate(ComponentContext componentContext) {
this.bundleContext = componentContext.getBundleContext();
bundleContext = componentContext.getBundleContext();
thingTypeRegistryServiceTracker = new ServiceTracker<>(bundleContext, ThingTypeRegistry.class.getName(), null);
thingTypeRegistryServiceTracker.open();
configDescriptionRegistryServiceTracker = new ServiceTracker<>(bundleContext,
Expand Down Expand Up @@ -132,7 +132,6 @@ public ThingHandler registerHandler(Thing thing) {
throw new IllegalStateException(
"Created handler of bridge '" + thing.getUID() + "' must implement the BridgeHandler interface.");
}
setHandlerContext(thingHandler);
registerConfigStatusProvider(thing, thingHandler);
registerFirmwareUpdateHandler(thing, thingHandler);
registerServices(thing, thingHandler);
Expand Down Expand Up @@ -174,7 +173,7 @@ private void registerServices(Thing thing, ThingHandler thingHandler) {
if (!serviceNames.isEmpty()) {
String[] serviceNamesArray = serviceNames.toArray(new String[serviceNames.size()]);

ServiceRegistration<?> serviceReg = this.bundleContext.registerService(serviceNamesArray,
ServiceRegistration<?> serviceReg = bundleContext.registerService(serviceNamesArray,
serviceInstance, null);

if (serviceReg != null) {
Expand Down Expand Up @@ -219,16 +218,6 @@ private void unregisterServices(Thing thing) {
*/
protected abstract @Nullable ThingHandler createHandler(Thing thing);

private void setHandlerContext(ThingHandler thingHandler) {
if (thingHandler instanceof BaseThingHandler) {
if (bundleContext == null) {
throw new IllegalStateException(
"Base thing handler factory has not been properly initialized. Did you forget to call super.activate()?");
}
((BaseThingHandler) thingHandler).setBundleContext(bundleContext);
}
}

private void registerConfigStatusProvider(Thing thing, ThingHandler thingHandler) {
if (thingHandler instanceof ConfigStatusProvider) {
ServiceRegistration<ConfigStatusProvider> serviceRegistration = registerAsService(thingHandler,
Expand Down Expand Up @@ -257,7 +246,6 @@ public void unregisterHandler(Thing thing) {
ThingHandler thingHandler = thing.getHandler();
if (thingHandler != null) {
removeHandler(thingHandler);
unsetBundleContext(thingHandler);
}
unregisterConfigStatusProvider(thing);
unregisterFirmwareUpdateHandler(thing);
Expand All @@ -275,12 +263,6 @@ protected void removeHandler(ThingHandler thingHandler) {
// can be overridden
}

private void unsetBundleContext(ThingHandler thingHandler) {
if (thingHandler instanceof BaseThingHandler) {
((BaseThingHandler) thingHandler).unsetBundleContext(bundleContext);
}
}

private void unregisterConfigStatusProvider(Thing thing) {
ServiceRegistration<ConfigStatusProvider> serviceRegistration = configStatusProviders
.remove(thing.getUID().getAsString());
Expand Down

0 comments on commit 931b200

Please sign in to comment.