Search in sources :

Example 36 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project sling by apache.

the class ScriptEngineConsolePlugin method printConfiguration.

public void printConfiguration(final PrintWriter pw) {
    pw.println("Available Script Engines");
    pw.println("========================");
    ScriptEngineManager manager = scriptEngineManagerFactory.getScriptEngineManager();
    List<?> factories = manager.getEngineFactories();
    for (Iterator<?> fi = factories.iterator(); fi.hasNext(); ) {
        final ScriptEngineFactory factory = (ScriptEngineFactory) fi.next();
        pw.println();
        pw.print(factory.getEngineName());
        pw.print(" ");
        pw.println(factory.getEngineVersion());
        pw.println("-------------------------------------");
        pw.print("- Language : ");
        pw.print(factory.getLanguageName());
        pw.print(", ");
        pw.println(factory.getLanguageVersion());
        pw.print("- Extensions : ");
        printArray(pw, factory.getExtensions());
        pw.print("- MIME Types : ");
        printArray(pw, factory.getMimeTypes());
        pw.print("- Names : ");
        printArray(pw, factory.getNames());
    }
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager)

Example 37 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project sling by apache.

the class ScriptEngineManagerFactoryTest method checkAddingScriptBundle.

@Test
public void checkAddingScriptBundle() throws Exception {
    context.checking(new Expectations() {

        {
            exactly(1).of(bundleCtx).registerService(with(equal(new String[] { "javax.script.ScriptEngineManager", "org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager" })), with(any(Object.class)), with(any(Dictionary.class)));
            will(returnValue(new MockServiceRegistration()));
        }
    });
    ScriptEngineManagerFactory factory = new ScriptEngineManagerFactory();
    factory.activate(componentCtx);
    ScriptEngineManager first = factory.getScriptEngineManager();
    assertNull(first.getEngineByName("dummy"));
    final Bundle bundle = context.mock(Bundle.class);
    final File factoryFile = createFactoryFile();
    context.checking(new Expectations() {

        {
            atLeast(1).of(bundle).getEntry("META-INF/services/javax.script.ScriptEngineFactory");
            will(returnValue(factoryFile.toURI().toURL()));
            atLeast(1).of(bundle).loadClass(SCRIPT_ENGINE_FACTORY.getName());
            will(returnValue(SCRIPT_ENGINE_FACTORY));
        }
    });
    factory.bundleChanged(new BundleEvent(BundleEvent.STARTED, bundle));
    ScriptEngineManager second = factory.getScriptEngineManager();
    assertNotNull(second.getEngineByName("dummy"));
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) Bundle(org.osgi.framework.Bundle) ScriptEngineManager(javax.script.ScriptEngineManager) BundleEvent(org.osgi.framework.BundleEvent) File(java.io.File) Test(org.junit.Test)

Example 38 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project spring-framework by spring-projects.

the class ScriptTemplateView method createEngineFromName.

protected ScriptEngine createEngineFromName() {
    if (this.scriptEngineManager == null) {
        this.scriptEngineManager = new ScriptEngineManager(getApplicationContext().getClassLoader());
    }
    ScriptEngine engine = StandardScriptUtils.retrieveEngineByName(this.scriptEngineManager, this.engineName);
    loadScripts(engine);
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 39 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project opennms by OpenNMS.

the class GraphMLEdgeStatusProviderIT method verify.

@Test
public void verify() throws Exception {
    final GraphMLServiceAccessor serviceAccessor = new GraphMLServiceAccessor();
    serviceAccessor.setTransactionOperations(transactionOperations);
    serviceAccessor.setNodeDao(nodeDao);
    serviceAccessor.setSnmpInterfaceDao(snmpInterfaceDao);
    serviceAccessor.setMeasurementsService(request -> new QueryResponse());
    final GraphMLGraph graph = GraphMLReader.read(getClass().getResourceAsStream("/test-graph2.xml")).getGraphs().get(0);
    final GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(null, graph, serviceAccessor);
    final GraphMLEdgeStatusProvider provider = new GraphMLEdgeStatusProvider(topologyProvider, new ScriptEngineManager(), serviceAccessor, Paths.get("src", "test", "opennms-home", "etc", "graphml-edge-status"));
    assertThat(provider.contributesTo("acme:regions"), is(true));
    assertThat(provider.getNamespace(), is("acme:regions"));
    // Calculating the status executes some tests defined int the according scripts as a side effect
    final EdgeRef edgeRef = topologyProvider.getEdge("acme:regions", "center_north");
    final Map<? extends EdgeRef, ? extends Status> status = provider.getStatusForEdges(topologyProvider, ImmutableList.of(edgeRef), new Criteria[0]);
    // Checking nodeID creation for vertices with only foreignSource/foreignID set
    final VertexRef vertexRef = topologyProvider.getVertex("acme:regions", "west");
    assertThat(vertexRef, is(notNullValue()));
    assertThat(vertexRef, is(instanceOf(GraphMLVertex.class)));
    assertThat(((GraphMLVertex) vertexRef).getNodeID(), is(4));
    // Testing status merging from two scripts
    assertThat(status, is(notNullValue()));
    assertThat(status, is(hasEntry(edgeRef, new GraphMLEdgeStatus().severity(OnmsSeverity.WARNING).style("stroke", "pink").style("stroke-width", "3em"))));
}
Also used : GraphMLEdgeStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLEdgeStatusProvider) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) ScriptEngineManager(javax.script.ScriptEngineManager) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) GraphMLEdgeStatus(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLEdgeStatus) Test(org.junit.Test)

