Commit e6eddf829dd8d13a324a3a1207a3378152232c49
1 parent
a24a08d1
Commit inicial Demoiselle-script
Showing
5 changed files
with
143 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | + <modelVersion>4.0.0</modelVersion> | ||
| 4 | + <artifactId>demoiselle-script</artifactId> | ||
| 5 | + <packaging>jar</packaging> | ||
| 6 | + | ||
| 7 | + <name>Demoiselle Script</name> | ||
| 8 | + <description> | ||
| 9 | + Demoiselle Script | ||
| 10 | + </description> | ||
| 11 | + | ||
| 12 | + <parent> | ||
| 13 | + <groupId>org.demoiselle.jee</groupId> | ||
| 14 | + <artifactId>demoiselle-parent</artifactId> | ||
| 15 | + <version>3.0.0-BETA1-SNAPSHOT</version> | ||
| 16 | + <relativePath>../demoiselle-parent</relativePath> | ||
| 17 | + </parent> | ||
| 18 | + | ||
| 19 | + <dependencies> | ||
| 20 | + <dependency> | ||
| 21 | + <groupId>org.codehaus.groovy</groupId> | ||
| 22 | + <artifactId>groovy</artifactId> | ||
| 23 | + <version>2.1.0</version> | ||
| 24 | + </dependency> | ||
| 25 | + </dependencies> | ||
| 26 | +</project> |
demoiselle-script/src/main/java/org/demoiselle/jee/script/SimpleGroovyEngine.java
0 → 100644
| @@ -0,0 +1,96 @@ | @@ -0,0 +1,96 @@ | ||
| 1 | +package org.demoiselle.jee.script; | ||
| 2 | + | ||
| 3 | +import java.util.ArrayList; | ||
| 4 | +import java.util.HashMap; | ||
| 5 | + | ||
| 6 | +import java.util.Map; | ||
| 7 | + | ||
| 8 | +import groovy.lang.Binding; | ||
| 9 | +import groovy.lang.GroovyClassLoader; | ||
| 10 | +import groovy.lang.Script; | ||
| 11 | + | ||
| 12 | +// Implementacao simples que difere do GroovyScriptEngine aceitando ler um script direto de um objeto string previamente carregado, | ||
| 13 | +// permitindo guardar os objetos compilados em cache | ||
| 14 | +// sem monitorar as mudanças no fonte. | ||
| 15 | +public class SimpleGroovyEngine { | ||
| 16 | + | ||
| 17 | + private Map<String,Script> scriptCache; //cache simples | ||
| 18 | + private GroovyClassLoader groovyClassLoader; | ||
| 19 | + private boolean cache; | ||
| 20 | + | ||
| 21 | + public SimpleGroovyEngine(){ | ||
| 22 | + ClassLoader parent = getClass().getClassLoader(); | ||
| 23 | + groovyClassLoader = new GroovyClassLoader(parent); | ||
| 24 | + scriptCache = new HashMap<String,Script>(); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public Map<String, Script> getScriptCache() { | ||
| 28 | + return scriptCache; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public void setScriptCache(Map<String, Script> scriptCache) { | ||
| 32 | + this.scriptCache = scriptCache; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public Script getScript(String scriptId ) { | ||
| 36 | + return scriptCache.get(scriptId); | ||
| 37 | + | ||
| 38 | + } | ||
| 39 | + public boolean isCache() { | ||
| 40 | + return cache; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public void setCache(boolean cache) { | ||
| 44 | + this.cache = cache; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @SuppressWarnings("unchecked") | ||
| 48 | + public Script loadScript(String scriptId , String regraScript ) { | ||
| 49 | + Script script = null; | ||
| 50 | + try { | ||
| 51 | + script= scriptCache.get(scriptId); | ||
| 52 | + if (script == null) { | ||
| 53 | + Class<Script> clazz= groovyClassLoader.parseClass(regraScript); | ||
| 54 | + script = clazz.newInstance(); | ||
| 55 | + | ||
| 56 | + scriptCache.put(scriptId, script); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + } catch (InstantiationException | IllegalAccessException e) { | ||
| 60 | + e.printStackTrace(); | ||
| 61 | + } | ||
| 62 | + return script; | ||
| 63 | + | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + public void run(String scriptId, ArrayList<Object> listaFatos ) { | ||
| 67 | + Script script = scriptCache.get(scriptId); | ||
| 68 | + | ||
| 69 | + if( script != null ){ | ||
| 70 | + Binding binding = new Binding(); | ||
| 71 | + | ||
| 72 | + for(Object item : listaFatos ) { | ||
| 73 | + binding.setVariable(item.getClass().getSimpleName(), item); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + script.setBinding(binding); | ||
| 77 | + script.run(); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + public void run(String scriptId, Binding listaFatos ) { | ||
| 83 | + Script script = scriptCache.get(scriptId); | ||
| 84 | + | ||
| 85 | + if( script != null ){ | ||
| 86 | + script.setBinding(listaFatos); | ||
| 87 | + script.run(); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + public void removeScriptCache(String scriptId) { | ||
| 93 | + this.scriptCache.remove(scriptId); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | +} | ||
| 0 | \ No newline at end of file | 97 | \ No newline at end of file |
demoiselle-script/src/main/java/org/demoiselle/jee/script/exception/package-info.java
0 → 100644
demoiselle-script/src/main/java/org/demoiselle/jee/script/package-info.java
0 → 100644