org.jvnet.hk2.component
Class Habitat

java.lang.Object
  extended by org.jvnet.hk2.component.Habitat

public class Habitat
extends Object

A set of templates that constitute a world of objects.


Field Summary
 String DEFAULT_NAME
          Name to use to progammatically store default instances of a particular service.
 ScopeInstance singletonScope
           
 
Constructor Summary
Habitat()
           
 
Method Summary
 void add(Inhabitant<?> i)
          Adds a new inhabitant.
<T> void
addComponent(String name, T component)
          Add an already instantiated component to this manager.
 void addIndex(Inhabitant<?> i, String index, String name)
          Adds a new index to look up the given inhabitant.
<T> Collection<T>
getAllByContract(Class<T> contractType)
          Gets all the inhabitants registered under the given Contract.
<T> Collection<T>
getAllByType(Class<T> implType)
          Gets the object of the given type.
 Iterator<String> getAllContracts()
           
 Iterator<String> getAllTypes()
           
<T> T
getByContract(Class<T> contractType)
          Gets the object that has the given contract.
<T> T
getByType(Class<T> implType)
          Gets the object of the given type.
<T> T
getComponent(Class<T> clazz)
          Obtains a reference to the component inside the manager.
<T> T
getComponent(Class<T> contract, String name)
          Loads a component that implements the given contract and has the given name.
 Object getComponent(String fullQualifiedName, String name)
           
<T> Inhabitant<? extends T>
getInhabitant(Class<T> contract, String name)
          Gets a lazy reference to the component.
 Inhabitant<?> getInhabitantByAnnotation(Class<? extends Annotation> contract, String name)
          Gets the inhabitant that has the given contract annotation and the given name.
 Inhabitant getInhabitantByContract(String fullyQualifiedName, String name)
           
<T> Inhabitant<T>
getInhabitantByType(Class<T> implType)
          Gets a lazy reference to the component.
 Inhabitant<?> getInhabitantByType(String fullyQualifiedClassName)
           
<T> Collection<Inhabitant<? extends T>>
getInhabitants(Class<T> contract)
          Gets all the inhabitants that has the given contract.
<T> Iterable<Inhabitant<? extends T>>
getInhabitants(Class<T> contract, String name)
          Gets all the inhabitants that has the given contract and the given name
 Iterable<Inhabitant<?>> getInhabitantsByAnnotation(Class<? extends Annotation> contract, String name)
          Gets all the inhabitants that has the given contract annotation and the given name.
 Collection<Inhabitant<?>> getInhabitantsByContract(String fullyQualifiedClassName)
           
<T> Collection<Inhabitant<T>>
getInhabitantsByType(Class<T> implType)
          Gets all the inhabitants that has the given implementation type.
 Collection<Inhabitant<?>> getInhabitantsByType(String fullyQualifiedClassName)
          Gets all the inhabitants that has the given implementation type name.
 boolean isContract(Class<?> type)
          Checks if the given type is a contract interface that has some implementations in this Habitat.
 boolean isContract(String fullyQualifiedClassName)
           
 void release()
          Releases all the components.
 boolean remove(Inhabitant<?> inhabitant)
          Removes a inhabitant
 void removeAllByType(Class<?> type)
          Removes all imhabitants for a particular type
 boolean removeIndex(String index, Object service)
          Removes a Contracted service
 boolean removeIndex(String index, String name)
          Removes a NamedInhabitant for a specific contract
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NAME

public final String DEFAULT_NAME
Name to use to progammatically store default instances of a particular service.

See Also:
Constant Field Values

singletonScope

public final ScopeInstance singletonScope
Constructor Detail

Habitat

public Habitat()
Method Detail

removeAllByType

public void removeAllByType(Class<?> type)
Removes all imhabitants for a particular type

Parameters:
type - of the compoment

add

public void add(Inhabitant<?> i)
Adds a new inhabitant.

See Inhabitants for typical ways to create Inhabitants.


addIndex

public void addIndex(Inhabitant<?> i,
                     String index,
                     String name)
Adds a new index to look up the given inhabitant.

Parameters:
index - Primary index name, such as contract FQCN.
name - Name that identifies the inhabitant among other inhabitants in the same index. Can be null for unnamed inhabitants.

remove

public boolean remove(Inhabitant<?> inhabitant)
Removes a inhabitant

Parameters:
inhabitant - inhabitant to be removed

removeIndex

public boolean removeIndex(String index,
                           String name)
Removes a NamedInhabitant for a specific contract

Parameters:
index - contract name
name - instance name
Returns:
true if the removal was successful

removeIndex

public boolean removeIndex(String index,
                           Object service)
Removes a Contracted service

Parameters:
index - the contract name
service - the instance

isContract

public boolean isContract(Class<?> type)
Checks if the given type is a contract interface that has some implementations in this Habitat.

There are two ways for a type to be marked as a contract. Either it has Contract, or it's marked by ContractProvided from the implementation.

