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          // MAGNOLIA-3491: this is used to ensure that the attributes in workflow items are encoded/decoded correctly
88          System.setProperty("openwfe.xml.encoding", "UTF-8");
89          instance = this;
90          startEngine();
91          initializeWorkItemStore();
92      }
93  
94      @Override
95      public void stop(ModuleLifecycleContext moduleLifecycleContext) {
96          JCRPersistedEngine engine = getEngine();
97          if (engine != null && engine.isRunning()) {
98              log.info("Stopping workflow engine..");
99              try {
100                 // before try to stop purge and scheduling tasks
101                 // TODO : this is already done by engine.stop() ... ?
102                 ((SimpleExpressionPool) engine.getExpressionPool()).stop();
103                 engine.stop();
104             }
105             catch (ServiceException se) {
106                 log.error("Failed to stop Open WFE engine");
107                 log.error(se.getMessage(), se);
108             }
109         }
110     }
111 
112     /**
113      * Cleanup empty parent nodes (for expressions, workitems).
114      */
115     private boolean cleanup = true;
116 
117     protected void startEngine() {
118         try {
119             log.info("Starting openwfe engine");
120             wfEngine = new JCRPersistedEngine();
121             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_USER+".*"));
122             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_GROUP+".*"));
123             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_ROLE+".*"));
124             wfEngine.registerParticipant(new MgnlParticipant(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND+".*"));
125         }
126         catch (Exception e) {
127             log.error("An exception arised when creating the workflow engine", e);
128         }
129     }
130 
131     protected void initializeWorkItemStore() {
132         try {
133             workItemStore = new JCRWorkItemStore(this.isUseLifeTimeJCRSession(), this.isCleanup(), this.isBackupWorkItems());
134         }
135         catch (Exception e) {
136             log.error("can't initialize the workflow util", e);
137         }
138     }
139 
140     /**
141      * return the global work flow engine.
142      */
143     static public JCRPersistedEngine getEngine() {
144         return instance.wfEngine;
145     }
146 
147     /**
148      * return the global work flow engine.
149      */
150     static public JCRWorkItemStore getWorkItemStore() {
151         return instance.workItemStore;
152     }
153 
154     public static FlowDefinitionManager getFlowDefinitionManager() {
155         return Components.getSingleton(FlowDefinitionManager.class);
156     }
157 
158 
159     public boolean isBackupWorkItems() {
160         return this.backupWorkItems;
161     }
162 
163 
164     public void setBackupWorkItems(boolean backupWorkItems) {
165         this.backupWorkItems = backupWorkItems;
166     }
167 
168     public boolean isUseLifeTimeJCRSession() {
169         return useLifeTimeJCRSession;
170     }
171 
172     public void setUseLifeTimeJCRSession(boolean useLifeTimeJCRSession) {
173         log.warn("Setting useLifeTimeJCRSession flag should not be used unless you really know what you do.");
174 
175         this.useLifeTimeJCRSession = useLifeTimeJCRSession;
176     }
177 
178     public boolean isCleanup() {
179         return cleanup;
180     }
181 
182     public void setCleanup(boolean cleanup) {
183         log.warn("Setting cleanup flag should not be used unless you really know what you do.");
184         this.cleanup = cleanup;
185     }
186 
187     /**
188      * @deprecated since 4.5, use IoC !
189      */
190     public static WorkflowModule getInstance() {
191         DeprecationUtil.isDeprecated("Use IoC!");
192         return instance;
193     }
194 
195 }