]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/detached/SlcDetachedTask.java
Introduce org.argeo.slc.lib.detached
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.ant / src / main / java / org / argeo / slc / ant / detached / SlcDetachedTask.java
1 package org.argeo.slc.ant.detached;
2
3 import java.util.Properties;
4 import java.util.UUID;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.ant.spring.MapArg;
9 import org.argeo.slc.ant.structure.SAwareTask;
10 import org.argeo.slc.core.SlcException;
11 import org.argeo.slc.detached.DetachedAnswer;
12 import org.argeo.slc.detached.DetachedClient;
13 import org.argeo.slc.detached.DetachedRequest;
14 import org.argeo.slc.spring.SpringUtils;
15
16 public class SlcDetachedTask extends SAwareTask {
17 private final static Log log = LogFactory.getLog(SlcDetachedTask.class);
18
19 private String client;
20 private String action;
21
22 private MapArg properties;
23
24 @Override
25 protected void executeActions(String mode) {
26 // Find detached client
27 DetachedClient detachedClient = null;
28 if (client != null)
29 detachedClient = getBean(client);
30 else
31 detachedClient = SpringUtils.loadSingleFromContext(getContext(),
32 DetachedClient.class);
33
34 if (detachedClient == null)
35 throw new SlcException("Could not find any detached client.");
36
37 // Prepare and send request
38 DetachedRequest request = new DetachedRequest(UUID.randomUUID()
39 .toString());
40 request.setRef(action);
41
42 if (properties != null) {
43 Properties props = new Properties();
44 props.putAll(properties.getMap());
45 request.setProperties(props);
46 }
47
48 try {
49 detachedClient.sendRequest(request);
50 DetachedAnswer answer = detachedClient.receiveAnswer();
51 if (answer.getStatus() == DetachedAnswer.ERROR)
52 throw new SlcException("Error when executing request "
53 + answer.getUuid() + ": " + answer.getLog());
54 else
55 log.info("Admin answer: " + answer.getLog());
56 } catch (Exception e) {
57 throw new SlcException("Could not send request.", e);
58 }
59 }
60
61 public void setClient(String driverBean) {
62 this.client = driverBean;
63 }
64
65 public void setAction(String action) {
66 this.action = action;
67 }
68
69 public MapArg createProperties() {
70 if (properties == null)
71 properties = new MapArg();
72 else
73 throw new SlcException("Properties already declared.");
74 return properties;
75 }
76 }