felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From don...@apache.org
Subject svn commit: r602928 - in /felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox: sosoc/ sosoc/descriptor/json/ sosoc/extender/ sosoc/manager/rhino/ sosoc/manager/script/ util/load/
Date Mon, 10 Dec 2007 15:15:29 GMT
Author: donsez
Date: Mon Dec 10 07:15:27 2007
New Revision: 602928

URL: http://svn.apache.org/viewvc?rev=602928&view=rev
Log:
add compiled script support

Added:
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/descriptor/json/
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/extender/
Modified:
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/SOSOCActivator.java
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptComponentManager.java
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptContainer.java
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptComponentManager.java
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptContainer.java
    felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/SOSOCActivator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/SOSOCActivator.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/SOSOCActivator.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/SOSOCActivator.java
Mon Dec 10 07:15:27 2007
@@ -236,10 +236,10 @@
 			componentManager = new JavaComponentManager(this, descriptor,m_logger);
 		} else if(language.toLowerCase().startsWith("javascript") || language.toLowerCase().startsWith("jscript"))
{
 			 // create the instance manager for JavaScript component
-			 componentManager = new JavaScriptComponentManager(this, descriptor,m_logger,m_baseUrlStr);
+			 componentManager = new JavaScriptComponentManager(this, bundleContext, descriptor,m_logger,m_baseUrlStr);
 		} else {
 			 // create the instance manager for Script component (javax.script)
-			componentManager = new ScriptComponentManager(this,descriptor,m_logger,m_baseUrlStr);
+			componentManager = new ScriptComponentManager(this, bundleContext, descriptor,m_logger,m_baseUrlStr);
 		}
 //		 else {
 //			throw new IllegalArgumentException(language+ " components are not supported in this
version");

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptComponentManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptComponentManager.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptComponentManager.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptComponentManager.java
Mon Dec 10 07:15:27 2007
@@ -27,6 +27,7 @@
 import java.util.Map;
 
 import org.apache.felix.sandbox.sosoc.SOSOCActivator;
+import org.apache.felix.sandbox.sosoc.descriptor.xml.Attribute;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Component;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Implementation;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Reference;
@@ -37,6 +38,8 @@
 import org.apache.felix.sandbox.sosoc.manager.ReferenceManager;
 import org.apache.felix.sandbox.util.load.LoadUtils;
 import org.apache.felix.sandbox.util.replace.ReplaceUtility;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 
 /**
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -45,10 +48,13 @@
 
 	JavaScriptContainer container;
 	
+	private BundleContext bundleContext; 	
+	
 	private String baseUrlStr; 
 	
-	public JavaScriptComponentManager(SOSOCActivator activator, Component componentDescriptor,
Logger logger, String baseUrlStr) throws Exception {
+	public JavaScriptComponentManager(SOSOCActivator activator, BundleContext bundleContext,
Component componentDescriptor, Logger logger, String baseUrlStr) throws Exception {
 		super(activator, componentDescriptor, logger);
+		this.bundleContext=bundleContext;
 		this.baseUrlStr=baseUrlStr;
 	}
 
@@ -57,6 +63,18 @@
 		// load the text of the script
 		Implementation implementation = componentDescription.getImplementation();
 		List attributes=implementation.getAttributes();
+		
+		Map/*<String, String>*/ mapAttributes=new HashMap/*<String, String>*/();
+		if(attributes!=null) {
+			for (Iterator iterator = attributes.iterator(); iterator.hasNext();) {
+				Attribute attribute = (Attribute) iterator.next();
+				String value=attribute.getValue();
+				if(value==null) value=attribute.getText();
+				if(value!=null) mapAttributes.put(attribute.getName(), value);
+				
+			}
+		}
+		
 		List sources=implementation.getSources();
 		
 		String scriptText = null;
@@ -83,7 +101,7 @@
 					}
 					scriptText = LoadUtils.loadAsUrl(url.toExternalForm());
 				} else if (source.getEntry() != null) {
-					scriptText = LoadUtils.loadAsRessource(m_activator.getClass().getClassLoader(), source.getEntry());
+					scriptText = LoadUtils.loadAsRessource(bundleContext, source.getEntry());
 				}
 						
 				// text is appended to the external script texts
@@ -111,18 +129,20 @@
 		}
 		
 		String[] interfaceNames = componentDescription.getServiceInterfaces();
-		ClassLoader cl = m_activator.getClass().getClassLoader();
 		Class[] interfaces=null;
