labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r740948 - in /labs/magma/trunk/foundation-website/src: main/java/org/apache/magma/website/ main/java/org/apache/magma/website/context/ main/java/org/apache/magma/website/producers/ test/java/org/apache/magma/website/ test/java/org/apache/ma...
Date Thu, 05 Feb 2009 00:15:45 GMT
Author: simoneg
Date: Thu Feb  5 00:15:45 2009
New Revision: 740948

URL: http://svn.apache.org/viewvc?rev=740948&view=rev
Log:
LABS-279 : foundation-website using running context.

Added:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebLocalizable.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/ContextWebMethods.aj
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/Contextualizer.aj
      - copied, changed from r738083, labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Contextualizer.aj
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/WebMethodContextElement.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
Removed:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Contextualizer.aj
Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/RootWebHandler.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebHandler.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizeHtmlProducer.aj
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/RecallingWebHandler.java

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
Thu Feb  5 00:15:45 2009
@@ -169,11 +169,13 @@
 		} catch (IOException e) {
 			throw new MagmaException(e, "Error getting servlet output stream");
 		}
-		if (t.wasMain() && producer.getCompletePath() != null) {
+		if (t.wasMain() && producer.isRepeatable()) {
+			System.out.println("Producer has a reproducible main path of " + producer.getCompletePath());
 			session.setAttribute("__magma_lastmain", producer.getCompletePath());			
 			session.setAttribute("__magma_lasttemplate", t.getClass());
-		}		
-		System.out.println("Producer has a complete path of " + producer.getCompletePath());
+		} else {	
+			System.out.println("Producer has a not reproducible OR not main path of " + producer.getCompletePath());
+		}
 	}
 	
 	public Template getDefaultTemplate() {

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HandlerSetup.aj
Thu Feb  5 00:15:45 2009
@@ -31,7 +31,7 @@
 import org.apache.magma.conversion.Converter;
 import org.apache.magma.conversion.Converters;
 import org.apache.magma.website.utils.Binder;
-import org.apache.magma.website.producers.ParametrizeHtmlProducer;
+import org.apache.magma.website.context.Contextualizer;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -41,7 +41,7 @@
 
 public aspect HandlerSetup percflow(inRequest()) {
 
-	declare precedence: HandlerSetup, ParametrizeHtmlProducer;
+	declare precedence: Contextualizer, HandlerSetup;
 	
 	declare parents : (WebHandler) implements MagmaBeanSupport;
 
@@ -115,48 +115,6 @@
 		} else {
 			retObj = proceed(h);
 		}
-		if (retObj instanceof WebHandler) {
-			WebHandler ret = (WebHandler)retObj;
-			if (!ret.inited) {
-				ret.setCreatingHandler(h);
-				String methname = ((MethodSignature)thisJoinPoint.getSignature()).getName();
-				boolean valid = true;
-				methname = methname.substring(6);
-				methname = Character.toLowerCase(methname.charAt(0)) + methname.substring(1);
-				String relpath = h.getRelativePath() + methname;
-				Object[] args = thisJoinPoint.getArgs();
-				if (args != null && args.length > 0) {
-					for (int i = 0; i < args.length; i++) {
-						relpath += "-";
-						if (args[i] == null) continue;
-						Converter conv = Converters.getConverterFor(args[i].getClass());
-						if (conv == null) {
-							valid = false;
-							break;
-						}
-						try {
-							relpath += conv.toString(args[i]);
-						} catch (Throwable e) {
-							valid = false;
-							break;
-						}
-					}
-				}
-				if (valid) {
-					relpath += "/";
-					ret.setRelativePath(relpath);
-				} else {
-					ret.setRelativePath(null);
-				}
-				String unique = Integer.toString(ret.getRelativePath().hashCode());
-				try {
-					Binder.bind(ret, request, unique, uploads);
-					ret.inited = true;
-				} catch (Throwable e) {
-					throw new MagmaException(e, "Error while setting up {0}", ret.getClass());
-				}
-			}
-		}
 		return retObj;
 	}
 

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/HtmlProducer.java
Thu Feb  5 00:15:45 2009
@@ -23,6 +23,7 @@
 
 public abstract class HtmlProducer extends Producer implements Templatable {
 
+	protected boolean repeatable = true;
 	protected Template template;
 
 	@Override
@@ -46,5 +47,13 @@
 		}
 		return null;
 	}
