You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by do...@apache.org on 2009/08/07 16:03:40 UTC

svn commit: r802014 - in /felix/sandbox/donsez/runscript: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/felix/ src/main/java/org/apache/felix/sandbox/ src/main/java/org/apache/felix/sandbox/runsc...

Author: donsez
Date: Fri Aug  7 14:03:40 2009
New Revision: 802014

URL: http://svn.apache.org/viewvc?rev=802014&view=rev
Log:
creation of a bundle that provides a command to run scripts using JSR 223 engines.

Added:
    felix/sandbox/donsez/runscript/
    felix/sandbox/donsez/runscript/pom.xml   (with props)
    felix/sandbox/donsez/runscript/src/
    felix/sandbox/donsez/runscript/src/main/
    felix/sandbox/donsez/runscript/src/main/java/
    felix/sandbox/donsez/runscript/src/main/java/org/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java   (with props)
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java   (with props)
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java   (with props)
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/
    felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java   (with props)
    felix/sandbox/donsez/runscript/src/site/
    felix/sandbox/donsez/runscript/src/site/bench.js   (with props)
    felix/sandbox/donsez/runscript/src/site/echocommand.js   (with props)
    felix/sandbox/donsez/runscript/src/site/extendersample.js   (with props)
    felix/sandbox/donsez/runscript/src/site/hello.groovy
    felix/sandbox/donsez/runscript/src/site/hello.js   (with props)
    felix/sandbox/donsez/runscript/src/site/ps.js   (with props)
    felix/sandbox/donsez/runscript/src/site/readme.html   (with props)
    felix/sandbox/donsez/runscript/src/site/script.txt   (with props)

