Class Kernel

  • Direct Known Subclasses:
    RegistrarKernel

    public class Kernel
    extends java.lang.Object
    Main class of Kernel.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.util.logging.Logger log  
    • Constructor Summary

      Constructors 
      Constructor Description
      Kernel()
      Creates instance of Kernel.
      Kernel​(java.lang.String name)
      Creates instance of kernel.
    • Field Detail

      • log

        protected static final java.util.logging.Logger log
    • Constructor Detail

      • Kernel

        public Kernel()
        Creates instance of Kernel.
      • Kernel

        public Kernel​(java.lang.String name)
        Creates instance of kernel.
        Parameters:
        name - kernel name.
    • Method Detail

      • initBean

        protected void initBean​(BeanConfig tmpBC,
                                java.util.Set<BeanConfig> createdBeansConfig,
                                int deep)
                         throws java.lang.IllegalAccessException,
                                java.lang.IllegalArgumentException,
                                java.lang.reflect.InvocationTargetException,
                                java.lang.InstantiationException
        Throws:
        java.lang.IllegalAccessException
        java.lang.IllegalArgumentException
        java.lang.reflect.InvocationTargetException
        java.lang.InstantiationException
      • gc

        public void gc()
      • getInstance

        public <T> T getInstance​(java.lang.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​(java.lang.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​(java.lang.String beanName,
                                           java.util.function.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 java.lang.String getName()
        Returns name of Kernel.
        Returns:
        name of Kernel.
      • setName

        public void setName​(java.lang.String name)
        Set name of the Kernel.
        Parameters:
        name - to set
      • registerLinks

        public void registerLinks​(java.lang.String beanName)
        Register links for bean of the passed name.
      • getNamesOf

        public java.util.Collection<java.lang.String> getNamesOf​(java.lang.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 null if there is no parent Kernel.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • initAll

        public void initAll()
        Forces initiate all registered beans.
      • isBeanClassRegistered

        public boolean isBeanClassRegistered​(java.lang.String beanName)
        Checks if bean with given name is registered in Kernel.
        Parameters:
        beanName - name of bean to check.
        Returns:
        true if bean is registered (it may be not initialized!).
      • isBeanClassRegistered

        public boolean isBeanClassRegistered​(java.lang.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:
        true if bean is registered (it may be not initialized!).
      • ln

        public void ln​(java.lang.String exportingBeanName,
                       Kernel destinationKernel,
                       java.lang.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​(java.lang.Class<?> beanClass)
        Registers bean as class in Kernel. Class must be annotated with Bean annotation.

        For example:

         
        
          // If Bean1.class is annotated by @Bean annotation.
          registerBean(Bean1.class).exec();
         
         
        Parameters:
        beanClass - class of bean to register.
        Returns:
        config builder what allows to finish bean registering.
      • registerBean

        public BeanConfigBuilder registerBean​(java.lang.String beanName)
        Registers bean with given name. Class or instance of bean must be defined in returned config 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 builder what allows to finish bean registering.
      • beginDependencyDelayedInjection

        public Kernel.DelayedDependencyInjectionQueue beginDependencyDelayedInjection()
        Calling this method instructs Kernel to delay dependency injection until finishDependecyDelayedInjection() method is called.
        Returns:
        instance of a queue with delayed dependency injections
      • finishDependecyDelayedInjection

        public void finishDependecyDelayedInjection​(Kernel.DelayedDependencyInjectionQueue queue)
                                             throws java.lang.IllegalAccessException,
                                                    java.lang.InstantiationException,
                                                    java.lang.reflect.InvocationTargetException
        Calling this method instructs Kernel to end delaying dependency injection and inject all queued items.
        Throws:
        java.lang.IllegalAccessException
        java.lang.InstantiationException
        java.lang.reflect.InvocationTargetException
      • setBeanActive

        public void setBeanActive​(java.lang.String beanName,
                                  boolean value)
        Change state of a bean (activate/deactivate).
        Parameters:
        beanName - name of a bean
        value - 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.
      • shutdown

        public void shutdown()
        Shutdown kernel.
      • shutdown

        public void shutdown​(java.util.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​(java.lang.String beanName)
        Removes bean from Kernel.
        Parameters:
        beanName - name of bean to be removed.
      • toPrintable

        public java.lang.String toPrintable()
      • getInstance

        protected <T> T getInstance​(java.lang.Class<T> beanClass,
                                    boolean allowNonExportable)
      • injectIfRequired

        protected void injectIfRequired​(BeanConfig beanConfig)