2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.web
.mvc
.controllers
;
19 import java
.io
.BufferedReader
;
20 import java
.util
.Comparator
;
21 import java
.util
.List
;
22 import java
.util
.SortedSet
;
23 import java
.util
.TreeSet
;
24 import java
.util
.UUID
;
26 import javax
.servlet
.http
.HttpServletRequest
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
31 import org
.argeo
.slc
.dao
.process
.SlcExecutionDao
;
32 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
33 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
34 import org
.argeo
.slc
.msg
.MsgConstants
;
35 import org
.argeo
.slc
.msg
.ObjectList
;
36 import org
.argeo
.slc
.process
.SlcExecution
;
37 import org
.argeo
.slc
.process
.SlcExecutionStep
;
38 import org
.argeo
.slc
.runtime
.SlcAgent
;
39 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
40 import org
.argeo
.slc
.services
.SlcExecutionService
;
41 import org
.springframework
.oxm
.Marshaller
;
42 import org
.springframework
.oxm
.Unmarshaller
;
43 import org
.springframework
.stereotype
.Controller
;
44 import org
.springframework
.ui
.Model
;
45 import org
.springframework
.util
.Assert
;
46 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
47 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
48 import org
.springframework
.xml
.transform
.StringSource
;
51 public class ProcessController
{
53 public final static String KEY_ANSWER
= "__answer";
54 private final static Log log
= LogFactory
.getLog(ProcessController
.class);
56 private SlcExecutionDao slcExecutionDao
;
57 private SlcAgentFactory agentFactory
;
58 private Unmarshaller unmarshaller
;
59 private Marshaller marshaller
;
60 private SlcExecutionService slcExecutionService
;
61 private AttachmentsStorage attachmentsStorage
;
63 private SlcExecutionManager slcExecutionManager
;
65 @RequestMapping("/listSlcExecutions.service")
66 protected ObjectList
listSlcExecutions() {
67 List
<SlcExecution
> list
= slcExecutionDao
.listSlcExecutions();
68 return new ObjectList(list
);
71 @RequestMapping("/getExecutionDescriptor.service")
72 protected ExecutionModuleDescriptor
getExecutionDescriptor(
73 @RequestParam String agentId
, @RequestParam String moduleName
,
74 @RequestParam String version
) {
76 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
78 ExecutionModuleDescriptor md
= slcAgent
.getExecutionModuleDescriptor(
83 @RequestMapping("/listModulesDescriptors.service")
84 protected ObjectList
listModulesDescriptors(@RequestParam String agentId
) {
85 // TODO: use centralized agentId property (from MsgConstants)?
86 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
88 List
<ExecutionModuleDescriptor
> descriptors
= slcAgent
89 .listExecutionModuleDescriptors();
90 SortedSet
<ExecutionModuleDescriptor
> set
= new TreeSet
<ExecutionModuleDescriptor
>(
91 new Comparator
<ExecutionModuleDescriptor
>() {
93 public int compare(ExecutionModuleDescriptor md1
,
94 ExecutionModuleDescriptor md2
) {
95 String str1
= md1
.getLabel() != null ? md1
.getLabel()
97 String str2
= md2
.getLabel() != null ? md2
.getLabel()
99 return str1
.compareTo(str2
);
102 set
.addAll(descriptors
);
103 return new ObjectList(set
);
106 @RequestMapping("/getSlcExecution.service")
107 protected SlcExecution
getSlcExecution(@RequestParam String uuid
) {
108 SlcExecution slcExecution
= slcExecutionDao
.getSlcExecution(uuid
);
110 slcExecutionManager
.retrieveRealizedFlows(slcExecution
);
114 @RequestMapping("/newSlcExecution.service")
115 protected ExecutionAnswer
newSlcExecution(HttpServletRequest request
,
116 Model model
) throws Exception
{
118 String agentId
= request
119 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
120 Assert
.notNull(agentId
, "agent id");
122 String answer
= request
.getParameter("body");
123 if (answer
== null) {
124 // lets read the message body instead
125 BufferedReader reader
= request
.getReader();
126 StringBuffer buffer
= new StringBuffer();
128 while (((line
= reader
.readLine()) != null)) {
131 answer
= buffer
.toString();
134 if (log
.isTraceEnabled())
135 log
.debug("Received message:\n" + answer
);
137 StringSource source
= new StringSource(answer
);
138 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
141 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
142 if (slcExecution
.getUuid() == null
143 || slcExecution
.getUuid().length() < 8)
144 slcExecution
.setUuid(UUID
.randomUUID().toString());
146 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
147 slcExecution
.getSteps().add(
148 new SlcExecutionStep(SlcExecutionStep
.START
,
149 "Process started from the Web UI"));
152 slcExecutionManager
.storeRealizedFlows(slcExecution
);
153 slcExecutionService
.newExecution(slcExecution
);
154 SlcAgent agent
= agentFactory
.getAgent(agentId
);
155 agent
.runSlcExecution(slcExecution
);
157 return ExecutionAnswer
.ok("Execution completed properly");
160 @RequestMapping("/tailSlcExecutionStepsCount.service")
161 protected ObjectList
tailSlcExecutionSteps(@RequestParam String uuid
,
162 @RequestParam Integer stepCount
) {
163 List
<SlcExecutionStep
> list
= slcExecutionDao
164 .tailSteps(uuid
, stepCount
);
165 return new ObjectList(list
);
168 @RequestMapping("/tailSlcExecutionStepsOffset.service")
169 protected ObjectList
tailSlcExecutionSteps(@RequestParam String uuid
,
170 @RequestParam String stepOffsetUuid
) {
171 List
<SlcExecutionStep
> list
= slcExecutionDao
.tailSteps(uuid
,
173 return new ObjectList(list
);
176 private void initializeSEM() {
177 slcExecutionManager
= new SlcExecutionManager(unmarshaller
, marshaller
,
181 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao
) {
182 this.slcExecutionDao
= slcExecutionDao
;
185 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
186 this.slcExecutionService
= slcExecutionService
;
189 public void setUnmarshaller(Unmarshaller unmarshaller
) {
190 this.unmarshaller
= unmarshaller
;
193 public void setMarshaller(Marshaller marshaller
) {
194 this.marshaller
= marshaller
;
197 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
198 this.attachmentsStorage
= attachmentsStorage
;
201 public void setAgentFactory(SlcAgentFactory agentFactory
) {
202 this.agentFactory
= agentFactory
;