You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/11/16 22:35:50 UTC
svn commit: r880969 - in
/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website:
producers/ProducerHelper.java producers/TemplatingProducerHook.aj
utils/LinkHelper.java
Author: simoneg
Date: Mon Nov 16 21:35:49 2009
New Revision: 880969
URL: http://svn.apache.org/viewvc?rev=880969&view=rev
Log:
LABS-221 : LinkHelper with checking features
Modified:
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/TemplatingProducerHook.aj
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java?rev=880969&r1=880968&r2=880969&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java Mon Nov 16 21:35:49 2009
@@ -115,7 +115,7 @@
if (value == null) return "";
Formatter df = Formatters.getFormatterFor(value.getClass());
df.setFormat(format);
- return df.to(date);
+ return df.to(value);
}
/**
Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/TemplatingProducerHook.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/TemplatingProducerHook.aj?rev=880969&r1=880968&r2=880969&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/TemplatingProducerHook.aj (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/TemplatingProducerHook.aj Mon Nov 16 21:35:49 2009
@@ -20,6 +20,7 @@
import org.apache.magma.basics.context.RunningContext;
import org.apache.magma.website.context.WebMethodContextElement;
+import org.apache.magma.website.utils.LinkHelper;
public aspect TemplatingProducerHook {
@@ -33,14 +34,7 @@
if (ele == null) return proceed("/?/?/" + path);
if (path == null) {
- String methodName = ele.getMethod().getName();
- if (methodName.startsWith("do")) {
- methodName = methodName.substring(2);
- } else if (methodName.startsWith("handle")) {
- methodName = methodName.substring(6);
- }
- methodName = Introspector.decapitalize(methodName);
- path = methodName;
+ path = LinkHelper.linkNameFromMethodName(ele.getMethod().getName());
}
String complete = null;
Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java?rev=880969&r1=880968&r2=880969&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java Mon Nov 16 21:35:49 2009
@@ -1,13 +1,18 @@
package org.apache.magma.website.utils;
+import java.beans.Introspector;
+import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.magma.basics.MagmaException;
import org.apache.magma.basics.utils.GenericClass;
import org.apache.magma.basics.utils.GenericClass.MethodDef;
import org.apache.magma.conversion.Converter;
import org.apache.magma.conversion.Converters;
+import org.apache.magma.website.Producer;
import org.apache.magma.website.WebHandler;
/**
@@ -41,14 +46,7 @@
}
public LinkHelper(String methodName, Class<?>[] parameters) {
- if (methodName.startsWith("do")) {
- methodName = methodName.substring(2);
- } else if (method.startsWith("handle")) {
- methodName = methodName.substring(6);
- } else if (method.startsWith("hidden")) {
- methodName = methodName.substring(6);
- }
-
+ methodName = linkNameFromMethodName(methodName);
this.baseMethod = methodName;
if (parameters == null) {
converters = new Converter[0];
@@ -73,7 +71,11 @@
ret.append("_null");
} else {
String converted = converters[i].to(param);
- converted = URLEncoder.encode(converted, "UTF-8");
+ try {
+ converted = URLEncoder.encode(converted, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // should never happen for UTF-8
+ }
converted = converted.replaceAll("\\!", "%21");
ret.append(converted);
}
@@ -83,6 +85,59 @@
return ret.toString();
}
+ /**
+ * Check if links created by this instance will work against the given handler.
+ * @param handler A WebHandler to check against.
+ */
+ public void checkAgainst(Class<? extends WebHandler> handlerClass) {
+ findMethod(handlerClass);
+ }
+
+ /**
+ * Searches for the method resulting from this link in the specified handler.
+ * @param handlerClass A WebHandler to search in
+ * @return A method in the specified class (or superclass)
+ * @throws MagmaException if a method is not found or if more than one method is found.
+ */
+ public Method findMethod(Class<? extends WebHandler> handlerClass) {
+ GenericClass gc = GenericClass.forClass(handlerClass);
+
+ // search for name
+ List<MethodDef> allMethods = new ArrayList<MethodDef>();
+ allMethods.addAll(gc.findAllMethods("handle" + this.baseMethod));
+ allMethods.addAll(gc.findAllMethods("do" + this.baseMethod));
+ allMethods.addAll(gc.findAllMethods("hidden" + this.baseMethod));
+ if (allMethods.size() == 0) throw new MagmaException("Cannot find a method do{0}, handle{0} or hidden{0} for link in class {1}", this.baseMethod, handlerClass);
+
+ // Check parameters
+ Method found = null;
+ for (MethodDef methodDef : allMethods) {
+ Class<?> rettype = methodDef.getReturnType().getBaseClass();
+ if (!Producer.class.isAssignableFrom(rettype)
+ && !WebHandler.class.isAssignableFrom(rettype)) {
+ continue;
+ }
+ GenericClass[] genparams = methodDef.getParameterTypes();
+ Class[] params = GenericClass.toRawClasses(genparams);
+ if (params.length != converters.length) continue;
+ boolean ok = true;
+ for (int i = 0; i < params.length; i++) {
+ if (!converters[i].converts(params[i])) {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) {
+ if (found == null) {
+ found = methodDef.getBaseMethod();
+ } else {
+ throw new MagmaException("Link {0}/{1} would result in ambiguous call between {2} and {3}", handlerClass, this.baseMethod, found.toString(), methodDef.getBaseMethod().toString());
+ }
+ }
+ }
+ if (found == null) throw new MagmaException("Cannot find any method suitable for {0}/{1}", handlerClass, this.baseMethod);
+ return found;
+ }
/**
* Static method for creating a relative link.
@@ -111,7 +166,11 @@
Converter conv = Converters.getConverterFor(param.getClass());
if (conv == null) throw new MagmaException("Cannot find a converter for {0}, needed to create link for {1}", param.getClass(), method);
String converted = conv.to(param);
- converted = URLEncoder.encode(converted, "UTF-8");
+ try {
+ converted = URLEncoder.encode(converted, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // should never happen for UTF-8
+ }
converted = converted.replaceAll("\\!", "%21");
ret.append(converted);
}
@@ -127,4 +186,16 @@
return makeLink(method.getName(), params);
}
+ public static String linkNameFromMethodName(String methodName) {
+ if (methodName.startsWith("do")) {
+ methodName = methodName.substring(2);
+ } else if (methodName.startsWith("handle")) {
+ methodName = methodName.substring(6);
+ } else if (methodName.startsWith("hidden")) {
+ methodName = methodName.substring(6);
+ }
+ methodName = Introspector.decapitalize(methodName);
+ return methodName;
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org