root/tags/0.2/src/springy/context/JRubyApplicationContext.java

Revision 6999 (checked in by jan, 2 years ago)

version 0.2

Line 
1 package springy.context;
2
3 import org.apache.bsf.BSFException;
4 import org.apache.bsf.BSFManager;
5 import org.jruby.RubyArray;
6 import org.jruby.exceptions.RaiseException;
7 import org.springframework.beans.BeansException;
8 import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
9 import org.springframework.beans.factory.parsing.Location;
10 import org.springframework.beans.factory.parsing.Problem;
11 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
12 import org.springframework.context.support.AbstractRefreshableApplicationContext;
13 import org.springframework.core.io.ByteArrayResource;
14 import org.springframework.core.io.Resource;
15 import org.w3c.dom.Document;
16 import springy.util.IOHelper;
17 import springy.util.JRubyHelper;
18
19 import java.io.IOException;
20 import java.util.Map;
21
22 /**
23  * A Spring application context configured with a Ruby DSL.
24  */
25 public class JRubyApplicationContext extends AbstractRefreshableApplicationContext {
26
27     private String serializedContext;
28     private Document serializedContextAsDocument;
29
30     static {
31         BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String[]{"rb"});
32     }
33
34     private Resource contextResource;
35     private BSFManager bsfManager;
36
37     /**
38      * @param context as a string. used for testing.
39      */
40     public JRubyApplicationContext(String context) {
41         this(new ByteArrayResource(context.getBytes()));
42     }
43
44     /**
45      * @param aContextResource where to find the ruby configuration
46      */
47     public JRubyApplicationContext(Resource aContextResource) {
48         this(aContextResource, true);
49     }
50
51     /**
52      * @param aContextResource where to find the ruby configuration
53      * @param refresh          refreshs the context immediately
54      */
55     public JRubyApplicationContext(Resource aContextResource, boolean refresh) {
56         bsfManager = new BSFManager();
57
58         this.contextResource = aContextResource;
59
60         if (refresh) {
61             refresh();
62         }
63     }
64
65     /**
66      * @return the context serialized as xml document.
67      */
68     public String getContextAsXml() {
69         serializeContext();
70         return serializedContext;
71     }
72
73     /**
74      * @return the context serialized as xml document.
75      */
76     public Document getContextAsDocument() {
77         serializeContext();
78         return serializedContextAsDocument;
79     }
80
81     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException, BeansException {
82         try {
83             bsfManager.declareBean("bean_factory", beanFactory, DefaultListableBeanFactory.class);
84             bsfManager.declareBean("system_properties", System.getProperties(), Map.class);
85             bsfManager.declareBean("bsf_manager", bsfManager, BSFManager.class);
86
87             String springy = IOHelper.inputStreamToString(getClass().getResourceAsStream("springy.rb"));
88             String ctxt = IOHelper.inputStreamToString(contextResource.getInputStream());
89
90             bsfManager.eval("ruby", "(java-springy)", 1, 1, springy);
91             bsfManager.eval("ruby", "(java-context)", 1, 1, ctxt);
92
93         } catch (BSFException e) {
94             //JRubyHelper.printBsfException(e);
95
96             RaiseException rex = (RaiseException) e.getTargetException();
97             RubyArray array = (RubyArray) rex.getException().backtrace();
98             String lastLine = null;
99             if (array.getLength() > 0) {
100                 lastLine = array.get(array.getLength() - 1).toString();
101             }
102
103             String message = rex.getMessage();
104             if (message == null) {
105                 message = rex.getException().message.toString();
106             }
107
108             throw new BeanDefinitionParsingException(
109                     new Problem(lastLine + ": " + message,
110                             new Location(contextResource, lastLine)));
111         }
112     }
113
114     /**
115      * Serializes the context to XML.
116      */
117     private void serializeContext() {
118         if (serializedContext == null || serializedContextAsDocument == null) {
119             if (!isActive())
120                 refreshBeanFactory();
121
122             try {
123                 RubyArray a = (RubyArray) bsfManager.eval("ruby", "(serialize-context)", 1, 1, "serialize_context");
124                 serializedContext = a.get(0).toString();
125                 serializedContextAsDocument = (Document) a.get(1);
126             } catch (BSFException e) {
127                 JRubyHelper.printBsfException(e);
128                 throw new RuntimeException(e);
129             }
130         }
131     }
132 }
133
Note: See TracBrowser for help on using the browser.