Skip to content

Commit

Permalink
Fix bug in migrateThingType to not call managed thing provider direct…
Browse files Browse the repository at this point in the history
…ly (#3162)

The `migrateThingType` call the managed thing provider directly instead of checking if it's actually a managed thing.
This check is done in the `thingUpdated` method. Changed the code so it makes the same call as in `thingUpdated`.

Also-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
  • Loading branch information
Hilbrand authored Dec 10, 2022
1 parent 9c3e2f3 commit 207c4a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public interface ThingHandlerCallback {
* Informs about an update of the whole thing.
*
* @param thing thing that was updated (must not be null)
* @throws IllegalStateException if the {@link Thing} is read-only.
* @throws IllegalStateException if the {@link Thing} is can't be found
*/
void thingUpdated(Thing thing);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,7 @@ private void handleBridgeChildStatusUpdate(Thing thing, ThingStatusInfo oldStatu

@Override
public void thingUpdated(final Thing thing) {
thingUpdatedLock.add(thing.getUID());
AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> {
Provider<Thing> provider = thingRegistry.getProvider(thing);
if (provider == null) {
throw new IllegalArgumentException(MessageFormat.format(
"Provider for thing {0} cannot be determined because it is not known to the registry",
thing.getUID().getAsString()));
}
if (provider instanceof ManagedProvider) {
@SuppressWarnings("unchecked")
ManagedProvider<Thing, ThingUID> managedProvider = (ManagedProvider<Thing, ThingUID>) provider;
managedProvider.update(thing);
} else {
logger.debug("Only updating thing {} in the registry because provider {} is not managed.",
thing.getUID().getAsString(), provider);
Thing oldThing = thingRegistry.get(thing.getUID());
if (oldThing == null) {
throw new IllegalArgumentException(
MessageFormat.format("Cannot update thing {0} because it is not known to the registry",
thing.getUID().getAsString()));
}
thingRegistry.updated(provider, oldThing, thing);
}
return null;
});
thingUpdatedLock.remove(thing.getUID());
ThingManagerImpl.this.thingUpdated(thing);
}

@Override
Expand Down Expand Up @@ -472,13 +447,40 @@ protected synchronized void deactivate(ComponentContext componentContext) {
readyService.unregisterTracker(this);
}

private void thingUpdated(final Thing thing) {
thingUpdatedLock.add(thing.getUID());
final Thing oldThing = thingRegistry.get(thing.getUID());
if (oldThing == null) {
throw new IllegalArgumentException(MessageFormat.format(
"Cannot update thing {0} because it is not known to the registry", thing.getUID().getAsString()));
}
AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> {
final Provider<Thing> provider = thingRegistry.getProvider(thing);
if (provider == null) {
throw new IllegalArgumentException(MessageFormat.format(
"Provider for thing {0} cannot be determined because it is not known to the registry",
thing.getUID().getAsString()));
}
if (provider instanceof ManagedProvider) {
final ManagedProvider<Thing, ThingUID> managedProvider = (ManagedProvider<Thing, ThingUID>) provider;
managedProvider.update(thing);
} else {
logger.debug("Only updating thing {} in the registry because provider {} is not managed.",
thing.getUID().getAsString(), provider);
thingRegistry.updated(provider, oldThing, thing);
}
return null;
});
thingUpdatedLock.remove(thing.getUID());
}

@Override
public void migrateThingType(final Thing thing, final ThingTypeUID thingTypeUID,
final @Nullable Configuration configuration) {
final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
if (thingType == null) {
throw new IllegalStateException(
MessageFormat.format("No thing type {0} registered, cannot change thing type for thing {1}",
MessageFormat.format("No thing type {0} registered, cannot change thing type for thing {1}",
thingTypeUID.getAsString(), thing.getUID().getAsString()));
}
scheduler.schedule(new Runnable() {
Expand Down Expand Up @@ -519,11 +521,15 @@ public void run() {
thing.setProperty(entry.getKey(), entry.getValue());
}

// Change the ThingType
// set the new ThingTypeUID
((ThingImpl) thing).setThingTypeUID(thingTypeUID);

// Register the new Handler - ThingManager.updateThing() is going to take care of that
thingRegistry.update(thing);
// update the thing and register a new handler
thingUpdated(thing);
final ThingHandlerFactory newThingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
if (newThingHandlerFactory != null) {
registerAndInitializeHandler(thing, newThingHandlerFactory);
}

ThingHandler handler = thing.getHandler();
logger.debug("Changed ThingType of Thing {} to {}. New ThingHandler is {}.", thingUID,
Expand Down

0 comments on commit 207c4a0

Please sign in to comment.