@Override public void attributes(final Map<String, ?> newAttributes) { super.attributes(newAttributes); synchronized (ConcurrentServiceWatcher.this) { if (null != currentExport && thisImport.equals(currentImport)) { currentExport.attributes(newAttributes); } // has ranking changed? updateBestService(); } }
@Override @SuppressWarnings("unchecked") public void put(final T newInstance) { super.put(newInstance); synchronized (ConcurrentServiceWatcher.this) { if (null != currentExport && thisImport.equals(currentImport)) { currentExport.put((S) newInstance); // is this being removed? if (null == newInstance) { updateBestService(); } } } }
void updateBestService() { // check the last-known best service is still the best final Iterator<Import<S>> i = services.iterator(); final Import<S> bestImport = i.hasNext() ? i.next() : null; if (null != currentImport && currentImport.equals(bestImport)) { return; // still the same... } // best service has changed if (null != currentExport) { currentExport.unput(); } // report service (if any) currentImport = bestImport; currentExport = null == bestImport ? null : watcher.add(bestImport); }