public static <T> T autoSet(final T instance) throws Exception { return autoSet(null, instance); }
public static <T> T autoSet(final String key, final T instance) throws Exception { return autoSet(key, instance, instance.getClass()); }
private static <T> T internalProcessInstance(final T instance) throws Exception { final Class<?> loadedClass = instance.getClass(); // autoset before invoking @Created if (loadedClass.getAnnotation(AutoSet.class) != null) { autoSet(null, instance, loadedClass); } Class<?> clazz = loadedClass; while (clazz != null && !Object.class.equals(clazz)) { for (final Method m : clazz.getDeclaredMethods()) { if (m.getAnnotation(Created.class) != null) { m.setAccessible(true); m.invoke(instance); } else if (m.getAnnotation(Destroying.class) != null) { m.setAccessible(true); if (shutdownHook == null == Configuration.is(Configuration.CONFIG_PROPERTY_PREFIX + "shutdown.hook", true)) { shutdownHook = new Thread() { @Override public void run() { shutdown(); } }; Runtime.getRuntime().addShutdownHook(shutdownHook); } INSTANCES.add(new ToDestroy(m, instance)); } } clazz = clazz.getSuperclass(); } return instance; }
protected static Collection<AlertListener> findAlerters() { final Collection<AlertListener> listeners = new ArrayList<AlertListener>(); final String alerters = Configuration.getProperty(Configuration.CONFIG_PROPERTY_PREFIX + "alerters", null); if (alerters != null && alerters.trim().length() > 0) { for (final String alert : alerters.split(" *, *")) { final String classKey = alert + ".class"; final String type = Configuration.getProperty(classKey, null); if (type == null) { throw new IllegalArgumentException("Missing configuration " + classKey); } try { final Class<?> clazz = ClassLoaders.current().loadClass(type); final AlertListener listener = IoCs.autoSet(alert, AlertListener.class.cast(clazz.newInstance())); listeners.add(listener); } catch (final Exception e) { throw new IllegalArgumentException(e); } } } return listeners; }