View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
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  
17  package com.fatwire.gst.foundation.controller.annotation;
18  
19  import java.lang.reflect.Method;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  
23  /**
24   * This class resolves a specific method that handles a specific request as a
25   * controller. The method needs to be annotated with the IcsVariable annotation
26   * and need to accept one argument of type ICS.
27   * <p/>
28   * The name of the method can be freely chosen. The method needs to have public
29   * visibility.
30   * 
31   * @author Dolf Dijkstra
32   * @since Mar 21, 2011
33   */
34  public class ControllerMappingResolver {
35  
36      /**
37       * @param ics
38       * @param o object with method annotations of type IcsVariable
39       * @return the method with the IcsVariable annotation that has a match for
40       *         the name value pair as set in the annotation.
41       */
42      public Method findControllerMethod(final ICS ics, final Object o) {
43          for (final Method m : o.getClass().getMethods()) {
44              final IcsVariable p = m.getAnnotation(IcsVariable.class);
45              if (p != null) {
46                  for (final String param : p.var()) {
47                      final String[] split = param.split("=");
48                      if (split[1].equals(ics.GetVar(split[0]))) {
49  
50                          if (m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(ICS.class)) {
51                              return m;
52                          }
53                          throw new UnsupportedOperationException("Method " + m.getName()
54                                  + " does not have a single argument of type ICS though the method is annotated "
55                                  + "with a IcsVariable annation.");
56  
57                      }
58                  }
59              }
60  
61          }
62          return null;
63      }
64  }