Package org.smooks.engine.bean.context
Class StandaloneBeanContext
- java.lang.Object
-
- org.smooks.engine.bean.context.StandaloneBeanContext
-
- All Implemented Interfaces:
BeanContext
public class StandaloneBeanContext extends Object implements BeanContext
-
-
Constructor Summary
Constructors Constructor Description StandaloneBeanContext(ExecutionContext executionContext, BeanIdStore beanIdStore, Map<String,Object> beanMap)
Create the StandAloneBeanContext
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBean(String beanId, Object bean)
Add a bean instance under the specified beanId.void
addBean(String beanId, Object bean, Fragment<?> source)
Add a bean instance under the specified beanId.void
addBean(BeanId beanId, Object bean)
Add a bean instance under the specifiedBeanId
.void
addBean(BeanId beanId, Object bean, Fragment<?> source)
Add a bean instance under the specified beanId string.void
addObserver(BeanContextLifecycleObserver observer)
Registers a bean context observer.void
changeBean(BeanId beanId, Object bean, Fragment<?> source)
Changes a bean instance of the givenBeanId
.void
clear()
Removes all the beans from the bean mapboolean
containsBean(BeanId beanId)
Looks if a bean instance is set under theBeanId
<T> T
getBean(Class<T> beanType)
Returns the bean by it's beanId name.static <T> T
getBean(Class<T> beanType, Map<String,Object> beanMap)
Object
getBean(String beanId)
Returns the bean by it's beanId name.Object
getBean(BeanId beanId)
Get the current bean, specified by the suppliedBeanId
.BeanId
getBeanId(String beanId)
Get theBeanId
instance for the specified beanId String.Map<String,Object>
getBeanMap()
This returns a map which is backed by this repository.BeanContext
newSubContext(ExecutionContext executionContext)
void
notifyObservers(BeanContextLifecycleEvent event)
Notify all observers of a specific bean lifecycle event.Object
removeBean(String beanId, Fragment<?> source)
Removes a bean and all its associated lifecycle beans from the bean mapObject
removeBean(BeanId beanId, Fragment<?> source)
Removes a bean and all its associated lifecycle beans from the bean mapvoid
removeObserver(BeanContextLifecycleObserver observer)
Unregisters a bean observer.void
setBeanInContext(BeanId beanId, boolean inContext)
Mark the bean as being in context.String
toString()
-
-
-
Constructor Detail
-
StandaloneBeanContext
public StandaloneBeanContext(ExecutionContext executionContext, BeanIdStore beanIdStore, Map<String,Object> beanMap)
Create the StandAloneBeanContext- Parameters:
executionContext
- TheExecutionContext
to which this object is bound to.beanIdStore
- TheBeanIdStore
to which this object is bound to.beanMap
- TheMap
in which the bean's will be set. It is important not to modify this map outside of the BeanRepository! It is only provided as constructor parameter because in some situations we need to control whichMap
is used.
-
-
Method Detail
-
addBean
public void addBean(BeanId beanId, Object bean)
Description copied from interface:BeanContext
Add a bean instance under the specifiedBeanId
.- Specified by:
addBean
in interfaceBeanContext
- Parameters:
beanId
- TheBeanId
under which the bean is to be stored.bean
- The bean instance to be stored.
-
addBean
public void addBean(BeanId beanId, Object bean, Fragment<?> source)
Description copied from interface:BeanContext
Add a bean instance under the specified beanId string. Generates aBeanId
in the background.- Specified by:
addBean
in interfaceBeanContext
- Parameters:
beanId
- TheBeanId
under which the bean is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
addBean
public void addBean(String beanId, Object bean)
Description copied from interface:BeanContext
Add a bean instance under the specified beanId. If performance is important, you should get (and cache) aBeanId
instance for the beanId String and then use theBeanContext.addBean(BeanId, Object, Fragment)
method.- Specified by:
addBean
in interfaceBeanContext
- Parameters:
beanId
- The beanId under which the bean is to be stored.bean
- The bean instance to be stored.
-
addBean
public void addBean(String beanId, Object bean, Fragment<?> source)
Description copied from interface:BeanContext
Add a bean instance under the specified beanId. If performance is important, you should get (and cache) aBeanId
instance for the beanId String and then use theBeanContext.addBean(BeanId, Object, Fragment)
method.- Specified by:
addBean
in interfaceBeanContext
- Parameters:
beanId
- The beanId under which the bean is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
containsBean
public boolean containsBean(BeanId beanId)
Description copied from interface:BeanContext
Looks if a bean instance is set under theBeanId
- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
beanId
- TheBeanId
under which is looked.
-
getBeanId
public BeanId getBeanId(String beanId)
Description copied from interface:BeanContext
Get theBeanId
instance for the specified beanId String. Regsiters the beanId if it's not already registered.- Specified by:
getBeanId
in interfaceBeanContext
- Parameters:
beanId
- The beanId String.- Returns:
- The associated
BeanId
instance.
-
getBean
public Object getBean(BeanId beanId)
Description copied from interface:BeanContext
Get the current bean, specified by the suppliedBeanId
.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
beanId
- TheBeanId
of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
getBean
public Object getBean(String beanId)
Description copied from interface:BeanContext
Returns the bean by it's beanId name. Returns the first bean of the specified type from the BeanContext instance.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
beanId
- The type of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
getBean
public <T> T getBean(Class<T> beanType)
Description copied from interface:BeanContext
Returns the bean by it's beanId name. Returns the first bean of the specified type from the BeanContext instance.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
beanType
- The type of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
changeBean
public void changeBean(BeanId beanId, Object bean, Fragment<?> source)
Description copied from interface:BeanContext
Changes a bean instance of the givenBeanId
. The difference toBeanContext.addBean(BeanId, Object)
is that the bean must exist, the associated beans aren't removed and the observers of theBeanLifecycle.CHANGE
event are notified.- Specified by:
changeBean
in interfaceBeanContext
- Parameters:
beanId
- TheBeanId
under which the bean instance is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
removeBean
public Object removeBean(BeanId beanId, Fragment<?> source)
Description copied from interface:BeanContext
Removes a bean and all its associated lifecycle beans from the bean map- Specified by:
removeBean
in interfaceBeanContext
- Parameters:
beanId
- The beanId to remove the beans from.source
- Source fragment.
-
removeBean
public Object removeBean(String beanId, Fragment<?> source)
Description copied from interface:BeanContext
Removes a bean and all its associated lifecycle beans from the bean map- Specified by:
removeBean
in interfaceBeanContext
- Parameters:
beanId
- The beanId to remove the beans from.source
- Source fragment.
-
clear
public void clear()
Description copied from interface:BeanContext
Removes all the beans from the bean map- Specified by:
clear
in interfaceBeanContext
-
getBeanMap
public Map<String,Object> getBeanMap()
Description copied from interface:BeanContext
This returns a map which is backed by this repository. Changes made in the map are reflected back into the repository. There are some important side notes:- The write performance of the map isn't as good as the write performance of the BeanRepository because it needs to find or register the BeanId every time. The read performance are as good as any normal Map.
- The entrySet() method returns an UnmodifiableSet
- When a bean gets removed from the BeanRepository then only the value of the map entry is set to null. This means that null values should be regarded as deleted beans. That is also why the size() of the bean map isn't accurate. It also counts the null value entries.
Only use the Map if you absolutely needed it else you should use the BeanRepository.
- Specified by:
getBeanMap
in interfaceBeanContext
-
setBeanInContext
public void setBeanInContext(BeanId beanId, boolean inContext)
Description copied from interface:BeanContext
Mark the bean as being in context. This is "set" when we enter the fragment around which the bean is created and unset when we exit.- Specified by:
setBeanInContext
in interfaceBeanContext
- Parameters:
beanId
- The bean ID.inContext
- True if the bean is in context, otherwise false.
-
newSubContext
public BeanContext newSubContext(ExecutionContext executionContext)
Description copied from interface:BeanContext
- Specified by:
newSubContext
in interfaceBeanContext
- Parameters:
executionContext
- The AssociatedExecutionContext
.- Returns:
- The new sub-
BeanContext
.
-
addObserver
public void addObserver(BeanContextLifecycleObserver observer)
Description copied from interface:BeanContext
Registers a bean context observer.- Specified by:
addObserver
in interfaceBeanContext
- Parameters:
observer
- The actual BeanObserver instance.
-
notifyObservers
public void notifyObservers(BeanContextLifecycleEvent event)
Description copied from interface:BeanContext
Notify all observers of a specific bean lifecycle event.- Specified by:
notifyObservers
in interfaceBeanContext
- Parameters:
event
- The event.
-
removeObserver
public void removeObserver(BeanContextLifecycleObserver observer)
Description copied from interface:BeanContext
Unregisters a bean observer.- Specified by:
removeObserver
in interfaceBeanContext
- Parameters:
observer
- The actual BeanObserver instance.
-
-