Package tigase.kernel.core
Class Kernel
- java.lang.Object
-
- tigase.kernel.core.Kernel
-
- Direct Known Subclasses:
RegistrarKernel
public class Kernel extends Object
Main class of Kernel.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classKernel.DelayedDependencyInjectionQueueClass implements a queue for delayed dependency injection.static classKernel.DelegatedBeanConfigClass used for delegating beans from one kernel to the other kernel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Kernel.DelayedDependencyInjectionQueuebeginDependencyDelayedInjection()Calling this method instructs Kernel to delay dependency injection untilfinishDependecyDelayedInjection()method is called.voidfinishDependecyDelayedInjection(Kernel.DelayedDependencyInjectionQueue queue)Calling this method instructs Kernel to end delaying dependency injection and inject all queued items.voidgc()DependencyManagergetDependencyManager()ReturnsDependencyManagerused in Kernel.<T> TgetInstance(Class<T> beanClass)Returns instance of bean.protected <T> TgetInstance(Class<T> beanClass, boolean allowNonExportable)<T> TgetInstance(String beanName)Returns instance of bean.<T> TgetInstanceIfExistsOr(String beanName, Function<BeanConfig,T> function)Returns instance of bean if instance exists already or calls passed function.StringgetName()Returns name of Kernel.Collection<String>getNamesOf(Class<?> beanType)Returns name of beans matching to given type.KernelgetParent()Returns parent Kernel.voidinitAll()Forces initiate all registered beans.protected voidinitBean(BeanConfig tmpBC, Set<BeanConfig> createdBeansConfig, int deep)protected voidinjectIfRequired(BeanConfig beanConfig)booleanisBeanClassRegistered(String beanName)Checks if bean with given name is registered in Kernel.booleanisBeanClassRegistered(String beanName, boolean checkInParent)Checks if bean with given name is registered in Kernel.voidln(String exportingBeanName, Kernel destinationKernel, String destinationName)Makes symlink to bean in another Kernel.BeanConfigBuilderregisterBean(Class<?> beanClass)Registers bean as class in Kernel.BeanConfigBuilderregisterBean(String beanName)Registers bean with given name.protected BeanConfigregisterBean(BeanConfig beanConfig, BeanConfig factoryBeanConfig, Object beanInstance)voidregisterLinks(String beanName)Register links for bean of the passed name.voidsetBeanActive(String beanName, boolean value)Change state of a bean (activate/deactivate).voidsetForceAllowNull(boolean forceAllowNull)Force injection of nulls in all dependency injection fields of all beans if required bean for injections are not available.voidsetName(String name)Set name of the Kernel.voidshutdown()Shutdown kernel.voidshutdown(Comparator<BeanConfig> shutdownOrder)Shutdown kernel with passed comparator to define order in which bean will be stopped.StringtoPrintable()StringtoString()voidunregister(String beanName)Removes bean from Kernel.
-
-
-
Field Detail
-
log
protected static final Logger log
-
-
Constructor Detail
-
Kernel
public Kernel()
Creates instance of Kernel.
-
Kernel
public Kernel(String name)
Creates instance of kernel.- Parameters:
name- kernel name.
-
-
Method Detail
-
initBean
protected void initBean(BeanConfig tmpBC, Set<BeanConfig> createdBeansConfig, int deep) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException
-
gc
public void gc()
-
getDependencyManager
public DependencyManager getDependencyManager()
ReturnsDependencyManagerused in Kernel.- Returns:
depenency manager.
-
getInstance
public <T> T getInstance(Class<T> beanClass) throws KernelException
Returns instance of bean.- Type Parameters:
T- type of bean to be returned.- Parameters:
beanClass- type of requested bean. Note that if more than one instance of bean will match, then Kernel throws exception.- Returns:
- instance of bean if bean exists and there is only single instance of it.
- Throws:
KernelException- when more than one instance of matching beans will be found or none of matching beans is registered.
-
getInstance
public <T> T getInstance(String beanName)
Returns instance of bean. It creates bean if it is required.- Type Parameters:
T- type of bean to be returned.- Parameters:
beanName- name of bean to be returned.- Returns:
- instance of bean if bean exists and there is only single instance of it.
- Throws:
KernelException- when bean with given name doesn't exists.
-
getInstanceIfExistsOr
public <T> T getInstanceIfExistsOr(String beanName, Function<BeanConfig,T> function)
Returns instance of bean if instance exists already or calls passed function.- Type Parameters:
T- type of bean to be returned.- Parameters:
beanName- name of bean to be returned.function- function to call if instance does not exist.- Returns:
- instance of bean if bean exists and there is only single instance of it or .
- Throws:
KernelException- when bean with given name doesn't exists.
-
getName
public String getName()
Returns name of Kernel.- Returns:
- name of Kernel.
-
setName
public void setName(String name)
Set name of the Kernel.- Parameters:
name- to set
-
registerLinks
public void registerLinks(String beanName)
Register links for bean of the passed name.- Parameters:
beanName-
-
getNamesOf
public Collection<String> getNamesOf(Class<?> beanType)
Returns name of beans matching to given type.- Parameters:
beanType- type of searched beans.- Returns:
- collection of matching bean names.
-
getParent
public Kernel getParent()
Returns parent Kernel.- Returns:
- parent Kernel or
nullif there is no parent Kernel.
-
initAll
public void initAll()
Forces initiate all registered beans.
-
isBeanClassRegistered
public boolean isBeanClassRegistered(String beanName)
Checks if bean with given name is registered in Kernel.- Parameters:
beanName- name of bean to check.- Returns:
trueif bean is registered (it may be not initialized!).
-
isBeanClassRegistered
public boolean isBeanClassRegistered(String beanName, boolean checkInParent)
Checks if bean with given name is registered in Kernel.- Parameters:
beanName- name of bean to check.checkInParent- should check in parent kernel if not found in the current Kernel.- Returns:
trueif bean is registered (it may be not initialized!).
-
ln
public void ln(String exportingBeanName, Kernel destinationKernel, String destinationName)
Makes symlink to bean in another Kernel.- Parameters:
exportingBeanName- name bean to be linked.destinationKernel- destination Kernel.destinationName- name of bean in destination Kernel.
-
registerBean
public BeanConfigBuilder registerBean(Class<?> beanClass)
Registers bean as class in Kernel. Class must be annotated withBeanannotation.For example:
// If Bean1.class is annotated by @Bean annotation. registerBean(Bean1.class).exec();- Parameters:
beanClass- class of bean to register.- Returns:
config builderwhat allows to finish bean registering.
-
registerBean
public BeanConfigBuilder registerBean(String beanName)
Registers bean with given name. Class or instance of bean must be defined in returnedconfig builder.For example:
// To register already created variable bean4 as bean "bean4". krnl.registerBean("bean4").asInstance(bean4).exec(); // If Bean5 have to been created by Bean5Factory. krnl.registerBean("bean5").asClass(Bean5.class).withFactory(Bean5Factory.class).exec();- Parameters:
beanName- name of bean.- Returns:
config builderwhat allows to finish bean registering.
-
beginDependencyDelayedInjection
public Kernel.DelayedDependencyInjectionQueue beginDependencyDelayedInjection()
Calling this method instructs Kernel to delay dependency injection untilfinishDependecyDelayedInjection()method is called.- Returns:
- instance of a queue with delayed dependency injections
-
finishDependecyDelayedInjection
public void finishDependecyDelayedInjection(Kernel.DelayedDependencyInjectionQueue queue) throws IllegalAccessException, InstantiationException, InvocationTargetException
Calling this method instructs Kernel to end delaying dependency injection and inject all queued items.- Parameters:
queue-- Throws:
IllegalAccessExceptionInstantiationExceptionInvocationTargetException
-
setBeanActive
public void setBeanActive(String beanName, boolean value)
Change state of a bean (activate/deactivate).- Parameters:
beanName- name of a beanvalue- new state of a bean
-
setForceAllowNull
public void setForceAllowNull(boolean forceAllowNull)
Force injection of nulls in all dependency injection fields of all beans if required bean for injections are not available.- Parameters:
forceAllowNull-
-
shutdown
public void shutdown()
Shutdown kernel.
-
shutdown
public void shutdown(Comparator<BeanConfig> shutdownOrder)
Shutdown kernel with passed comparator to define order in which bean will be stopped.- Parameters:
shutdownOrder- comparator defining order of beans for shutdown
-
unregister
public void unregister(String beanName)
Removes bean from Kernel.- Parameters:
beanName- name of bean to be removed.
-
toPrintable
public String toPrintable()
-
getInstance
protected <T> T getInstance(Class<T> beanClass, boolean allowNonExportable)
-
injectIfRequired
protected void injectIfRequired(BeanConfig beanConfig)
-
registerBean
protected BeanConfig registerBean(BeanConfig beanConfig, BeanConfig factoryBeanConfig, Object beanInstance)
-
-