View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.template;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.module.blossom.annotation.Available;
38  import info.magnolia.module.blossom.annotation.I18nBasename;
39  import info.magnolia.module.blossom.annotation.Template;
40  import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
41  import info.magnolia.module.blossom.support.MethodInvocationUtils;
42  import info.magnolia.module.blossom.support.ParameterResolver;
43  import org.apache.commons.lang.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  import org.springframework.util.ClassUtils;
47  
48  import java.lang.reflect.Method;
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  /**
53   * Builds template descriptions from annotations.
54   */
55  public class TemplateDescriptionBuilder {
56  
57      private final Logger logger = LoggerFactory.getLogger(getClass());
58  
59      public BlossomTemplateDescription buildDescription(BlossomDispatcher dispatcher, Object handler, String handlerPath) {
60  
61          Template annotation = handler.getClass().getAnnotation(Template.class);
62          I18nBasename i18nBasename = handler.getClass().getAnnotation(I18nBasename.class);
63  
64          BlossomTemplateDescription description = new BlossomTemplateDescription();
65          description.setName(resolveName(handlerPath, handler, annotation));
66          description.setTitle(resolveTitle(handlerPath, handler, annotation));
67          description.setDescription(resolveDescription(handlerPath, handler, annotation));
68          description.setI18nBasename(i18nBasename != null ? i18nBasename.value() : null);
69          description.setHandlerPath(handlerPath);
70          description.setVisible(annotation.visible());
71          description.setDispatcher(dispatcher);
72          description.setHandler(handler);
73          description.setAvailabilityStrategy(resolveAvailabilityStrategy(handler));
74  
75          return description;
76      }
77  
78      protected String resolveName(String handlerPath, Object handler, Template annotation) {
79          if (StringUtils.isNotEmpty(annotation.name()))
80              return annotation.name();
81          return StringUtils.replaceChars(StringUtils.strip(handlerPath, "/"), '/', '_');
82      }
83  
84      protected String resolveTitle(String handlerPath, Object handler, Template annotation) {
85          return annotation.value();
86      }
87  
88      protected String resolveDescription(String handlerPath, Object handler, Template annotation) {
89          return annotation.description();
90      }
91  
92      protected TemplateAvailabilityStrategy resolveAvailabilityStrategy(final Object handler) {
93  
94          final Class<?> handlerClass = handler.getClass();
95  
96          final List<Method> matchingMethods = new ArrayList<Method>();
97  
98          // Iterate class hierarchy to find the method also in super classes
99  
100         Class<?> clazz = handler.getClass();
101         do {
102             Method[] methods = clazz.getDeclaredMethods();
103             for (final Method method : methods) {
104 
105                 // The method must have the annotation
106                 if (!method.isAnnotationPresent(Available.class))
107                     continue;
108 
109                 // The method must not be overridden
110                 if (!method.equals(ClassUtils.getMostSpecificMethod(method, handlerClass)))
111                     continue;
112 
113                 if (!method.getReturnType().equals(Boolean.TYPE))
114                     continue;
115 
116                 matchingMethods.add(method);
117             }
118             clazz = clazz.getSuperclass();
119         } while (clazz != null);
120 
121         if (!matchingMethods.isEmpty()) {
122             if (matchingMethods.size() == 1) {
123                 final Method method = matchingMethods.get(0);
124                 return new TemplateAvailabilityStrategy() {
125                     public boolean isAvailable(Content node) {
126 
127                         ParameterResolver parameters = getTemplateAvailabilityParameters(node);
128                         return (Boolean) MethodInvocationUtils.invoke(method, handler, parameters);
129                     }
130                 };
131             } else {
132                 logger.error("Multiple @Available annotated methods found for handler [" + handler.getClass() + "]");
133             }
134         }
135 
136         return null;
137     }
138 
139     protected ParameterResolver getTemplateAvailabilityParameters(final Content node) {
140         return new ParameterResolver() {
141             public Object resolveParameter(Class<?> parameterType) {
142                 if (parameterType.equals(Content.class))
143                     return node;
144                 return super.resolveParameter(parameterType);
145             }
146         };
147     }
148 }