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.cms.util.DeprecationUtil;
37 import info.magnolia.module.ModuleLifecycle;
38 import info.magnolia.module.ModuleLifecycleContext;
39 import info.magnolia.module.workflow.flows.FlowDefinitionManager;
40 import info.magnolia.module.workflow.jcr.JCRPersistedEngine;
41 import info.magnolia.module.workflow.jcr.JCRWorkItemStore;
42 import info.magnolia.objectfactory.Components;
43
44 import java.util.HashMap;
45 import java.util.Map;
46
47 import openwfe.org.ServiceException;
48 import openwfe.org.engine.impl.expool.SimpleExpressionPool;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58
59
60
61 public class WorkflowModule implements ModuleLifecycle {
62
63 private static final Logger log = LoggerFactory.getLogger(WorkflowModule.class);
64
65
66
67
68 private static WorkflowModule instance;
69
70
71
72
73 private JCRPersistedEngine wfEngine;
74
75 private JCRWorkItemStore workItemStore;
76
77
78
79
80 private boolean backupWorkItems = false;
81
82
83
84
85 private boolean useLifeTimeJCRSession = true;
86
87 private Map<String, String> showFunctions = new HashMap<String, String>();
88
89 @Override
90 public void start(ModuleLifecycleContext moduleLifecycleContext) {
91
92 System.setProperty("openwfe.xml.encoding", "UTF-8");
93 instance = this;
94 startEngine();
95 initializeWorkItemStore();
96 }
97
98 @Override
99 public void stop(ModuleLifecycleContext moduleLifecycleContext) {
100 JCRPersistedEngine engine = getEngine();
101 if (engine != null && engine.isRunning()) {
102 log.info("Stopping workflow engine..");
103 try {
104
105
106 ((SimpleExpressionPool) engine.getExpressionPool()).stop();
107 engine.stop();
108 } catch (ServiceException se) {
109 log.error("Failed to stop Open WFE engine");
110 log.error(se.getMessage(), se);
111 }
112 }
113 }
114
115
116
117
118 private boolean cleanup = true;
119
120 protected void startEngine() {
121 try {
122 log.info("Starting openwfe engine");
123 wfEngine = new JCRPersistedEngine();
124 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_USER + ".*"));
125 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_GROUP + ".*"));
126 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_ROLE + ".*"));
127 wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND + ".*"));
128 } catch (Exception e) {
129 log.error("An exception arised when creating the workflow engine", e);
130 }
131 }
132
133 protected void initializeWorkItemStore() {
134 try {
135 workItemStore = new JCRWorkItemStore(this.isUseLifeTimeJCRSession(), this.isCleanup(), this.isBackupWorkItems());
136 } catch (Exception e) {
137 log.error("can't initialize the workflow util", e);
138 }
139 }
140
141
142
143
144 static public JCRPersistedEngine getEngine() {
145 return instance.wfEngine;
146 }
147
148
149
150
151 static public JCRWorkItemStore getWorkItemStore() {
152 return instance.workItemStore;
153 }
154
155 public static FlowDefinitionManager getFlowDefinitionManager() {
156 return Components.getSingleton(FlowDefinitionManager.class);
157 }
158
159 public boolean isBackupWorkItems() {
160 return this.backupWorkItems;
161 }
162
163 public void setBackupWorkItems(boolean backupWorkItems) {
164 this.backupWorkItems = backupWorkItems;
165 }
166
167 public boolean isUseLifeTimeJCRSession() {
168 return useLifeTimeJCRSession;
169 }
170
171 public void setUseLifeTimeJCRSession(boolean useLifeTimeJCRSession) {
172 log.warn("Setting useLifeTimeJCRSession flag should not be used unless you really know what you do.");
173
174 this.useLifeTimeJCRSession = useLifeTimeJCRSession;
175 }
176
177 public boolean isCleanup() {
178 return cleanup;
179 }
180
181 public void setCleanup(boolean cleanup) {
182 log.warn("Setting cleanup flag should not be used unless you really know what you do.");
183 this.cleanup = cleanup;
184 }
185
186 public Map<String, String> getShowFunctions() {
187 return this.showFunctions;
188 }
189
190 public void setShowFunctions(Map<String, String> showFunctions) {
191 this.showFunctions = showFunctions;
192 }
193
194 public void addShowFunction(String repository, String showFunction) {
195 this.showFunctions.put(repository, showFunction);
196 }
197
198
199
200
201 public static WorkflowModule getInstance() {
202 DeprecationUtil.isDeprecated("Use IoC!");
203 return instance;
204 }
205
206 }