Package com.helger.scope.singleton
Class AbstractSingleton
- java.lang.Object
-
- com.helger.scope.singleton.AbstractSingleton
-
- All Implemented Interfaces:
IScopeDestructionAware
- Direct Known Subclasses:
AbstractGlobalSingleton,AbstractRequestSingleton,AbstractSessionSingleton
@ThreadSafe public abstract class AbstractSingleton extends Object implements IScopeDestructionAware
Base class for all singletons.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description protected com.helger.commons.concurrent.SimpleReadWriteLockm_aRWLock
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractSingleton()Ctor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T extends AbstractSingleton>
com.helger.commons.collection.impl.ICommonsList<T>getAllSingletons(IScope aScope, Class<T> aDesiredClass)Get all singleton objects registered in the respective sub-class of this class.static <T extends AbstractSingleton>
TgetSingleton(IScope aScope, Class<T> aClass)Get the singleton object in the passed scope, using the passed class.static <T extends AbstractSingleton>
TgetSingletonIfInstantiated(IScope aScope, Class<T> aClass)Get the singleton object if it is already instantiated inside a scope ornullif it is not instantiated.static StringgetSingletonScopeKey(Class<? extends AbstractSingleton> aClass)Create the key which is used to reference the object within the scope.booleanisDestroyed()booleanisInDestruction()booleanisInInstantiation()booleanisInPreDestruction()booleanisInstantiated()static booleanisSingletonInstantiated(IScope aScope, Class<? extends AbstractSingleton> aClass)Check if a singleton is already instantiated inside a scopebooleanisUsableObject()protected voidonAfterInstantiation(IScope aScope)Called after the singleton was instantiated.protected voidonBeforeDestroy(IScope aScopeToBeDestroyed)Called before this singleton is destroyed.voidonBeforeScopeDestruction(IScope aScopeToBeDestroyed)Called before the owning scope is destroyed.protected voidonDestroy(IScope aScopeInDestruction)Called when this singleton is destroyed.voidonScopeDestruction(IScope aScopeInDestruction)Called when the owning scope is destroyed.protected voidreadAbstractSingletonFields(ObjectInputStream aOIS)Set all internal status variables to the values read from the specifiedObjectInputStream.protected voidsetDestroyed(boolean bDestroyed)protected voidsetInDestruction(boolean bInDestruction)protected voidsetInInstantiation(boolean bInInstantiation)protected voidsetInPreDestruction(boolean bInPreDestruction)protected voidsetInstantiated(boolean bInstantiated)StringtoString()protected voidwriteAbstractSingletonFields(ObjectOutputStream aOOS)Write the internal status variables to the passedObjectOutputStream.
-
-
-
Method Detail
-
writeAbstractSingletonFields
protected final void writeAbstractSingletonFields(@Nonnull ObjectOutputStream aOOS) throws IOException
Write the internal status variables to the passedObjectOutputStream. This can be used to make singletons serializable.- Parameters:
aOOS- The output stream to write to. May not benull.- Throws:
IOException- In case writing failed
-
readAbstractSingletonFields
protected final void readAbstractSingletonFields(@Nonnull ObjectInputStream aOIS) throws IOException, ClassNotFoundException
Set all internal status variables to the values read from the specifiedObjectInputStream. This can be used to make singletons serializable.- Parameters:
aOIS- The input stream to read from. May not benull.- Throws:
IOException- In case reading failedClassNotFoundException- In case reading failed
-
onAfterInstantiation
@OverrideOnDemand protected void onAfterInstantiation(@Nonnull IScope aScope)
Called after the singleton was instantiated. The constructor has finished, and calling getInstance will work! This method is present to init the object with a virtual table present.- Parameters:
aScope- The scope in which the object was instantiated. Nevernull.
-
setInInstantiation
protected final void setInInstantiation(boolean bInInstantiation)
-
isInInstantiation
public final boolean isInInstantiation()
- Returns:
trueif this singleton is currently in the phase of instantiation,falseif it is instantiated or already destroyed.
-
setInstantiated
protected final void setInstantiated(boolean bInstantiated)
-
isInstantiated
public final boolean isInstantiated()
- Returns:
trueif this singleton was already instantiated,falseif it is active.
-
setInPreDestruction
protected final void setInPreDestruction(boolean bInPreDestruction)
-
isInPreDestruction
public final boolean isInPreDestruction()
- Returns:
trueif this singleton is currently in the phase of pre destruction,falseif it is active or already destroyed.
-
setInDestruction
protected final void setInDestruction(boolean bInDestruction)
-
isInDestruction
public final boolean isInDestruction()
- Returns:
trueif this singleton is currently in the phase of destruction,falseif it is active or already destroyed.
-
setDestroyed
protected final void setDestroyed(boolean bDestroyed)
-
isDestroyed
public final boolean isDestroyed()
- Returns:
trueif this singleton was already destroyed,falseif it is active.
-
onBeforeDestroy
@OverrideOnDemand protected void onBeforeDestroy(@Nonnull IScope aScopeToBeDestroyed) throws Exception
Called before this singleton is destroyed. This method is called when "inPreDestruction" istrue, "inDestruction" is stillfalseand "isDestroyed" isfalse.- Parameters:
aScopeToBeDestroyed- The scope that will be destroyed. Nevernull.- Throws:
Exception- If something goes wrong
-
onBeforeScopeDestruction
public final void onBeforeScopeDestruction(@Nonnull IScope aScopeToBeDestroyed) throws Exception
Description copied from interface:IScopeDestructionAwareCalled before the owning scope is destroyed. You may perform some last actions before the scope is really destroyed. This method is called after theIScope.preDestroy()callback is invoked and before the scope is set as being "in destruction".- Specified by:
onBeforeScopeDestructionin interfaceIScopeDestructionAware- Parameters:
aScopeToBeDestroyed- The scope that will be destroyed. Nevernull.- Throws:
Exception- in case of an error
-
onDestroy
@OverrideOnDemand protected void onDestroy(@Nonnull IScope aScopeInDestruction) throws Exception
Called when this singleton is destroyed. Perform all cleanup in this method. This method is called when "inPreDestruction" isfalse, "inDestruction" istrueand "isDestroyed" isfalse.- Parameters:
aScopeInDestruction- The scope in destruction. Nevernull.- Throws:
Exception- If something goes wrong
-
onScopeDestruction
public final void onScopeDestruction(@Nonnull IScope aScopeInDestruction) throws Exception
Description copied from interface:IScopeDestructionAwareCalled when the owning scope is destroyed. You may perform some cleanup work in here. This is method is called when the scope is already "in destruction".- Specified by:
onScopeDestructionin interfaceIScopeDestructionAware- Parameters:
aScopeInDestruction- The scope in destruction. Nevernull.- Throws:
Exception- in case of an error
-
isUsableObject
public final boolean isUsableObject()
- Returns:
trueif the object is instantiated and neither in destruction nor destroyed.
-
getSingletonScopeKey
@Nonnull public static final String getSingletonScopeKey(@Nonnull Class<? extends AbstractSingleton> aClass)
Create the key which is used to reference the object within the scope.- Parameters:
aClass- The class for which the key is to be created. May not benull.- Returns:
- The non-
nullkey.
-
getSingletonIfInstantiated
@Nullable public static final <T extends AbstractSingleton> T getSingletonIfInstantiated(@Nullable IScope aScope, @Nonnull Class<T> aClass)
Get the singleton object if it is already instantiated inside a scope ornullif it is not instantiated.- Type Parameters:
T- The type to be returned- Parameters:
aScope- The scope to check. May benullto avoid constructing a scope.aClass- The class to be checked. May not benull.- Returns:
- The singleton for the specified class is already instantiated,
nullotherwise.
-
isSingletonInstantiated
public static final boolean isSingletonInstantiated(@Nullable IScope aScope, @Nonnull Class<? extends AbstractSingleton> aClass)
Check if a singleton is already instantiated inside a scope- Parameters:
aScope- The scope to check. May benullto avoid constructing a scope.aClass- The class to be checked. May not benull.- Returns:
trueif the singleton for the specified class is already instantiated,falseotherwise.
-
getSingleton
@Nonnull public static final <T extends AbstractSingleton> T getSingleton(@Nonnull IScope aScope, @Nonnull Class<T> aClass)
Get the singleton object in the passed scope, using the passed class. If the singleton is not yet instantiated, a new instance is created.- Type Parameters:
T- The singleton type- Parameters:
aScope- The scope to be used. May not benull.aClass- The class to be used. May not benull. The class must be public as needs to have a public no-argument constructor.- Returns:
- The singleton object and never
null.
-
getAllSingletons
@Nonnull @ReturnsMutableCopy public static final <T extends AbstractSingleton> com.helger.commons.collection.impl.ICommonsList<T> getAllSingletons(@Nullable IScope aScope, @Nonnull Class<T> aDesiredClass)
Get all singleton objects registered in the respective sub-class of this class.- Type Parameters:
T- The singleton type to be retrieved- Parameters:
aScope- The scope to use. May benullto avoid creating a new scope.aDesiredClass- The desired sub-class of this class. May not benull.- Returns:
- A non-
nulllist with all instances of the passed class in the passed scope.
-
-