View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
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  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   * Standard implementation.
29   * 
30   * @author Ramon Servadei
31   */
32  public final class RpcResult implements IRpcResult
33  {
34  
35      /** The marker for the RPC call this result is for. */
36      private IRpcMarker marker;
37  
38      /** The exception message from the RPC */
39      private final String exceptionMessage;
40  
41      /** The result */
42      private final IField result;
43  
44      /** Whether the RPC executed successfully */
45      private final boolean successful;
46  
47      /**
48       * Construct the RPC result from a result record. This constructor is called
49       * in the remote context that called the RPC.
50       * 
51       * @param definition
52       *            the definition for the RPC
53       * @param resultRecord
54       *            the result record
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                  // construct the result field
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      * Construct the RPC result.
105      * 
106      * @param successful
107      *            whether the RPC executed successfully
108      * @param result
109      *            the result
110      * @param exceptionMessage
111      *            the exception, if any
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         // we send the result as its string value, not great but it'll do
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 }