Added: felix/sandbox/donsez/runscript/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/pom.xml?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/pom.xml (added)
+++ felix/sandbox/donsez/runscript/pom.xml Fri Aug  7 14:03:40 2009
@@ -0,0 +1,90 @@
+<!--
+ 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.
+-->
+<project>
+
+	<properties>
+		<repositoryLocation>http://people.apache.org/~donsez/dev/felix/sandbox/</repositoryLocation>
+		<description>provides a command to run scripts using JSR 223 engines.</description>
+	</properties>  
+
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>bundle</packaging>
+  <name>Run Script Command</name>
+  <artifactId>org.apache.felix.sandbox.runscript</artifactId>
+  <groupId>org.apache.felix.sandbox</groupId>
+  <version>0.2.0</version>
+  <description>${description}</description>
+
+  <dependencies>
+
+	<dependency>
+		<groupId>org.apache.felix</groupId>
+		<artifactId>org.osgi.core</artifactId>
+  		<version>1.0.0</version>
+	</dependency>
+	
+	<dependency>
+		<groupId>org.apache.felix</groupId>
+		<artifactId>org.osgi.compendium</artifactId>
+  		<version>1.0.0</version>
+	</dependency>
+        
+	<dependency>
+		<groupId>org.apache.felix</groupId>
+		<artifactId>org.apache.felix.shell</artifactId>
+  		<version>1.0.0</version>
+	</dependency>
+
+
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+          
+          	<!-- docs in http://cwiki.apache.org/FELIX/bundle-plugin-for-maven-bnd.html and http://cwiki.apache.org/FELIX/osgi-plugin-for-maven-2.html -->
+          	
+            <Import-Package>
+            	org.osgi.framework,
+            	org.apache.felix.shell
+            </Import-Package>
+
+            <DynamicImport-Package>*</DynamicImport-Package>
+
+            <Private-Package>
+            		${pom.artifactId},
+            		org.apache.felix.sandbox.util.*
+            </Private-Package>
+
+            <Bundle-Activator>${pom.artifactId}.RunScriptCommand</Bundle-Activator>
+
+            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+            <Bundle-Description>${description}</Bundle-Description>
+
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: felix/sandbox/donsez/runscript/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java (added)
+++ felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java Fri Aug  7 14:03:40 2009
@@ -0,0 +1,238 @@
+/*
+ * 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.
+ */
+
+package org.apache.felix.sandbox.runscript;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+
+import org.apache.felix.sandbox.util.load.LoadUtils;
+import org.apache.felix.shell.Command;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+
+/**
+ * This class provides a shell command to run scripts using JSR 223 engines.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class RunScriptCommand implements Command, BundleActivator {
+
+	private Properties properties=new Properties();
+
+	private void initProperties() {
+		properties.setProperty("runscript.showtrace","false");
+		properties.setProperty("runscript.showerrors","false");
+		properties.setProperty("runscript.showversion","false");
+		properties.setProperty("runscript.strictlycompliant","true");
+		properties.setProperty("runscript.bundleContextVariableName","bundleContext");
+	}
+	
+	private static final String BASE_CMD = "base";
+
+	private static final String ENGINES_CMD = "engines";
+
+	private static final String HELP_CMD = "help";
+
+	private static final String PROP_CMD = "prop";
+
+	private BundleContext bundleContext;
+
+	private String baseUrlStr;
+	
+	private ServiceRegistration serviceRegistration;
+
+    private ScriptEngineManager scriptEngineManager;
+
+	public RunScriptCommand() {
+		initProperties();
+	    scriptEngineManager = new ScriptEngineManager();
+
+	}
+
+	public String getName() {
+		return "runscript";
+	}
+
+	public String getUsage() {
+		return "runscript [help|base|list|prop]";
+	}
+
+	public String getShortDescription() {
+		return "run scripts using JSR 223 engines.";
+	}
+
+	private void printUsage(PrintStream out, String message) {
+		StringBuffer sb = new StringBuffer();
+		if (message != null)
+			sb.append(message);
+		sb.append(getUsage()).append('\n');
+		sb.append("runscript " + BASE_CMD + " <url> : define the base for relative urls").append('\n');
+		sb.append("runscript <language> <url> : start a script from the url").append('\n');
+		sb.append("runscript <url> : run a script. the language is defining by the file/url extension").append('\n');
+		sb.append("runscript " + ENGINES_CMD + " : list the available JSR223 engines").append('\n');
+		sb.append("runscript " + PROP_CMD + " : [<name> <value>] display/set trace properties").append('\n');
+		out.println(sb.toString());
+		return;
+	}
+
+	public void execute(String commandline, PrintStream out, PrintStream err) {
+
+		StringTokenizer st = new StringTokenizer(commandline, " ");
+
+		if (st.countTokens() == 1) {
+			printUsage(out, null);
+			return;
+		}
+
+		// Ignore the command name.
+		st.nextToken();
+
+		if (!st.hasMoreTokens()) {
+			printUsage(out, null);
+			return;
+		}
+
+		String option = st.nextToken().trim();
+
+		if (option.equals(HELP_CMD)) {
+			printUsage(out, null);
+			return;
+		} else if (option.equals(BASE_CMD)) {
+			if (!st.hasMoreTokens()) {
+				out.println("base url is :"+baseUrlStr);
+				return;
+			} else {
+				baseUrlStr = st.nextToken().trim();
+				return;
+			}
+		} else if (option.equals(PROP_CMD)) {
+			if (!st.hasMoreTokens()) {
+				out.println("trace properties are:"+properties);
+				return;
+			} else {
+				String name = st.nextToken().trim();
+				if (!st.hasMoreTokens()) {
+					out.println(name+" value is:"+properties.getProperty(name));					
+					return;
+				} else {
+					String value = st.nextToken().trim();
+					properties.put(name, value);
+					return;
+				}
+			}
+		} else if (option.equals(ENGINES_CMD)) {
+			for (Iterator iterator = scriptEngineManager.getEngineFactories().iterator(); iterator.hasNext();) {
+				ScriptEngineFactory scriptEngineFactory  = (ScriptEngineFactory) iterator.next();
+				out.print(scriptEngineFactory.getLanguageName() + " "+scriptEngineFactory.getLanguageVersion());
+				out.print(" (");
+				int i=0;
+				for (Iterator it = scriptEngineFactory.getExtensions().iterator(); it.hasNext();) {
+					String extension=(String) it.next();
+					if(i++!=0) out.print(", ");
+					out.print(extension);
+				}
+				out.print(")\n");
+			}
+		} else {
+			String language;
+			String urlstr;
+			
+			if (!st.hasMoreTokens()) {
+				urlstr=option;
+				language=null;
+				String extension=option.substring(option.lastIndexOf(".")+1);
+				// TODO language =scriptEngineManager.getEngineByExtension(extension).;
+			} else {
+				language=option;
+				urlstr = st.nextToken().trim();
+			}
+			
+			try {
+				String script=load(urlstr, baseUrlStr);
+				run(script,language,out,err);
+			} catch (Exception e) {
+				err.println(e);
+				e.printStackTrace(err);
+				return;
+			}
+		}
+	}
+
+	public void start(BundleContext bundleContext) throws Exception {
+		this.bundleContext = bundleContext;
+		Dictionary properties = new Hashtable();
+		properties.put("categories", new String[] { "command", "script" });
+		serviceRegistration = bundleContext.registerService(Command.class
+				.getName(), this, properties);
+	}
+
+	public void stop(BundleContext bundleContext) throws Exception {
+		serviceRegistration.unregister();
+	}
+
+	private String load(String urlStr, String baseUrlStr) throws Exception {
+		URL url=null;
+		try {
+			url=new URL(urlStr);
+		} catch (MalformedURLException e) {
+			try {
+				if(baseUrlStr==null){
+					throw e;
+				}
+				url=new URL(new URL(baseUrlStr),urlStr);
+			} catch (MalformedURLException e1) {
+				throw e1;
+			}
+		}
+		
+		String scriptText = LoadUtils.loadAsUrl(url.toExternalForm());
+		return scriptText;
+	}
+	
+	public void run(String scriptText, String language, PrintStream out, PrintStream err) throws Exception {
+            ScriptEngine engine = scriptEngineManager.getEngineByName(language);
+            // add the bundleContext
+            String bundleContextVariableName=properties.getProperty("runscript.bundleContextVariableName");
+            if(bundleContextVariableName!=null && !bundleContextVariableName.equals("")){
+            	engine.put(bundleContextVariableName, bundleContext);
+            }
+            
+            // TODO add other properties 
+            
+            ScriptContext context = engine.getContext();
+            context.setWriter(new PrintWriter(out));
+            context.setErrorWriter(new PrintWriter(err));
+            Object res = engine.eval(scriptText);
+            if(res!=null) out.println(res);
+    }
+}

Propchange: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java (added)
+++ felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java Fri Aug  7 14:03:40 2009
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+
+package org.apache.felix.sandbox.runscript;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.net.URL;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.apache.felix.sandbox.util.load.LoadUtils;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+
+
+/**
+ * TODO This class provides a extender to run scripts using JSR 223 engines located in bundles.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class RunScriptExtender implements BundleActivator, BundleListener {
+
+    private ScriptEngineManager scriptEngineManager;
+	private BundleContext bundleContext;
+
+	public RunScriptExtender() {
+	    scriptEngineManager = new ScriptEngineManager();
+	}
+
+	public void start(BundleContext bundleContext) throws Exception {
+		this.bundleContext = bundleContext;
+		bundleContext.addBundleListener(this);
+	}
+
+	public void stop(BundleContext bundleContext) throws Exception {
+		bundleContext.removeBundleListener(this);
+	}
+
+	private String load(URL url) throws Exception {
+		String scriptText = LoadUtils.loadAsUrl(url.toExternalForm());
+		return scriptText;
+	}
+	
+	public void run(String scriptText, String language, BundleContext bundleContext, PrintStream out, PrintStream err) throws Exception {
+            ScriptEngine engine = scriptEngineManager.getEngineByName(language);
+            // add the bundleContext
+            engine.put("bundleContext", bundleContext);
+            ScriptContext context = engine.getContext();
+            context.setWriter(new PrintWriter(out));
+            context.setErrorWriter(new PrintWriter(err));
+            Object res = engine.eval(scriptText);
+            if(res!=null) out.println(res);
+    }
+
+	public void bundleChanged(BundleEvent bundleEvent) {
+		// maintain a Map<BundleId,[launched scripts]>
+		
+		// on bundle starting
+		// load the script
+		// eval the script
+		// if start(BundleContext) function exists then invoke the start(BundleContext) function
+		
+		// on bundle stopping
+		// invoke the stop(BundleContext) function		
+	}
+	
+	class Script {
+		
+	}
+}

Propchange: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptExtender.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java (added)
+++ felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java Fri Aug  7 14:03:40 2009
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+package org.apache.felix.sandbox.runscript;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
+import javax.management.AttributeNotFoundException;
+import javax.management.DynamicMBean;
+import javax.management.InvalidAttributeValueException;
+import javax.management.MBeanException;
+import javax.management.MBeanInfo;
+import javax.management.ReflectionException;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+
+
+/**
+ * TODO This class provides a MBean to run scripts using JSR 223 engines.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class RunScriptMBean implements BundleActivator, DynamicMBean {
+
+    private ScriptEngineManager scriptEngineManager;
+	private BundleContext bundleContext;
+
+	public RunScriptMBean() {
+	    scriptEngineManager = new ScriptEngineManager();
+	}
+
+	public void start(BundleContext bundleContext) throws Exception {
+		this.bundleContext = bundleContext;
+	}
+
+	public void stop(BundleContext bundleContext) throws Exception {
+	}
+	
+	public void run(String scriptText, String language, BundleContext bundleContext, PrintStream out, PrintStream err) throws Exception {
+            ScriptEngine engine = scriptEngineManager.getEngineByName(language);
+            // add the bundleContext
+            engine.put("bundleContext", bundleContext);
+            ScriptContext context = engine.getContext();
+            context.setWriter(new PrintWriter(out));
+            context.setErrorWriter(new PrintWriter(err));
+            Object res = engine.eval(scriptText);
+            if(res!=null) out.println(res);
+    }
+
+	public void bundleChanged(BundleEvent bundleEvent) {
+		// maintain a Map<BundleId,[launched scripts]>
+		
+		// on bundle starting
+		// load the script
+		// eval the script
+		// if start(BundleContext) function exists then invoke the start(BundleContext) function
+		
+		// on bundle stopping
+		// invoke the stop(BundleContext) function		
+	}
+	
+	class Script {
+		
+	}
+
+	public Object getAttribute(String arg0) throws AttributeNotFoundException,
+			MBeanException, ReflectionException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public AttributeList getAttributes(String[] arg0) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public MBeanInfo getMBeanInfo() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public Object invoke(String arg0, Object[] arg1, String[] arg2)
+			throws MBeanException, ReflectionException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public void setAttribute(Attribute arg0) throws AttributeNotFoundException,
+			InvalidAttributeValueException, MBeanException, ReflectionException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	public AttributeList setAttributes(AttributeList arg0) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+}

Propchange: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/runscript/RunScriptMBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java (added)
+++ felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java Fri Aug  7 14:03:40 2009
@@ -0,0 +1,127 @@
+/*
+ * 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.
+ */
+package org.apache.felix.sandbox.util.load;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * Utility class to load text from URL or Ressources
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class LoadUtils {
+
+	/**
+	 * Load the text from a document located at an URL
+	 * @param urlStr the url of the document to load
+	 * @return the text contained in the document
+	 * @throws Exception
+	 */
+	public static String loadAsUrl(String urlStr) throws Exception {
+		URL url = null;
+		url = new URL(urlStr);
+		InputStream inputStream = null;
+		BufferedInputStream bufferedInputStream = null;
+		ByteArrayOutputStream byteArrayOutputStream = null;
+
+		try {
+			inputStream = url.openStream();
+			bufferedInputStream = new BufferedInputStream(inputStream);
+			byteArrayOutputStream = new ByteArrayOutputStream();
+			int n;
+			byte[] buffer = new byte[512];
+			while ((n = bufferedInputStream.read(buffer)) != -1) {
+				byteArrayOutputStream.write(buffer, 0, n);
+			}
+			bufferedInputStream.close();
+			inputStream=null;
+			bufferedInputStream=null;
+
+			String res = new String(byteArrayOutputStream.toByteArray());
+			return res;
+		} finally {
+			if (bufferedInputStream != null)
+				try {
+					bufferedInputStream.close();
+					inputStream = null;
+				} catch (IOException e) {
+				}
+			if (inputStream != null)
+				try {
+					inputStream.close();
+				} catch (IOException e) {
+				}
+			if (byteArrayOutputStream != null)
+				try {
+					byteArrayOutputStream.close();
+				} catch (IOException e) {
+				}
+		}
+	}
+
+	/**
+	 * Load the text from a ressource
+	 * @param classLoader classLoader from which the ressource is accessible
+	 * @param path path of the ressource
+	 * @return the text contained in the ressource
+	 * @throws Exception
+	 */public static String loadAsRessource(ClassLoader classLoader, String path)
+	 throws Exception {
+		InputStream inputStream = null;
+		BufferedInputStream bufferedInputStream = null;
+		ByteArrayOutputStream byteArrayOutputStream = null;
+
+		try {
+			inputStream = classLoader.getResourceAsStream(path);
+			bufferedInputStream = new BufferedInputStream(inputStream);
+			byteArrayOutputStream = new ByteArrayOutputStream();
+			int n;
+			byte[] buffer = new byte[512];
+			while ((n = bufferedInputStream.read(buffer)) != -1) {
+				byteArrayOutputStream.write(buffer, 0, n);
+			}
+			bufferedInputStream.close();
+			inputStream=null;
+			bufferedInputStream=null;
+
+			String res = new String(byteArrayOutputStream.toByteArray());
+			return res;
+		} finally {
+			if (bufferedInputStream != null)
+				try {
+					bufferedInputStream.close();
+					inputStream = null;
+				} catch (IOException e) {
+				}
+			if (inputStream != null)
+				try {
+					inputStream.close();
+				} catch (IOException e) {
+				}
+			if (byteArrayOutputStream != null)
+				try {
+					byteArrayOutputStream.close();
+				} catch (IOException e) {
+				}
+		}
+	}
+}