+
+	public boolean isRepeatable() {
+		return repeatable;
+	}
+
+	public void setRepeatable(boolean repeatable) {
+		this.repeatable = repeatable;
+	}
 	
 }

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
Thu Feb  5 00:15:45 2009
@@ -20,27 +20,17 @@
 import java.io.OutputStream;
 import java.lang.reflect.Method;
 
-public abstract class Producer {
+import org.apache.magma.basics.context.ContextOwner;
+import org.apache.magma.basics.context.RunningContext;
 
-	private Method method;
-	private WebHandler handler;
-	private String basePath;
-	private String completePath;
+public abstract class Producer implements ContextOwner {
 
 	public Method getCreatingMethod() {
-		return this.method;
-	}
-	
-	public void setCreatingMethod(Method method) {
-		this.method = method;
+		return RunningContext.get().getPreviousMethod();
 	}
 	
 	public WebHandler getCreatingHandler() {
-		return this.handler;
-	}
-	
-	public void setCreatingHandler(WebHandler handler) {
-		this.handler = handler;
+		return RunningContext.get().getPreviousHandler();
 	}
 	
 	public abstract void produce(OutputStream stream);	
@@ -61,11 +51,7 @@
 	}
 
 	public String getBasePath() {
-		return basePath;
-	}
-
-	public void setBasePath(String basePath) {
-		this.basePath = basePath;
+		return getCreatingHandler().getRelativePath();
 	}
 
 	/**
@@ -87,11 +73,7 @@
 	 * @return A path that, if reused, should return a producer equivalent to this one, or null
is such a path could not be constructed.
 	 */
 	public String getCompletePath() {
-		return completePath;
-	}
-
-	public void setCompletePath(String completePath) {
-		this.completePath = completePath;
+		return RunningContext.get().getCurrentURL();
 	}
 
 }

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/RootWebHandler.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/RootWebHandler.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/RootWebHandler.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/RootWebHandler.java
Thu Feb  5 00:15:45 2009
@@ -31,10 +31,6 @@
 		return "/";
 	}
 	
-	@Override
-	public void setRelativePath(String path) {
-	}
-	
 	public static RootWebHandler getInstance() {
 		return new RootWebHandler();
 	}

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebHandler.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebHandler.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebHandler.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebHandler.java
Thu Feb  5 00:15:45 2009
@@ -17,6 +17,8 @@
 package org.apache.magma.website;
 
 import org.apache.magma.basics.MagmaException;
+import org.apache.magma.basics.context.ContextOwner;
+import org.apache.magma.basics.context.RunningContext;
 import org.apache.magma.basics.utils.GenericClass;
 import org.apache.magma.basics.utils.GenericClass.MethodDef;
 import org.apache.magma.conversion.Converter;
@@ -30,13 +32,11 @@
 
 // TODO optimization : GenericClass could be taken only once
 // TODO optimization : getRightMethod could return a MethodRef directly
