This repository has been archived by the owner on May 26, 2022. It is now read-only.
/
ResponseContent.java
187 lines (151 loc) · 6.52 KB
/
ResponseContent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.drools.mas.util.helper;
import org.drools.command.CommandFactory;
import org.drools.grid.Grid;
import org.drools.grid.helper.GridHelper;
import org.drools.grid.remote.command.AsyncBatchExecutionCommandImpl;
import org.drools.mas.Encodings;
import org.drools.mas.util.MessageContentEncoder;
import org.drools.runtime.StatefulKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ResponseContent implements Serializable{
private String nodeId;
private String sessionId;
private String messageId;
private Object data;
private Fault fault;
private static Logger logger = LoggerFactory.getLogger( ResponseContent.class );
public static void deliverResponse( String nodeId, String sessionId, String msgId, Object ret, Fault fault ) {
deliverResponse( nodeId, sessionId, msgId, ret, fault, false );
}
public static void deliverResponse( Grid grid, String nodeId, String sessionId, String msgId, Object ret, Fault fault ) {
deliverResponse( grid, nodeId, sessionId, msgId, ret, fault, false );
}
public static void deliverResponse( String nodeId, String sessionId, String msgId, Object ret, Fault fault, boolean needEncoding ) {
Grid grid = GridHelper.createGrid();
deliverResponse( grid, nodeId, sessionId, msgId, ret, fault, needEncoding );
}
public static void deliverResponse( Grid grid, String nodeId, String sessionId, String msgId, Object ret, Fault fault, boolean needEncoding ) {
try {
if ( logger.isDebugEnabled() ) {
logger.debug( "(" + Thread.currentThread().getId() + ")"+Thread.currentThread().getName() + "Content helper is now active" );
}
Map results = null;
if ( ret != null ) {
results = new HashMap();
if ( ret instanceof String ) {
results.put( "?return", ret );
} else {
if ( needEncoding ) {
ret = MessageContentEncoder.encode( ret, Encodings.XML );
}
results.put( "?return", ret );
}
if ( logger.isDebugEnabled() ) {
logger.debug("(" + Thread.currentThread().getId() + ")" + Thread.currentThread().getName() + "Content helper would like to return" + results.get("?return"));
}
}
ResponseContent response = new ResponseContent( nodeId, sessionId, msgId, results );
response.setFault( fault );
if ( logger.isDebugEnabled() ) {
logger.debug( "(" + Thread.currentThread().getId() + ")" + Thread.currentThread().getName() + "Content helper fault is expected to be null " + fault );
}
StatefulKnowledgeSession kSession = GridHelper.getStatefulKnowledgeSession( grid, nodeId, sessionId, true );
if ( logger.isDebugEnabled() ) {
logger.debug( "(" + Thread.currentThread().getId() + ")"+Thread.currentThread().getName() +"Content helper ksession found!" );
}
List list = new ArrayList(2);
list.add( CommandFactory.newInsert(response) );
list.add( CommandFactory.newFireAllRules() );
AsyncBatchExecutionCommandImpl batch = new AsyncBatchExecutionCommandImpl( list );
if ( logger.isDebugEnabled() ) {
logger.debug( "(" + Thread.currentThread().getId() + ")"+Thread.currentThread().getName() +"Content helper batch is ready" );
}
kSession.execute( batch );
if ( logger.isDebugEnabled() ) {
logger.debug("(" + Thread.currentThread().getId() + ")" + Thread.currentThread().getName() + "Content helper batch dispatched" );
}
} catch (Exception e) { e.printStackTrace(); }
}
public ResponseContent(String nodeId, String sessionId, String messageId, Object data) {
this.nodeId = nodeId;
this.sessionId = sessionId;
this.messageId = messageId;
this.data = data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public Fault getFault() {
return fault;
}
public void setFault(Fault fault) {
this.fault = fault;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ResponseContent other = (ResponseContent) obj;
if ((this.nodeId == null) ? (other.nodeId != null) : !this.nodeId.equals(other.nodeId)) {
return false;
}
if ((this.sessionId == null) ? (other.sessionId != null) : !this.sessionId.equals(other.sessionId)) {
return false;
}
if ((this.messageId == null) ? (other.messageId != null) : !this.messageId.equals(other.messageId)) {
return false;
}
if (this.data != other.data && (this.data == null || !this.data.equals(other.data))) {
return false;
}
if (this.fault != other.fault && (this.fault == null || !this.fault.equals(other.fault))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (this.nodeId != null ? this.nodeId.hashCode() : 0);
hash = 89 * hash + (this.sessionId != null ? this.sessionId.hashCode() : 0);
hash = 89 * hash + (this.messageId != null ? this.messageId.hashCode() : 0);
hash = 89 * hash + (this.data != null ? this.data.hashCode() : 0);
hash = 89 * hash + (this.fault != null ? this.fault.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return "ResponseContent{" + "nodeId=" + nodeId + ", sessionId=" + sessionId + ", messageId=" + messageId + ", data=" + data + ", fault=" + fault + '}';
}
}