/** * Method checks if the given unitRemote is managed by this unit remote * pool. * * @param unitRemote the remote to check. * @return true if the given remote is managed by this pool otherwise false * is returned. * @throws CouldNotPerformException is thrown if the given remote is not * valid. For instance if the given unit remote is not initialized. */ public static boolean contains(final UnitRemote<? extends GeneratedMessage> unitRemote) throws CouldNotPerformException { return unitRemoteRegistry.contains(unitRemote); }
@Override public void shutdown() { try { shutdownInitialized = true; unitRemoteRegistry.getEntries().stream().parallel().forEach(((org.openbase.bco.dal.lib.layer.unit.UnitRemote unitRemote) -> { try { unitRemote.unlock(unitRemoteRegistry); unitRemote.shutdown(); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not properly shutdown " + unitRemote, ex, LOGGER); } })); } catch (Exception ex) { ExceptionPrinter.printHistory("Could not properly shutdown remote pool!", ex, LOGGER); } finally { unitRemoteRegistry.shutdown(); } }
@Override public void update(Observable<UnitRegistryData> source, UnitRegistryData data) throws Exception { UNIT_REMOTE_REGISTRY_LOCK.writeLock().lock(); UNIT_DIFF.diff(Registries.getUnitRegistry().getUnitConfigs()); try { for (String unitId : UNIT_DIFF.getRemovedMessageMap().keySet()) { if (unitRemoteRegistry.contains(unitId)) { removeUnitRemote(unitRemoteRegistry.get(unitId)); } } } finally { UNIT_REMOTE_REGISTRY_LOCK.writeLock().unlock(); } } };
/** * Reset the unit remote pool by shutting down every remote instance and * clearing the local registry. This is needed for unit tests because else * remote states are not cleared between tests which can result in * unpredictable values. This method can only be triggered in test mode. * Else it throws a FatalImplementationErrorException. * * @param responsibleObject should be the object which triggers the reset * which is used for proper exception handling * @throws CouldNotPerformException if reset fails */ public static void reset(final Object responsibleObject) throws CouldNotPerformException { try { if (!JPService.testMode()) { throw new FatalImplementationErrorException("Units can only be reseted in test mode!", responsibleObject); } UNIT_REMOTE_REGISTRY_LOCK.writeLock().lock(); try { for (UnitRemote unitRemote : new ArrayList<>(unitRemoteRegistry.getEntries())) { removeUnitRemote(unitRemote); } } finally { UNIT_REMOTE_REGISTRY_LOCK.writeLock().unlock(); } } catch (CouldNotPerformException ex) { throw new CouldNotPerformException("Could not reset Units!", ex); } }
private static void removeUnitRemote(final UnitRemote unitRemote) { try { unitRemoteRegistry.remove(unitRemote); unitRemote.unlock(unitRemoteRegistry); unitRemote.shutdown(); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not properly shutdown " + unitRemote, ex, LOGGER); } }
/** * Method forces a resynchronization on all unit remotes. * * @throws CouldNotPerformException * @throws InterruptedException */ public static void reinitialize() throws CouldNotPerformException, InterruptedException { UNIT_REMOTE_REGISTRY_LOCK.writeLock().lock(); try { for (UnitRemote unitRemote : unitRemoteRegistry.getEntries()) { try { unitRemote.unlock(unitRemoteRegistry); unitRemote.init(unitRemote.getConfig()); unitRemote.lock(unitRemoteRegistry); unitRemote.requestData().get(500, TimeUnit.MILLISECONDS); } catch (ExecutionException | TimeoutException ex) { throw new CouldNotPerformException("Could not reinitialize Units"); } } } finally { UNIT_REMOTE_REGISTRY_LOCK.writeLock().unlock(); } resetUnitRegistryObserver(); }