KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jfun.yan.xml.nuts.spring;
2
3 import java.io.IOException JavaDoc;
4
5 import jfun.yan.Container;
6 import jfun.yan.containers.ManualContainer;
7 import jfun.yan.lifecycle.DefaultLifecycleManager;
8 import jfun.yan.lifecycle.Procedure;
9 import jfun.yan.spring.Container2ApplicationContext;
10 import jfun.yan.xml.NutsProcessor;
11 import org.springframework.beans.factory.DisposableBean;
12
13 /**
14  * The utility class to decrate a Nuts Interpreter object with tags that are
15  * aware of Spring beans.
16  * <p>
17  * @author Ben Yu
18  * Nov 17, 2005 2:57:21 PM
19  */

20 public class SpringNuts {
21
22   /**
23    * Make an processor spring-aware by registering spring-aware
24    * nuts tags such as ctor, bean, method etc.
25    * @param module_name the name of the current module.
26    * @param processor the processor object.
27    * @param loader the class loader used to load the spring-aware
28    * nuts classes.
29    */

30   public static void setSpringAware(String JavaDoc module_name,
31       NutsProcessor processor,
32       ClassLoader JavaDoc loader){
33     setSpringAware(module_name, processor, loader,
34         "jfun/yan/xml/nuts/spring/spring.xml");
35   }
36   /**
37    * Make an processor spring-aware by registering spring-aware
38    * nuts tags such as ctor, bean, method etc.
39    * @param module_name the name of the current module.
40    * @param processor the processor object.
41    * @param loader the class loader used to load the spring-aware
42    * @param nuts_resource the resource name of the config file
43    * that configures the nut classes.
44    * nuts classes.
45    */

46   public static void setSpringAware(String JavaDoc module_name,
47       NutsProcessor processor,
48       ClassLoader JavaDoc loader, String JavaDoc nuts_resource){
49     setSpringAware(module_name, processor, loader, nuts_resource,
50         "jfun/yan/xml/nuts/spring/appcontext.xml");
51   }
52
53   /**
54    * Make an processor spring-aware by registering spring-aware
55    * nuts tags such as ctor, bean, method etc.
56    * @param module_name the name of the current module.
57    * @param processor the processor object.
58    * @param loader the class loader used to load the spring-aware
59    * @param nuts_resource the resource name of the config file
60    * that configures the nut classes.
61    * @param appcontext_resource the resource name of the config file
62    * that configures the ApplicationContext.
63    */

64   public static void setSpringAware(String JavaDoc module_name,
65       NutsProcessor processor,
66       ClassLoader JavaDoc loader, String JavaDoc nuts_resource,
67       String JavaDoc appcontext_resource){
68     final NutsProcessor spring =
69       processor.createMetaProcessor(loader);
70     final Container yan = processor.getContainer();
71     /*
72     final ReloadableResourceBundleMessageSource msgsrc = new ReloadableResourceBundleMessageSource();
73     msgsrc.setBasename("org/springframework/context/support/messages");
74     final ApplicationEventPublisher aep = new ApplicationEventPublisher(){
75       
76     }*/

77     final Container base = new ManualContainer();
78     base.registerValue("modulename", module_name);
79     base.registerValue("timestamp", new Long JavaDoc(System.currentTimeMillis()));
80     base.registerValue("classloader", loader);
81     base.registerValue("container", yan);
82     final NutsProcessor cinterpreter = new NutsProcessor();
83     //cinterpreter.setParameterWiring(Modes.params_bytype);
84
try{
85       cinterpreter.processResource(appcontext_resource);
86     }
87     catch(IOException JavaDoc e){
88       throw new IllegalStateException JavaDoc("cannot startup spring integration.");
89     }
90     
91     final Container context = cinterpreter.getContainer().inherit(base);
92     final Container2ApplicationContext ctxt = (Container2ApplicationContext)
93     context.getInstance("appcontext");
94     
95     final Container nuts = spring.getContainer();
96     final DefaultLifecycleManager manager = processor.getLifecycleManager();
97     
98     //final BeanPostProcessorQueue bppq = new BeanPostProcessorQueue();
99
//nuts.registerValue(newBeanLifecycle(manager));
100
//nuts.registerValue(ctxt);
101
//nuts.registerValue(yan);
102
//nuts.registerValue(bppq);
103
nuts.registerValue(new SpringService(ctxt, manager));
104     final String JavaDoc resource = nuts_resource;
105     try{
106       spring.processResource(resource);
107     }
108     catch(IOException JavaDoc e){
109       throw new IllegalStateException JavaDoc("cannot load spring nuts: "
110           + resource);
111     }
112     processor.loadNutsFromContainer(nuts);
113   }
114
115   /**
116    * Make an processor spring-aware by registering spring-aware
117    * nuts tags such as ctor, bean, method etc.
118    * <p>
119    * The class loader for component classes is used to load
120    * the spring nut classes.
121    * </p>
122    * @param module_name the current module name.
123    * @param processor the processor object.
124    */

125   public static void setSpringAware(String JavaDoc module_name, NutsProcessor processor){
126     setSpringAware(module_name, processor, processor.getClassLoader());
127   }
128   /**
129    * Make an processor spring-aware by registering spring-aware
130    * nuts tags such as ctor, bean, method etc.
131    * <p>
132    * The class loader for component classes is used to load
133    * the spring nut classes.
134    * </p>
135    * @param module_name the current module name.
136    * @param processor the processor object.
137    * @param nuts_resource the resource name of the config file
138    * that configures the nut classes.
139    */

140   public static void setSpringAware(String JavaDoc module_name, NutsProcessor processor,
141       String JavaDoc nuts_resource){
142     setSpringAware(module_name, processor, processor.getClassLoader(), nuts_resource);
143   }
144   private static DefaultLifecycleManager.DefaultLifecycle newBeanLifecycle(
145       DefaultLifecycleManager man){
146     return man.newLifecycle().disposer(new Procedure(){
147       public void invoke(Object JavaDoc self, Object JavaDoc[] args) throws Throwable JavaDoc {
148         ((DisposableBean)self).destroy();
149       }
150     });
151   }
152 }
153
Popular Tags