View Javadoc

1   //
2   // Licensed under the Apache License, Version 2.0 (the "License");
3   // you may not use this file except in compliance with the License.
4   // You may obtain a copy of the License at
5   //
6   //     http://www.apache.org/licenses/LICENSE-2.0
7   //
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  
14  package net.sf.tacos.annotations;
15  
16  import java.lang.reflect.Method;
17  import java.lang.reflect.Modifier;
18  
19  import org.apache.hivemind.ApplicationRuntimeException;
20  import org.apache.hivemind.Location;
21  import org.apache.hivemind.service.BodyBuilder;
22  import org.apache.hivemind.service.MethodSignature;
23  import org.apache.tapestry.annotations.AnnotationUtils;
24  import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
25  import org.apache.tapestry.engine.ILink;
26  import org.apache.tapestry.enhance.EnhancementOperation;
27  import org.apache.tapestry.spec.IComponentSpecification;
28  
29  /**
30   * @author Ming Jiang
31   * @author Andreas Andreou
32   */
33  
34  public class InjectPageLinkAnnotationWorker implements MethodAnnotationEnhancementWorker {
35  
36  	public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
37                  Method method, Location location) {
38              if (!method.getReturnType().equals(ILink.class))
39                      throw new ApplicationRuntimeException(
40                                      "InjectPageLink annotation must return ILink");
41  
42              InjectPageLink injectPageLink = method
43                              .getAnnotation(InjectPageLink.class);
44  
45              String pageName = injectPageLink.value();
46  
47              BodyBuilder builder = new BodyBuilder();
48  
49              builder.begin();
50              builder.addln("String pageName=\"{0}\";", pageName);
51              if (injectPageLink.currentNamespace())
52                  builder.addln("pageName=getNamespace().constructQualifiedName(pageName);");
53              builder.addln( "return getPage().getRequestCycle().getInfrastructure().getServiceMap()." +
54                      "getService(org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,pageName);");
55  
56              builder.end();
57  
58              op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder
59                              .toString(), location);
60  
61              if (isGetter(method))
62                      op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
63  	}
64  
65  	boolean isGetter(Method method) {
66  		return method.getName().startsWith("get")
67  				&& method.getParameterTypes().length == 0;
68  	}
69  }