KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scripting > jruby > JRubyScriptFactory


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scripting.jruby;
18
19 import java.io.IOException JavaDoc;
20
21 import org.jruby.RubyException;
22 import org.jruby.exceptions.JumpException;
23 import org.jruby.exceptions.RaiseException;
24
25 import org.springframework.beans.factory.BeanClassLoaderAware;
26 import org.springframework.scripting.ScriptCompilationException;
27 import org.springframework.scripting.ScriptFactory;
28 import org.springframework.scripting.ScriptSource;
29 import org.springframework.util.Assert;
30 import org.springframework.util.ClassUtils;
31
32 /**
33  * {@link org.springframework.scripting.ScriptFactory} implementation
34  * for a JRuby script.
35  *
36  * <p>Typically used in combination with a
37  * {@link org.springframework.scripting.support.ScriptFactoryPostProcessor};
38  * see the latter's javadoc for a configuration example.
39  *
40  * @author Juergen Hoeller
41  * @author Rob Harrop
42  * @since 2.0
43  * @see JRubyScriptUtils
44  * @see org.springframework.scripting.support.ScriptFactoryPostProcessor
45  */

46 public class JRubyScriptFactory implements ScriptFactory, BeanClassLoaderAware {
47
48     private final String JavaDoc scriptSourceLocator;
49
50     private final Class JavaDoc[] scriptInterfaces;
51
52     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
53
54
55     /**
56      * Create a new JRubyScriptFactory for the given script source.
57      * @param scriptSourceLocator a locator that points to the source of the script.
58      * Interpreted by the post-processor that actually creates the script.
59      * @param scriptInterfaces the Java interfaces that the scripted object
60      * is supposed to implement
61      */

62     public JRubyScriptFactory(String JavaDoc scriptSourceLocator, Class JavaDoc[] scriptInterfaces) {
63         Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
64         Assert.notEmpty(scriptInterfaces, "'scriptInterfaces' must not be empty");
65         this.scriptSourceLocator = scriptSourceLocator;
66         this.scriptInterfaces = scriptInterfaces;
67     }
68
69
70     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
71         this.beanClassLoader = classLoader;
72     }
73
74
75     public String JavaDoc getScriptSourceLocator() {
76         return this.scriptSourceLocator;
77     }
78
79     public Class JavaDoc[] getScriptInterfaces() {
80         return this.scriptInterfaces;
81     }
82
83     /**
84      * JRuby scripts do require a config interface.
85      */

86     public boolean requiresConfigInterface() {
87         return true;
88     }
89
90     /**
91      * Load and parse the JRuby script via JRubyScriptUtils.
92      * @see JRubyScriptUtils#createJRubyObject(String, Class[], ClassLoader)
93      */

94     public Object JavaDoc getScriptedObject(ScriptSource scriptSource, Class JavaDoc[] actualInterfaces)
95             throws IOException JavaDoc, ScriptCompilationException {
96         try {
97             return JRubyScriptUtils.createJRubyObject(
98                     scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
99         }
100         catch (RaiseException ex) {
101             RubyException rubyEx = ex.getException();
102             String JavaDoc msg = (rubyEx != null && rubyEx.message != null) ?
103                     rubyEx.message.toString() : "Unexpected JRuby error";
104             throw new ScriptCompilationException(
105                     "Could not compile JRuby script [" + scriptSource + "]: " + msg, ex);
106         }
107         catch (JumpException ex) {
108             throw new ScriptCompilationException(
109                     "Could not compile JRuby script [" + scriptSource + "]", ex);
110         }
111     }
112
113     public Class JavaDoc getScriptedObjectType(ScriptSource scriptSource)
114             throws IOException JavaDoc, ScriptCompilationException {
115
116         return null;
117     }
118
119
120     public String JavaDoc toString() {
121         return "JRubyScriptFactory: script source locator [" + this.scriptSourceLocator + "]";
122     }
123
124 }
125
Popular Tags