harden against missing bundle symbolic name (#510)

Fixes: https://github.com/openhab/openhab-core/issues/508

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
This commit is contained in:
Markus Rathgeb 2019-02-01 13:53:38 +00:00 committed by Kai Kreuzer
parent 68c8db8227
commit 7a1982e785
3 changed files with 47 additions and 27 deletions

View File

@ -422,19 +422,23 @@ public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
}
private void registerReadyMarker(Bundle bundle) {
String bsn = bundle.getSymbolicName();
if (!bundleReadyMarkerRegistrations.containsKey(bsn)) {
ReadyMarker readyMarker = new ReadyMarker(readyMarkerKey, bsn);
readyService.markReady(readyMarker);
bundleReadyMarkerRegistrations.put(bsn, readyMarker);
final String bsn = bundle.getSymbolicName();
if (bsn != null) {
if (!bundleReadyMarkerRegistrations.containsKey(bsn)) {
ReadyMarker readyMarker = new ReadyMarker(readyMarkerKey, bsn);
readyService.markReady(readyMarker);
bundleReadyMarkerRegistrations.put(bsn, readyMarker);
}
}
}
private void unregisterReadyMarker(Bundle bundle) {
String bsn = bundle.getSymbolicName();
ReadyMarker readyMarker = bundleReadyMarkerRegistrations.remove(bsn);
if (readyMarker != null) {
readyService.unmarkReady(readyMarker);
final String bsn = bundle.getSymbolicName();
if (bsn != null) {
ReadyMarker readyMarker = bundleReadyMarkerRegistrations.remove(bsn);
if (readyMarker != null) {
readyService.unmarkReady(readyMarker);
}
}
}

View File

@ -248,8 +248,11 @@ public class SyntheticBundleInstaller {
private static boolean isBundleAvailable(BundleContext context, String bsn) {
for (Bundle bundle : context.getBundles()) {
if (bundle.getSymbolicName().equals(bsn) && bundle.getState() == Bundle.ACTIVE) {
return true;
final String bsnCurrentBundle = bundle.getSymbolicName();
if (bsnCurrentBundle != null) {
if (bsnCurrentBundle.equals(bsn) && bundle.getState() == Bundle.ACTIVE) {
return true;
}
}
}
return false;
@ -288,14 +291,17 @@ public class SyntheticBundleInstaller {
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
return;
}
final String bsn = bundle.getSymbolicName();
if (bsn == null) {
return;
}
long startTime = System.nanoTime();
ServiceReference<?> readyServiceRef = context.getServiceReference(ReadyService.class.getName());
ReadyService readyService = (ReadyService) context.getService(readyServiceRef);
ReadyMarker expected = new ReadyMarker(marker, bundle.getSymbolicName());
ReadyMarker expected = new ReadyMarker(marker, bsn);
while (!readyService.isReady(expected)) {
if (System.nanoTime() - startTime > TimeUnit.SECONDS.toNanos(WAIT_TIMOUT)) {
Assert.fail(MessageFormat.format("Timout waiting for marker {0} at bundle {1}", marker,
bundle.getSymbolicName()));
Assert.fail(MessageFormat.format("Timout waiting for marker {0} at bundle {1}", marker, bsn));
}
try {
Thread.sleep(100);

View File

@ -34,6 +34,7 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.ConfigDescriptionRegistry;
@ -1008,7 +1009,10 @@ public class ThingManagerImpl
protected synchronized void activate(ComponentContext componentContext) {
readyService.registerTracker(this, new ReadyMarkerFilter().withType(XML_THING_TYPE));
for (ThingHandlerFactory factory : thingHandlerFactories) {
handleThingHandlerFactoryAddition(getBundleName(factory));
final String bsn = getBundleName(factory);
if (bsn != null) {
handleThingHandlerFactoryAddition(bsn);
}
}
thingRegistry.addThingTracker(this);
active = true;
@ -1019,7 +1023,10 @@ public class ThingManagerImpl
logger.debug("Thing handler factory '{}' added", thingHandlerFactory.getClass().getSimpleName());
thingHandlerFactories.add(thingHandlerFactory);
if (active) {
handleThingHandlerFactoryAddition(getBundleName(thingHandlerFactory));
final String bsn = getBundleName(thingHandlerFactory);
if (bsn != null) {
handleThingHandlerFactoryAddition(getBundleName(thingHandlerFactory));
}
}
}
@ -1047,7 +1054,7 @@ public class ThingManagerImpl
private void handleThingHandlerFactoryAddition(String bsn) {
thingHandlerFactories.stream().filter(it -> {
return getBundleName(it).equals(bsn);
return bsn.equals(getBundleName(it));
}).forEach(thingHandlerFactory -> {
things.forEach(thing -> {
if (thingHandlerFactory.supportsThingType(thing.getThingTypeUID())) {
@ -1061,20 +1068,22 @@ public class ThingManagerImpl
});
}
private String getBundleName(ThingHandlerFactory thingHandlerFactory) {
private @Nullable String getBundleName(ThingHandlerFactory thingHandlerFactory) {
return bundleResolver.resolveBundle(thingHandlerFactory.getClass()).getSymbolicName();
}
private void registerAndInitializeHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
if (thingHandlerFactory != null) {
String bsn = getBundleName(thingHandlerFactory);
if (loadedXmlThingTypes.contains(bsn)) {
registerHandler(thing, thingHandlerFactory);
initializeHandler(thing);
} else {
logger.debug(
"Not registering a handler at this point. The thing types of bundle {} are not fully loaded yet.",
bsn);
final String bsn = getBundleName(thingHandlerFactory);
if (bsn != null) {
if (loadedXmlThingTypes.contains(bsn)) {
registerHandler(thing, thingHandlerFactory);
initializeHandler(thing);
} else {
logger.debug(
"Not registering a handler at this point. The thing types of bundle {} are not fully loaded yet.",
bsn);
}
}
} else {
logger.debug("Not registering a handler at this point. No handler factory for thing '{}' found.",
@ -1266,7 +1275,8 @@ public class ThingManagerImpl
private void persistThingEnableStatus(ThingUID thingUID, boolean enabled) {
if (storage == null) {
logger.debug("Cannot persist enable status of thing with UID {}. Persistent storage unavailable.", thingUID);
logger.debug("Cannot persist enable status of thing with UID {}. Persistent storage unavailable.",
thingUID);
return;
}