]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsTreeTestResultListener.java
Implement kill and process progress
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsTreeTestResultListener.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.jms;
18
19 import javax.jms.Destination;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.slc.SlcException;
24 import org.argeo.slc.core.attachment.Attachment;
25 import org.argeo.slc.core.attachment.SimpleAttachment;
26 import org.argeo.slc.core.test.tree.TreeTestResult;
27 import org.argeo.slc.core.test.tree.TreeTestResultListener;
28 import org.argeo.slc.msg.test.tree.AddTreeTestResultAttachmentRequest;
29 import org.argeo.slc.msg.test.tree.CloseTreeTestResultRequest;
30 import org.argeo.slc.msg.test.tree.CreateTreeTestResultRequest;
31 import org.argeo.slc.msg.test.tree.ResultPartRequest;
32 import org.argeo.slc.test.TestResultPart;
33 import org.springframework.jms.JmsException;
34 import org.springframework.jms.core.JmsTemplate;
35
36 /** JMS based tree test result listener implementation. */
37 public class JmsTreeTestResultListener implements TreeTestResultListener {
38 private final Log log = LogFactory.getLog(getClass());
39
40 private Boolean onlyOnClose = false;
41 private JmsTemplate jmsTemplate;
42
43 private Destination executionEventDestination;
44
45 public void resultPartAdded(TreeTestResult testResult,
46 TestResultPart testResultPart) {
47 if (onlyOnClose)
48 return;
49
50 try {
51 if (testResult.getResultParts().size() == 1
52 && testResult.getResultParts().values().iterator().next()
53 .getParts().size() == 1) {
54 CreateTreeTestResultRequest req = new CreateTreeTestResultRequest(
55 testResult);
56
57 if (log.isTraceEnabled())
58 log.trace("Send create result request for result "
59 + testResult.getUuid());
60
61 jmsTemplate.convertAndSend(executionEventDestination, req);
62 } else {
63 ResultPartRequest req = new ResultPartRequest(testResult);
64
65 if (log.isTraceEnabled())
66 log.trace("Send result parts for result "
67 + testResult.getUuid());
68
69 jmsTemplate.convertAndSend(executionEventDestination, req);
70 }
71 } catch (JmsException e) {
72 log.warn("Could not notify result part to server: "
73 + e.getMessage());
74 if (log.isTraceEnabled())
75 log.debug("Original error.", e);
76 } catch (Exception e) {
77 throw new SlcException("Could not notify to JMS", e);
78 }
79 }
80
81 public void close(TreeTestResult testResult) {
82 try {
83 if (onlyOnClose) {
84 CreateTreeTestResultRequest req = new CreateTreeTestResultRequest(
85 testResult);
86
87 if (log.isTraceEnabled())
88 log.trace("Send onClose create result request for result "
89 + testResult.getUuid());
90
91 jmsTemplate.convertAndSend(executionEventDestination, req);
92 } else {
93 CloseTreeTestResultRequest req = new CloseTreeTestResultRequest(
94 testResult);
95
96 if (log.isTraceEnabled())
97 log.trace("Send close result request for result "
98 + testResult.getUuid());
99
100 jmsTemplate.convertAndSend(executionEventDestination, req);
101
102 }
103 } catch (JmsException e) {
104 log.warn("Could not notify result close to server: "
105 + e.getMessage());
106 if (log.isTraceEnabled())
107 log.debug("Original error.", e);
108 } catch (Exception e) {
109 throw new SlcException("Could not notify to JMS", e);
110 }
111 }
112
113 public void addAttachment(TreeTestResult testResult, Attachment attachment) {
114 if (onlyOnClose)
115 return;
116
117 try {
118 AddTreeTestResultAttachmentRequest req = new AddTreeTestResultAttachmentRequest();
119 req.setResultUuid(testResult.getUuid());
120 req.setAttachment((SimpleAttachment) attachment);
121 jmsTemplate.convertAndSend(executionEventDestination, req);
122
123 } catch (JmsException e) {
124 log
125 .warn("Could not notify attachment to server: "
126 + e.getMessage());
127 if (log.isTraceEnabled())
128 log.debug("Original error.", e);
129 } catch (Exception e) {
130 throw new SlcException("Could not notify to JMS", e);
131 }
132
133 }
134
135 /** Publishes the test result only when it gets closed. */
136 public void setOnlyOnClose(Boolean onlyOnClose) {
137 this.onlyOnClose = onlyOnClose;
138 }
139
140 public void setJmsTemplate(JmsTemplate jmsTemplate) {
141 this.jmsTemplate = jmsTemplate;
142 }
143
144 public void setExecutionEventDestination(
145 Destination executionEventDestination) {
146 this.executionEventDestination = executionEventDestination;
147 }
148
149 }