IDEMPIERE-3243 - peer review

This commit is contained in:
Carlos Ruiz 2016-11-23 21:08:46 +01:00
parent c8245e7555
commit 7bceecdb88
4 changed files with 25 additions and 72 deletions

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.codehaus.groovy.jsr223.GroovyScriptEngineFactory">
<implementation class="org.codehaus.groovy.jsr223.GroovyScriptEngineFactory"/>
<service>
<provide interface="javax.script.ScriptEngineFactory"/>
</service>
</scr:component>

View File

@ -26,7 +26,6 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.adempiere.base.osgi.OSGiScriptEngineManager; import org.adempiere.base.osgi.OSGiScriptEngineManager;
import org.adempiere.model.IAddressValidation; import org.adempiere.model.IAddressValidation;
@ -411,15 +410,9 @@ public class Core {
*/ */
public static ScriptEngine getScriptEngine(String engineName) public static ScriptEngine getScriptEngine(String engineName)
{ {
ScriptEngineManager factory = new ScriptEngineManager(Core.class.getClassLoader()); OSGiScriptEngineManager osgiFactory = new OSGiScriptEngineManager( FrameworkUtil.getBundle(Core.class).getBundleContext());
ScriptEngine engine = factory.getEngineByName(engineName); ScriptEngine engine = osgiFactory.getEngineByName(engineName);
if(engine == null)
{
OSGiScriptEngineManager osgiFactory = new OSGiScriptEngineManager( FrameworkUtil.getBundle(Core.class).getBundleContext());
engine = osgiFactory.getEngineByName(engineName);
}
return engine; return engine;
} }
} }

View File

@ -76,10 +76,10 @@ public class OSGiScriptEngineFactory implements ScriptEngineFactory{
public ScriptEngine getScriptEngine() { public ScriptEngine getScriptEngine() {
ScriptEngine engine=null; ScriptEngine engine=null;
if(contextClassLoader!=null){ if(contextClassLoader!=null){
ClassLoader old=Thread.currentThread().getContextClassLoader(); ClassLoader old=Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader); Thread.currentThread().setContextClassLoader(contextClassLoader);
engine=factory.getScriptEngine(); engine=factory.getScriptEngine();
Thread.currentThread().setContextClassLoader(old); Thread.currentThread().setContextClassLoader(old);
} }
else engine=factory.getScriptEngine(); else engine=factory.getScriptEngine();
return engine; return engine;

View File

@ -16,12 +16,7 @@
*/ */
package org.adempiere.base.osgi; package org.adempiere.base.osgi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -32,8 +27,7 @@ import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings; import javax.script.SimpleBindings;
import org.compiere.util.Util; import org.adempiere.base.Service;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
/** /**
@ -201,63 +195,22 @@ public class OSGiScriptEngineManager extends ScriptEngineManager{
} }
private Map<ScriptEngineManager, ClassLoader> findManagers(BundleContext context) { private Map<ScriptEngineManager, ClassLoader> findManagers(BundleContext context) {
Map<ScriptEngineManager, ClassLoader> managers=new HashMap<ScriptEngineManager, ClassLoader>(); Map<ScriptEngineManager, ClassLoader> managers=new HashMap<ScriptEngineManager, ClassLoader>();
try {
for(String factoryName: findFactoryCandidates(context)){ List<ScriptEngineFactory> seFactoryList =
//We do not really need the class, but we need the classloader Service.locator().list(ScriptEngineFactory.class).getServices();
ClassLoader factoryLoader=Class.forName(factoryName).getClassLoader(); if (seFactoryList != null) {
for(ScriptEngineFactory seFactory : seFactoryList) {
ClassLoader factoryLoader = seFactory.getClass().getClassLoader();
ScriptEngineManager manager=new ScriptEngineManager(factoryLoader); ScriptEngineManager manager=new ScriptEngineManager(factoryLoader);
manager.setBindings(bindings); manager.setBindings(bindings);
managers.put(manager, factoryLoader); managers.put(manager, factoryLoader);
} }
return managers;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException(cnfe);
} }
return managers;
} }
/**
* Iterates through all bundles to get the available @link ScriptEngineFactory classes
* @return the names of the available ScriptEngineFactory classes
* @throws IOException
*/
private List<String> findFactoryCandidates(BundleContext context) throws IOException{
Bundle[] bundles = context.getBundles();
List<String> factoryCandidates = new ArrayList<String>();
for (Bundle bundle : bundles) {
// IDEMPIERE-3243: removed print and small improvements on enumeration
// System.out.println(bundle.getSymbolicName());
if(bundle.getSymbolicName().equals("system.bundle")) continue;
Enumeration<URL> urls = bundle.findEntries("META-INF/services",
"javax.script.ScriptEngineFactory", false);
if (urls == null)
continue;
while (urls.hasMoreElements()) {
URL u = urls.nextElement();
BufferedReader reader = new BufferedReader(
new InputStreamReader(u.openStream()));
String line;
while ((line = reader.readLine()) != null)
{
// IDEMPIERE-3243: removed comment lines
int commentIdx = line.indexOf('#');
if(commentIdx >= 0)
line = line.substring(0, commentIdx);
line = line.trim();
if(Util.isEmpty(line) == false)
{
factoryCandidates.add(line);
}
}
}
}
return factoryCandidates;
}
} }