Menu

[r381]: / trunk / php-java-bridge / server / javax / script / ScriptEngineManager.java  Maximize  Restore  History

Download this file

258 lines (220 with data), 8.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package javax.script;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
* ScriptEngineManager implements a discovery and instantiation
* mechanisams for ScriptEngine class. It also contains a collection
* of key-value pairs which storing state shared by all engines
* created by manager.
*
* Nandika Jayawardana <nandika@opensource.lk>
* Sanka Samaranayake <sanka@opensource.lk>
*/
public class ScriptEngineManager {
/** Stores all instances of classes which implements
* ScriptEngineFactory which are found in resources
* META-INF/services/javax.script.ScriptEngineFactory
*/
protected HashSet engineSpis = new HashSet();
/**
* Stores language names with an associated
* ScriptEngineFactory
*/
protected HashMap nameAssociations = new HashMap();
/**
* Stores file extensions with an associated
* ScriptEngineFactory
*/
protected HashMap extensionAssocitions = new HashMap();
/** Stores MIME types with an associated ScriptEngineFactory */
protected HashMap mimeTypeAssociations = new HashMap();
/** Stores the namespace associated with GLOBAL_SCOPE */
protected Bindings globalscope = new SimpleBindings();
private Iterator getServiceProviders() {
Iterator i = null;
try {
Class c = Class.forName("gnu.classpath.ServiceFactory");
Method m = c.getMethod("lookupProviders", new Class[]{java.lang.Class.class});
try {m.setAccessible(true);} catch (Throwable tt) {/*ignore*/}
i = (Iterator) m.invoke(c, new Class[]{ScriptEngineFactory.class});
} catch (Throwable ex) {/*ignore*/}
if(i!=null) return i;
try {
Class c = Class.forName("sun.misc.Service");
Method m = c.getMethod("providers", new Class[]{java.lang.Class.class});
try {m.setAccessible(true);} catch (Throwable tt) {/*ignore*/}
i = (Iterator) m.invoke(c, new Class[]{ScriptEngineFactory.class});
} catch (Throwable ex) {/*ignore*/}
return i;
}
/**
* Constructs ScriptEngineManager and initializes it.
*/
public ScriptEngineManager() {
Iterator iterator = getServiceProviders();
while (iterator.hasNext()) {
ScriptEngineFactory factory = (ScriptEngineFactory) iterator.next();
engineSpis.add(factory);
String[] data = (String[]) factory.getNames().toArray(new String[]{});
// gets all descriptinve names for Scripting Engine
for (int i=0; i<data.length; i++) {
nameAssociations.put(data[i], factory);
}
// gets all supported extensions
data = (String[]) factory.getExtensions().toArray(new String[]{});
for (int i=0; i<data.length; i++) {
extensionAssocitions.put(data[i], factory);
}
// gets all supported MIME types
data = (String[]) factory.getMimeTypes().toArray(new String[]{});
for (int i=0; i<data.length; i++) {
mimeTypeAssociations.put(data[i], factory);
}
}
}
/**
* Retrieves the associated value for the spefied key in the
* GLOBAL_SCOPE
*
* @param key the associated key of the value stored in the
* GLOBAL_SCOPE
* @return the value associated with the specifed key
*/
public Object get(String key){
return globalscope.get(key);
}
/**
* Retrieves a new instance of a ScriptingEngine for the
* specified extension of a scirpt file. Returns null if no
* suitable ScriptingEngine is found.
*
* @param extension the specified extension of a script file
* @return a new instance of a ScriptingEngine which supports the
* specified script file extension
*/
public ScriptEngine getEngineByExtention(String extension){
ScriptEngine engine = null;
ScriptEngineFactory factory =
(ScriptEngineFactory) extensionAssocitions.get(extension);
if (factory != null) {
// gets a new instance of the Scripting Engine
engine = factory.getScriptEngine();
// sets the GLOBAL SCOPE
engine.setBindings(globalscope,ScriptContext.GLOBAL_SCOPE);
}
return engine;
}
/**
* Retrieves new instance the ScriptingEngine for a specifed MIME
* type. Returns null if no suitable ScriptingEngine is found.
*
* @param mimeType the specified MIME type
* @return a new instance of a ScriptingEngine which supports the
* specified MIME type
*/
public ScriptEngine getEngineByMimeType(String mimeType){
ScriptEngine engine = null;
ScriptEngineFactory factory =
(ScriptEngineFactory) mimeTypeAssociations.get(mimeType);
if (factory != null) {
// gets a new instance of the Scripting Engine
engine = factory.getScriptEngine();
// sets the GLOBAL SCOPE
engine.setBindings(globalscope,ScriptContext.GLOBAL_SCOPE);
}
return engine;
}
/**
* Retrieves a new instance of a ScriptEngine the specified
* descriptieve name. Returns null if no suitable ScriptEngine is
* found.
*
* @param name the descriptive name
* @return a new instance of a ScriptEngine which supports the
* specifed descriptive name
*/
public ScriptEngine getEngineByName(String name){
ScriptEngine engine = null;
ScriptEngineFactory factory =
(ScriptEngineFactory) nameAssociations.get(name);
if (factory != null) {
engine = factory.getScriptEngine();
engine.setBindings(globalscope,ScriptContext.GLOBAL_SCOPE);
}
return engine;
}
/**
* Retrieves an array of instances of ScriptEngineFactory class
* which are found by the discovery mechanism.
*
* @return an array of all discovered ScriptEngineFactory
* instances
*/
public ScriptEngineFactory[] getEngineFactories(){
return (ScriptEngineFactory[])engineSpis.toArray();
}
/**
* Retrieves the namespace corresponds to GLOBAL_SCOPE.
*
* @return the namespace of GLOBAL_SCOPE
*/
public Bindings getBindings(){
return globalscope;
}
/**
* Associates the specifed value with the specified key in
* GLOBAL_SCOPE.
*
* @param key the associated key for specified value
* @param value the associated value for the specified key
*/
public void put(String key,Object value){
globalscope.put(key,value);
}
/**
* Register a extension with a ScriptEngineFactory class. It
* overrides any such association discovered previously.
*
* @param extension the extension associated with the specified
* ScriptEngineFactory class
* @param factory the ScriptEngineFactory class associated with
* the specified extension
*/
public void registerEngineExtension(String extension, Class factory){
extensionAssocitions.put(extension, factory);
}
/**
* Registers descriptive name with a ScriptEngineFactory class.
* It overrides any associations discovered previously.
*
* @param name a descriptive name associated with the specifed
* ScriptEngineFactory class
* @param factory the ScriptEngineFactory class associated with
* the specified descriptive name
*/
public void registerEngineName(String name, Class factory){
nameAssociations.put(name, factory);
}
/**
* Registers a MIME type with a ScriptEngineFactory class. It
* overrides any associations discovered previously.
*
* @param mimeType the MIME type associated with specified
* ScriptEngineFactory class
* @param factory the ScriptEngineFactory associated with the
* specified MIME type
*/
public void registerEngineMimeType(String mimeType,Class factory){
mimeTypeAssociations.put(mimeType,factory);
}
/**
* Sets the GLOBAL_SCOPE value to the specified namespace.
*
* @param namespace the namespace to be stored in GLOBAL_SCOPE
*/
public void setBindings(Bindings namespace){
globalscope = namespace;
}
}