@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void resolveTestScriptEngine() throws MalformedURLException, ClassNotFoundException { Bundle bundle = mock(Bundle.class); BundleContext bundleContext = mock(BundleContext.class); when(bundle.getBundleContext()).thenReturn(bundleContext); Class factoryClazz = TestScriptEngineFactory.class; when(bundle.loadClass(eq(factoryClazz.getName()))).thenReturn(factoryClazz); URL configFile = new File("src/test/resources/javax.script.ScriptEngineFactory").toURI().toURL(); BundleScriptEngineResolver scriptEngineResolver = new BundleScriptEngineResolver(bundle, configFile); ScriptEngine scriptEngine = scriptEngineResolver.resolveScriptEngine("Uber-language"); assertThat(scriptEngine, is(instanceOf(TestScriptEngineFactory.TestScriptEngine.class))); }
@Override public ScriptEngine resolveScriptEngine(String name) { try { BufferedReader in = new BufferedReader(new InputStreamReader( configFile.openStream())); String className = removeCommentsFromInput(in); in.close(); Class<?> cls = bundle.loadClass(className); if (!ScriptEngineFactory.class.isAssignableFrom(cls)) { throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName()); } ScriptEngineFactory factory = (ScriptEngineFactory) cls .newInstance(); List<String> names = factory.getNames(); for (String n : names) { if (n.equals(name)) { ClassLoader old = Thread.currentThread() .getContextClassLoader(); ScriptEngine engine; try { Thread.currentThread().setContextClassLoader( factory.getClass().getClassLoader()); engine = factory.getScriptEngine(); } finally { Thread.currentThread().setContextClassLoader(old); } LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name); return engine; } } LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name); return null; } catch (Exception e) { LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e); return null; } }
BundleScriptEngineResolver implements ScriptEngineResolver { @Override public ScriptEngine resolveScriptEngine(String name) { try { BufferedReader in = new BufferedReader(new InputStreamReader( configFile.openStream())); String className = removeCommentsFromInput(in); in.close(); Class<?> cls = bundle.loadClass(className); if (!ScriptEngineFactory.class.isAssignableFrom(cls)) { throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName()); } ScriptEngineFactory factory = (ScriptEngineFactory) cls .newInstance(); List<String> names = factory.getNames(); for (String n : names) { if (n.equals(name)) { ClassLoader old = Thread.currentThread() .getContextClassLoader(); ScriptEngine engine; try { Thread.currentThread().setContextClassLoader( factory.getClass().getClassLoader()); engine = factory.getScriptEngine(); } finally { Thread.currentThread().setContextClassLoader(old); } LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name); return engine; } } LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name); return null; } catch (Exception e) { LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e); return null; } } }
BundleScriptEngineResolver implements ScriptEngineResolver { @Override public ScriptEngine resolveScriptEngine(String name) { try { BufferedReader in = new BufferedReader(new InputStreamReader( configFile.openStream())); String className = removeCommentsFromInput(in); in.close(); Class<?> cls = bundle.loadClass(className); if (!ScriptEngineFactory.class.isAssignableFrom(cls)) { throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName()); } ScriptEngineFactory factory = (ScriptEngineFactory) cls .newInstance(); List<String> names = factory.getNames(); for (String n : names) { if (n.equals(name)) { ClassLoader old = Thread.currentThread() .getContextClassLoader(); ScriptEngine engine; try { Thread.currentThread().setContextClassLoader( factory.getClass().getClassLoader()); engine = factory.getScriptEngine(); } finally { Thread.currentThread().setContextClassLoader(old); } LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name); return engine; } } LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name); return null; } catch (Exception e) { LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e); return null; } } BundleScriptEngineResolver(Bundle bundle, URL configFile); }
BundleScriptEngineResolver implements ScriptEngineResolver { @Override public ScriptEngine resolveScriptEngine(String name) { try { BufferedReader in = new BufferedReader(new InputStreamReader( configFile.openStream())); String className = removeCommentsFromInput(in); in.close(); Class<?> cls = bundle.loadClass(className); if (!ScriptEngineFactory.class.isAssignableFrom(cls)) { throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName()); } ScriptEngineFactory factory = (ScriptEngineFactory) cls .newInstance(); List<String> names = factory.getNames(); for (String n : names) { if (n.equals(name)) { ClassLoader old = Thread.currentThread() .getContextClassLoader(); ScriptEngine engine; try { Thread.currentThread().setContextClassLoader( factory.getClass().getClassLoader()); engine = factory.getScriptEngine(); } finally { Thread.currentThread().setContextClassLoader(old); } LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name); return engine; } } LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name); return null; } catch (Exception e) { LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e); return null; } } BundleScriptEngineResolver(Bundle bundle, URL configFile); void register(); void unregister(); @Override ScriptEngine resolveScriptEngine(String name); @Override String toString(); }
BundleScriptEngineResolver implements ScriptEngineResolver { @Override public ScriptEngine resolveScriptEngine(String name) { try { BufferedReader in = new BufferedReader(new InputStreamReader( configFile.openStream())); String className = removeCommentsFromInput(in); in.close(); Class<?> cls = bundle.loadClass(className); if (!ScriptEngineFactory.class.isAssignableFrom(cls)) { throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName()); } ScriptEngineFactory factory = (ScriptEngineFactory) cls .newInstance(); List<String> names = factory.getNames(); for (String n : names) { if (n.equals(name)) { ClassLoader old = Thread.currentThread() .getContextClassLoader(); ScriptEngine engine; try { Thread.currentThread().setContextClassLoader( factory.getClass().getClassLoader()); engine = factory.getScriptEngine(); } finally { Thread.currentThread().setContextClassLoader(old); } LOGGER.finest("Resolved ScriptEngineFactory: " + engine + " for expected name: " + name); return engine; } } LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName() + " does not match expected name: " + name); return null; } catch (Exception e) { LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: " + e.getClass().getName(), e); return null; } } BundleScriptEngineResolver(Bundle bundle, URL configFile); void register(); void unregister(); @Override ScriptEngine resolveScriptEngine(String name); @Override String toString();  }