001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.core;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.FxContext;
037: import com.flexive.shared.exceptions.FxApplicationException;
038: import com.flexive.shared.exceptions.FxInvalidParameterException;
039: import com.flexive.shared.scripting.FxScriptBinding;
040: import com.flexive.shared.scripting.FxScriptResult;
041:
042: import javax.script.ScriptEngineFactory;
043: import javax.script.ScriptEngine;
044: import java.io.Serializable;
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: /**
049: * Java 6 Scripting extensions for [fleXive]
050: *
051: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: * @version $Rev
053: */
054: public class JDK6Scripting {
055:
056: /**
057: * Run a script.
058: * <b>This method is internal and intended to be exclusively called by the ScriptingEngine!</b>
059: * If its a groovy script (determined by extension "gy" or "groovy"), the native groovy shell is used, else
060: * a scripting engine registered with the JDK is probed.
061: *
062: * @param name name of the script - needed to determine language
063: * @param binding bindings
064: * @param code code to execute
065: * @return FxScriptResult
066: * @throws FxApplicationException on errors
067: */
068: public static FxScriptResult runScript(String name,
069: FxScriptBinding binding, String code)
070: throws FxApplicationException {
071: if (name == null)
072: name = "unknown";
073: if (binding != null) {
074: if (!binding.getProperties().containsKey("ticket"))
075: binding.setVariable("ticket", FxContext.get()
076: .getTicket());
077: if (!binding.getProperties().containsKey("environment"))
078: binding.setVariable("environment", CacheAdmin
079: .getEnvironment());
080: binding.setVariable("scriptname", name);
081: }
082: try {
083: Class.forName("javax.script.ScriptEngine"); //provoke exception if no JDK >= 6 installed
084: String ext = name.substring(name.lastIndexOf('.') + 1);
085: javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager();
086: javax.script.ScriptEngine engine = manager
087: .getEngineByExtension(ext);
088: if (engine == null)
089: throw new FxInvalidParameterException(name,
090: "ex.general.scripting.noEngine", name)
091: .asRuntimeException();
092: javax.script.Bindings b = engine.createBindings();
093: if (binding != null)
094: b.putAll(binding.getProperties());
095: b.put(ScriptEngine.FILENAME, name);
096: engine.setBindings(b,
097: javax.script.ScriptContext.ENGINE_SCOPE);
098: Object result = engine.eval(code);
099: if (binding != null) {
100: binding.getProperties().clear();
101: Object o;
102: for (String key : engine.getBindings(
103: javax.script.ScriptContext.ENGINE_SCOPE)
104: .keySet()) {
105: o = engine.getBindings(
106: javax.script.ScriptContext.ENGINE_SCOPE)
107: .get(key);
108: if (o instanceof Serializable)
109: binding.getProperties().put(key,
110: (Serializable) o);
111: }
112: }
113: return new FxScriptResult(binding, result);
114: } catch (ClassNotFoundException cnfe) {
115: throw new FxInvalidParameterException(name,
116: "ex.general.scripting.needJDK6", name)
117: .asRuntimeException();
118: } catch (javax.script.ScriptException e) {
119: throw new FxInvalidParameterException(name,
120: "ex.general.scripting.exception", name, e
121: .getMessage()).asRuntimeException();
122: }
123: }
124:
125: /**
126: * Get a list of all available scripting engines
127: *
128: * @return list of all available scripting engines, [0]=ext, [1]=description
129: */
130: public static List<String[]> getAvailableScriptEngines() {
131: List<String[]> res = new ArrayList<String[]>(5);
132: for (ScriptEngineFactory f : new javax.script.ScriptEngineManager()
133: .getEngineFactories()) {
134: for (String ext : f.getExtensions())
135: res.add(new String[] {
136: ext,
137: ext + ": " + f.getLanguageName() + " v"
138: + f.getLanguageVersion() + " ("
139: + f.getEngineName() + " v"
140: + f.getEngineVersion() + ")" });
141: }
142: return res;
143: }
144:
145: }
|