001 //
002 // Licensed under the Apache License, Version 2.0 (the "License");
003 // you may not use this file except in compliance with the License.
004 // You may obtain a copy of the License at
005 //
006 // http://www.apache.org/licenses/LICENSE-2.0
007 //
008 // Unless required by applicable law or agreed to in writing, software
009 // distributed under the License is distributed on an "AS IS" BASIS,
010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 // See the License for the specific language governing permissions and
012 // limitations under the License.
013
014 package net.sf.tacos.annotations;
015
016 import java.lang.reflect.Method;
017 import java.lang.reflect.Modifier;
018
019 import org.apache.hivemind.ApplicationRuntimeException;
020 import org.apache.hivemind.Location;
021 import org.apache.hivemind.service.BodyBuilder;
022 import org.apache.hivemind.service.MethodSignature;
023 import org.apache.tapestry.annotations.AnnotationUtils;
024 import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
025 import org.apache.tapestry.engine.ILink;
026 import org.apache.tapestry.enhance.EnhancementOperation;
027 import org.apache.tapestry.spec.IComponentSpecification;
028
029 /**
030 * @author Ming Jiang
031 * @author Andreas Andreou
032 */
033
034 public class InjectPageLinkAnnotationWorker implements MethodAnnotationEnhancementWorker {
035
036 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
037 Method method, Location location) {
038 if (!method.getReturnType().equals(ILink.class))
039 throw new ApplicationRuntimeException(
040 "InjectPageLink annotation must return ILink");
041
042 InjectPageLink injectPageLink = method
043 .getAnnotation(InjectPageLink.class);
044
045 String pageName = injectPageLink.value();
046
047 BodyBuilder builder = new BodyBuilder();
048
049 builder.begin();
050 builder.addln("String pageName=\"{0}\";", pageName);
051 if (injectPageLink.currentNamespace())
052 builder.addln("pageName=getNamespace().constructQualifiedName(pageName);");
053 builder.addln( "return getPage().getRequestCycle().getInfrastructure().getServiceMap()." +
054 "getService(org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,pageName);");
055
056 builder.end();
057
058 op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder
059 .toString(), location);
060
061 if (isGetter(method))
062 op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
063 }
064
065 boolean isGetter(Method method) {
066 return method.getName().startsWith("get")
067 && method.getParameterTypes().length == 0;
068 }
069 }