1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.rpc;
17
18 import static fulmine.util.Utils.string;
19 import fulmine.model.container.IContainer;
20 import fulmine.model.field.BooleanField;
21 import fulmine.model.field.FieldUtils;
22 import fulmine.model.field.IField;
23 import fulmine.model.field.IntegerField;
24 import fulmine.model.field.StringField;
25 import fulmine.util.reference.is;
26
27
28
29
30
31
32 public final class RpcResult implements IRpcResult
33 {
34
35
36 private IRpcMarker marker;
37
38
39 private final String exceptionMessage;
40
41
42 private final IField result;
43
44
45 private final boolean successful;
46
47
48
49
50
51
52
53
54
55
56 public RpcResult(IRpcDefinition definition, IContainer resultRecord)
57 {
58 if (!resultRecord.contains(MARKER))
59 {
60 throw new IllegalArgumentException(
61 "No RPC result marker in result record " + resultRecord + " "
62 + definition);
63 }
64 if (!resultRecord.contains(IS_SUCCESSFUL))
65 {
66 throw new IllegalArgumentException(
67 "No success flag in result record " + resultRecord + " "
68 + definition);
69 }
70 this.marker = new RpcMarker(resultRecord.getIntegerField(MARKER).get());
71 this.successful = resultRecord.getBooleanField(IS_SUCCESSFUL).get();
72 if (resultRecord.contains(EXCEPTION))
73 {
74 this.exceptionMessage =
75 resultRecord.getStringField(EXCEPTION).get();
76 }
77 else
78 {
79 this.exceptionMessage = null;
80 }
81 if (resultRecord.contains(RESULT))
82 {
83 try
84 {
85
86 this.result =
87 FieldUtils.createFromString(definition.getResultType(),
88 RESULT, resultRecord.getStringField(RESULT).get());
89 }
90 catch (Exception e)
91 {
92 throw new IllegalArgumentException(
93 "Could not get result value from result record "
94 + resultRecord + " " + definition, e);
95 }
96 }
97 else
98 {
99 this.result = null;
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public RpcResult(boolean successful, IField result, String exceptionMessage)
114 {
115 super();
116 this.successful = successful;
117 this.result = result;
118 this.exceptionMessage = exceptionMessage;
119 }
120
121 public String getExceptionMessage()
122 {
123 return this.exceptionMessage;
124 }
125
126 public IField getResult()
127 {
128 return this.result;
129 }
130
131 public boolean isSuccessful()
132 {
133 return this.successful;
134 }
135
136 public IRpcMarker getMarker()
137 {
138 return this.marker;
139 }
140
141 public void updateResultRecord(IRpcMarker marker, IContainer resultRecord)
142 {
143 if (!resultRecord.contains(MARKER))
144 {
145 resultRecord.add(new IntegerField(MARKER));
146 }
147 resultRecord.getIntegerField(MARKER).set(marker.getId());
148
149 if (!resultRecord.contains(IS_SUCCESSFUL))
150 {
151 resultRecord.add(new BooleanField(IS_SUCCESSFUL));
152 }
153 resultRecord.getBooleanField(IS_SUCCESSFUL).set(isSuccessful());
154
155 if (!resultRecord.contains(EXCEPTION))
156 {
157 resultRecord.add(new StringField(EXCEPTION));
158 }
159 resultRecord.getStringField(EXCEPTION).set(getExceptionMessage());
160
161
162 if (!resultRecord.contains(RESULT))
163 {
164 resultRecord.add(new StringField(RESULT));
165 }
166 final IField rpcResult = getResult();
167 resultRecord.getStringField(RESULT).set(
168 rpcResult == null ? null : rpcResult.getValueAsString());
169 }
170
171 @Override
172 public String toString()
173 {
174 return string(this, "marker=" + getMarker() + ", success="
175 + isSuccessful() + ", result=" + getResult() + ", exception="
176 + getExceptionMessage());
177 }
178
179 @Override
180 public int hashCode()
181 {
182 final int prime = 31;
183 int result = 1;
184 result =
185 prime
186 * result
187 + ((exceptionMessage == null) ? 0 : exceptionMessage.hashCode());
188 result = prime * result + ((marker == null) ? 0 : marker.hashCode());
189 result =
190 prime * result
191 + ((this.result == null) ? 0 : this.result.hashCode());
192 result = prime * result + (successful ? 1231 : 1237);
193 return result;
194 }
195
196 @Override
197 public boolean equals(Object obj)
198 {
199 if (is.same(this, obj))
200 {
201 return true;
202 }
203 if (is.differentClass(this, obj))
204 {
205 return false;
206 }
207 RpcResult other = (RpcResult) obj;
208 return is.eq(getMarker(), other.getMarker())
209 && is.eq(getExceptionMessage(), other.getExceptionMessage())
210 && is.eq(getResult(), other.getResult())
211 && is.eq(isSuccessful(), other.isSuccessful());
212 }
213 }