Package org.smooks.api.bean.context
Interface BeanContext
-
- All Known Implementing Classes:
StandaloneBeanContext
public interface BeanContext
Bean Context This class represents a context of bean's and the means to get and set there instances. This class uses aBeanIdStore
to optimize the access performance. If all theBeanId
objects are registered with the BeanIdStore before this object is created then you get 'direct access' performance. For performance reasons it is best to register all BeanId objects up front. Because if new BeanId objects are registered after the BeanContext is created then the BeanContext needs to do synchronize with the BeanIdStore, It is possible to get the bean by it's bean id String name. However this isn't as fast as using the BeanId objects.- Author:
- maurice.zeijen@smies.com
-
-
Method Summary
All Methods Instance Methods Abstract 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.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.
-
-
-
Method Detail
-
addBean
void addBean(BeanId beanId, Object bean)
Add a bean instance under the specifiedBeanId
.- Parameters:
beanId
- TheBeanId
under which the bean is to be stored.bean
- The bean instance to be stored.
-
addBean
void addBean(BeanId beanId, Object bean, Fragment<?> source)
Add a bean instance under the specified beanId string. Generates aBeanId
in the background.- Parameters:
beanId
- TheBeanId
under which the bean is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
addBean
void addBean(String beanId, Object bean)
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 theaddBean(BeanId, Object, Fragment)
method.- Parameters:
beanId
- The beanId under which the bean is to be stored.bean
- The bean instance to be stored.
-
addBean
void addBean(String beanId, Object bean, Fragment<?> source)
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 theaddBean(BeanId, Object, Fragment)
method.- Parameters:
beanId
- The beanId under which the bean is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
getBeanId
BeanId getBeanId(String beanId)
Get theBeanId
instance for the specified beanId String. Regsiters the beanId if it's not already registered.- Parameters:
beanId
- The beanId String.- Returns:
- The associated
BeanId
instance.
-
containsBean
boolean containsBean(BeanId beanId)
Looks if a bean instance is set under theBeanId
- Parameters:
beanId
- TheBeanId
under which is looked.
-
getBean
Object getBean(BeanId beanId)
Get the current bean, specified by the suppliedBeanId
.- Parameters:
beanId
- TheBeanId
of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
getBean
Object getBean(String beanId)
Returns the bean by it's beanId name. Returns the first bean of the specified type from the BeanContext instance.- Parameters:
beanId
- The type of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
getBean
<T> T getBean(Class<T> beanType)
Returns the bean by it's beanId name. Returns the first bean of the specified type from the BeanContext instance.- Parameters:
beanType
- The type of the bean to be returned.- Returns:
- The bean instance, otherwise
null
.
-
changeBean
void changeBean(BeanId beanId, Object bean, Fragment<?> source)
Changes a bean instance of the givenBeanId
. The difference toaddBean(BeanId, Object)
is that the bean must exist, the associated beans aren't removed and the observers of theBeanLifecycle.CHANGE
event are notified.- Parameters:
beanId
- TheBeanId
under which the bean instance is to be stored.bean
- The bean instance to be stored.source
- Source fragment.
-
removeBean
Object removeBean(BeanId beanId, Fragment<?> source)
Removes a bean and all its associated lifecycle beans from the bean map- Parameters:
beanId
- The beanId to remove the beans from.source
- Source fragment.
-
removeBean
Object removeBean(String beanId, Fragment<?> source)
Removes a bean and all its associated lifecycle beans from the bean map- Parameters:
beanId
- The beanId to remove the beans from.source
- Source fragment.
-
clear
void clear()
Removes all the beans from the bean map
-
addObserver
void addObserver(BeanContextLifecycleObserver observer)
Registers a bean context observer.- Parameters:
observer
- The actual BeanObserver instance.
-
notifyObservers
void notifyObservers(BeanContextLifecycleEvent event)
Notify all observers of a specific bean lifecycle event.- Parameters:
event
- The event.
-
removeObserver
void removeObserver(BeanContextLifecycleObserver observer)
Unregisters a bean observer.- Parameters:
observer
- The actual BeanObserver instance.
-
getBeanMap
Map<String,Object> getBeanMap()
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.
-
setBeanInContext
void setBeanInContext(BeanId beanId, boolean inContext)
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.- Parameters:
beanId
- The bean ID.inContext
- True if the bean is in context, otherwise false.
-
newSubContext
BeanContext newSubContext(ExecutionContext executionContext)
- Parameters:
executionContext
- The AssociatedExecutionContext
.- Returns:
- The new sub-
BeanContext
.
-
-