+		Bundle bundle=bundleContext.getBundle();
 		if(interfaceNames!=null){
 			interfaces = new Class[interfaceNames.length];
 			for (int i = 0; i < interfaceNames.length; i++) {
-				interfaces[i] = cl.loadClass(interfaceNames[i]);
+				interfaces[i] = bundle.loadClass(interfaceNames[i]);
 			}
 		}
 
-		container=new JavaScriptContainer(scriptText,m_logger);
+		container=new JavaScriptContainer(scriptText,m_logger,"true".equalsIgnoreCase((String)mapAttributes.get("compile")));
 
 		if(interfaces!=null){
+			// May not work with extender model
+			ClassLoader cl=m_activator.getClass().getClassLoader();
 			return Proxy.newProxyInstance(cl, interfaces, container);
 		} else {
 			return container;

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptContainer.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptContainer.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptContainer.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/rhino/JavaScriptContainer.java
Mon Dec 10 07:15:27 2007
@@ -42,11 +42,14 @@
 
 		private Scriptable scope = null;
 
+		private boolean compile;
+
 		private Logger m_logger;
 
-		public JavaScriptContainer(String scriptText, Logger logger) {
+		public JavaScriptContainer(String scriptText, Logger logger, boolean compile) {
 			this.scriptText = scriptText;
 			this.m_logger = logger;
+			this.compile = compile;
 			start();
 
 		}
@@ -74,8 +77,13 @@
 			// Now we can evaluate a script. Let's create a new object
 			// using the object literal notation.
 			/* Object result = */
-			scriptContext.evaluateString(scope, scriptText, this.getClass().getName()
-			/* + (++javaScriptInstanceNumber) */, 1, null);
+			
+			if(compile){
+				scriptContext.compileString(scriptText, this.getClass().getName()/* + (++javaScriptInstanceNumber)
*/, 1, null);				
+			} else {
+				scriptContext.evaluateString(scope, scriptText, this.getClass().getName()/* + (++javaScriptInstanceNumber)
*/, 1, null);
+			}
+			
 			// TODO give a script instance number instead of the name of the manager
 
 			Context.exit();

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptComponentManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptComponentManager.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptComponentManager.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptComponentManager.java
Mon Dec 10 07:15:27 2007
@@ -27,6 +27,7 @@
 import java.util.Map;
 
 import org.apache.felix.sandbox.sosoc.SOSOCActivator;
+import org.apache.felix.sandbox.sosoc.descriptor.xml.Attribute;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Component;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Implementation;
 import org.apache.felix.sandbox.sosoc.descriptor.xml.Reference;
@@ -37,6 +38,8 @@
 import org.apache.felix.sandbox.sosoc.manager.ReferenceManager;
 import org.apache.felix.sandbox.util.load.LoadUtils;
 import org.apache.felix.sandbox.util.replace.ReplaceUtility;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 
 /**
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -47,8 +50,12 @@
 
 	private String baseUrlStr; 
 	
-	public ScriptComponentManager(SOSOCActivator activator, Component componentDescriptor, Logger
logger, String baseUrlStr) throws Exception {
+	private BundleContext bundleContext; 	
+
+	
+	public ScriptComponentManager(SOSOCActivator activator, BundleContext bundleContext, Component
componentDescriptor, Logger logger, String baseUrlStr) throws Exception {
 		super(activator, componentDescriptor,logger);
+		this.bundleContext=bundleContext;
 		this.baseUrlStr=baseUrlStr;
 	}
 
@@ -57,6 +64,18 @@
 		// load the text of the script
 		Implementation implementation = componentDescription.getImplementation();
 		List attributes=implementation.getAttributes();
+		
+		Map/*<String, String>*/ mapAttributes=new HashMap/*<String, String>*/();
+		if(attributes!=null) {
+			for (Iterator iterator = attributes.iterator(); iterator.hasNext();) {
+				Attribute attribute = (Attribute) iterator.next();
+				String value=attribute.getValue();
+				if(value==null) value=attribute.getText();
+				if(value!=null) mapAttributes.put(attribute.getName(), value);
+				
+			}
+		}
+				
 		List sources=implementation.getSources();
 		
 		String scriptText = null;
@@ -83,7 +102,7 @@
 					}
 					scriptText = LoadUtils.loadAsUrl(url.toExternalForm());
 				} else if (source.getEntry() != null) {
-					scriptText = LoadUtils.loadAsRessource(m_activator.getClass().getClassLoader(), source.getEntry());
+					scriptText = LoadUtils.loadAsRessource(bundleContext, source.getEntry());
 				}
 						
 				// text is appended to the external script texts
