View Javadoc

1   package org.astrogrid.portal.myspace.acting.framework;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   /***
7    * Implements a base class which all MySpace handlers <b>MUST</b> inherit.
8    * 
9    * <p>
10   * The class uses the <b>Template Method</b> pattern to factor out all common
11   * functionality from handlers and put it into its own <code>execute()</code>
12   * method.  All subclasses must implement the constructor and the <code>
13   * executeTemplateMethod()</code> to perform their work.
14   * </p>
15   * 
16   * @author peter.shillan
17   */
18  public abstract class AbstractMySpaceHandler implements MySpaceHandler {
19    protected ContextWrapper context;
20    
21    /***
22     * Stores the environmental context for later use.
23     * 
24     * @param context wrapper class that provides access to the environment.
25     */
26    protected AbstractMySpaceHandler(ContextWrapper context) {
27      this.context = context;
28    }
29      
30    /***
31     * Execute the handler's <code>executeTemplateMethod()</code> providing a
32     * generic error handling and reporting mechanism.
33     * 
34     * <p>
35     * Subclasses can use the <code>results</code> map to report back to the
36     * caller and any exceptions thrown will result in a <code>null</code>
37     * return (meaning <b>ERROR</b> in the sitemap) and a local attribute
38     * holding the error message.
39     * </p>
40     */
41    public final Map execute() {
42      HashMap results = new HashMap();
43      
44      final String className = getClass().getName();
45      try{
46        executeTemplateMethod(results);
47        
48        // Nothing thrown means success.
49        addLocalResult(MySpaceHandler.PARAM_ACTION, "true", results);
50        addLocalResult(MySpaceHandler.PARAM_ACTION_HANDLER, className, results);
51      }
52      catch(Throwable t) {
53        context.setLocalAttribute(MySpaceHandler.PARAM_ACTION, "false");
54        context.setLocalAttribute(MySpaceHandler.PARAM_ACTION_HANDLER, className);
55        context.setLocalAttribute(MySpaceHandler.PARAM_ACTION_ERR_MSG, t.getLocalizedMessage());
56        
57        results = null;
58      }
59      
60      return results;
61    }
62    
63    /***
64     * Convenience method for adding a local attribute and a result at the same
65     * time.
66     * 
67     * @param name name of result attribute
68     * @param value value of result attribute
69     * @param results map of results to use
70     */
71    public void addLocalResult(String name, String value, Map results) {
72      context.setLocalAttribute(name, value);
73      results.put(name, value);
74    }
75    
76    /***
77     * Convenience method for adding a global attribute and a result at the same
78     * time.
79     * 
80     * @param name name of result attribute
81     * @param value value of result attribute
82     * @param results map of results to use
83     */
84    public void addGlobalResult(String name, String value, Map results) {
85      context.setGlobalAttribute(name, value);
86      results.put(name, value);
87    }
88    
89    /***
90     * Template method which does the subclass-specific work.
91     * 
92     * @param results attributes which will be returned can be set here
93     * @throws Throwable
94     */
95    abstract protected void executeTemplateMethod(Map results) throws Throwable;
96  }