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 org.springframework.remoting.support.RemoteInvocationFactory;
21  import org.springframework.remoting.support.RemoteInvocation;
22  import org.aopalliance.intercept.MethodInvocation;
23  
24  import java.util.Map;
25  import java.util.HashMap;
26  import java.lang.reflect.Method;
27  
28  /***
29   * A factory of remote invocation instances which includes the extra
30   * Lingo metadata.
31   *
32   * @version $Revision: 1.1 $
33   */
34  public class LingoRemoteInvocationFactory implements RemoteInvocationFactory {
35      private static final MethodMetadata DEFAULT_METHOD_METADATA = new MethodMetadata(false);
36  
37      private MetadataStrategy metadataStrategy;
38      private Map cache = new HashMap();
39  
40      public LingoRemoteInvocationFactory(MetadataStrategy metadataStrategy) {
41          this.metadataStrategy = metadataStrategy;
42      }
43  
44      public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
45          MethodMetadata metadata = getMethodMetadata(methodInvocation);
46          return new LingoInvocation(methodInvocation, metadata);
47      }
48  
49      protected synchronized MethodMetadata getMethodMetadata(MethodInvocation methodInvocation) {
50          Method method = methodInvocation.getMethod();
51          MethodMetadata answer = (MethodMetadata) cache.get(method);
52          if (answer == null) {
53              if (metadataStrategy == null) {
54                  // TODO we could use a singleton here
55                  answer = DEFAULT_METHOD_METADATA;
56              }
57              else {
58                  answer = metadataStrategy.getMethodMetadata(method);
59              }
60              cache.put(method, answer);
61          }
62          return answer;
63      }
64      
65  }