@@ -109,31 +109,6 @@ public void setBeanClassLoader(ClassLoader classLoader) {
109
109
this .beanClassLoader = classLoader ;
110
110
}
111
111
112
- protected ScriptEngine retrieveScriptEngine (ScriptSource scriptSource ) {
113
- ScriptEngineManager scriptEngineManager = new ScriptEngineManager (this .beanClassLoader );
114
- if (this .scriptEngineName != null ) {
115
- ScriptEngine engine = scriptEngineManager .getEngineByName (this .scriptEngineName );
116
- if (engine == null ) {
117
- throw new IllegalStateException ("Script engine named '" + this .scriptEngineName + "' not found" );
118
- }
119
- return engine ;
120
- }
121
- if (scriptSource instanceof ResourceScriptSource ) {
122
- String filename = ((ResourceScriptSource ) scriptSource ).getResource ().getFilename ();
123
- if (filename != null ) {
124
- String extension = StringUtils .getFilenameExtension (filename );
125
- if (extension != null ) {
126
- ScriptEngine engine = scriptEngineManager .getEngineByExtension (extension );
127
- if (engine != null ) {
128
- return engine ;
129
- }
130
- }
131
- }
132
- }
133
- return null ;
134
- }
135
-
136
-
137
112
@ Override
138
113
public String getScriptSourceLocator () {
139
114
return this .scriptSourceLocator ;
@@ -157,54 +132,18 @@ public boolean requiresConfigInterface() {
157
132
public Object getScriptedObject (ScriptSource scriptSource , Class <?>... actualInterfaces )
158
133
throws IOException , ScriptCompilationException {
159
134
160
- Object script ;
161
-
162
- try {
163
- if (this .scriptEngine == null ) {
164
- this .scriptEngine = retrieveScriptEngine (scriptSource );
165
- if (this .scriptEngine == null ) {
166
- throw new IllegalStateException ("Could not determine script engine for " + scriptSource );
167
- }
168
- }
169
- script = this .scriptEngine .eval (scriptSource .getScriptAsString ());
170
- }
171
- catch (Exception ex ) {
172
- throw new ScriptCompilationException (scriptSource , ex );
173
- }
135
+ Object script = evaluateScript (scriptSource );
174
136
175
137
if (!ObjectUtils .isEmpty (actualInterfaces )) {
176
138
boolean adaptationRequired = false ;
177
139
for (Class <?> requestedIfc : actualInterfaces ) {
178
- if (!requestedIfc .isInstance (script )) {
140
+ if (script instanceof Class ? !requestedIfc .isAssignableFrom ((Class <?>) script ) :
141
+ !requestedIfc .isInstance (script )) {
179
142
adaptationRequired = true ;
180
143
}
181
144
}
182
145
if (adaptationRequired ) {
183
- Class <?> adaptedIfc ;
184
- if (actualInterfaces .length == 1 ) {
185
- adaptedIfc = actualInterfaces [0 ];
186
- }
187
- else {
188
- adaptedIfc = ClassUtils .createCompositeInterface (actualInterfaces , this .beanClassLoader );
189
- }
190
- if (adaptedIfc != null ) {
191
- if (!(this .scriptEngine instanceof Invocable )) {
192
- throw new ScriptCompilationException (scriptSource ,
193
- "ScriptEngine must implement Invocable in order to adapt it to an interface: " +
194
- this .scriptEngine );
195
- }
196
- Invocable invocable = (Invocable ) this .scriptEngine ;
197
- if (script != null ) {
198
- script = invocable .getInterface (script , adaptedIfc );
199
- }
200
- if (script == null ) {
201
- script = invocable .getInterface (adaptedIfc );
202
- if (script == null ) {
203
- throw new ScriptCompilationException (scriptSource ,
204
- "Could not adapt script to interface [" + adaptedIfc .getName () + "]" );
205
- }
206
- }
207
- }
146
+ script = adaptToInterfaces (script , scriptSource , actualInterfaces );
208
147
}
209
148
}
210
149
@@ -226,6 +165,75 @@ public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInt
226
165
return script ;
227
166
}
228
167
168
+ protected Object evaluateScript (ScriptSource scriptSource ) {
169
+ try {
170
+ if (this .scriptEngine == null ) {
171
+ this .scriptEngine = retrieveScriptEngine (scriptSource );
172
+ if (this .scriptEngine == null ) {
173
+ throw new IllegalStateException ("Could not determine script engine for " + scriptSource );
174
+ }
175
+ }
176
+ return this .scriptEngine .eval (scriptSource .getScriptAsString ());
177
+ }
178
+ catch (Exception ex ) {
179
+ throw new ScriptCompilationException (scriptSource , ex );
180
+ }
181
+ }
182
+
183
+ protected ScriptEngine retrieveScriptEngine (ScriptSource scriptSource ) {
184
+ ScriptEngineManager scriptEngineManager = new ScriptEngineManager (this .beanClassLoader );
185
+
186
+ if (this .scriptEngineName != null ) {
187
+ return StandardScriptUtils .retrieveEngineByName (scriptEngineManager , this .scriptEngineName );
188
+ }
189
+
190
+ if (scriptSource instanceof ResourceScriptSource ) {
191
+ String filename = ((ResourceScriptSource ) scriptSource ).getResource ().getFilename ();
192
+ if (filename != null ) {
193
+ String extension = StringUtils .getFilenameExtension (filename );
194
+ if (extension != null ) {
195
+ ScriptEngine engine = scriptEngineManager .getEngineByExtension (extension );
196
+ if (engine != null ) {
197
+ return engine ;
198
+ }
199
+ }
200
+ }
201
+ }
202
+
203
+ return null ;
204
+ }
205
+
206
+ protected Object adaptToInterfaces (Object script , ScriptSource scriptSource , Class <?>... actualInterfaces ) {
207
+ Class <?> adaptedIfc ;
208
+ if (actualInterfaces .length == 1 ) {
209
+ adaptedIfc = actualInterfaces [0 ];
210
+ }
211
+ else {
212
+ adaptedIfc = ClassUtils .createCompositeInterface (actualInterfaces , this .beanClassLoader );
213
+ }
214
+
215
+ if (adaptedIfc != null ) {
216
+ if (!(this .scriptEngine instanceof Invocable )) {
217
+ throw new ScriptCompilationException (scriptSource ,
218
+ "ScriptEngine must implement Invocable in order to adapt it to an interface: " +
219
+ this .scriptEngine );
220
+ }
221
+ Invocable invocable = (Invocable ) this .scriptEngine ;
222
+ if (script != null ) {
223
+ script = invocable .getInterface (script , adaptedIfc );
224
+ }
225
+ if (script == null ) {
226
+ script = invocable .getInterface (adaptedIfc );
227
+ if (script == null ) {
228
+ throw new ScriptCompilationException (scriptSource ,
229
+ "Could not adapt script to interface [" + adaptedIfc .getName () + "]" );
230
+ }
231
+ }
232
+ }
233
+
234
+ return script ;
235
+ }
236
+
229
237
@ Override
230
238
public Class <?> getScriptedObjectType (ScriptSource scriptSource )
231
239
throws IOException , ScriptCompilationException {
0 commit comments