This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit af1d4e3450f78a4b9deae68502efcec395b64c59
Author: juanpablo <[email protected]>
AuthorDate: Sat Apr 11 18:03:06 2020 +0200

    Add WorkflowEventEmitter enum to emit workflow-related events and register 
as client on DefaultWorkflowManager, which is an event listener
    
    Also DefaultWorkflowManager, reacts to new Workflow events DQ_ADDITION and 
DQ_REMOVAL
---
 .../wiki/workflow/DefaultWorkflowManager.java      | 53 ++++++++++++++--------
 .../apache/wiki/workflow/WorkflowEventEmitter.java | 48 ++++++++++++++++++++
 2 files changed, 82 insertions(+), 19 deletions(-)

diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java
 
b/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java
index 4703763..d837611 100644
--- 
a/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java
+++ 
b/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java
@@ -24,6 +24,7 @@ import org.apache.wiki.api.exceptions.WikiException;
 import org.apache.wiki.auth.AuthorizationManager;
 import org.apache.wiki.auth.acl.UnresolvedPrincipal;
 import org.apache.wiki.event.WikiEvent;
+import org.apache.wiki.event.WikiEventManager;
 import org.apache.wiki.event.WorkflowEvent;
 
 import java.security.Principal;
@@ -59,6 +60,7 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
         m_workflows = ConcurrentHashMap.newKeySet();
         m_approvers = new ConcurrentHashMap<>();
         m_completed = new CopyOnWriteArrayList<>();
+        WikiEventManager.addWikiEventListener( WorkflowEventEmitter.get(), 
this );
     }
 
     /**
@@ -67,7 +69,6 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
     @Override
     public void start( final Workflow workflow ) throws WikiException {
         m_workflows.add( workflow );
-        workflow.setWorkflowManager( this );
         workflow.setId( nextId() );
         workflow.start();
     }
@@ -170,16 +171,15 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
      *
      * @return the decision queue
      */
-    @Override public DecisionQueue getDecisionQueue()
-    {
+    @Override
+    public DecisionQueue getDecisionQueue() {
         return m_queue;
     }
 
     private volatile int m_next;
 
     /**
-     * Returns the next available unique identifier, which is subsequently
-     * incremented.
+     * Returns the next available unique identifier, which is subsequently 
incremented.
      *
      * @return the id
      */
@@ -211,25 +211,35 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
     }
 
     /**
-     * Listens for {@link WorkflowEvent} objects emitted by Workflows. In 
particular, this
-     * method listens for {@link WorkflowEvent#CREATED},
-     * {@link WorkflowEvent#ABORTED} and {@link WorkflowEvent#COMPLETED}
-     * events. If a workflow is created, it is automatically added to the 
cache. If one is aborted or completed, it 
-     * is automatically removed.
+     * Listens for {@link WorkflowEvent} objects emitted by Workflows. In 
particular, this method listens for {@link WorkflowEvent#CREATED},
+     * {@link WorkflowEvent#ABORTED}, {@link WorkflowEvent#COMPLETED} and 
{@link WorkflowEvent#DQ_REMOVAL} events. If a workflow is created,
+     * it is automatically added to the cache. If one is aborted or completed, 
it is automatically removed. If a removal from decision queue
+     * is issued, the current step from workflow, which is assumed to be a 
{@link Decision}, is removed from the {@link DecisionQueue}.
      * 
      * @param event the event passed to this listener
      */
     @Override
     public void actionPerformed( final WikiEvent event ) {
         if( event instanceof WorkflowEvent ) {
-            final Workflow workflow = event.getSrc();
-            switch ( event.getType() ) {
-                 // Remove from manager
-                 case WorkflowEvent.ABORTED   :
-                 case WorkflowEvent.COMPLETED : remove( workflow ); break;
+            if( event.getSrc() instanceof Workflow ) {
+                final Workflow workflow = event.getSrc();
+                switch( event.getType() ) {
+                // Remove from manager
+                case WorkflowEvent.ABORTED   :
+                case WorkflowEvent.COMPLETED : remove( workflow ); break;
                 // Add to manager
-                case WorkflowEvent.CREATED    : add( workflow ); break;
+                case WorkflowEvent.CREATED   : add( workflow ); break;
+                default: break;
+                }
+            } else if( event.getSrc() instanceof Decision ) {
+                final Decision decision = event.getSrc();
+                switch( event.getType() ) {
+                // Add to DecisionQueue
+                case WorkflowEvent.DQ_ADDITION : addToDecisionQueue( decision 
); break;
+                // Remove from DecisionQueue
+                case WorkflowEvent.DQ_REMOVAL  : removeFromDecisionQueue( 
decision ); break;
                 default: break;
+                }
             }
         }
     }
@@ -241,9 +251,6 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
      * @param workflow the workflow to add
      */
     protected void add( final Workflow workflow ) {
-        if ( workflow.getWorkflowManager() == null ) {
-            workflow.setWorkflowManager( this );
-        }
         if ( workflow.getId() == Workflow.ID_NOT_SET ) {
             workflow.setId( nextId() );
         }
@@ -263,4 +270,12 @@ public class DefaultWorkflowManager implements 
WorkflowManager {
         }
     }
 
+    protected void removeFromDecisionQueue( final Decision decision ) {
+        getDecisionQueue().remove( decision );
+    }
+
+    protected void addToDecisionQueue( final Decision decision ) {
+        getDecisionQueue().add( decision );
+    }
+
 }
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/workflow/WorkflowEventEmitter.java 
b/jspwiki-main/src/main/java/org/apache/wiki/workflow/WorkflowEventEmitter.java
new file mode 100644
index 0000000..6ab5f86
--- /dev/null
+++ 
b/jspwiki-main/src/main/java/org/apache/wiki/workflow/WorkflowEventEmitter.java
@@ -0,0 +1,48 @@
+/*
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, 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.apache.wiki.workflow;
+
+import org.apache.wiki.event.WikiEventManager;
+import org.apache.wiki.event.WorkflowEvent;
+
+
+/**
+ * Emits all kind of {@link WorkflowEvent}s.
+ */
+public enum WorkflowEventEmitter {
+
+    INSTANCE;
+
+    public static WorkflowEventEmitter get() {
+        return INSTANCE;
+    }
+
+    public static void fireEvent( final Decision decision, final int type ) {
+        if ( WikiEventManager.isListening( get() ) ) {
+            WikiEventManager.fireEvent( get(), new WorkflowEvent( decision, 
type ) );
+        }
+    }
+
+    public static void fireEvent( final Workflow workflow, final int type ) {
+        if ( WikiEventManager.isListening( get() ) ) {
+            WikiEventManager.fireEvent( get(), new WorkflowEvent( workflow, 
type ) );
+        }
+    }
+
+}

Reply via email to