You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by co...@google.com on 2008/06/05 19:47:42 UTC
[sling-scala commit] r4 - trunk/scala-standalone
Author: janandith
Date: Thu Jun 5 10:44:51 2008
New Revision: 4
Added:
trunk/scala-standalone/AbstractScriptEngineFactory.java
trunk/scala-standalone/AbstractSlingScriptEngine.java
trunk/scala-standalone/Scala.java
trunk/scala-standalone/ScalaScriptEngine.java
trunk/scala-standalone/ScalaScriptEngineFactory.java
Log:
Added: trunk/scala-standalone/AbstractScriptEngineFactory.java
==============================================================================
--- (empty file)
+++ trunk/scala-standalone/AbstractScriptEngineFactory.java Thu Jun 5
10:44:51 2008
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+
+public abstract class AbstractScriptEngineFactory implements
+ ScriptEngineFactory {
+
+ private String engineName;
+
+ private String engineVersion;
+
+ private List<String> extensions;
+
+ private List<String> mimeTypes;
+
+ private List<String> names;
+
+ protected AbstractScriptEngineFactory() {
+ String name = null;
+ String version = null;
+
+ // try to get the manifest
+ Manifest manifest = null;
+ InputStream ins = null;
+ try {
+ ins = getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
+ if (ins != null) {
+ manifest = new Manifest(ins);
+ Attributes attrs = manifest.getMainAttributes();
+ name = attrs.getValue("ScriptEngine-Name");
+ version = attrs.getValue("ScriptEngine-Version");
+ }
+ } catch (IOException ioe) {
+ // might want to log ?
+ } finally {
+ if (ins != null) {
+ try {
+ ins.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+
+ // fall back to class name and version zero
+ if (name == null) {
+ String className = getClass().getName();
+ name = className.substring(className.lastIndexOf('.') + 1);
+ }
+ if (version == null) {
+ version = "0";
+ }
+
+ setEngineName(name);
+ setEngineVersion(version);
+
+ setExtensions((String[]) null);
+ setMimeTypes((String[]) null);
+ setNames((String[]) null);
+ }
+
+ public String getEngineName() {
+ return engineName;
+ }
+
+ protected void setEngineName(String engineName) {
+ this.engineName = engineName;
+ }
+
+ public String getEngineVersion() {
+ return engineVersion;
+ }
+
+ protected void setEngineVersion(String engineVersion) {
+ this.engineVersion = engineVersion;
+ }
+
+ public List<String> getExtensions() {
+ return extensions;
+ }
+
+ protected void setExtensions(String... extensions) {
+ if (extensions == null) {
+ this.extensions = Collections.emptyList();
+ } else {
+ this.extensions = Arrays.asList(extensions);
+ }
+ }
+
+ public List<String> getMimeTypes() {
+ return mimeTypes;
+ }
+
+ protected void setMimeTypes(String... mimeTypes) {
+ if (mimeTypes == null) {
+ this.mimeTypes = Collections.emptyList();
+ } else {
+ this.mimeTypes = Arrays.asList(mimeTypes);
+ }
+ }
+
+ public List<String> getNames() {
+ return names;
+ }
+
+ protected void setNames(String... names) {
+ if (names == null) {
+ this.names = Collections.emptyList();
+ } else {
+ this.names = Arrays.asList(names);
+ }
+ }
+
+ public String getMethodCallSyntax(String obj, String m, String[]
args) {
+ StringBuffer callSyntax = new StringBuffer();
+ callSyntax.append(obj).append('.').append(m).append('(');
+ for (int i = 0; args != null && i < args.length; i++) {
+ if (i > 0) callSyntax.append(',');
+ callSyntax.append(args[i]);
+ }
+ callSyntax.append(')');
+ return callSyntax.toString();
+ }
+
+ public String getOutputStatement(String value) {
+ return "out.print(" + value + ")";
+ }
+
+ public Object getParameter(String name) {
+ if (ScriptEngine.ENGINE.equals(name)) {
+ return getEngineName();
+ } else if (ScriptEngine.ENGINE_VERSION.equals(name)) {
+ return getEngineVersion();
+ } else if (ScriptEngine.NAME.equals(name)) {
+ return getNames();
+ } else if (ScriptEngine.LANGUAGE.equals(name)) {
+ return getLanguageName();
+ } else if (ScriptEngine.LANGUAGE_VERSION.equals(name)) {
+ return getLanguageVersion();
+ }
+ return null;
+ }
+
+ public String getProgram(String[] arg0) {
+ return null;
+ }
+
+}
Added: trunk/scala-standalone/AbstractSlingScriptEngine.java
==============================================================================
--- (empty file)
+++ trunk/scala-standalone/AbstractSlingScriptEngine.java Thu Jun 5
10:44:51 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.StringReader;
+
+import javax.script.AbstractScriptEngine;
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptException;
+import javax.script.SimpleBindings;
+
+public abstract class AbstractSlingScriptEngine extends
AbstractScriptEngine {
+
+ private final ScriptEngineFactory scriptEngineFactory;
+
+ protected AbstractSlingScriptEngine(ScriptEngineFactory
scriptEngineFactory) {
+ this.scriptEngineFactory = scriptEngineFactory;
+ }
+
+ public Bindings createBindings() {
+ return new SimpleBindings();
+ }
+
+ public Object eval(String script, ScriptContext context) throws
ScriptException {
+ StringReader reader = new StringReader(script);
+ return eval(reader, context);
+ }
+
+ public ScriptEngineFactory getFactory() {
+ return scriptEngineFactory;
+ }
+
+}
Added: trunk/scala-standalone/Scala.java
==============================================================================
--- (empty file)
+++ trunk/scala-standalone/Scala.java Thu Jun 5 10:44:51 2008
@@ -0,0 +1,30 @@
+
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngine;
+
+
+
+public class Scala {
+
+ public static void main(String[] args)
+ {
+ ScriptEngineFactory f = new ScalaScriptEngineFactory();
+ System.out.println("created scala factory");
+
+ ScriptEngine e = f.getScriptEngine();
+ System.out.println("created scala engine");
+
+ Object sresult = null;
+ try {
+ sresult = e.eval("4+2");
+ System.out.println("result eveluated");
+ } catch (Exception ex) {
+
+ ex.printStackTrace();
+ }
+ System.out.println(sresult.toString());
+
+
+ }
+
+}
Added: trunk/scala-standalone/ScalaScriptEngine.java
==============================================================================
--- (empty file)
+++ trunk/scala-standalone/ScalaScriptEngine.java Thu Jun 5 10:44:51 2008
@@ -0,0 +1,84 @@
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptException;
+import javax.script.ScriptEngineFactory;
+
+import scala.tools.nsc.Interpreter;
+import scala.tools.nsc.Settings;
+
+
+public class ScalaScriptEngine extends AbstractSlingScriptEngine{
+
+ Settings interpreterSettings = null;
+ Interpreter interpreter = null;
+ File engineLog = new File("index.html");
+ PrintWriter logPrinter = null;
+ StringWriter interpreterOutput = null;
+
+ public ScalaScriptEngine(ScriptEngineFactory factory) {
+
+
+ super(factory);
+
+ try{
+
+ interpreterOutput = new StringWriter();
+ logPrinter = new PrintWriter(interpreterOutput);
+ interpreterSettings = new Settings();
+
+
+
+ interpreter = new Interpreter(interpreterSettings , logPrinter);
+
+ }catch(Exception e){
+
+ e.printStackTrace();
+
+ }
+
+
+ }
+
+ public Object eval(Reader script , ScriptContext scriptContext)
throws ScriptException
+ {
+ Object result =null;
+
+ try {
+ StringBuffer scriptString = new StringBuffer();
+ BufferedReader bufferedScript = new BufferedReader(script);
+ String nextLine = bufferedScript.readLine();
+ while (nextLine != null) {
+ scriptString.append(nextLine);
+ scriptString.append("\n");
+ nextLine = bufferedScript.readLine();
+ }
+
+ result = interpreter.interpret(script.toString());
+
+ }
+ catch(Exception ex)
+ {
+ ex.printStackTrace();
+ }
+
+ return result;
+ }
+
+
+ public Object eval(String script , ScriptContext scriptContext)
throws ScriptException
+ {
+ Object result = interpreter.interpret(script);
+ System.out.println(interpreterOutput.toString());
+ return result;
+
+ }
+
+
+
+}
Added: trunk/scala-standalone/ScalaScriptEngineFactory.java
==============================================================================
--- (empty file)
+++ trunk/scala-standalone/ScalaScriptEngineFactory.java Thu Jun 5
10:44:51 2008
@@ -0,0 +1,53 @@
+/*
+ * ScalaScriptEngineFactory.java
+ *
+ * Created on April 15, 2008, 9:49 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+
+import javax.script.ScriptEngine;
+
+
+
+/**
+ *
+ * @author Janandith
+ */
+
+public class ScalaScriptEngineFactory extends
AbstractScriptEngineFactory {
+
+ public final static String SCALA_SCRIPT_EXTENSION = "scala";
+
+ public final static String SCALA_MIME_TYPE = "text/scala";
+
+ public final static String SHORT_NAME = "scala";
+
+ /** Creates a new instance of ScalaScriptEngineFactory */
+ public ScalaScriptEngineFactory() {
+
+ setExtensions(SCALA_SCRIPT_EXTENSION);
+ setMimeTypes(SCALA_MIME_TYPE);
+ setNames(SHORT_NAME);
+ }
+
+ public ScriptEngine getScriptEngine(){
+
+ return new ScalaScriptEngine(this);
+ }
+
+ public String getLanguageName(){
+
+ return "scala";
+ }
+
+ public String getLanguageVersion(){
+
+ return "2.7.1";
+ }
+
+}
+
+