View Javadoc
1   /*
2   
3    * Copyright (C) 2007-2012 Argeo GmbH
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.argeo.slc.jcr.execution;
18  
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.Session;
24  
25  import org.argeo.jcr.JcrUtils;
26  import org.argeo.slc.SlcException;
27  import org.argeo.slc.SlcNames;
28  import org.argeo.slc.core.execution.ProcessThread;
29  import org.argeo.slc.execution.ExecutionModulesManager;
30  import org.argeo.slc.execution.ExecutionProcess;
31  import org.argeo.slc.execution.RealizedFlow;
32  
33  /** Where the actual execution takes place */
34  public class JcrProcessThread extends ProcessThread implements SlcNames {
35  
36  	public JcrProcessThread(ThreadGroup processesThreadGroup,
37  			ExecutionModulesManager executionModulesManager,
38  			JcrExecutionProcess process) {
39  		super(processesThreadGroup, executionModulesManager, process);
40  	}
41  
42  	/** Overridden in order to set progress status on realized flow nodes. */
43  	@Override
44  	protected void process() throws InterruptedException {
45  		Session session = null;
46  		if (getProcess() instanceof JcrExecutionProcess)
47  			try {
48  				session = ((JcrExecutionProcess) getProcess()).getRepository()
49  						.login();
50  
51  				List<RealizedFlow> realizedFlows = getProcess()
52  						.getRealizedFlows();
53  				for (RealizedFlow realizedFlow : realizedFlows) {
54  					Node realizedFlowNode = session
55  							.getNode(((JcrRealizedFlow) realizedFlow).getPath());
56  					setFlowStatus(realizedFlowNode, ExecutionProcess.RUNNING);
57  
58  					try {
59  						//
60  						// EXECUTE THE FLOW
61  						//
62  						execute(realizedFlow, true);
63  
64  						setFlowStatus(realizedFlowNode,
65  								ExecutionProcess.COMPLETED);
66  					} catch (RepositoryException e) {
67  						throw e;
68  					} catch (InterruptedException e) {
69  						setFlowStatus(realizedFlowNode, ExecutionProcess.KILLED);
70  						throw e;
71  					} catch (RuntimeException e) {
72  						setFlowStatus(realizedFlowNode, ExecutionProcess.ERROR);
73  						throw e;
74  					}
75  				}
76  			} catch (RepositoryException e) {
77  				throw new SlcException("Cannot process "
78  						+ getJcrExecutionProcess().getNodePath(), e);
79  			} finally {
80  				JcrUtils.logoutQuietly(session);
81  			}
82  		else
83  			super.process();
84  	}
85  
86  	protected void setFlowStatus(Node realizedFlowNode, String status)
87  			throws RepositoryException {
88  		realizedFlowNode.setProperty(SLC_STATUS, status);
89  		realizedFlowNode.getSession().save();
90  	}
91  
92  	protected JcrExecutionProcess getJcrExecutionProcess() {
93  		return (JcrExecutionProcess) getProcess();
94  	}
95  }