com.sun.hk2.component
Class AbstractCreatorImpl<T>

java.lang.Object
  extended by com.sun.hk2.component.AbstractInhabitantImpl<T>
      extended by com.sun.hk2.component.AbstractCreatorImpl<T>
All Implemented Interfaces:
Holder<T>, Binding<T>, Factory<T>, ManagedComponentProvider<T>, Provider<T>, Creator<T>, Inhabitant<T>
Direct Known Subclasses:
ConstructorCreator, FactoryCreator

public abstract class AbstractCreatorImpl<T>
extends AbstractInhabitantImpl<T>
implements Creator<T>

Author:
Kohsuke Kawaguchi

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.sun.hk2.component.Holder
Holder.Impl<T>
 
Field Summary
protected  Habitat habitat
           
protected  Class<? extends T> type
           
 
Fields inherited from class com.sun.hk2.component.AbstractInhabitantImpl
descriptor
 
Constructor Summary
AbstractCreatorImpl(Class<? extends T> type, Habitat habitat, MultiMap<String,String> metadata)
           
 
Method Summary
protected  InjectionManager createInjectionManager()
           
 T get(Inhabitant onBehalfOf)
          Returns the instance of this inhabitant.
protected  ExecutorService getExecutorService(Habitat h, Inhabitant<?> onBehalfOf)
           
protected  InjectionResolver[] getInjectionResolvers(T t, Habitat h)
           
 void initialize(T t, Inhabitant onBehalfOf)
          Performs initialization of object, such as dependency injection.
protected  void inject(Habitat habitat, T t, Inhabitant<?> onBehalfOf)
          Performs resource injection on the given instance from the given habitat.
 boolean isActive()
           
 MultiMap<String,String> metadata()
          Gets the metadata associated with this inhabitant.
 void release()
          Called to orderly shutdown Habitat.
 Class<? extends T> type()
          Type of the inhabitant.
 String typeName()
          The short-cut for type().getName() but this allows us to defer loading the actual types.
 
Methods inherited from class com.sun.hk2.component.AbstractInhabitantImpl
companions, dispose, get, getAnnotation, getAnnotation, getAnnotations, getDescriptor, getDescriptorFor, getProvider, getProvider, getSerializedMetadata, getSerializedMetadata, lead, matches, setCompanions, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jvnet.hk2.component.Creator
create, get
 
Methods inherited from interface org.jvnet.hk2.component.Inhabitant
companions, getSerializedMetadata, getSerializedMetadata, lead, setCompanions
 
Methods inherited from interface org.glassfish.hk2.Binding
getDescriptor, getProvider, getProvider
 
Methods inherited from interface org.glassfish.hk2.Provider
getAnnotations
 

Field Detail

type

protected final Class<? extends T> type

habitat

protected final Habitat habitat
Constructor Detail

AbstractCreatorImpl

public AbstractCreatorImpl(Class<? extends T> type,
                           Habitat habitat,
                           MultiMap<String,String> metadata)
Method Detail

typeName

public String typeName()
Description copied from interface: Inhabitant
The short-cut for type().getName() but this allows us to defer loading the actual types.

Specified by:
typeName in interface Inhabitant<T>

type

public final Class<? extends T> type()
Description copied from interface: Inhabitant
Type of the inhabitant.

The only binding contract that needs to be honored is that the Inhabitant.get() method returns an instance assignable to this type. That is, get().getClass()==type() doesn't necessarily have to hold, but type().isInstance(get()) must.

This is particularly true when Factory is involved, as in such case HK2 has no way of knowing the actual type. That said, this method is not designed for the semantics of contract/implementation split --- implementations of a contract should return the concrete type from this method, and use habitat index to support look-up by contract.

Specified by:
type in interface Provider<T>
Specified by:
type in interface Inhabitant<T>
Returns:
Always non-null, same value.

get

public final T get(Inhabitant onBehalfOf)
            throws ComponentException
Description copied from interface: Inhabitant
Returns the instance of this inhabitant.

THIS METHOD SHOULD BE ONLY USED BY HK2 IMPLEMENTATION.

Inhabitants are often used with the decorator pattern (see AbstractCreatorInhabitantImpl for example), yet during the object initializtion inside the Inhabitant.get() method, we often need the reference to the outer-most Inhabitant registered to the Habitat (for example so that we can request the injection of {link Inhabita} that represents itself, or to inject companions.)

So this overloaded version of the get method takes the outer-most Inhabitant. This method is only invoked from within HK2 where the decorator pattern is used.

Specified by:
get in interface Inhabitant<T>
Throws:
ComponentException

isActive

public boolean isActive()
Specified by:
isActive in interface ManagedComponentProvider<T>

initialize

public void initialize(T t,
                       Inhabitant onBehalfOf)
                throws ComponentException
Description copied from interface: Creator
Performs initialization of object, such as dependency injection.

Specified by:
initialize in interface Creator<T>
Throws:
ComponentException

release

public void release()
Description copied from interface: Inhabitant
Called to orderly shutdown Habitat.

The expected behavior is for objects to get its PreDestroy callback invoked, and its reference released. For singleton objects, this method is expected to dispose that object.

For scoped objects, those are released when ScopeInstance.release() is invoked.

Specified by:
release in interface ManagedComponentProvider<T>
Specified by:
release in interface Inhabitant<T>

metadata

public MultiMap<String,String> metadata()
Description copied from interface: Inhabitant
Gets the metadata associated with this inhabitant.

This data is usually used by a sub-system of HK2, and not really meant to be used by applications. (At least for now.) The main benefit of metadata is that it's available right away as soon as the Habitat is properly initialized, even before component classes are loaded. In contrast, accessing annotations would require classes to be loaded and resolved.

Specified by:
metadata in interface Inhabitant<T>
Returns:
can be empty but never null. The values are read-only.
See Also:
Service.metadata()

inject

protected void inject(Habitat habitat,
                      T t,
                      Inhabitant<?> onBehalfOf)
Performs resource injection on the given instance from the given habitat.

This method is an utility method for subclasses for performing injection.


createInjectionManager

protected InjectionManager createInjectionManager()

getInjectionResolvers

protected InjectionResolver[] getInjectionResolvers(T t,
                                                    Habitat h)

getExecutorService

protected ExecutorService getExecutorService(Habitat h,
                                             Inhabitant<?> onBehalfOf)


Copyright © 2011 Oracle Corporation. All Rights Reserved.