@Override public boolean matches(Object key) { for (Selector s: selectors) { if (!s.matches(key)) { return false; } } return true; }
@Override @SuppressWarnings("unchecked") public HttpMappings add(Predicate<? super HttpChannel> condition, Function<? super HttpChannel, ? extends Publisher<Void>> handler) { Selector<HttpChannel> selector; if(Selector.class.isAssignableFrom(condition.getClass())) { selector = (Selector<HttpChannel>) condition; } else{ selector = Selectors.predicate(condition); } routedWriters.register(selector, new HttpHandlerMapping(condition, handler, selector.getHeaderResolver())); return this; }
/** * Register the given {@link reactor.fn.Consumer} on an anonymous {@link reactor.bus.selector.Selector} and * set the given event's {@code replyTo} property to the corresponding anonymous key, then register the consumer to * receive replies from the {@link reactor.fn.Function} assigned to handle the given key. * * @param key The key to be matched by {@link Selector Selectors} * @param event The event to notify. * @param reply The consumer to register as a reply handler. * @return {@literal this} */ public <T extends Event<?>> EventBus sendAndReceive(Object key, Event<?> event, Consumer<T> reply) { Selector sel = Selectors.anonymous(); on(sel, reply).cancelAfterUse(); notify(key, event.setReplyTo(sel.getObject())); return this; }
@Override public void accept(T e) { if (null != selector.getHeaderResolver()) { e.getHeaders().setAll(selector.getHeaderResolver().resolve(e.getKey())); } if (tClass == null || e.getData() == null || tClass.isAssignableFrom(e.getData().getClass())) { consumer.accept(e); } } };
@Override public void value(final MutableList<Registration<K, ? extends V>> regs) { Iterator<Registration<K, ? extends V>> registrationIterator = regs.iterator(); Registration<K, ? extends V> reg; while (registrationIterator.hasNext()) { reg = registrationIterator.next(); if (reg.getSelector().matches(key)) { registrationIterator.remove(); modified.compareAndSet(false, true); } } if (useCache && modified.get()) { threadLocalCache.clear(); } } });
@Override @SuppressWarnings("unchecked") public synchronized boolean unregister(Object key) { boolean found = false; for (Selector sel : registrations.keySet()) { if (!sel.matches(key)) { continue; } if (null != registrations.remove(sel) && !found) { found = true; } } if (useCache) cache.remove(key); return found; }
@Override public List<Registration<K, ? extends V>> select(K key) { // use a thread-local cache UnifiedMap<Object, List<Registration<K, ? extends V>>> allRegs = threadLocalRegs(); // maybe pull Registrations from cache for this key List<Registration<K, ? extends V>> selectedRegs = null; if (useCache && (null != (selectedRegs = allRegs.get(key)))) { return selectedRegs; } // cache not used or cache miss cacheMiss(key); selectedRegs = FastList.newList(); // find Registrations based on Selector for (Registration<K, ? extends V> reg : this) { if (reg.getSelector().matches(key)) { selectedRegs.add(reg); } } if (useCache && (!selectedRegs.isEmpty() || cacheNotFound)) { allRegs.put(key, selectedRegs); } // nothing found, maybe invoke handler if (selectedRegs.isEmpty() && (null != onNotFound)) { onNotFound.accept(key); } // return return selectedRegs; }