Propchange: felix/sandbox/donsez/runscript/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/bench.js
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/bench.js?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/bench.js (added)
+++ felix/sandbox/donsez/runscript/src/site/bench.js Fri Aug  7 14:03:40 2009
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+		
+
+java.lang.System.out.println("Hello world ");
+
+java.lang.System.out.println(bundleContext.toString());
+				

Propchange: felix/sandbox/donsez/runscript/src/site/bench.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/echocommand.js
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/echocommand.js?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/echocommand.js (added)
+++ felix/sandbox/donsez/runscript/src/site/echocommand.js Fri Aug  7 14:03:40 2009
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+
+// This program registers a echo command
+
+function init(){
+}
+
+function start(bc){
+}
+
+function stop(bc){
+}
+
+init();

Propchange: felix/sandbox/donsez/runscript/src/site/echocommand.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/extendersample.js
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/extendersample.js?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/extendersample.js (added)
+++ felix/sandbox/donsez/runscript/src/site/extendersample.js Fri Aug  7 14:03:40 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+
+var out=java.lang.System.out;
+var startTime = java.lang.System.currentTimeMillis();
+
+function init(){
+    out.println("Script in bundle #"+bundleContext.getBundle().getBundleId()+ " is initialized");
+}
+
+function start(bc){
+    out.println("Script in bundle #"+bc.getBundle().getBundleId()+ " is starting");
+}
+
+function stop(bc){
+    out.println("Script in bundle #"+bc.getBundle().getBundleId()+ " is stopping");
+    var endTime = java.lang.System.currentTimeMillis();
+	out.println("\nTTL was " + (endTime-startTime)/1000.0+ " sec.");
+}
+
+init();

