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
;
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
.argeo
.slc
.web
.mvc
.process
.SlcExecutionManager
;
42 import org
.springframework
.oxm
.Marshaller
;
43 import org
.springframework
.oxm
.Unmarshaller
;
44 import org
.springframework
.stereotype
.Controller
;
45 import org
.springframework
.ui
.Model
;
46 import org
.springframework
.util
.Assert
;
47 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
48 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
49 import org
.springframework
.xml
.transform
.StringSource
;
52 public class ExecutionServiceController
{
54 public final static String KEY_ANSWER
= "__answer";
55 private final static Log log
= LogFactory
56 .getLog(ExecutionServiceController
.class);
58 private SlcExecutionDao slcExecutionDao
;
59 private SlcAgentFactory agentFactory
;
60 private Unmarshaller unmarshaller
;
61 private Marshaller marshaller
;
62 private SlcExecutionService slcExecutionService
;
63 private AttachmentsStorage attachmentsStorage
;
65 private SlcExecutionManager slcExecutionManager
;
67 @RequestMapping("/listSlcExecutions.service")
68 protected ObjectList
listSlcExecutions() {
69 List
<SlcExecution
> list
= slcExecutionDao
.listSlcExecutions();
70 return new ObjectList(list
);
73 @RequestMapping("/getExecutionDescriptor.service")
74 protected ExecutionModuleDescriptor
getExecutionDescriptor(
75 @RequestParam String agentId
, @RequestParam String moduleName
,
76 @RequestParam String version
) {
78 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
80 ExecutionModuleDescriptor md
= slcAgent
.getExecutionModuleDescriptor(
85 @RequestMapping("/listModulesDescriptors.service")
86 protected ObjectList
listModulesDescriptors(@RequestParam String agentId
) {
87 // TODO: use centralized agentId property (from MsgConstants)?
88 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
90 List
<ExecutionModuleDescriptor
> descriptors
= slcAgent
91 .listExecutionModuleDescriptors();
92 SortedSet
<ExecutionModuleDescriptor
> set
= new TreeSet
<ExecutionModuleDescriptor
>(
93 new Comparator
<ExecutionModuleDescriptor
>() {
95 public int compare(ExecutionModuleDescriptor md1
,
96 ExecutionModuleDescriptor md2
) {
97 String str1
= md1
.getLabel() != null ? md1
.getLabel()
99 String str2
= md2
.getLabel() != null ? md2
.getLabel()
101 return str1
.compareTo(str2
);
104 set
.addAll(descriptors
);
105 return new ObjectList(set
);
108 @RequestMapping("/getSlcExecution.service")
109 protected SlcExecution
getSlcExecution(@RequestParam String uuid
) {
110 SlcExecution slcExecution
= slcExecutionDao
.getSlcExecution(uuid
);
112 slcExecutionManager
.retrieveRealizedFlows(slcExecution
);
116 @RequestMapping("/newSlcExecution.service")
117 protected ExecutionAnswer
newSlcExecution(HttpServletRequest request
,
118 Model model
) throws Exception
{
120 String agentId
= request
121 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
122 Assert
.notNull(agentId
, "agent id");
124 String answer
= request
.getParameter("body");
125 if (answer
== null) {
126 // lets read the message body instead
127 BufferedReader reader
= request
.getReader();
128 StringBuffer buffer
= new StringBuffer();
130 while (((line
= reader
.readLine()) != null)) {
133 answer
= buffer
.toString();
136 if (log
.isTraceEnabled())
137 log
.debug("Received message:\n" + answer
);
139 StringSource source
= new StringSource(answer
);
140 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
143 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
144 if (slcExecution
.getUuid() == null
145 || slcExecution
.getUuid().length() < 8)
146 slcExecution
.setUuid(UUID
.randomUUID().toString());
148 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
149 slcExecution
.getSteps().add(
150 new SlcExecutionStep(SlcExecutionStep
.TYPE_START
,
151 "Process started from the Web UI"));
154 slcExecutionManager
.storeRealizedFlows(slcExecution
);
155 slcExecutionService
.newExecution(slcExecution
);
156 SlcAgent agent
= agentFactory
.getAgent(agentId
);
157 agent
.runSlcExecution(slcExecution
);
159 return ExecutionAnswer
.ok("Execution completed properly");
162 private void initializeSEM() {
163 slcExecutionManager
= new SlcExecutionManager(agentFactory
,
164 unmarshaller
, marshaller
, slcExecutionService
,
169 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao
) {
170 this.slcExecutionDao
= slcExecutionDao
;
173 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
174 this.slcExecutionService
= slcExecutionService
;
177 public void setUnmarshaller(Unmarshaller unmarshaller
) {
178 this.unmarshaller
= unmarshaller
;
181 public void setMarshaller(Marshaller marshaller
) {
182 this.marshaller
= marshaller
;
185 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
186 this.attachmentsStorage
= attachmentsStorage
;
189 public void setAgentFactory(SlcAgentFactory agentFactory
) {
190 this.agentFactory
= agentFactory
;