]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsTreeTestResultListener.java
Start working on serialized JMS
[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 public class JmsTreeTestResultListener implements TreeTestResultListener {
37 private final Log log = LogFactory.getLog(getClass());
38
39 private Boolean onlyOnClose = false;
40 private JmsTemplate jmsTemplate;
41
42 private Destination executionEventDestination;
43
44 public void resultPartAdded(TreeTestResult testResult,
45 TestResultPart testResultPart) {
46 if (onlyOnClose)
47 return;
48
49 try {
50 if (testResult.getResultParts().size() == 1
51 && testResult.getResultParts().values().iterator().next()
52 .getParts().size() == 1) {
53 CreateTreeTestResultRequest req = new CreateTreeTestResultRequest(
54 testResult);
55
56 if (log.isDebugEnabled())
57 log.debug("Send create result request for result "
58 + testResult.getUuid());
59
60 jmsTemplate.convertAndSend(executionEventDestination, req);
61 } else {
62 ResultPartRequest req = new ResultPartRequest(testResult);
63
64 if (log.isDebugEnabled())
65 log.debug("Send result parts for result "
66 + testResult.getUuid());
67
68 jmsTemplate.convertAndSend(executionEventDestination, req);
69 }
70 } catch (JmsException e) {
71 log.warn("Could not notify result part to server: "
72 + e.getMessage());
73 if (log.isTraceEnabled())
74 log.debug("Original error.", e);
75 } catch (Exception e) {
76 throw new SlcException("Could not notify to JMS", e);
77 }
78 }
79
80 public void close(TreeTestResult testResult) {
81 try {
82 if (onlyOnClose) {
83 CreateTreeTestResultRequest req = new CreateTreeTestResultRequest(
84 testResult);
85
86 if (log.isDebugEnabled())
87 log.debug("Send create result request for result "
88 + testResult.getUuid());
89
90 jmsTemplate.convertAndSend(executionEventDestination, req);
91 } else {
92 CloseTreeTestResultRequest req = new CloseTreeTestResultRequest(
93 testResult);
94
95 if (log.isDebugEnabled())
96 log.debug("Send close result request for result "
97 + testResult.getUuid());
98
99 jmsTemplate.convertAndSend(executionEventDestination, req);
100
101 }
102 } catch (JmsException e) {
103 log.warn("Could not notify result close to server: "
104 + e.getMessage());
105 if (log.isTraceEnabled())
106 log.debug("Original error.", e);
107 } catch (Exception e) {
108 throw new SlcException("Could not notify to JMS", e);
109 }
110 }
111
112 public void addAttachment(TreeTestResult testResult, Attachment attachment) {
113 try {
114 AddTreeTestResultAttachmentRequest req = new AddTreeTestResultAttachmentRequest();
115 req.setResultUuid(testResult.getUuid());
116 req.setAttachment((SimpleAttachment) attachment);
117 jmsTemplate.convertAndSend(executionEventDestination, req);
118
119 } catch (JmsException e) {
120 log
121 .warn("Could not notify attachment to server: "
122 + e.getMessage());
123 if (log.isTraceEnabled())
124 log.debug("Original error.", e);
125 } catch (Exception e) {
126 throw new SlcException("Could not notify to JMS", e);
127 }
128
129 }
130
131 public void setOnlyOnClose(Boolean onlyOnClose) {
132 this.onlyOnClose = onlyOnClose;
133 }
134
135 public void setJmsTemplate(JmsTemplate jmsTemplate) {
136 this.jmsTemplate = jmsTemplate;
137 }
138
139 public void setExecutionEventDestination(
140 Destination executionEventDestination) {
141 this.executionEventDestination = executionEventDestination;
142 }
143
144 }