@@ -111,16 +130,18 @@
 		}
 				
 		String[] interfaceNames = componentDescription.getServiceInterfaces();
-		ClassLoader cl = m_activator.getClass().getClassLoader();
 		Class[] interfaces=null;
+		Bundle bundle=bundleContext.getBundle();
 		if(interfaceNames!=null){
 			interfaces = new Class[interfaceNames.length];
 			for (int i = 0; i < interfaceNames.length; i++) {
-				interfaces[i] = cl.loadClass(interfaceNames[i]);
+				interfaces[i] = bundle.loadClass(interfaceNames[i]);
 			}
 		}
-
-		container = new ScriptContainer(implementation.getLanguage(),scriptText,m_logger);
+		
+		// May not work with extender model
+		ClassLoader cl=m_activator.getClass().getClassLoader();
+		container = new ScriptContainer(implementation.getLanguage(),scriptText,cl,m_logger,"true".equalsIgnoreCase((String)mapAttributes.get("compile")));
 
 		if(interfaces!=null){
 			return Proxy.newProxyInstance(cl, interfaces, container);

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptContainer.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptContainer.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptContainer.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/sosoc/manager/script/ScriptContainer.java
Mon Dec 10 07:15:27 2007
@@ -22,6 +22,7 @@
 import java.lang.reflect.Method;
 
 import javax.script.Bindings;
+import javax.script.Compilable;
 import javax.script.Invocable;
 import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
@@ -37,7 +38,7 @@
  */
 public class ScriptContainer implements InvocationHandler {
 
-	private static ScriptEngineManager manager = new ScriptEngineManager();
+	private ScriptEngineManager manager;
 	
 	private String scriptText;
 
@@ -51,10 +52,13 @@
 
 	private Logger m_logger;
 
-	public ScriptContainer(String language, String scriptText, Logger logger) throws Exception
{
+	public ScriptContainer(String language, String scriptText, ClassLoader classLoader, Logger
logger, boolean compile) throws Exception {
 		this.scriptText = scriptText;
 		this.m_logger = logger;
+		this.language=language;
 		// Return script engine by name
+		//manager= new ScriptEngineManager(classLoader);
+		manager= new ScriptEngineManager();
 		engine = manager.getEngineByName(language);
 
 		if (engine == null)
@@ -65,7 +69,12 @@
 	    scriptContext = engine.getContext();
 	    bindings=engine.getBindings(ScriptContext.ENGINE_SCOPE);
 	    scriptContext.setBindings(bindings,ScriptContext.ENGINE_SCOPE);
-		engine.eval(this.scriptText);
+	    
+	    if(compile && engine instanceof Compilable) {
+	    	((Compilable)engine).compile(this.scriptText);
+	    } else {
+	    	engine.eval(this.scriptText);
+	    }
 	}
 
 	/**

Modified: felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java
URL: http://svn.apache.org/viewvc/felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java?rev=602928&r1=602927&r2=602928&view=diff
==============================================================================
--- felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java
(original)
+++ felix/sandbox/donsez/sosoc/src/main/java/org/apache/felix/sandbox/util/load/LoadUtils.java
Mon Dec 10 07:15:27 2007
@@ -25,6 +25,8 @@
 import java.io.InputStream;
 import java.net.URL;
 
+import org.osgi.framework.BundleContext;
+
 /**
  * Utility class to load text from URL or Ressources
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -34,53 +36,25 @@
 	public static String loadAsUrl(String urlStr) throws Exception {
 		URL url = null;
 		url = new URL(urlStr);
-		InputStream inputStream = null;
-		BufferedInputStream bufferedInputStream = null;
-		ByteArrayOutputStream byteArrayOutputStream = null;
+		return load(url.openStream());
+	}
 
-		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;
+	public static String loadAsRessource(ClassLoader classLoader, String path)
+	 throws Exception {
+		return load(classLoader.getResourceAsStream(path));
+	}
 
-			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) {
-				}
-		}
+	public static String loadAsRessource(BundleContext bundleContext, String path)
+	 throws Exception {
+		return load(bundleContext.getBundle().getEntry(path).openStream());
 	}
 
-	public static String loadAsRessource(ClassLoader classLoader, String path)
+	public static String load(InputStream inputStream)
 	 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;
@@ -112,6 +86,5 @@
 				} catch (IOException e) {
 				}
 		}
-	}
-
+	}	
 }



Mime
View raw message