1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.workflow;
35
36 import info.magnolia.module.ModuleLifecycle;
37 import info.magnolia.module.ModuleLifecycleContext;
38 import info.magnolia.module.workflow.flows.FlowDefinitionManager;
39 import info.magnolia.module.workflow.jcr.JCRPersistedEngine;
40 import info.magnolia.module.workflow.jcr.JCRWorkItemStore;
41 import info.magnolia.objectfactory.Components;
42 import openwfe.org.ServiceException;
43 import openwfe.org.engine.impl.expool.SimpleExpressionPool;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52
53
54
55
56
57 public class WorkflowModule implements ModuleLifecycle {
58
59 private static final Logger log = LoggerFactory.getLogger(WorkflowModule.class);
60
61 private static WorkflowModule instance;
62
63
64
65
66 private JCRPersistedEngine wfEngine;
67
68 private JCRWorkItemStore workItemStore;
69
70
71
72
73 private boolean backupWorkItems = false;
74
75
76
77
78 private boolean useLifeTimeJCRSession = true;
79
80
81 public void start(ModuleLifecycleContext moduleLifecycleContext) {
82 instance = this;
83 startEngine();
84 initializeWorkItemStore();
85 }
86
87 public void stop(ModuleLifecycleContext moduleLifecycleContext) {
88 JCRPersistedEngine engine = getEngine();
89 if (engine != null && engine.isRunning()) {
90 log.info("Stopping workflow engine..");
91 try {
92
93
94 ((SimpleExpressionPool) engine.getExpressionPool()).stop();
95 engine.stop();
96 }
97 catch (ServiceException se) {
98 log.error("Failed to stop Open WFE engine");
99 log.error(se.getMessage(), se);
100 }
101 }
102 }
103
104
105
106
107 private boolean cleanup = true;
108
109 protected void startEngine() {
110 try {
111 log.info("Starting openwfe engine");
112 wfEngine = new JCRPersistedEngine();
113 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_USER+".*"));
114 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_GROUP+".*"));
115 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_ROLE+".*"));
116 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND+".*"));
117 }
118 catch (Exception e) {
119 log.error("An exception arised when creating the workflow engine", e);
120 }
121 }
122
123 protected void initializeWorkItemStore() {
124 try {
125 workItemStore = new JCRWorkItemStore(this.isUseLifeTimeJCRSession(), this.isCleanup(), this.isBackupWorkItems());
126 }
127 catch (Exception e) {
128 log.error("can't initialize the workflow util", e);
129 }
130 }
131
132
133
134
135 static public JCRPersistedEngine getEngine() {
136 return instance.wfEngine;
137 }
138
139
140
141
142 static public JCRWorkItemStore getWorkItemStore() {
143 return instance.workItemStore;
144 }
145
146 public static FlowDefinitionManager getFlowDefinitionManager() {
147 return Components.getSingleton(FlowDefinitionManager.class);
148 }
149
150
151 public boolean isBackupWorkItems() {
152 return this.backupWorkItems;
153 }
154
155
156 public void setBackupWorkItems(boolean backupWorkItems) {
157 this.backupWorkItems = backupWorkItems;
158 }
159
160 public boolean isUseLifeTimeJCRSession() {
161 return useLifeTimeJCRSession;
162 }
163
164 public void setUseLifeTimeJCRSession(boolean useLifeTimeJCRSession) {
165 log.warn("Setting useLifeTimeJCRSession flag should not be used unless you really know what you do.");
166
167 this.useLifeTimeJCRSession = useLifeTimeJCRSession;
168 }
169
170 public boolean isCleanup() {
171 return cleanup;
172 }
173
174 public void setCleanup(boolean cleanup) {
175 log.warn("Setting cleanup flag should not be used unless you really know what you do.");
176 this.cleanup = cleanup;
177 }
178
179 public static WorkflowModule getInstance() {
180 return instance;
181 }
182
183 }