-public class WebHandler implements Templatable {
+public class WebHandler implements Templatable, ContextOwner {
 
 	public static final String defaultPostfix = "_default";
 	public static final String defaultMethod = "do_default";
 	
-	protected String relativePath = "";
-	protected WebHandler creatingHandler = null;
 	protected Template template;
 	
 	
@@ -63,11 +63,7 @@
 	 * @return A path that, if reused, should return a producer equivalent to this one, or null
is such a path could not be constructed.
 	 */	
 	public String getRelativePath() {
-		return this.relativePath;
-	}
-	
-	public void setRelativePath(String path) {
-		this.relativePath = path;
+		return RunningContext.get().getCurrentBaseURL();
 	}
 	
 	public void setTemplate(Template t) {
@@ -98,7 +94,7 @@
 		if (pathInfo == null) {
 			info = new String[] { "" };
 		} else {
-			StringTokenizer stok = new StringTokenizer(pathInfo, "/-");
+			StringTokenizer stok = new StringTokenizer(pathInfo, "/;");
 			info = new String[stok.countTokens()];
 			int pos = 0;
 			while (stok.hasMoreTokens()) {
@@ -109,6 +105,9 @@
 	}
 	
 	public Producer find(String info) {		
+		if (info == null || info.length() == 0) {
+			info = defaultPostfix;
+		}
 		return find(tokenizeRequest(info));
 	}
 	
@@ -271,18 +270,30 @@
 		}
 		return rightMethod;
 	}
-
-	public WebHandler getCreatingHandler() {
-		return creatingHandler;
+	
+	public HtmlProducer redirect(HtmlProducer to) {
+		return to;
 	}
-
-	public void setCreatingHandler(WebHandler creatingHandler) {
-		this.creatingHandler = creatingHandler;
+	
+	public WebHandler redirect(WebHandler to) {
+		return to;
 	}
 	
+	protected RootWebHandler root() {
+		return RootWebHandler.getInstance();
+	}
+	
+	protected WebHandler parent() {
+		return getCreatingHandler();
+	}
+
 	protected HtmlProducer unrepeatable(HtmlProducer producer) {
-		producer.setCompletePath(null);
+		producer.setRepeatable(false);
 		return producer;
+	}	
+
+	public WebHandler getCreatingHandler() {
+		return RunningContext.get().getPreviousHandler();
 	}
-	
+
 }

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebLocalizable.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebLocalizable.java?rev=740948&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebLocalizable.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/WebLocalizable.java
Thu Feb  5 00:15:45 2009
@@ -0,0 +1,15 @@
+package org.apache.magma.website;
+
+import java.lang.reflect.Method;
+
+import org.apache.magma.basics.context.ContextOwner;
+
+public interface WebLocalizable extends ContextOwner {
+
+	public String getCompletePath();
+	public String getBasePath();
+	
+	public WebHandler getCreatingHandler();
+	public Method getCreatingMethod();
+	
+}

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/ContextWebMethods.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/ContextWebMethods.aj?rev=740948&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/ContextWebMethods.aj
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/ContextWebMethods.aj
Thu Feb  5 00:15:45 2009
@@ -0,0 +1,71 @@
+package org.apache.magma.website.context;
+
+import java.lang.reflect.Method;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.magma.basics.context.SubRunningContext;
+import org.apache.magma.website.WebHandler;
+
+public aspect ContextWebMethods {
+
+	public String SubRunningContext.getCurrentURL() {
+		List<WebMethodContextElement> methods = getElements(WebMethodContextElement.class);
+		if (methods.size() == 0) return null;
+		return calculateURL(methods);
+	}
+	
+	private String SubRunningContext.calculateURL(List<WebMethodContextElement> methods)
{
+		StringBuilder ret = new StringBuilder();
+		WebHandler prevh = null;
+		String prevseg = null;
+		for (WebMethodContextElement ctxele : methods) {
+			if (ctxele.getHandlerInstance() != prevh) {
+				if (prevseg != null) {
+					ret.append('/');
+					ret.append(prevseg);					
+				}
+			}
+			prevh = ctxele.getHandlerInstance();
+			prevseg = ctxele.getURLSegment();
+		}
+		
+		ret.append('/');
+		ret.append(prevseg);							
+		return ret.toString();		
+	}
+	
+	public String SubRunningContext.getCurrentBaseURL() {
+		List<WebMethodContextElement> methods = getCommonElements(WebMethodContextElement.class);
+		if (methods.size() == 0) return null;
+		return calculateURL(methods) + "/";
+	}
+
+	public Method SubRunningContext.getLastMethod() {
+		WebMethodContextElement last = getLast(WebMethodContextElement.class);
+		if (last == null) return null;
+		return last.getMethod();
+	}
+	
+	public WebHandler SubRunningContext.getLastHandler() {
+		WebMethodContextElement last = getLast(WebMethodContextElement.class);
+		if (last == null) return null;
+		return last.getHandlerInstance();
+	}
+	
+	private WebMethodContextElement SubRunningContext.getPreviousElement() {
+		List<WebMethodContextElement> methods = getCommonElements(WebMethodContextElement.class);
+		if (methods.size() == 0) return null;
+		return methods.get(methods.size() - 1);
+	}
+	
+	public WebHandler SubRunningContext.getPreviousHandler() {
+		WebMethodContextElement prevele = getPreviousElement();
+		return prevele == null ? null : prevele.getHandlerInstance();
+	}
+	public Method SubRunningContext.getPreviousMethod() {
+		WebMethodContextElement prevele = getPreviousElement();
+		return prevele == null ? null : prevele.getMethod();
+	}
+	
+}

Copied: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/Contextualizer.aj
(from r738083, labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Contextualizer.aj)
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/Contextualizer.aj?p2=labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/Contextualizer.aj&p1=labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Contextualizer.aj&r1=738083&r2=740948&rev=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Contextualizer.aj
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/Contextualizer.aj
Thu Feb  5 00:15:45 2009
@@ -1,24 +1,37 @@
-package org.apache.magma.website;
+package org.apache.magma.website.context;
 
+import org.apache.magma.basics.MagmaException;
 import org.apache.magma.basics.context.RunningContext;
 import org.apache.magma.basics.context.SubRunningContext;
 import org.apache.magma.website.HandlerSetup;
 import org.aspectj.lang.reflect.MethodSignature;
+import org.apache.magma.website.WebHandler;
+import org.apache.magma.website.Producer;
 
 public aspect Contextualizer {
 
-	before(WebHandler h) : HandlerSetup.doMethod(h) || HandlerSetup.subHandle(h) || HandlerSetup.hiddenMethod(h)
{
+	before(WebHandler h) : HandlerSetup.doMethod(h) {
 		SubRunningContext context = RunningContext.get();
-		context.push(h.getClass());
-		context.push(((MethodSignature)thisJoinPoint.getSignature()).getMethod());
+		WebMethodContextElement wmce = new WebMethodContextElement(((MethodSignature)thisJoinPoint.getSignature()).getMethod(),
thisJoinPoint.getArgs(), h);
+		context.push(wmce);
 	}
 
-	after(WebHandler h) : HandlerSetup.doMethod(h) || HandlerSetup.subHandle(h) || HandlerSetup.hiddenMethod(h)
{
+	after(WebHandler h) : HandlerSetup.doMethod(h) {
 		SubRunningContext context = RunningContext.get();
-		context.popMethod();
-		context.popClass();
+		context.pop(WebMethodContextElement.class);
 	}
-	
+
+	before(WebHandler h) : HandlerSetup.subHandle(h) {
+		SubRunningContext context = RunningContext.get();
+		WebMethodContextElement wmce = new WebMethodContextElement(((MethodSignature)thisJoinPoint.getSignature()).getMethod(),
thisJoinPoint.getArgs(), h);
+		context.push(wmce);
+	}
+
+	after(WebHandler h) : HandlerSetup.subHandle(h) {
+		SubRunningContext context = RunningContext.get();
+		context.pop(WebMethodContextElement.class);
+	}
+		
 	before(Producer p) : (execution(* Producer+.produce(..)) || execution(* Producer+.head(..)))
&& this(p) {
 		SubRunningContext context = RunningContext.get();
 		context.push(p.getClass());
@@ -29,4 +42,5 @@
 		context.popClass();
 	}
 	
+	
 }

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/WebMethodContextElement.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/WebMethodContextElement.java?rev=740948&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/WebMethodContextElement.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/context/WebMethodContextElement.java
Thu Feb  5 00:15:45 2009
@@ -0,0 +1,68 @@
+package org.apache.magma.website.context;
+
+import java.lang.reflect.Method;
+
+import org.apache.magma.basics.context.MethodContextElement;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+import org.apache.magma.website.WebHandler;
+
+public class WebMethodContextElement extends MethodContextElement {
+
+	private boolean urlSegmentComputed = false;
+	private String urlSegment = null;
+	private WebHandler handlerInstance = null;
+	
+	public WebMethodContextElement(Method method, Object[] args, WebHandler instance) {
+		super(method, args);
+		this.handlerInstance = instance;
+	}
+	
+	public String getURLSegment() {
+		if (urlSegmentComputed) return urlSegment;
+		urlSegment = null;
+		StringBuilder sb = new StringBuilder(getMethod().getName());
+		if (sb.subSequence(0, 6).equals("hidden")) {
+			urlSegmentComputed = true;
+		}
+		if (sb.subSequence(0, 6).equals("handle")) {
+			sb.delete(0, 6);
+		} else if (sb.subSequence(0, 2).equals("do")) {
+			sb.delete(0, 2);
+		}
+		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
+		Object[] params = getArgs();
+		boolean valid = true;
+		if (params != null && params.length > 0) {
+			for (Object object : params) {
+				if (object != null) {
+					Converter converter = Converters.getConverterFor(object.getClass());
+					if (converter != null) {
+						try {
+							sb.append(';');
+							sb.append(converter.toString(object));
+						} catch (Throwable e) {
+							valid = false;
+							break;
+						}
+					} else {
+						valid = false;
+						break;
+					}
+				} else {
+					sb.append(';');					
+				}
+			}
+		}
+		if (valid) {
+			urlSegment = sb.toString();
+		}
+		urlSegmentComputed = true;
+		return urlSegment;
+	}
+
+	public WebHandler getHandlerInstance() {
+		return handlerInstance;
+	}
+
+}

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizeHtmlProducer.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizeHtmlProducer.aj?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizeHtmlProducer.aj
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizeHtmlProducer.aj
Thu Feb  5 00:15:45 2009
@@ -24,169 +24,35 @@
 import org.aspectj.lang.reflect.MethodSignature;
 import java.util.Stack;
 import java.lang.reflect.Method;
+import org.apache.magma.website.context.WebMethodContextElement;
+import org.apache.magma.basics.context.RunningContext;
 
 
 public privileged aspect ParametrizeHtmlProducer {
 
-	private ThreadLocal<Stack<Object[]>> params = new ThreadLocal<Stack<Object[]>>();
-	private ThreadLocal<Stack<WebHandler>> handlers = new ThreadLocal<Stack<WebHandler>>();
-	private ThreadLocal<Stack<Method>> methods = new ThreadLocal<Stack<Method>>();
-	
 	private boolean Producer.inited = false;
 	private boolean ParametrizableProducer.paramsinited = false;
-	 	
-	pointcut doMethod() : execution(Producer+ WebHandler+.do*(..)) || call(Producer+ WebHandler+.do*(..))
||
-						  execution(Producer+ WebHandler+.hidden*(..)) || call(Producer+ WebHandler+.hidden*(..));
-
-	pointcut handleMethod() : execution(WebHandler+ WebHandler+.handle*(..)) || call(WebHandler+
WebHandler+.handle*(..));
 	
 	pointcut parametrizableProducerCreation(ParametrizableProducer p) : execution(ParametrizableProducer+.new(..))
&& target(p);
 	
-	pointcut producerCreation(Producer p) : execution(Producer+.new(..)) && target(p);
-	
-	before() : doMethod() || handleMethod() {
-		Stack<Object[]> stack = params.get();
-		if (stack == null) {
-			stack = new Stack<Object[]>();
-			params.set(stack);
-		}
-		stack.push(thisJoinPoint.getArgs());
-		
-		Stack<WebHandler> handler = handlers.get();
-		if (handler == null) {
-			handler = new Stack<WebHandler>();
-			handlers.set(handler);
-		}
-		Object trg = thisJoinPoint.getThis();
-		if (!(trg instanceof WebHandler)) {
-			trg = thisJoinPoint.getTarget();
-		}
-		if (!(trg instanceof WebHandler)) {
-			handler.push(null);
-		} else {
-			handler.push((WebHandler)trg);
-		}			
-		
-		Stack<Method> name = methods.get();
-		if (name == null) {
-			name = new Stack<Method>();
-			methods.set(name);
-		}
-		name.push(((MethodSignature)thisJoinPoint.getSignature()).getMethod());
-	}
-	
-	after() : doMethod() || handleMethod() {
-		Stack<Object[]> stack = params.get();
-		if (stack != null && stack.size() != 0) {
-			stack.pop();
-		}
-		Stack<WebHandler> handler = handlers.get();
-		if (handler != null && handler.size() != 0) {
-			handler.pop();
-		}
-		Stack<Method> name = methods.get();
-		if (name != null && name.size() != 0) {
-			name.pop();
-		}
-	}
-	
-	after(Producer producer) : producerCreation(producer) {
-		if (producer.inited) return;
-		producer.inited = true;
-		Stack<WebHandler> handler = handlers.get();
-		if (handler != null && handler.size() != 0) {
-			WebHandler h = handler.peek();
-			producer.setCreatingHandler(h);
-			producer.setBasePath(h.getRelativePath());
-		} 
-		
-		
-		Method acmethod = null;
-		Stack<Method> method = methods.get();
-		if (method != null && method.size() != 0) {
-			acmethod = method.peek();
-		}
-		if (acmethod != null) {
-			producer.setCreatingMethod(acmethod);
-			String methodName = acmethod.getName();
-			boolean valid = true;
-			if (methodName.startsWith("do")) {
-				methodName = methodName.substring(2);
-				methodName = Introspector.decapitalize(methodName);
-				Stack<Object[]> stack = params.get();
-				if (stack != null && stack.size() != 0) {
-					Object[] params = stack.peek();
-					for (Object object : params) {
-						String rep = "";
-						if (object != null) {
-							Converter converter = Converters.getConverterFor(object.getClass());
-							if (converter != null) {
-								try {
-									rep = converter.toString(object);
-								} catch (Throwable e) {
-									valid = false;
-									break;
-								}
-							} else {
-								valid = false;
-								break;
-							}
-						}
-						methodName += "-" + rep; 
-					}
-				}
-				if (valid) {
-					String parentPath = producer.getBasePath();
-					if (parentPath == null) {
-						valid = false;
-					} else {
-						methodName = parentPath + methodName;
-					}
-				}
-				if (valid) {
-					producer.setCompletePath(methodName);
-				} else {
-					producer.setCompletePath(null);
-				}
-			}
-		}
-	}
-	
 	after(ParametrizableProducer producer) : parametrizableProducerCreation(producer) {
 		if (producer.paramsinited) return;
 		producer.paramsinited = true;
-		Stack<Object[]> stack = params.get();
-		if (stack != null && stack.size() != 0) {
-			Object[] params = stack.peek();
-			for (Object object : params) {
-				producer.addParameter(object);
-			}
+		WebMethodContextElement ele = RunningContext.get().getLast(WebMethodContextElement.class);
+		if (ele == null) return;
+		
+		for (Object object : ele.getArgs()) {
+			producer.addParameter(object);
+		}
+		String methodName = ele.getMethod().getName();
+		if (methodName.startsWith("do")) {
+			methodName = methodName.substring(2);
+			methodName = Introspector.decapitalize(methodName);
 		}
+		producer.setTemplateName(methodName);
 		
 		producer.parametrizeByHandler();
 		
-		/*
-		Stack<WebHandler> handler = handlers.get();
-		if (handler != null && handler.size() != 0) {
-			WebHandler h = handler.peek();
-			producer.setCreatingHandler(h);
-		} 
-		*/
-		
-		
-		Method acmethod = null;
-		Stack<Method> method = methods.get();
-		if (method != null && method.size() != 0) {
-			acmethod = method.peek();
-		}
-		if (acmethod != null) {
-			String methodName = acmethod.getName();
-			if (methodName.startsWith("do")) {
-				methodName = methodName.substring(2);
-				methodName = Introspector.decapitalize(methodName);
-			}
-			producer.setTemplateName(methodName);
-		}		
 	}
 }
 

Added: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java?rev=740948&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
(added)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebContextTest.java
Thu Feb  5 00:15:45 2009
@@ -0,0 +1,97 @@
+package org.apache.magma.website;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+import java.util.List;
+
+import org.apache.magma.basics.context.RunningContext;
+import org.apache.magma.website.context.WebMethodContextElement;
+import org.apache.magma.website.templating.Template;
+import org.apache.magma.website.teststuff.RecallingWebHandler;
+import org.apache.magma.website.teststuff.SimpleWebHandler;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class WebContextTest {
+
+	@Before
+	@After
+	public void cleanupContext() {
+		RunningContext.cleanup();
+	}
+	
+	@Test
+	public void handlerContextTest() throws Exception {
+		List<WebMethodContextElement> elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(0));
+		assertThat(RunningContext.get().getCurrentURL(), nullValue());
+		assertThat(RunningContext.get().getCurrentBaseURL(), nullValue());		
+		RootWebHandler root = RootWebHandler.getInstance();
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(0));
+		assertThat(RunningContext.get().getCurrentURL(), nullValue());
+		assertThat(RunningContext.get().getCurrentBaseURL(), nullValue());		
+		SimpleWebHandler lev1 = root.handleTest();
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(0));
+		assertThat(RunningContext.get().getCurrentURL(), nullValue());
+		assertThat(RunningContext.get().getCurrentBaseURL(), nullValue());				
+		WebHandler lev2 = lev1.handleResend2();
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(0));
+		assertThat(RunningContext.get().getCurrentURL(), nullValue());
+		assertThat(RunningContext.get().getCurrentBaseURL(), nullValue());				
+
+		RunningContext.entering(root);
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(0));
+		assertThat(RunningContext.get().getCurrentURL(), nullValue());
+		assertThat(RunningContext.get().getCurrentBaseURL(), nullValue());		
+		WebHandler creatingHandler = root.getCreatingHandler();
+		assertNull(creatingHandler);
+		RunningContext.exiting(root);
+		
+		
+		
+		RunningContext.entering(lev1);
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(1));
+		assertThat(RunningContext.get().getCurrentURL(), equalTo("/test"));
+		assertThat(RunningContext.get().getCurrentBaseURL(), equalTo("/test/"));		
+		creatingHandler = lev1.getCreatingHandler();
+		assertSame(root, creatingHandler);
+		RunningContext.exiting(lev1);
+		
+		RunningContext.entering(lev2);
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(2));
+		assertThat(RunningContext.get().getCurrentURL(), equalTo("/test/resend2"));
+		assertThat(RunningContext.get().getCurrentBaseURL(), equalTo("/test/resend2/"));
+		creatingHandler = lev2.getCreatingHandler();
+		assertSame(lev1, creatingHandler);
+		RunningContext.exiting(lev2);
+		
+		Template template = lev2.getTemplate();
+		assertNull(template);
+		
+		RecallingWebHandler rwh = (RecallingWebHandler) lev2;
+		HtmlProducer prod = rwh.doNormal();
+		RunningContext.entering(prod);
+		elements = RunningContext.get().getElements(WebMethodContextElement.class);
+		assertThat(elements.size(), equalTo(3));
+		assertThat(RunningContext.get().getCurrentURL(), equalTo("/test/resend2/normal"));
+		assertThat(RunningContext.get().getCurrentBaseURL(), equalTo("/test/resend2/normal/"));
+		assertThat(RunningContext.get().getPreviousHandler(), sameInstance(lev2));
+		
+		RunningContext.exiting(prod);
+		
+		assertThat(prod.getBasePath(), equalTo("/test/resend2/"));
+		assertThat(prod.getCompletePath(), equalTo("/test/resend2/normal"));
+		assertThat(prod.getCreatingHandler(), sameInstance(lev2));
+		assertThat(prod.getCreatingMethod().getName(), equalTo("doNormal"));
+		
+	}
+	
+}

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
Thu Feb  5 00:15:45 2009
@@ -180,7 +180,7 @@
 	@Test
 	public void parametrizedSimple() throws Exception {
 		DefaultExpectations def = new DefaultExpectations() {{
-			one(session).setAttribute("__magma_lastmain","/test/named-ciao");
+			one(session).setAttribute("__magma_lastmain","/test/named;ciao");
 			one(session).setAttribute("name", "ciao");			
 		}};
 		def.setupDefaults("/test/named/ciao");
@@ -215,7 +215,7 @@
 	@Test
 	public void parametrizedObject() throws Exception {
 		DefaultExpectations def = new DefaultExpectations() {{
-			one(session).setAttribute("__magma_lastmain","/test/userEntered-Arianna");
+			one(session).setAttribute("__magma_lastmain","/test/userEntered;Arianna");
 			one(req).setAttribute("user", Person.getPerson("Arianna"));
 		}};
 		def.setupDefaults("/test/userEntered/Arianna");
@@ -250,9 +250,9 @@
 	@Test
 	public void parametrizedPrimitive() throws Exception {
 		DefaultExpectations def = new DefaultExpectations() {{
-			one(session).setAttribute("__magma_lastmain","/test/goToPage-5");
+			one(session).setAttribute("__magma_lastmain","/test/goToPage;5");
 		}};
-		def.setupDefaults("/test/goToPage/5");
+		def.setupDefaults("/test/goToPage;5");
 		Dispatch disp = def.start();
 		Object[] params = FakeHtmlProducer.getInstance().getParams();
 		assertNotNull(params);
@@ -262,7 +262,7 @@
 	@Test
 	public void parametrizedObjectMissing() throws Exception {
 		DefaultExpectations def = new DefaultExpectations() {{
-			one(session).setAttribute("__magma_lastmain","/test/userEntered-");
+			one(session).setAttribute("__magma_lastmain","/test/userEntered;");
 			one(req).removeAttribute("user");
 		}};
 		def.setupDefaults("/test/userEntered");

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/RecallingWebHandler.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/RecallingWebHandler.java?rev=740948&r1=740947&r2=740948&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/RecallingWebHandler.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/RecallingWebHandler.java
Thu Feb  5 00:15:45 2009
@@ -33,4 +33,8 @@
 		}		
 	}
 	
+	public HtmlProducer doNormal() {
+		return new FakeHtmlProducer();
+	}
+	
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message