Search in sources :

Example 56 with ScriptEngineManager

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

the class ScriptLoginModule method login.

@Override
public boolean login() throws LoginException {
    File script = getScriptFile((String) this.options.get("scriptURI"));
    if (script == null) {
        script = getScriptFile(JavaSecurityManagers.getSystemProperty("openejb.ScriptLoginModule.scriptURI"));
        if (script == null) {
            script = getScriptFile(null);
        }
    }
    if (script == null) {
        throw new LoginException("No login script defined");
    }
    final String scriptText;
    try {
        scriptText = new Scanner(script).useDelimiter("\\Z").next();
    } catch (final FileNotFoundException e) {
        throw new LoginException("Invalid login script URI.");
    }
    this.userData = getUserData();
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName((String) this.options.get("engineName"));
    // new context for the execution of this script
    final ScriptContext newContext = new SimpleScriptContext();
    // creating the bidings object for the current execution
    final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("user", this.userData.user);
    bindings.put("password", this.userData.pass);
    final List<String> myGroups;
    try {
        myGroups = (List) engine.eval(scriptText, newContext);
    } catch (final ScriptException e) {
        throw new LoginException("Cannot execute login script. Msg: " + e.getMessage());
    }
    this.userData.groups.addAll(myGroups);
    return true;
}
Also used : Scanner(java.util.Scanner) ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) FileNotFoundException(java.io.FileNotFoundException) ScriptEngineManager(javax.script.ScriptEngineManager) LoginException(javax.security.auth.login.LoginException) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) File(java.io.File) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 57 with ScriptEngineManager

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