Example 40 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project opennms by OpenNMS.

the class GraphMLVertexStatusProviderIT method testScriptStatusProvider.

@Test
public void testScriptStatusProvider() throws InvalidGraphException {
    GraphML graphML = GraphMLReader.read(getClass().getResourceAsStream("/test-graph.xml"));
    GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(null, graphML.getGraphs().get(0), new GraphMLServiceAccessor());
    GraphMLScriptVertexStatusProvider statusProvider = new GraphMLScriptVertexStatusProvider(topologyProvider.getNamespace(), this.alarmSummaryWrapper, new ScriptEngineManager(), this.serviceAccessor, Paths.get("src", "test", "opennms-home", "etc", "graphml-vertex-status"));
    List<VertexRef> vertices = topologyProvider.getVertices().stream().map(eachVertex -> (VertexRef) eachVertex).collect(Collectors.toList());
    Assert.assertEquals(4, vertices.size());
    Assert.assertEquals(topologyProvider.getNamespace(), statusProvider.getNamespace());
    Assert.assertTrue(statusProvider.contributesTo(topologyProvider.getNamespace()));
    Map<? extends VertexRef, ? extends Status> statusForVertices = statusProvider.getStatusForVertices(topologyProvider, vertices, new Criteria[0]);
    Assert.assertEquals(4, statusForVertices.size());
    Assert.assertEquals(ImmutableMap.of(createVertexRef(topologyProvider.getNamespace(), "north"), createStatus(OnmsSeverity.NORMAL, 0), createVertexRef(topologyProvider.getNamespace(), "west"), createStatus(OnmsSeverity.NORMAL, 0), createVertexRef(topologyProvider.getNamespace(), "south"), createStatus(OnmsSeverity.WARNING, 42), createVertexRef(topologyProvider.getNamespace(), "east"), createStatus(OnmsSeverity.CRITICAL, 23)), statusForVertices);
}
Also used : Date(java.util.Date) AlarmSummary(org.opennms.netmgt.model.alarm.AlarmSummary) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) GraphMLReader(org.opennms.features.graphml.model.GraphMLReader) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) GraphMLPropagateVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLPropagateVertexStatusProvider) SnmpInterfaceDao(org.opennms.netmgt.dao.api.SnmpInterfaceDao) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) Map(java.util.Map) InvalidGraphException(org.opennms.features.graphml.model.InvalidGraphException) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before) DatabasePopulator(org.opennms.netmgt.dao.DatabasePopulator) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) NodeDao(org.opennms.netmgt.dao.api.NodeDao) JUnitConfigurationEnvironment(org.opennms.test.JUnitConfigurationEnvironment) Resources(com.google.common.io.Resources) ImmutableMap(com.google.common.collect.ImmutableMap) ScriptEngineManager(javax.script.ScriptEngineManager) Test(org.junit.Test) GraphML(org.opennms.features.graphml.model.GraphML) EasyMock(org.easymock.EasyMock) Collectors(java.util.stream.Collectors) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) BeanUtils(org.opennms.core.spring.BeanUtils) List(java.util.List) StatusProvider(org.opennms.features.topology.api.topo.StatusProvider) Rule(org.junit.Rule) OpenNMSJUnit4ClassRunner(org.opennms.core.test.OpenNMSJUnit4ClassRunner) GraphMLDefaultVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLDefaultVertexStatusProvider) Paths(java.nio.file.Paths) GraphMLVertexStatus(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLVertexStatus) AlarmSummaryWrapper(org.opennms.features.topology.plugins.topo.graphml.internal.AlarmSummaryWrapper) TransactionOperations(org.springframework.transaction.support.TransactionOperations) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Criteria(org.opennms.features.topology.api.topo.Criteria) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Status(org.opennms.features.topology.api.topo.Status) GraphMLScriptVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLScriptVertexStatusProvider) Assert(org.junit.Assert) VertexRef(org.opennms.features.topology.api.topo.VertexRef) TemporaryFolder(org.junit.rules.TemporaryFolder) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) GraphML(org.opennms.features.graphml.model.GraphML) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) ScriptEngineManager(javax.script.ScriptEngineManager) GraphMLScriptVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLScriptVertexStatusProvider) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Test(org.junit.Test)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)109 ScriptEngine (javax.script.ScriptEngine)66 Test (org.junit.Test)23 ScriptException (javax.script.ScriptException)20 ScriptEngineFactory (javax.script.ScriptEngineFactory)14 File (java.io.File)12 Invocable (javax.script.Invocable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 IOException (java.io.IOException)10 FileReader (java.io.FileReader)9 Bindings (javax.script.Bindings)9 Before (org.junit.Before)9 Map (java.util.Map)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)6 SimpleBindings (javax.script.SimpleBindings)6 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 InputStreamReader (java.io.InputStreamReader)3