/** * Sets the context. By default, it also updates the context of already registered services. * * @param context The context. */ public void setContext(Context context) { this.context = context; for (Service service : this) { service.setContext(context); } }
/** * Replaces or adds a service. The replacement is based on the service class. * * @param newService The new service to set. */ public synchronized void set(Service newService) { List<Service> services = new CopyOnWriteArrayList<Service>(); Service service; boolean replaced = false; for (int i = 0; (i < size()); i++) { service = get(i); if (service != null) { if (service.getClass().isAssignableFrom(newService.getClass())) { try { service.stop(); } catch (Exception e) { Context.getCurrentLogger().log(Level.WARNING, "Unable to stop service replaced", e); } services.add(newService); replaced = true; } else { services.add(service); } } } if (!replaced) { services.add(newService); } set(services); }
/** * Returns a service matching a given service class. * * @param <T> The service type. * @param clazz The service class to match. * @return The matched service instance. */ @SuppressWarnings("unchecked") public <T extends Service> T get(Class<T> clazz) { for (Service service : this) { if (clazz.isAssignableFrom(service.getClass())) { return (T) service; } } return null; }
@Override public boolean addAll(int index, Collection<? extends Service> services) { if (services != null) { for (Service service : services) { service.setContext(getContext()); } } return super.addAll(index, services); }
@Override public synchronized void stop() throws Exception { eventBus.unregisterHandler(created); eventBus.unregisterHandler(deleted); eventBus.unregisterHandler(retrieved); eventBus.unregisterHandler(updated); super.stop(); logger.log(Level.INFO, "Not Logging CRUD events"); }
@Override public boolean add(Service service) { service.setContext(getContext()); return super.add(service); }
@Override public void add(int index, Service service) { service.setContext(getContext()); super.add(index, service); }
/** * Stops each service. * * @throws Exception */ public void stop() throws Exception { for (Service service : this) { service.stop(); } }