the class TomEEEmbeddedMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
    if (customizers == null || customizers.isEmpty()) {
        return;
    }
    final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
    if (engine == null) {
        throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
    }
    for (final String js : customizers) {
        try {
            final SimpleBindings bindings = new SimpleBindings();
            bindings.put("catalinaBase", base);
            engine.eval(new StringReader(js), bindings);
        } catch (final ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 58 with ScriptEngineManager

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

the class AbstractTomEEMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException {
    if (customizers != null) {
        final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
        if (engine == null) {
            throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
        }
        for (final String js : customizers) {
            try {
                final SimpleBindings bindings = new SimpleBindings();
                bindings.put("catalinaBase", catalinaBase.getAbsolutePath());
                bindings.put("resolver", new Resolver() {

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String classifier, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }
                });
                engine.eval(new StringReader(js), bindings);
            } catch (final ScriptException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ZipFile(java.util.zip.ZipFile) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Example 59 with ScriptEngineManager

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

the class GraphMLMetaTopologyFactory method updated.

@Override
public void updated(String pid, @SuppressWarnings("rawtypes") Dictionary properties) throws ConfigurationException {
    LOG.debug("updated(String, Dictionary) invoked");
    String location = (String) properties.get(TOPOLOGY_LOCATION);
    if (!m_serviceRegistration.containsKey(pid)) {
        LOG.debug("Service with pid '{}' is new. Register {}", pid, GraphMLMetaTopologyProvider.class.getSimpleName());
        final Dictionary<String, Object> metaData = new Hashtable<>();
        metaData.put(Constants.SERVICE_PID, pid);
        if (properties.get(LABEL) != null) {
            metaData.put(LABEL, properties.get(LABEL));
        }
        // Expose the MetaTopologyProvider
        try {
            final GraphMLMetaTopologyProvider metaTopologyProvider = new GraphMLMetaTopologyProvider(m_serviceAccessor);
            metaTopologyProvider.setTopologyLocation(location);
            metaTopologyProvider.reload();
            registerService(pid, MetaTopologyProvider.class, metaTopologyProvider, metaData);
            // Create and register additional services
            final Set<String> iconKeys = metaTopologyProvider.getGraphProviders().stream().map(GraphProvider::getNamespace).flatMap(eachNamespace -> metaTopologyProvider.getRawTopologyProvider(eachNamespace).getVertices().stream()).map(Vertex::getIconKey).filter(Objects::nonNull).collect(Collectors.toSet());
            registerService(pid, IconRepository.class, new GraphMLIconRepository(iconKeys));
            // Create an OSGi aware script engine manager
            final ScriptEngineManager scriptEngineManager = new OSGiScriptEngineManager(m_bundleContext);
            metaTopologyProvider.getGraphProviders().forEach(it -> {
                // Find Topology Provider
                final GraphMLTopologyProvider rawTopologyProvider = metaTopologyProvider.getRawTopologyProvider(it.getNamespace());
                // EdgeStatusProvider
                registerService(pid, EdgeStatusProvider.class, new GraphMLEdgeStatusProvider(rawTopologyProvider, scriptEngineManager, m_serviceAccessor));
                // SearchProvider
                registerService(pid, SearchProvider.class, new GraphMLSearchProvider(rawTopologyProvider));
                // Vertex Status Provider
                // Only add status provider if explicitly set in GraphML document
                this.buildStatusProvider(metaTopologyProvider, scriptEngineManager, rawTopologyProvider).ifPresent(statusProvider -> registerService(pid, StatusProvider.class, statusProvider));
            });
        } catch (InvalidGraphException | IOException e) {
            LOG.error("An error occurred while loading GraphMLTopology from file {}. Ignoring...", location, e);
        }
    } else {
        LOG.warn("Service with pid '{}' updated. Updating is not supported. Ignoring...");
    }
}
Also used : GraphMLSearchProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLSearchProvider) Constants(org.osgi.framework.Constants) Vertex(org.opennms.features.topology.api.topo.Vertex) LoggerFactory(org.slf4j.LoggerFactory) EdgeStatusProvider(org.opennms.features.topology.api.topo.EdgeStatusProvider) OSGiScriptEngineManager(org.opennms.features.osgi.script.OSGiScriptEngineManager) GraphMLPropagateVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLPropagateVertexStatusProvider) Lists(com.google.common.collect.Lists) ConfigurationException(org.osgi.service.cm.ConfigurationException) Map(java.util.Map) InvalidGraphException(org.opennms.features.graphml.model.InvalidGraphException) IconRepository(org.opennms.features.topology.api.IconRepository) Hashtable(java.util.Hashtable) SearchProvider(org.opennms.features.topology.api.topo.SearchProvider) GraphProvider(org.opennms.features.topology.api.topo.GraphProvider) ServiceRegistration(org.osgi.framework.ServiceRegistration) ManagedServiceFactory(org.osgi.service.cm.ManagedServiceFactory) Logger(org.slf4j.Logger) Set(java.util.Set) ScriptEngineManager(javax.script.ScriptEngineManager) GraphMLTopologyProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLTopologyProvider) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) MetaTopologyProvider(org.opennms.features.topology.api.topo.MetaTopologyProvider) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) List(java.util.List) StatusProvider(org.opennms.features.topology.api.topo.StatusProvider) GraphMLDefaultVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLDefaultVertexStatusProvider) GraphMLEdgeStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLEdgeStatusProvider) Optional(java.util.Optional) GraphMLMetaTopologyProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLMetaTopologyProvider) GraphMLScriptVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLScriptVertexStatusProvider) Dictionary(java.util.Dictionary) Vertex(org.opennms.features.topology.api.topo.Vertex) GraphMLTopologyProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLTopologyProvider) OSGiScriptEngineManager(org.opennms.features.osgi.script.OSGiScriptEngineManager) InvalidGraphException(org.opennms.features.graphml.model.InvalidGraphException) Hashtable(java.util.Hashtable) EdgeStatusProvider(org.opennms.features.topology.api.topo.EdgeStatusProvider) GraphMLPropagateVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLPropagateVertexStatusProvider) StatusProvider(org.opennms.features.topology.api.topo.StatusProvider) GraphMLDefaultVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLDefaultVertexStatusProvider) GraphMLEdgeStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLEdgeStatusProvider) GraphMLScriptVertexStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLScriptVertexStatusProvider) GraphMLMetaTopologyProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLMetaTopologyProvider) OSGiScriptEngineManager(org.opennms.features.osgi.script.OSGiScriptEngineManager) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) GraphMLEdgeStatusProvider(org.opennms.features.topology.plugins.topo.graphml.status.GraphMLEdgeStatusProvider) GraphProvider(org.opennms.features.topology.api.topo.GraphProvider) GraphMLSearchProvider(org.opennms.features.topology.plugins.topo.graphml.GraphMLSearchProvider)

Example 60 with ScriptEngineManager

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

the class OSGiScriptEngineManager method findManagers.

private Map<ScriptEngineManager, ClassLoader> findManagers(BundleContext context) {
    Map<ScriptEngineManager, ClassLoader> managers = new HashMap<ScriptEngineManager, ClassLoader>();
    for (ClassLoader classLoader : findClassLoaders(context)) {
        ScriptEngineManager manager = new ScriptEngineManager(classLoader);
        manager.setBindings(bindings);
        managers.put(manager, classLoader);
    }
    return managers;
}
Also used : HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager)

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