View Javadoc

1   /***
2    *
3    * Copyright 2005 LogicBlaze, Inc.
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 java.lang.reflect.Method;
21  import java.rmi.Remote;
22  import java.util.EventListener;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  /***
28   * A simple metadata strategy which uses POJO naming conventions.
29   *
30   * By default all method invocations are synchronous to avoid surprising users of Spring Remoting. However
31   * if you set the {@link #setOneWayForVoidMethods(boolean)} value to true then all void methods which do
32   * not throw checked exceptions become asynchronous one way methods.
33   *
34   * <p/>
35   * Also any object which implements the {@link Remote} interface or the {@link EventListener}
36   * are assumed to be remote and so a remote proxy is used to allow remote notifications and asynchronous
37   * messaging.
38   *
39   * @version $Revision: 1.2 $
40   */
41  public class SimpleMetadataStrategy implements MetadataStrategy {
42      private boolean oneWayForVoidMethods;
43      private Set remoteTypes;
44  
45      public SimpleMetadataStrategy() {
46      }
47  
48      public SimpleMetadataStrategy(boolean oneWayForVoidMethods) {
49          this.oneWayForVoidMethods = oneWayForVoidMethods;
50      }
51  
52      public MethodMetadata getMethodMetadata(Method method) {
53          boolean oneway = false;
54          if (oneWayForVoidMethods) {
55              oneway = method.getReturnType().equals(void.class) && method.getExceptionTypes().length == 0;
56          }
57          boolean[] remoteParams = null;
58          Class[] parameterTypes = method.getParameterTypes();
59          int size = parameterTypes.length;
60          if (size > 0) {
61              remoteParams = new boolean[size];
62              for (int i = 0; i < size; i++) {
63                  remoteParams[i] = isRemoteParameter(method, parameterTypes[i], i);
64              }
65          }
66          return new MethodMetadata(oneway, remoteParams);
67      }
68  
69      public boolean isOneWayForVoidMethods() {
70          return oneWayForVoidMethods;
71      }
72  
73      public void setOneWayForVoidMethods(boolean oneWayForVoidMethods) {
74          this.oneWayForVoidMethods = oneWayForVoidMethods;
75      }
76  
77      public Set getRemoteTypes() {
78          if (remoteTypes == null) {
79              remoteTypes = new HashSet();
80              populateDefaultRemoteTypes(remoteTypes);
81          }
82          return remoteTypes;
83      }
84  
85  
86      public void setRemoteTypes(Set remoteTypes) {
87          this.remoteTypes = remoteTypes;
88      }
89  
90      protected boolean isRemoteParameter(Method method, Class parameterType, int index) {
91          for (Iterator iter = getRemoteTypes().iterator(); iter.hasNext();) {
92              Class type = (Class) iter.next();
93              if (type.isAssignableFrom(parameterType)) {
94                  return true;
95              }
96          }
97          return false;
98      }
99  
100     protected void populateDefaultRemoteTypes(Set remoteTypes) {
101         remoteTypes.add(Remote.class);
102         remoteTypes.add(EventListener.class);
103     }
104 }