/jbpm-flow/src/main/java/org/jbpm/process/instance/event/DefaultSignalManager.java

https://github.com/mariofusco/jbpm · Java · 218 lines · 167 code · 34 blank · 17 comment · 35 complexity · e3eaf2ab519e2ac3b57366036d5b3e6f MD5 · raw file

  1. /**
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.process.instance.event;
  17. import java.io.IOException;
  18. import java.io.ObjectInput;
  19. import java.io.ObjectOutput;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.CopyOnWriteArrayList;
  24. import org.drools.core.common.InternalKnowledgeRuntime;
  25. import org.drools.core.common.InternalWorkingMemory;
  26. import org.drools.core.common.WorkingMemoryAction;
  27. import org.drools.core.marshalling.impl.MarshallerReaderContext;
  28. import org.drools.core.marshalling.impl.MarshallerWriteContext;
  29. import org.drools.core.marshalling.impl.ProtobufMessages.ActionQueue.Action;
  30. import org.kie.api.runtime.process.EventListener;
  31. import org.kie.api.runtime.process.ProcessInstance;
  32. import org.jbpm.process.instance.InternalProcessRuntime;
  33. public class DefaultSignalManager implements SignalManager {
  34. private Map<String, List<EventListener>> processEventListeners;
  35. private InternalKnowledgeRuntime kruntime;
  36. public DefaultSignalManager(InternalKnowledgeRuntime kruntime) {
  37. this.kruntime = kruntime;
  38. }
  39. public InternalKnowledgeRuntime getKnowledgeRuntime() {
  40. return kruntime;
  41. }
  42. public void addEventListener(String type, EventListener eventListener) {
  43. if (processEventListeners == null) {
  44. processEventListeners = new HashMap<String, List<EventListener>>();
  45. }
  46. List<EventListener> eventListeners = processEventListeners.get(type);
  47. if (eventListeners == null) {
  48. eventListeners = new CopyOnWriteArrayList<EventListener>();
  49. processEventListeners.put(type, eventListeners);
  50. }
  51. eventListeners.add(eventListener);
  52. }
  53. public void removeEventListener(String type, EventListener eventListener) {
  54. if (processEventListeners != null) {
  55. List<EventListener> eventListeners = processEventListeners.get(type);
  56. if (eventListeners != null) {
  57. eventListeners.remove(eventListener);
  58. }
  59. }
  60. }
  61. public void signalEvent(String type, Object event) {
  62. kruntime.queueWorkingMemoryAction(new SignalAction(type, event));
  63. kruntime.executeQueuedActions();
  64. }
  65. public void internalSignalEvent(String type, Object event) {
  66. if (processEventListeners != null) {
  67. List<EventListener> eventListeners = processEventListeners.get(type);
  68. if (eventListeners != null) {
  69. for (EventListener eventListener: eventListeners) {
  70. eventListener.signalEvent(type, event);
  71. }
  72. }
  73. }
  74. }
  75. public void signalEvent(long processInstanceId, String type, Object event) {
  76. ProcessInstance processInstance = kruntime.getProcessInstance(processInstanceId);
  77. if (processInstance != null) {
  78. kruntime.queueWorkingMemoryAction(new SignalProcessInstanceAction(processInstanceId, type, event));
  79. kruntime.executeQueuedActions();
  80. }
  81. }
  82. public static class SignalProcessInstanceAction implements WorkingMemoryAction {
  83. private long processInstanceId;
  84. private String type;
  85. private Object event;
  86. public SignalProcessInstanceAction(long processInstanceId, String type, Object event) {
  87. this.processInstanceId = processInstanceId;
  88. this.type = type;
  89. this.event = event;
  90. }
  91. public SignalProcessInstanceAction(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
  92. processInstanceId = context.readLong();
  93. type = context.readUTF();
  94. if (context.readBoolean()) {
  95. event = context.readObject();
  96. }
  97. }
  98. public void execute(InternalWorkingMemory workingMemory) {
  99. ProcessInstance processInstance = workingMemory.getProcessInstance(processInstanceId);
  100. if (processInstance != null) {
  101. processInstance.signalEvent(type, event);
  102. }
  103. }
  104. public void execute(InternalKnowledgeRuntime kruntime) {
  105. ProcessInstance processInstance = kruntime.getProcessInstance(processInstanceId);
  106. if (processInstance != null) {
  107. processInstance.signalEvent(type, event);
  108. }
  109. }
  110. public void write(MarshallerWriteContext context) throws IOException {
  111. context.writeInt( WorkingMemoryAction.SignalProcessInstanceAction );
  112. context.writeLong(processInstanceId);
  113. context.writeUTF(type);
  114. context.writeBoolean(event != null);
  115. if (event != null) {
  116. context.writeObject(event);
  117. }
  118. }
  119. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  120. processInstanceId = in.readLong();
  121. type = in.readUTF();
  122. if (in.readBoolean()) {
  123. event = in.readObject();
  124. }
  125. }
  126. public void writeExternal(ObjectOutput out) throws IOException {
  127. out.writeLong(processInstanceId);
  128. out.writeUTF(type);
  129. out.writeBoolean(event != null);
  130. if (event != null) {
  131. out.writeObject(event);
  132. }
  133. }
  134. public Action serialize(MarshallerWriteContext context) throws IOException {
  135. // TODO Auto-generated method stub
  136. return null;
  137. }
  138. }
  139. public static class SignalAction implements WorkingMemoryAction {
  140. private String type;
  141. private Object event;
  142. public SignalAction(String type, Object event) {
  143. this.type = type;
  144. this.event = event;
  145. }
  146. public SignalAction(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
  147. type = context.readUTF();
  148. if (context.readBoolean()) {
  149. event = context.readObject();
  150. }
  151. }
  152. public void execute(InternalWorkingMemory workingMemory) {
  153. ((DefaultSignalManager) ((InternalProcessRuntime) workingMemory.getProcessRuntime()).getSignalManager()).internalSignalEvent(type, event);
  154. }
  155. public void execute(InternalKnowledgeRuntime kruntime) {
  156. ((DefaultSignalManager) ((InternalProcessRuntime) kruntime.getProcessRuntime()).getSignalManager()).internalSignalEvent(type, event);
  157. }
  158. public void write(MarshallerWriteContext context) throws IOException {
  159. context.writeInt( WorkingMemoryAction.SignalAction );
  160. context.writeUTF(type);
  161. context.writeBoolean(event != null);
  162. if (event != null) {
  163. context.writeObject(event);
  164. }
  165. }
  166. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  167. type = in.readUTF();
  168. if (in.readBoolean()) {
  169. event = in.readObject();
  170. }
  171. }
  172. public void writeExternal(ObjectOutput out) throws IOException {
  173. out.writeUTF(type);
  174. out.writeBoolean(event != null);
  175. if (event != null) {
  176. out.writeObject(event);
  177. }
  178. }
  179. public Action serialize(MarshallerWriteContext context) throws IOException {
  180. // TODO Auto-generated method stub
  181. return null;
  182. }
  183. }
  184. }