public boolean inhabitantIndexChanged(EventType eventType, Habitat habitat, Inhabitant<?> i, String index, String name, Object service) { // for each FactoryFor component, insert inhabitant for components // created by the factory if (index.equals(FactoryFor.class.getName())) { FactoryFor ff = i.type().getAnnotation(FactoryFor.class); try { Class<?> targetClasses[] = ff.value(); if (null != targetClasses && targetClasses.length > 0) { for (Class<?> altClass : targetClasses) { FactoryCreator target = new FactoryCreator( altClass, i, habitat, MultiMap.<String, String>emptyMap()); habitat.add(target); habitat.addIndex(target, altClass.getName(), null); } } } catch (AnnotationTypeMismatchException e) { logger.log(Level.WARNING, "annotation error", e); } } // System.out.println("Habitat Index Changed: " + eventType + "; " + // i + "; index=" + index + "; name=" + name); return true; } }
/** * HK2 should do this for me. */ @Service @FactoryFor(OsInterface.class) public class OsInterfaceFactory implements Factory { @Inject Habitat habitat; @Override public Object get() throws ComponentException { Inhabitant<OsInterface> inh = habitat.getInhabitant(OsInterface.class, System.getProperty("os.name").replaceAll(" ", "_")); if (inh==null) { inh = habitat.getInhabitant(OsInterface.class, "ubuntu"); } if (inh!=null) { OsInterface os = null; try { os = inh.type().newInstance(); return habitat.inject(os); } catch (InstantiationException e) { RuntimeContext.logger.log(Level.SEVERE, "Cannot instantiate OsInterface implementation", e); } catch (IllegalAccessException e) { RuntimeContext.logger.log(Level.SEVERE, "Cannot instantiate OsInterface implementation", e); } } return null; } }
public boolean inhabitantIndexChanged(EventType eventType, Habitat habitat, Inhabitant<?> i, String index, String name, Object service) { // for each FactoryFor component, insert inhabitant for components // created by the factory if (index.equals(FactoryFor.class.getName())) { FactoryFor ff = i.type().getAnnotation(FactoryFor.class); try { Class<?> targetClasses[] = ff.value(); if (null != targetClasses && targetClasses.length > 0) { for (Class<?> altClass : targetClasses) { FactoryCreator target = new FactoryCreator( altClass, i, habitat, MultiMap.<String, String>emptyMap()); habitat.add(target); habitat.addIndex(target, altClass.getName(), null); } } } catch (AnnotationTypeMismatchException e) { logger.log(Level.WARNING, "annotation error", e); } } // System.out.println("Habitat Index Changed: " + eventType + "; " + // i + "; index=" + index + "; name=" + name); return true; } }