View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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  import openwfe.org.ServiceException;
44  import openwfe.org.engine.impl.expool.SimpleExpressionPool;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  
50  /**
51   * Module "workflow" entry class.
52   *
53   * @author Sameer Charles
54   * @author Fabrizio Giustina
55   * @author Nicolas Modrzyk
56   * @version 3.0
57   */
58  public class WorkflowModule implements ModuleLifecycle {
59  
60      private static final Logger log = LoggerFactory.getLogger(WorkflowModule.class);
61  
62      /**
63       * @deprecated since 4.5, use IoC !
64       */
65      private static WorkflowModule instance;
66  
67      /**
68       * The current used engine.
69       */
70      private JCRPersistedEngine wfEngine;
71  
72      private JCRWorkItemStore workItemStore;
73  
74      /**
75       * Do we backup the deleted workitems?
76       */
77      private boolean backupWorkItems = false;
78  
79      /**
80       * Use life time jcr sessions or a session per operation.
81       */
82      private boolean useLifeTimeJCRSession = true;
83  
84  
85      @Override
86      public void start(ModuleLifecycleContext moduleLifecycleContext) {
87          instance = this;
88          startEngine();
89          initializeWorkItemStore();
90      }
91  
92      @Override
93      public void stop(ModuleLifecycleContext moduleLifecycleContext) {
94          JCRPersistedEngine engine = getEngine();
95          if (engine != null && engine.isRunning()) {
96              log.info("Stopping workflow engine..");
97              try {
98                  // before try to stop purge and scheduling tasks
99                  // TODO : this is already done by engine.stop() ... ?
100                 ((SimpleExpressionPool) engine.getExpressionPool()).stop();
101                 engine.stop();
102             }
103             catch (ServiceException se) {
104                 log.error("Failed to stop Open WFE engine");
105                 log.error(se.getMessage(), se);
106             }
107         }
108     }
109 
110     /**
111      * Cleanup empty parent nodes (for expressions, workitems).
112      */
113     private boolean cleanup = true;
114 
115     protected void startEngine() {
116         try {
117             log.info("Starting openwfe engine");
118             wfEngine = new JCRPersistedEngine();
119             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_USER+".*"));
120             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_GROUP+".*"));
121             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_ROLE+".*"));
122             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND+".*"));
123         }
124         catch (Exception e) {
125             log.error("An exception arised when creating the workflow engine", e);
126         }
127     }
128 
129     protected void initializeWorkItemStore() {
130         try {
131             workItemStore = new JCRWorkItemStore(this.isUseLifeTimeJCRSession(), this.isCleanup(), this.isBackupWorkItems());
132         }
133         catch (Exception e) {
134             log.error("can't initialize the workflow util", e);
135         }
136     }
137 
138     /**
139      * return the global work flow engine.
140      */
141     static public JCRPersistedEngine getEngine() {
142         return instance.wfEngine;
143     }
144 
145     /**
146      * return the global work flow engine.
147      */
148     static public JCRWorkItemStore getWorkItemStore() {
149         return instance.workItemStore;
150     }
151 
152     public static FlowDefinitionManager getFlowDefinitionManager() {
153         return Components.getSingleton(FlowDefinitionManager.class);
154     }
155 
156 
157     public boolean isBackupWorkItems() {
158         return this.backupWorkItems;
159     }
160 
161 
162     public void setBackupWorkItems(boolean backupWorkItems) {
163         this.backupWorkItems = backupWorkItems;
164     }
165 
166     public boolean isUseLifeTimeJCRSession() {
167         return useLifeTimeJCRSession;
168     }
169 
170     public void setUseLifeTimeJCRSession(boolean useLifeTimeJCRSession) {
171         log.warn("Setting useLifeTimeJCRSession flag should not be used unless you really know what you do.");
172 
173         this.useLifeTimeJCRSession = useLifeTimeJCRSession;
174     }
175 
176     public boolean isCleanup() {
177         return cleanup;
178     }
179 
180     public void setCleanup(boolean cleanup) {
181         log.warn("Setting cleanup flag should not be used unless you really know what you do.");
182         this.cleanup = cleanup;
183     }
184 
185     /**
186      * @deprecated since 4.5, use IoC !
187      */
188     public static WorkflowModule getInstance() {
189         DeprecationUtil.isDeprecated("Use IoC!");
190         return instance;
191     }
192 
193 }