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.aopalliance.intercept.MethodInvocation;
21  import org.springframework.remoting.support.RemoteInvocation;
22  import org.springframework.remoting.support.RemoteInvocationFactory;
23  
24  import java.lang.reflect.Method;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /***
29   * A factory of remote invocation instances which includes the extra Lingo
30   * metadata.
31   * 
32   * @version $Revision: 1.3 $
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      public MetadataStrategy getMetadataStrategy() {
50          return metadataStrategy;
51      }
52  
53      public void setMetadataStrategy(MetadataStrategy metadataStrategy) {
54          this.metadataStrategy = metadataStrategy;
55      }
56  
57      protected synchronized MethodMetadata getMethodMetadata(MethodInvocation methodInvocation) {
58          Method method = methodInvocation.getMethod();
59          MethodMetadata answer = (MethodMetadata) cache.get(method);
60          if (answer == null) {
61              if (metadataStrategy == null) {
62                  // TODO we could use a singleton here
63                  answer = DEFAULT_METHOD_METADATA;
64              }
65              else {
66                  answer = metadataStrategy.getMethodMetadata(method);
67              }
68              cache.put(method, answer);
69          }
70          return answer;
71      }
72  
73  }