Return-Path: Delivered-To: apmail-incubator-felix-commits-archive@www.apache.org Received: (qmail 36693 invoked from network); 27 Sep 2006 23:08:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Sep 2006 23:08:21 -0000 Received: (qmail 379 invoked by uid 500); 27 Sep 2006 23:08:21 -0000 Delivered-To: apmail-incubator-felix-commits-archive@incubator.apache.org Received: (qmail 366 invoked by uid 500); 27 Sep 2006 23:08:21 -0000 Mailing-List: contact felix-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: felix-dev@incubator.apache.org Delivered-To: mailing list felix-commits@incubator.apache.org Received: (qmail 354 invoked by uid 99); 27 Sep 2006 23:08:21 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 27 Sep 2006 16:08:21 -0700 Authentication-Results: idunn.apache.osuosl.org smtp.mail=santillan@apache.org; spf=permerror X-ASF-Spam-Status: No, hits=-9.4 required=5.0 tests=ALL_TRUSTED,NO_REAL_NAME Received-SPF: error (idunn.apache.osuosl.org: domain apache.org from 140.211.166.113 cause and error) Received: from [140.211.166.113] ([140.211.166.113:63678] helo=eris.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id 53/E0-03285-4640B154 for ; Wed, 27 Sep 2006 16:08:21 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id E9D4B1A981A; Wed, 27 Sep 2006 16:08:18 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r450628 - /incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java Date: Wed, 27 Sep 2006 23:08:18 -0000 To: felix-commits@incubator.apache.org From: santillan@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060927230818.E9D4B1A981A@eris.apache.org> X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: santillan Date: Wed Sep 27 16:08:18 2006 New Revision: 450628 URL: http://svn.apache.org/viewvc?view=rev&rev=450628 Log: small test to see if the OSGiScriptEngine-related wrappers worked fine Added: incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java Added: incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java URL: http://svn.apache.org/viewvc/incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java?view=auto&rev=450628 ============================================================================== --- incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java (added) +++ incubator/felix/trunk/mishell/src/test/java/org/apache/felix/mishell/OSGiScriptEngineFinder.java Wed Sep 27 16:08:18 2006 @@ -0,0 +1,151 @@ +package org.apache.felix.mishell; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +public class OSGiScriptEngineFinder { + private BundleContext context; + + public OSGiScriptEngineFinder(BundleContext context) { + this.context = context; + } + private List getAllEngineFactoryCandidates() throws IOException{ + Bundle[] bundles = context.getBundles(); + List factoryCandidates = new ArrayList(); + for (Bundle bundle : bundles) { + System.out.println(bundle.getSymbolicName()); + if(bundle.getSymbolicName().equals("system.bundle")) continue; + Enumeration urls = bundle.findEntries("META-INF/services", + "javax.script.ScriptEngineFactory", false); + if (urls == null) + continue; + while (urls.hasMoreElements()) { + URL u = (URL) urls.nextElement(); + BufferedReader reader = new BufferedReader( + new InputStreamReader(u.openStream())); + String line; + while ((line = reader.readLine()) != null) { + factoryCandidates.add(line.trim()); + //Just for testing: + + try { + Class engineFactory=Class.forName(line.trim()); + ScriptEngineManager manager=new ScriptEngineManager(engineFactory.getClassLoader()); + for (ScriptEngineFactory f: manager.getEngineFactories()){ + ClassLoader old=Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(engineFactory.getClassLoader()); + ScriptEngine e=f.getScriptEngine();//.eval(f.getOutputStatement("Hello world")); + Thread.currentThread().setContextClassLoader(old); + e.eval("File.new(\"\")"); + Thread.currentThread().sleep(1000); + } + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (ScriptException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } + } + return factoryCandidates; + } + public ScriptEngineManager getManagerFor(String factoryName) throws ClassNotFoundException{ + Class factory=Class.forName(factoryName); + String a; + ScriptEngineManager manager=new ScriptEngineManager(factory.getClassLoader()); + //Does this manager know the factory? + boolean flag=false; + for (ScriptEngineFactory fac : manager.getEngineFactories()){ + if(fac.getClass().equals(factory)) flag=true; + } + return manager; + + } + /** + * Adds Engine factories found in installed bundles. The resolution order is the following: + *
    + *
  1. First, all the factories found by the manager are used. This includes all factories seen by + * the class loader that instantiated the ScriptEngineManager + *
  2. + *
  3. Then, by order of installation, the rest of the bundles are seeked. Therefore, if an engine appears in + * two bundles, only the first one is used. + *
  4. + *
+ * This should not be a problem, as this method is intended for sparse use (only when changing the language) + * @param the manager to whom the factories are going to be added + * @exception RuntimeException wrapper for IOException in case there is any problem with the bundle.findEntries methods + * @exception RuntimeException wrapper for ClassNotFoundException and InstantiationException which are supposed not to happen + */ + public void addEngineFactories(ScriptEngineManager manager){ + //TODO refactor all this to make it work. An option is to directly use + System.out.println("calling add engine factories"); + try{ + List factoryCandidates=this.getAllEngineFactoryCandidates(); + for (String candidate: factoryCandidates){ + System.out.println("Candidate: "+candidate); + Class factoryClazz=this.getClass().getClassLoader().loadClass(candidate); + ScriptEngineFactory factory=(ScriptEngineFactory) factoryClazz.newInstance(); + ScriptEngine engine=factory.getScriptEngine(); + try { + engine.eval("puts 'Hello world'"); + } catch (ScriptException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if(manager.getEngineByName(factory.getLanguageName())!=null){ + System.out.println("Engine already existing. Continuing"); + continue; //This means that there already is a factory for that language + } + //Now we register the factory for all its names, extensions and mime types + for(String name: factory.getNames()){ + System.out.println("registering "+name); + manager.registerEngineName(name, factory); + } + for (String type: factory.getMimeTypes()){ + manager.registerEngineMimeType(type, factory); + System.out.println(manager.getEngineByMimeType(type).equals(factory)); + } + for (String extension: factory.getExtensions()){ + manager.registerEngineExtension(extension, factory); + System.out.println(manager.getEngineByExtension(extension).equals(factory)); + + } + } + System.out.println("manager now has: "); + System.out.println(manager.getEngineByName("jruby")); + + for (ScriptEngineFactory f : manager.getEngineFactories()){ + System.out.println(f); + } + }catch (IOException ioe){ + throw new RuntimeException(ioe); + } catch (ClassNotFoundException cnfe) { + } catch (InstantiationException iee) { + throw new RuntimeException (iee); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } + } +} +