/** * Adds the given inhabitant to the habitat * @param i */ @Override public void add(Inhabitant<?> i) { logger.log(Level.FINE, "adding inhabitant: {0} to habitat {1}", new Object[] {i, habitat}); habitat.add(i); }
/* package */ void register() { habitat.add(this); String key = getKey(); for (String contract : model.contracts) { habitat.addIndex(this, contract, key); } if (key!=null) { habitat.addIndex(this, model.targetTypeName, key); } }
private void prepareHabitat() { // Bootstrap a hk2 environment. ModulesRegistry registry = new StaticModulesRegistry(getClass().getClassLoader()); habitat = registry.createHabitat("default"); StartupContext startupContext = new StartupContext(); habitat.add(new ExistingSingletonInhabitant(startupContext)); habitat.addComponent(null, new ProcessEnvironment(ProcessEnvironment.ProcessType.Other)); }
add(habitatInh); addIndex(habitatInh, Injector.class.getName(), null); addIndex(habitatInh, Services.class.getName(), null); ExistingSingletonInhabitant<RunLevelService> rlsI = new ExistingSingletonInhabitant<RunLevelService>( RunLevelService.class, rls); add(rlsI); addIndex(rlsI, RunLevelService.class.getName(), DefaultRunLevelService.DEFAULT_NAME);
/* package */ void register() { habitat.add(this); String key = getKey(); for (String contract : model.contracts) { habitat.addIndex(this, contract, key); } if (key!=null) { habitat.addIndex(this, model.targetTypeName, key); } }
add(new ExistingSingletonInhabitant<InjectionResolver>( InjectionResolver.class, injectresolver)); addIndex(new ExistingSingletonInhabitant<InjectionResolver>( add(habitatInh); addIndex(habitatInh, Injector.class.getName(), null); addIndex(habitatInh, Services.class.getName(), null); ExistingSingletonInhabitant<RunLevelService> rlsI = new ExistingSingletonInhabitant<RunLevelService>( RunLevelService.class, rls); add(rlsI); addIndex(rlsI, RunLevelService.class.getName(), DefaultRunLevelService.NAME);
static private Habitat getHabitat() { Habitat habitat = Globals.getStaticHabitat(); StartupContext startupContext = new StartupContext(); habitat.add(new ExistingSingletonInhabitant(startupContext)); habitat.addComponent(null, new ProcessEnvironment(ProcessEnvironment.ProcessType.Other)); return habitat; } }
/** * Adds the given inhabitant to the habitat * @param i */ @Override public void add(Inhabitant<?> i) { logger.log(Level.FINE, "adding inhabitant: {0} to habitat {1}", new Object[] {i, habitat}); habitat.add(i); }
protected void registerIn(Habitat habitat, Inhabitant<T> inhabitant) { List<String> contracts = new ArrayList<String>(); contracts.addAll(metadata.contracts); if (contracts.isEmpty() && (metadata.name==null || metadata.name.isEmpty())) { // there is no name, or contract, register by type habitat.add(inhabitant); } else { if (contracts.isEmpty()) { // if no contract is present, but there is a name, the type itself is the contract. contracts.add(inhabitant.type().getName()); } for (String contract : contracts) { habitat.addIndex(inhabitant, contract, metadata.name); } // finally, register by type habitat.add(inhabitant); } }
/** * Add an already instantiated component to this manager. The component has * been instantiated by external code, however dependency injection, * PostConstruct invocation and dependency extraction will be performed on * this instance before it is store in the relevant scope's resource * manager. * * @param component component instance * @throws ComponentException if the passed object is not an HK2 component or * injection/extraction failed. */ // TODO: mutating Habitat after it's created poses synchronization issue public <T> void addComponent(T component) throws ComponentException { add(new ExistingSingletonInhabitant<T>(component)); }
protected void registerIn(Habitat habitat, Inhabitant<?> inhabitant) { List<String> contracts = new ArrayList<String>(); contracts.addAll(metadata.contracts.keySet()); if (contracts.isEmpty() && (metadata.name == null || metadata.name.isEmpty())) { // there is no name, or contract, register by type habitat.add(inhabitant); } else { if (contracts.isEmpty()) { // if no contract is present, but there is a name, the type itself is the contract. contracts.add(inhabitant.type().getName()); } for (String contract : contracts) { habitat.addIndex(inhabitant, contract, metadata.name); } // finally, register by type habitat.add(inhabitant); } }
/** * Add an already instantiated component to this manager. The component has * been instantiated by external code, however dependency injection, * PostConstruct invocation and dependency extraction will be performed on * this instance before it is store in the relevant scope's resource * manager. * * @param component component instance * @throws ComponentException if the passed object is not an HK2 component or * injection/extraction failed. */ // TODO: mutating Habitat after it's created poses synchronization issue public <T> void addComponent(T component) throws ComponentException { add(new ExistingSingletonInhabitant<T>(component)); }
public Object addingService(final ServiceReference reference) { final Object object = ctx.getService(reference); // let's get the list of implemented contracts String[] contractNames = (String[]) reference.getProperty("objectclass"); if (contractNames != null && contractNames.length > 0) { // we will register this service under each contract it implements for (String contractName : contractNames) { String name = (String) reference.getProperty("component.name"); if (name == null) { // let's get a name if possible, that will only work with Spring OSGi services // we may need to find a better way to get a potential name. name = (String) reference.getProperty("org.springframework.osgi.bean.name"); } habitat.addIndex(new ExistingSingletonInhabitant(object), contractName, name); logger.logp(Level.FINE, "HK2Main$HK2ServiceTrackerCustomizer", "addingService", "registering service = {0}, contract = {1}, name = {2}", new Object[]{ object, contractName, name}); } } else { // this service does not implement a specific contract, let's register it by its type. habitat.add(new ExistingSingletonInhabitant(object)); logger.logp(Level.FINE, "HK2Main$HK2ServiceTrackerCustomizer", "addingService", "registering service = {0}", object); } return object; }
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; } }
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; } }
protected void addHabitatListener(HabitatListener listener, Set<String> typeNames) { if (null == listener) { throw new IllegalArgumentException(); } ExistingSingletonInhabitant<HabitatListener> inhabitant = new ExistingSingletonInhabitant<HabitatListener>( HabitatListener.class, listener, metaData(typeNames)); add(inhabitant); // track listeners by type - support of filtered notification for (String contract : typeNames) { ListenersByTypeInhabitant sameListeners; synchronized (byContract) { sameListeners = (ListenersByTypeInhabitant) getInhabitantByContract( ListenersByTypeInhabitant.class.getName(), contract); if (null == sameListeners) { sameListeners = new ListenersByTypeInhabitant(contract); addIndex(sameListeners, ListenersByTypeInhabitant.class.getName(), contract, false); // don't send notifications for // this type (no particular // reason really) } } sameListeners.add(listener); } }
protected void addHabitatListener(HabitatListener listener, Set<String> typeNames) { ExistingSingletonInhabitant<HabitatListener> inhabitant = new ExistingSingletonInhabitant<HabitatListener>( HabitatListener.class, listener, metaData(typeNames)); add(inhabitant); // track listeners by type - support of filtered notification for (String contract : typeNames) { ListenersByTypeInhabitant sameListeners; synchronized (byContract) { sameListeners = (ListenersByTypeInhabitant) getInhabitantByContract( ListenersByTypeInhabitant.class.getName(), contract); if (null == sameListeners) { sameListeners = new ListenersByTypeInhabitant(contract); addIndex(sameListeners, ListenersByTypeInhabitant.class.getName(), contract, false); // don't send notifications for // this type (no particular // reason really) } } sameListeners.add(listener); } }
habitat.add(Inhabitants.create(this)); fileSystem = new ExistingSingletonInhabitant<EmbeddedFileSystem>(fs); habitat.addIndex(fileSystem, EmbeddedFileSystem.class.getName(), null);