Author: cmailleux
Date: Fri May 18 16:37:17 2007
New Revision: 17332

URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17332&repname=
=3Djahia
Log:
Resolve issue JAHIA-1952
Store groovy script resolution and allow script to register for some events=
 only

Modified:
    branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/services/events/Gr=
oovyEventListener.java
    branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/var/scripts/groovy=
/events/EventListener.groovy

Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/services/eve=
nts/GroovyEventListener.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/java/org/jahia/services/events/GroovyEventListener.java&re=
v=3D17332&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/services/events/Gr=
oovyEventListener.java (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/java/org/jahia/services/events/Gr=
oovyEventListener.java Fri May 18 16:37:17 2007
@@ -14,7 +14,8 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied=
. =

  * See the License for the specific language governing permissions and =

  * limitations under the License.
- */package org.jahia.services.events;
+ */
+package org.jahia.services.events;
 =

 import groovy.lang.Binding;
 import groovy.util.GroovyScriptEngine;
@@ -28,8 +29,10 @@
 import org.jahia.params.ProcessingContext;
 import org.jahia.services.timebasedpublishing.RetentionRuleEvent;
 import org.jahia.services.workflow.WorkflowEvent;
+import org.apache.commons.collections.FastHashMap;
 =

 import java.io.File;
+import java.util.Map;
 =

 /**
  * <p>Title: A Jahia Event Listener that dispatches to Groovy script files=
</p>
@@ -67,6 +70,9 @@
         return configLoaded;
     }
 =

+    private static Map eventsHandled =3D null;
+    private static Map siteEventFile =3D new FastHashMap(64);
+    private static String staticeventsToHandleList =3D "";
     private void loadConfig (ProcessingContext processingContext) {
 =

         /**
@@ -85,55 +91,58 @@
     }
 =

     private void dispatchToGroovyScript (String eventName, JahiaEvent je) {
-        if (!checkConfig (je.getProcessingContext ())) {
-            return;
-        }
+        ProcessingContext processingContext =3D je.getProcessingContext();
+        if (checkConfig(processingContext) && (eventsHandled=3D=3D null ||=
 eventsHandled.containsKey(eventName))) {
+            try {
+                String groovyFileName =3D resolveGroovyFullFileName(proces=
singContext);
+
+                long startTime =3D System.currentTimeMillis();
+                synchronized (groovyScriptEngine) {
+                    Binding binding =3D new Binding();
+                    binding.setVariable("eventName", eventName);
+                    binding.setVariable("jahiaEvent", je);
+                    groovyScriptEngine.run(groovyFileName, binding);
+                }
+                long endTime =3D System.currentTimeMillis();
+                long executionTime =3D endTime - startTime;
+                logger.debug("Groovy script event listener=3D" + groovyFil=
eName + " event=3D" + eventName +
+                             " execution time=3D" + executionTime + "ms");
 =

-        ProcessingContext processingContext =3D je.getProcessingContext ();
-        try {
-            String groovyFileName =3D resolveGroovyFullFileName (processin=
gContext);
-
-            long startTime =3D System.currentTimeMillis();
-            synchronized(groovyScriptEngine) {
-                Binding binding =3D new Binding();
-                binding.setVariable ("eventName", eventName);
-                binding.setVariable ("jahiaEvent", je);
-                groovyScriptEngine.run(groovyFileName, binding);
+            } catch (Throwable t) {
+                logger.error("Error while dispatching to Groovy script : "=
 + groovyFileName, t);
             }
-            long endTime =3D System.currentTimeMillis();
-            long executionTime =3D endTime - startTime;
-            logger.debug("Groovy script event listener=3D"+ groovyFileName=
 + " event=3D" + eventName + " execution time=3D" + executionTime + "ms");
-
-        } catch (Throwable t) {
-            logger.error ("Error while dispatching to Groovy script : " + =
groovyFileName, t);
         }
     }
 =

-    private String resolveGroovyFullFileName (ProcessingContext processing=
Context) {
+    private String resolveGroovyFullFileName(ProcessingContext processingC=
ontext) {
         String groovyFullFileName;
         StringBuffer nameBuilder =3D new StringBuffer("/jsp/jahia/");
+        if (processingContext !=3D null && siteEventFile.containsKey(proce=
ssingContext.getSiteKey()))
+            return (String) siteEventFile.get(processingContext.getSiteKey=
());
+        else if ((processingContext !=3D null) &&
+                 (processingContext.getSite() !=3D null) &&
+                 (processingContext.getSite().getHomeContentPage() !=3D nu=
ll) &&
+                 (processingContext.getSite().getHomeContentPage().getPage=
Template(processingContext).getSourcePath() !=3D
+                  null)) {
+            groovyFullFileName =3D
+                    processingContext.getSite().getHomeContentPage().getPa=
geTemplate(processingContext).getSourcePath();
+            logger.debug("template source path :" + groovyFullFileName);
 =

-        if (    (processingContext !=3D null) &&
-                (processingContext.getContentPage() !=3D null) &&
-            (processingContext.getContentPage ().getPageTemplate (processi=
ngContext) !=3D null) &&
-            (processingContext.getContentPage ().getPageTemplate (processi=
ngContext).getSourcePath () !=3D null)) {
-            groovyFullFileName =3D processingContext.getContentPage ().
-                    getPageTemplate (processingContext).
-                    getSourcePath ();
-            logger.debug ("template source path :" + groovyFullFileName);
-
-            groovyFullFileName =3D groovyFullFileName.substring (0,
-                    groovyFullFileName.lastIndexOf ("/") + 1) + groovyFile=
Name;
+            groovyFullFileName =3D
+                    groovyFullFileName.substring(0, groovyFullFileName.las=
tIndexOf("/") + 1) + groovyFileName;
 =

             groovyFullFileName =3D groovyFullFileName.substring(nameBuilde=
r.length());
 =

-            logger.debug ("resolvedGroovyFullFileName :" + groovyFullFileN=
ame);
+            logger.debug("resolvedGroovyFullFileName :" + groovyFullFileNa=
me);
 =

-            File groovyScriptFile =3D new File (processingContext.settings=
().getPathResolver().resolvePath(groovyFullFileName));
-            if (!groovyScriptFile.exists ()) {
+            File groovyScriptFile =3D
+                    new File(processingContext.settings().getPathResolver(=
).resolvePath(groovyFullFileName));
+            if (!groovyScriptFile.exists()) {
                 groovyFullFileName =3D defaultPathToGroovy;
             }
-        } else {
+            siteEventFile.put(processingContext.getSiteKey(), groovyFullFi=
leName);
+        }
+        else {
             groovyFullFileName =3D defaultPathToGroovy;
         }
         return groovyFullFileName;
@@ -302,4 +311,15 @@
         dispatchToGroovyScript ("timeBasedPublishingEvent", theEvent);
     }
 =

+    public static void registerEvents(String eventsToHandleList) {
+        if (!staticeventsToHandleList.equals(eventsToHandleList)) {
+            staticeventsToHandleList =3D eventsToHandleList;
+            String eventsToHandle[] =3D eventsToHandleList.split(",");
+            eventsHandled =3D new FastHashMap(64);
+            for (int i =3D 0; i < eventsToHandle.length; i++) {
+                String s =3D eventsToHandle[i];
+                eventsHandled.put(s, s);
+            }
+        }
+    }
 }

Modified: branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/var/scripts/=
groovy/events/EventListener.groovy
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-5-0-SP=
-BRANCH/core/src/webapp/WEB-INF/var/scripts/groovy/events/EventListener.gro=
ovy&rev=3D17332&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/var/scripts/groovy=
/events/EventListener.groovy (original)
+++ branches/JAHIA-5-0-SP-BRANCH/core/src/webapp/WEB-INF/var/scripts/groovy=
/events/EventListener.groovy Fri May 18 16:37:17 2007
@@ -10,6 +10,9 @@
 import org.jahia.services.mail.*
 import org.jahia.registries.ServicesRegistry
 =

+// register comma separated lsit of events
+org.jahia.services.events.GroovyEventListener.registerEvents("contentObjec=
tCreated");
+
 org.apache.log4j.Logger logger =3D
     org.apache.log4j.Logger.getLogger(getClass())
 logger.debug("Entering in groovy Eventlistner")

_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list

Reply via email to