public synchronized Object getTarget() throws IllegalStateException { if (service == null) { // The service was released before, try to reaquire it... LOGGER.debug("Try to reaquire service..."); Object reaquiredService = bundleContext.getService(reference); if (reaquiredService != null) { // Successfull reaquired! LOGGER.debug("reaquire service was successfull"); service = reaquiredService; } else { LOGGER.debug( "reaquire service was not successfull, try to relocate to a different service..."); // Try to find a new one... ReleasableProxyTarget newProxyTarget = locateProxyTarget(); // If we are here a new ProxyTarget was bound delegatingProxy = newProxyTarget; // Fetch the target from the delegate service = newProxyTarget.getTarget(); } } return service; }
public synchronized ProxyTarget releaseTarget() { // When releasing, we unget the service so we don't keep stale references... service = null; if (delegatingProxy != null) { // return the delegate proxy for further usage... return delegatingProxy.releaseTarget(); } else { try { bundleContext.ungetService(reference); } catch (RuntimeException e) { // Sometimes a RuntimeException might occur here, we catch it to not prevent any other // cleanup actions LOGGER.trace("RuntimeException while ungetting service", e); } return this; } }