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

java.lang.Object
  extended by com.sun.hk2.component.AbstractInhabitantImpl<T>
      extended by com.sun.hk2.component.EventPublishingInhabitant<T>
          extended by com.sun.hk2.component.LazyInhabitant<T>
All Implemented Interfaces:
ClassLoaderHolder, Holder<T>, Inhabitant<T>

public class LazyInhabitant<T>
extends EventPublishingInhabitant<T>
implements ClassLoaderHolder

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
           
 
Fields inherited from class com.sun.hk2.component.EventPublishingInhabitant
real
 
Constructor Summary
LazyInhabitant(Habitat habitat, Holder<java.lang.ClassLoader> cl, java.lang.String typeName, MultiMap<java.lang.String,java.lang.String> metadata)
           
LazyInhabitant(Habitat habitat, Holder<java.lang.ClassLoader> cl, java.lang.String typeName, MultiMap<java.lang.String,java.lang.String> metadata, Inhabitant<?> lead)
           
 
Method Summary
protected  Creator<T> createCreator(java.lang.Class<T> c)
          Creates Creator for instantiating objects.
protected  void fetch()
           
 java.lang.ClassLoader getClassLoader()
           
 Inhabitant<?> lead()
          If this inhabitant is a companion to another inhabitant (called "lead"), This method returns that inhabitant.
protected  java.lang.Class<T> loadClass()
           
 MultiMap<java.lang.String,java.lang.String> metadata()
          Gets the metadata associated with this inhabitant.
 void release()
          Called to orderly shutdown Habitat.
 java.lang.Class<T> type()
          Type of the inhabitant.
 java.lang.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.EventPublishingInhabitant
addInhabitantListener, get, isInstantiated, notify, removeInhabitantListener, toString
 
Methods inherited from class com.sun.hk2.component.AbstractInhabitantImpl
companions, get, getAnnotation, getAnnotation, getSerializedMetadata, getSerializedMetadata, setCompanions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

habitat

protected final Habitat habitat
Constructor Detail

LazyInhabitant

public LazyInhabitant(Habitat habitat,
                      Holder<java.lang.ClassLoader> cl,
                      java.lang.String typeName,
                      MultiMap<java.lang.String,java.lang.String> metadata)

LazyInhabitant

public LazyInhabitant(Habitat habitat,
                      Holder<java.lang.ClassLoader> cl,
                      java.lang.String typeName,
                      MultiMap<java.lang.String,java.lang.String> metadata,
                      Inhabitant<?> lead)
Method Detail

lead

public Inhabitant<?> lead()
Description copied from interface: Inhabitant
If this inhabitant is a companion to another inhabitant (called "lead"), This method returns that inhabitant. Otherwise null.

Specified by:
lead in interface Inhabitant<T>
Overrides:
lead in class AbstractInhabitantImpl<T>

typeName

public java.lang.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>
Overrides:
typeName in class EventPublishingInhabitant<T>

type

public java.lang.Class<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 Inhabitant<T>
Overrides:
type in class EventPublishingInhabitant<T>
Returns:
Always non-null, same value.

metadata

public MultiMap<java.lang.String,java.lang.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>
Overrides:
metadata in class EventPublishingInhabitant<T>
Returns:
can be empty but never null. The values are read-only.
See Also:
Service.metadata()

fetch

protected void fetch()
Overrides:
fetch in class EventPublishingInhabitant<T>

getClassLoader

public final java.lang.ClassLoader getClassLoader()
Specified by:
getClassLoader in interface ClassLoaderHolder

loadClass

protected java.lang.Class<T> loadClass()

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 Inhabitant<T>
Overrides:
release in class EventPublishingInhabitant<T>

createCreator

protected Creator<T> createCreator(java.lang.Class<T> c)
Creates Creator for instantiating objects.



Copyright © 2011 Oracle Corporation. All Rights Reserved.