View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.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   * Module "workflow" entry class.
51   *
52   * @author Sameer Charles
53   * @author Fabrizio Giustina
54   * @author Nicolas Modrzyk
55   * @version 3.0
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       * The current used engine
65       */
66      private JCRPersistedEngine wfEngine;
67  
68      private JCRWorkItemStore workItemStore;
69  
70      /**
71       * Do we backup the deleted workitems?
72       */
73      private boolean backupWorkItems = false;
74  
75      /**
76       * Use life time jcr sessions or a session per operation
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                  // before try to stop purge and scheduling tasks
93                  // TODO : this is already done by engine.stop() ... ?
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      * Cleanup empty parent nodes (for expressions, workitems)
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      * return the global work flow engine
134      */
135     static public JCRPersistedEngine getEngine() {
136         return instance.wfEngine;
137     }
138 
139     /**
140      * return the global work flow engine
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 }