Note that just having Contract is not enough to make this method return true. It can still return false if the contract has no implementation in this habitat.

This method is useful during the injection to determine what lookup to perform, and it handles the case correctly when the type is marked as a contract by ContractProvided.


isContract

public boolean isContract(String fullyQualifiedClassName)

getAllByContract

public <T> Collection<T> getAllByContract(Class<T> contractType)
Gets all the inhabitants registered under the given Contract. This is an example of heterogeneous type-safe container.

Returns:
can be empty but never null.

getAllByType

public <T> Collection<T> getAllByType(Class<T> implType)
Gets the object of the given type.

Returns:
can be empty but never null.

addComponent

public <T> void addComponent(String name,
                             T component)
                  throws ComponentException
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.

Parameters:
name - name of the component, could be default name
component - component instance
Throws:
ComponentException - if the passed object is not an HK2 component or injection/extraction failed.

getComponent

public <T> T getComponent(Class<T> clazz)
               throws ComponentException
Obtains a reference to the component inside the manager.

This is the "new Foo()" equivalent in the IoC world.

Depending on the Scope of the component, a new instance might be created, or an existing instance might be returned.

Returns:
non-null.
Throws:
ComponentException - If failed to obtain a requested instance. In practice, failure only happens when we try to create a new instance of the component.

getComponent

public <T> T getComponent(Class<T> contract,
                          String name)
               throws ComponentException
Loads a component that implements the given contract and has the given name.

Parameters:
name - can be null, in which case it'll only match to the unnamed component.
Returns:
null if no such servce exists.
Throws:
ComponentException

getComponent

public Object getComponent(String fullQualifiedName,
                           String name)

getInhabitant

public <T> Inhabitant<? extends T> getInhabitant(Class<T> contract,
                                                 String name)
                                      throws ComponentException
Gets a lazy reference to the component.

This method defers the actual instantiation of the component until Inhabitant#get() is invoked.

Returns:
null if no such component is found.
Throws:
ComponentException

getInhabitantByType

public <T> Inhabitant<T> getInhabitantByType(Class<T> implType)
Gets a lazy reference to the component.

This method defers the actual instantiation of the component until Inhabitant#get() is invoked.

Returns:
null if no such component is found.

getInhabitantByType

public Inhabitant<?> getInhabitantByType(String fullyQualifiedClassName)

getInhabitantByAnnotation

public Inhabitant<?> getInhabitantByAnnotation(Class<? extends Annotation> contract,
                                               String name)
                                        throws ComponentException
Gets the inhabitant that has the given contract annotation and the given name.

This method defers the actual instantiation of the component until Inhabitant#get() is invoked.

Returns:
null if no such component is found.
Throws:
ComponentException

getInhabitants

public <T> Collection<Inhabitant<? extends T>> getInhabitants(Class<T> contract)
                                                   throws ComponentException
Gets all the inhabitants that has the given contract.

Throws:
ComponentException

getInhabitantsByType

public <T> Collection<Inhabitant<T>> getInhabitantsByType(Class<T> implType)
                                               throws ComponentException
Gets all the inhabitants that has the given implementation type.

Throws:
ComponentException

getInhabitantsByType

public Collection<Inhabitant<?>> getInhabitantsByType(String fullyQualifiedClassName)
Gets all the inhabitants that has the given implementation type name.


getInhabitantsByContract

public Collection<Inhabitant<?>> getInhabitantsByContract(String fullyQualifiedClassName)

getAllContracts

public Iterator<String> getAllContracts()

getAllTypes

public Iterator<String> getAllTypes()

getInhabitantByContract

public Inhabitant getInhabitantByContract(String fullyQualifiedName,
                                          String name)

getInhabitants

public <T> Iterable<Inhabitant<? extends T>> getInhabitants(Class<T> contract,
                                                            String name)
                                                 throws ComponentException
Gets all the inhabitants that has the given contract and the given name

This method defers the actual instantiation of the component until Inhabitant#get() is invoked.

Returns:
Can be empty but never null.
Throws:
ComponentException

getInhabitantsByAnnotation

public Iterable<Inhabitant<?>> getInhabitantsByAnnotation(Class<? extends Annotation> contract,
                                                          String name)
                                                   throws ComponentException
Gets all the inhabitants that has the given contract annotation and the given name.

This method defers the actual instantiation of the component until Inhabitant#get() is invoked.

Returns:
Can be empty but never null.
Throws:
ComponentException

getByType

public <T> T getByType(Class<T> implType)
Gets the object of the given type.

Returns:
null if not found.

getByContract

public <T> T getByContract(Class<T> contractType)
Gets the object that has the given contract.

If there are more than one of them, this method arbitrarily return one of them.


release

public void release()
Releases all the components. Should be called for orderly shut-down of the system. TODO: more javadoc needed



Submit a bug or feature

Copyright © 2009 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.

Generated on 12-March-2010 04:35