Propchange: felix/sandbox/donsez/runscript/src/site/extendersample.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/hello.groovy
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/hello.groovy?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/hello.groovy (added)
+++ felix/sandbox/donsez/runscript/src/site/hello.groovy Fri Aug  7 14:03:40 2009
@@ -0,0 +1,22 @@
+/*
+ * 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.PrintWriter;
+// import org.osgi.framework.Bundle;
+
+java.lang.System.out.println("Hello world ");

Added: felix/sandbox/donsez/runscript/src/site/hello.js
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/hello.js?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/hello.js (added)
+++ felix/sandbox/donsez/runscript/src/site/hello.js Fri Aug  7 14:03:40 2009
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+
+var startTime = java.lang.System.currentTimeMillis();
+var out=java.lang.System.out;
+
+out.println("Hello world ");
+
+var endTime = java.lang.System.currentTimeMillis();
+out.println("\nScript executed in " + (endTime-startTime)/1000.0+ " sec.");

Propchange: felix/sandbox/donsez/runscript/src/site/hello.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/ps.js
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/ps.js?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/ps.js (added)
+++ felix/sandbox/donsez/runscript/src/site/ps.js Fri Aug  7 14:03:40 2009
@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+
+// This javascript program displays the list of bundles as the ps command in the Felix shell
+
+// TODO add options -s -l -u
+
+function getBundleStateString(i){
+    if (i == org.osgi.framework.Bundle.ACTIVE) {
+        return "Active     ";
+    } else if (i == org.osgi.framework.Bundle.INSTALLED) {
+        return "Installed  ";
+    } else if (i == org.osgi.framework.Bundle.RESOLVED) {
+        return "Resolved   ";
+    } else if (i == org.osgi.framework.Bundle.STARTING) {
+        return "Starting   ";
+    }else if (i == org.osgi.framework.Bundle.STOPPING) {
+        return "Stopping   ";
+    }
+    return "Unknown    ";
+}
+
+var startTime = java.lang.System.currentTimeMillis();
+var out=java.lang.System.out;
+
+// get a reference to the StartLevel service
+var slref = bundleContext.getServiceReference("org.osgi.service.startlevel.StartLevel");
+var startLevel = null;
+if (slref != null) {
+    startLevel = bundleContext.getService(slref);
+}
+if (startLevel == null){
+    out.println("StartLevel service is unavailable.");
+}
+
+// Display active start level.
+if (startLevel != null){
+    out.println("START LEVEL " + startLevel.getStartLevel());
+}
+
+
+var bundles=bundleContext.getBundles();
+out.println("ID   State         Name");
+for (var i = 0; i < bundles.length; i++) {
+	var bdl = bundles [i];
+	
+	var id=""+bdl.getBundleId();
+	while (id.length < 4){
+       id = " " + id;
+    }	
+  	out.print("[" + id + "] ");
+
+  	out.print("[" + getBundleStateString(bdl.getState()) + "] ");
+
+	var level;
+    if (startLevel == null){
+        level = "1";
+    } else {
+        level = ""+startLevel.getBundleStartLevel(bdl);
+    }
+    while (level.length < 5){
+        level = " " + level;
+    }
+  	out.print("[" + level + "] ");
+
+    var name = bdl.getSymbolicName();
+    name = (name == null) ? "<no symbolic name>" : name;
+    var version = bdl.getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION);
+    name = (version != null) ? name + " (" + version + ")" : name;
+  	out.print(name);
+  	
+  	//out.print("\n\t\t\t" + bdl.getLocation());
+  	
+    out.println();
+}
+
+if (startLevel != null) {
+    bundleContext.ungetService(slref);
+    startLevel = null; // avoid stale reference
+}
+
+var endTime = java.lang.System.currentTimeMillis();
+out.println("\nScript executed in " + (endTime-startTime)/1000.0+ " sec.");

Propchange: felix/sandbox/donsez/runscript/src/site/ps.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/readme.html
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/readme.html?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/readme.html (added)
+++ felix/sandbox/donsez/runscript/src/site/readme.html Fri Aug  7 14:03:40 2009
@@ -0,0 +1,84 @@
+<html>
+<head>
+<title>RunScript Command</title>
+</head>
+<body>
+
+<!-- Start of Felix Bundle Documentation -->
+<hr width="100%" size="2">
+<h1><i><a name="runscript"></a><font color="#0000aa">RunScript Command</font></i></h1>
+
+<p>
+<b>Description</b><br>
+provides a command to run scripts using JSR 223 engines (JavaScript, Jython, Groovy, ...) deployed on the JVM.
+<br>provides also a extender running scripts located in the installed bundles when starting or stopping.
+</p>
+
+<p>
+<b>Usage</b><br>
+<pre>
+runscript [help|base|list|prop] : this help
+runscript base <url>            : define the base for relative urls
+runscript <language> <url>      : start a script from the url
+runscript <url>                 : run a script. the language is defining by the file/url extension
+runscript engines               : list the available JSR223 engines
+runscript prop [<name> <value>] : display/set trace properties
+</pre>
+</p>
+
+<p>
+<b>Author</b><br>
+<a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+</p>
+
+<p>
+<b>License</b><br>
+ASL 2.0
+</p>
+
+<p>
+<b>Services</b><br>
+org.apache.felix.shell.Command
+</p>
+
+<p>
+<b>Properties</b><br>
+<ul>
+<li>None</li>
+</ul>
+</p>
+
+<p><b>Requirements</b><br>
+TODO
+</p>
+
+<!-- End of Felix Bundle Documentation -->
+
+<hr width="100%" size="2">
+
+<p id="rundemo">
+<b>Run the demo</b><br>
+To run the demo of this bundle, enter in the Felix shell the <a href="script.txt">following commands</a><br>
+</p>
+
+<p id="typicalusage">
+<b>Typical Usages</b><br>
+<li>quick services assembling/prototyping</li>
+</p>
+
+<p id="todo">
+<b>TODO (contributions are welcome)</b><br>
+<ul>
+<li>TODO: persistence for scripts</li>
+<li>TODO: extenders</li>
+</ul>
+<p>
+
+<p id="links">
+<b>Links</b><br>
+<ul>
+</ul>
+</p>
+
+</body>
+</html>

Propchange: felix/sandbox/donsez/runscript/src/site/readme.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/sandbox/donsez/runscript/src/site/script.txt
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/runscript/src/site/script.txt?rev=802014&view=auto
==============================================================================
--- felix/sandbox/donsez/runscript/src/site/script.txt (added)
+++ felix/sandbox/donsez/runscript/src/site/script.txt Fri Aug  7 14:03:40 2009
@@ -0,0 +1,15 @@
+// Command lines to demonstrate the runscript command
+
+runscript help
+runscript base
+runscript base file:F:\osgi\felix-sandbox-donsez-2\runscript\src\site\
+runscript js hello.js
+runscript js ps.js
+runscript groovy hello.groovy
+
+// runscript hello.js
+// runscript hello.groovy
+
+runscript js bundle://[bundleId].[bundleRev]:[classPathId]/[path].js
+
+// TODO runscript -bg js simplegui.js
\ No newline at end of file

Propchange: felix/sandbox/donsez/runscript/src/site/script.txt
------------------------------------------------------------------------------
    svn:eol-style = native