View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.logicblaze.lingo;
19  
20  import org.springframework.remoting.support.RemoteInvocationResult;
21  
22  import java.util.Collection;
23  import java.util.Map;
24  
25  /***
26   * A default implementation of {@link ResultJoinStrategy} which will add
27   * together collection results, unblock the calling thread when there is a
28   * single result and let the handler timeout with whatever the default handler
29   * timeout policy is.
30   * 
31   * @version $Revision$
32   */
33  public class DefaultResultJoinStrategy implements ResultJoinStrategy {
34  
35      private int minimumResults = 1;
36      private int maximumResults = 0;
37  
38      /*
39       * (non-Javadoc)
40       * 
41       * @see org.logicblaze.lingo.jms.impl.ResultJoinStrategy#unblockCallerThread(org.springframework.remoting.support.RemoteInvocationResult,
42       *      int)
43       */
44      public boolean unblockCallerThread(RemoteInvocationResult response, int responseCount) {
45          return responseCount >= minimumResults;
46      }
47  
48      /*
49       * (non-Javadoc)
50       * 
51       * @see org.logicblaze.lingo.jms.impl.ResultJoinStrategy#removeHandler(org.springframework.remoting.support.RemoteInvocationResult,
52       *      int)
53       */
54      public boolean removeHandler(RemoteInvocationResult response, int responseCount) {
55          if (maximumResults > 0) {
56              return responseCount >= maximumResults;
57          }
58          return false;
59      }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.logicblaze.lingo.jms.impl.ResultJoinStrategy#mergeResponses(org.springframework.remoting.support.RemoteInvocationResult,
65       *      org.springframework.remoting.support.RemoteInvocationResult, int)
66       */
67      public RemoteInvocationResult mergeResponses(RemoteInvocationResult currentResult, RemoteInvocationResult newResult, int responseCount) {
68          Object value = currentResult.getValue();
69          Object newValue = newResult.getValue();
70          if (value instanceof Collection) {
71              Collection coll = (Collection) value;
72              if (newValue instanceof Collection) {
73                  coll.addAll((Collection) newValue);
74              }
75              else {
76                  coll.add(newValue);
77              }
78          }
79          else if (value instanceof Map && newValue instanceof Map) {
80              Map map = (Map) value;
81              map.putAll((Map) newValue);
82          }
83          return currentResult;
84      }
85  
86      public int getMaximumResults() {
87          return maximumResults;
88      }
89  
90      public void setMaximumResults(int maximumResults) {
91          this.maximumResults = maximumResults;
92      }
93  
94      public int getMinimumResults() {
95          return minimumResults;
96      }
97  
98      public void setMinimumResults(int minimiumResults) {
99          this.minimumResults = minimiumResults;
100     }
101 
102 }