View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.taglib;
17  
18  import java.util.Map.Entry;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.PageContext;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  import com.fatwire.gst.foundation.DebugHelper;
26  import com.fatwire.gst.foundation.controller.action.Action;
27  import com.fatwire.gst.foundation.controller.action.ActionLocator;
28  import com.fatwire.gst.foundation.controller.action.ActionLocatorUtils;
29  import com.fatwire.gst.foundation.controller.action.Model;
30  import com.fatwire.gst.foundation.controller.annotation.AnnotationUtils;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * JSP tag that replaces the GsfRoot tag and adds Action support. If the action
37   * name argument is provided, than a Action is looked up and executed.
38   * 
39   * @author Dolf Dijkstra
40   * @since Apr 13, 2011
41   */
42  public class ActionTag extends GsfSimpleTag {
43      static final Log LOG = LogFactory.getLog(ActionTag.class.getPackage().getName());
44      static final Log LOG_TIME = LogFactory.getLog(ActionTag.class.getPackage().getName() + ".time");
45  
46      private String action;
47  
48      /*
49       * (non-Javadoc)
50       * 
51       * @see com.fatwire.gst.foundation.taglib.GsfRootTag#doStartTag()
52       */
53      @Override
54      public void doTag() throws JspException {
55          final ICS ics = getICS();
56  
57          final long start = LOG_TIME.isDebugEnabled() ? System.nanoTime() : 0;
58          final ActionLocator locator = getActionLocator();
59          if (locator == null)
60              throw new IllegalStateException("The ActionLocator cannot be found.");
61          if ("+".equals(action)) {
62              action = ics.ResolveVariables("CS.elementname") + "_action";
63          }
64          final Action a = locator.getAction(ics, action);
65  
66          if (a != null) {
67              if (LOG_TIME.isDebugEnabled()) {
68  
69                  DebugHelper.printTime(LOG_TIME, "Locating Action " + a.getClass().getName(), start);
70              }
71  
72              final long beforeHandleRequest = LOG_TIME.isDebugEnabled() ? System.nanoTime() : 0;
73              a.handleRequest(ics);
74              copyModelData(a);
75              if (LOG_TIME.isDebugEnabled()) {
76                  DebugHelper.printTime(LOG_TIME, "Executing Action " + a.getClass().getName(), beforeHandleRequest);
77              }
78          } else {
79              throw new IllegalArgumentException("Action with name '" + action + "' cannot be found.");
80          }
81  
82      }
83  
84      /**
85       * Copies the data from the Model to the jsp page scope
86       * 
87       * @param a the action to copy from.
88       */
89      private void copyModelData(final Action a) {
90          if (a == null) {
91              return;
92          }
93          final Model model = AnnotationUtils.findService(a, Model.class);
94          if (model == null) {
95              return;
96          }
97          for (final Entry<String, ?> e : model.entries()) {
98              // don't overwrite or worse, delete in case value is null.
99              if (getJspContext().getAttribute(e.getKey(), PageContext.PAGE_SCOPE) == null) {
100                 getJspContext().setAttribute(e.getKey(), e.getValue(), PageContext.PAGE_SCOPE);
101             }
102         }
103     }
104 
105     /**
106      * @return the ActionLocator
107      */
108     protected ActionLocator getActionLocator() {
109 
110         return ActionLocatorUtils.getActionLocator(getPageContext().getServletContext());
111     }
112 
113     /**
114      * @return the action
115      */
116     public String getAction() {
117         return action;
118     }
119 
120     /**
121      * @param action the action to set
122      */
123     public void setAction(final String action) {
124         this.action = action;
125     }
126 
127 }