private Dispatcher() { IConfigurationElement[] config = RegistryFactory.getRegistry().getConfigurationElementsFor(DISPATCH_FILTER_ID); for (IConfigurationElement element : config) { try { boolean isFilter = "filter".equals(element.getName()); // $NON-NLS-1$ if (isFilter) { DispatchFilter filter = new DispatchFilter(element); filters.putSingle(filter.getPriority(), filter); } } catch (Exception e) { logger.error("Error processing dispatch filter", e); // $NON-NLS-1$ } } }
/** * Dispatch the given object to a dispatch handler if a matching filter can be found for the * object's class. The dispatch handler is instantiated using the {@link ContextInjectionFactory}, * which injects the handler using the provided context. * * @param object Object to dispatch * @param intent Intent that loaded the dispatched object, <code>null</code> if object is not the * result of an intent * @param context Context to use for injection into the handler * @return True if a dispatch handler was found to handle the object */ public boolean dispatch(Object object, Intent intent, IEclipseContext context) { if (object == null) { return false; } DispatchFilter filter = findFilter(object); Class<? extends IDispatchHandler> handlerClass = filter == null ? null : filter.getHandler(); if (handlerClass == null) { logger.error("Could not find dispatch handler for object: " + object); // $NON-NLS-1$ return false; } IEclipseContext activeLeaf = context.getActiveLeaf(); IEclipseContext child = activeLeaf.createChild(); IDispatchHandler handler = ContextInjectionFactoryThreadSafe.make(handlerClass, child); handler.handle(object, intent); return true; }
/** * Find the best dispatch filter for the given object's class. * * @param object Object to find a filter for * @return Dispatch filter for the object's class, or null if no matching filter could be found */ public DispatchFilter findFilter(Object object) { if (object == null) { return null; } int minDistance = Integer.MAX_VALUE; DispatchFilter closest = null; for (List<DispatchFilter> list : filters.values()) { for (DispatchFilter filter : list) { int distance = distanceToClosestType(object.getClass(), filter.getTypes()); if (distance >= 0 && distance < minDistance) { minDistance = distance; closest = filter; } } } return closest; }