View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Module "workflow" entry class.
55   * 
56   * @author Sameer Charles
57   * @author Fabrizio Giustina
58   * @author Nicolas Modrzyk
59   * @version 3.0
60   */
61  public class WorkflowModule implements ModuleLifecycle {
62  
63      private static final Logger log = LoggerFactory.getLogger(WorkflowModule.class);
64  
65      /**
66       * @deprecated since 4.5, use IoC !
67       */
68      private static WorkflowModule instance;
69  
70      /**
71       * The current used engine.
72       */
73      private JCRPersistedEngine wfEngine;
74  
75      private JCRWorkItemStore workItemStore;
76  
77      /**
78       * Do we backup the deleted workitems?
79       */
80      private boolean backupWorkItems = false;
81  
82      /**
83       * Use life time jcr sessions or a session per operation.
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          // MAGNOLIA-3491: this is used to ensure that the attributes in workflow items are encoded/decoded correctly
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                 // before try to stop purge and scheduling tasks
105                 // TODO : this is already done by engine.stop() ... ?
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      * Cleanup empty parent nodes (for expressions, workitems).
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      * return the global work flow engine.
143      */
144     static public JCRPersistedEngine getEngine() {
145         return instance.wfEngine;
146     }
147 
148     /**
149      * return the global work flow engine.
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      * @deprecated since 4.5, use IoC !
200      */
201     public static WorkflowModule getInstance() {
202         DeprecationUtil.isDeprecated("Use IoC!");
203         return instance;
204     }
205 
206 }