KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > spring > SpringService


1 package jfun.yan.xml.nuts.spring;
2
3 import jfun.yan.Component;
4 import jfun.yan.lifecycle.DefaultLifecycleManager;
5 import jfun.yan.lifecycle.Procedure;
6
7 import org.springframework.beans.factory.DisposableBean;
8 import org.springframework.beans.factory.config.BeanPostProcessor;
9 import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
10 import org.springframework.context.ApplicationContext;
11
12 /**
13  * SpringService contains all Spring-related data.
14  * <p>
15  * @author Ben Yu
16  * Nov 20, 2005 5:31:22 PM
17  */

18 public class SpringService {
19   private final ApplicationContext actxt;
20   private final DefaultLifecycleManager.DefaultLifecycle bean_lifecycle;
21   private final BeanPostProcessorQueue processors = new BeanPostProcessorQueue();
22   //private final BeanPostProcessorQueue instantiations = new BeanPostProcessorQueue();
23
private final BeanPostProcessorQueue destructions = new BeanPostProcessorQueue();
24   
25   /**
26    * Create a SpringService object.
27    * @param actxt the ApplicationContext object.
28    * @param manager the lifecycle manager.
29    */

30   public SpringService(ApplicationContext actxt, DefaultLifecycleManager manager){
31     this.actxt = actxt;
32     this.bean_lifecycle = getDisposerLifecycle(manager);
33   }
34   private static final class BeanDisposer{
35     //wrap around the DisposableBean so that this lifecycle does not
36
//overlap with the default Yan lifecycle.
37
private final DisposableBean db;
38     BeanDisposer(DisposableBean db) {
39       this.db = db;
40     }
41     void dispose()
42     throws Exception JavaDoc{
43       db.destroy();
44     }
45   }
46   private static DefaultLifecycleManager.DefaultLifecycle
47   getDisposerLifecycle(DefaultLifecycleManager manager){
48     return manager.newLifecycle().disposer(new Procedure(){
49       public void invoke(Object JavaDoc self, Object JavaDoc[] args)
50       throws Throwable JavaDoc {
51         final BeanDisposer bd = (BeanDisposer)self;
52         bd.dispose();
53       }
54     });
55   }
56   /**
57    * Get the ApplicationContext object.
58    */

59   public ApplicationContext getApplicationContext(){
60     return actxt;
61   }
62   /**
63    * Report an existence of a DisposableBean object.
64    * @param db the DisposableBean object.
65    */

66   public void manageDisposableBean(DisposableBean db){
67     bean_lifecycle.manageInstance(new BeanDisposer(db));
68   }
69   /**
70    * Report an existence of a Component that instantiates BeanPostProcessor.
71    * @param key the key of the component.
72    * @param type the type of the component.
73    * @param bpp the component that instantiates BeanPostProcessor.
74    */

75   public void addBeanPostProcessor(String JavaDoc key, Class JavaDoc type, Component bpp){
76     if(DestructionAwareBeanPostProcessor.class.isAssignableFrom(type)){
77       destructions.addBeanPostProcessor(key, bpp);
78     }
79     if(BeanPostProcessor.class.isAssignableFrom(type)){
80       processors.addBeanPostProcessor(key, bpp);
81     }
82   }
83   /**
84    * Get all the BeanPostProcessor components.
85    */

86   public BeanPostProcessorQueue getProcessorQueue(){
87     return processors;
88   }
89   /**
90    * Get all the DestructionAwareBeanPostProcessor components.
91    */

92   public BeanPostProcessorQueue getDestructionAwares(){
93     return destructions;
94   }
95 }
96
Popular Tags