View Javadoc

1   /*
2    * Copyright 2008 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  
17  package com.fatwire.gst.foundation.facade.runtag;
18  
19  import java.util.Enumeration;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import COM.FutureTense.Interfaces.ICS;
25  
26  import com.fatwire.gst.foundation.facade.FTValListFacade;
27  
28  /**
29   * 
30   * 
31   * @author Dolf Dijkstra
32   * 
33   */
34  
35  public abstract class AbstractTagRunner extends FTValListFacade implements TagRunner {
36  
37      private static final Log LOG = LogFactory.getLog(AbstractTagRunner.class.getPackage().getName());
38  
39      private final String tagName;
40  
41      /**
42       * @param tagName the name of the tag to be invoked.
43       */
44      protected AbstractTagRunner(String tagName) {
45          this.tagName = tagName;
46      }
47  
48      /**
49       * Template method to bind variables on ics (or any object space) to the
50       * current object.
51       * <p/>
52       * Implementation in this class does nothing, subclasses can override.
53       * 
54       * @param ics
55       */
56      protected void bind(ICS ics) {
57  
58      }
59  
60      /**
61       * Executes the tag via ics.runtag
62       * <p/>
63       * order is
64       * <ul>
65       * <li>bind(ics);</li>
66       * <li>preExecute();</li>
67       * <li>ics.runTag();</li>
68       * <li>postExceute();</li>
69       * <li>handleError() if runTag or postExecute set errno to anything else
70       * then zero.</li>
71       * </ul>
72       * 
73       * @see TagRunner#execute(ICS)
74       */
75      public String execute(ICS ics) {
76          bind(ics);
77          if (ics.GetErrno() != 0) {
78              ics.ClearErrno();
79          }
80          preExecute(ics);
81  
82          if (LOG.isTraceEnabled()) {
83              StringBuffer sb = new StringBuffer("About to execute runTag for tag: " + tagName + ".  ");
84              sb.append("\nInput param list:");
85              for (Object k : list.keySet()) {
86                  String key = (String) k;
87                  sb.append("\n\t").append(key).append("=")
88                          .append(isPW(key) ? "<password suppressed>" : list.getValString(key));
89              }
90              sb.append("\nVariables:");
91              Enumeration<?> e = ics.GetVars();
92              while (e.hasMoreElements()) {
93                  String key = (String) e.nextElement();
94                  sb.append("\n\t").append(key).append("=").append(isPW(key) ? "<password suppressed>" : ics.GetVar(key));
95              }
96              sb.append("\nSession Variables:");
97              e = ics.GetSSVars();
98              while (e.hasMoreElements()) {
99                  String key = (String) e.nextElement();
100                 sb.append("\n\t").append(key).append("=")
101                         .append(isPW(key) ? "<password suppressed>" : ics.GetSSVar(key));
102             }
103             LOG.trace(sb);
104         }
105         String s = ics.runTag(tagName, list);
106         if (LOG.isTraceEnabled()) {
107             StringBuffer sb = new StringBuffer("Just completed execution of runTag for tag: " + tagName + ".  ");
108             sb.append("\nInput param list:");
109             for (Object k : list.keySet()) {
110                 String key = (String) k;
111                 sb.append("\n\t").append(key).append("=")
112                         .append(isPW(key) ? "<password suppressed>" : list.getValString(key));
113             }
114             sb.append("\nVariables:");
115             Enumeration<?> e = ics.GetVars();
116             while (e.hasMoreElements()) {
117                 String key = (String) e.nextElement();
118                 sb.append("\n\t").append(key).append("=").append(isPW(key) ? "<password suppressed>" : ics.GetVar(key));
119             }
120             sb.append("\nSession Variables:");
121             e = ics.GetSSVars();
122             while (e.hasMoreElements()) {
123                 String key = (String) e.nextElement();
124                 sb.append("\n\t").append(key).append("=")
125                         .append(isPW(key) ? "<password suppressed>" : ics.GetSSVar(key));
126             }
127             LOG.trace(sb);
128         }
129         postExecute(ics);
130         if (ics.GetErrno() != 0) {
131             this.handleError(ics);
132         }
133         return s;
134     }
135 
136     private static final boolean isPW(String key) {
137         return key != null && key.toLowerCase().contains("password");
138     }
139 
140     /**
141      * Template method that is called before ics.runTag
142      * <p/>
143      * subclasses can override
144      * 
145      * @param ics
146      */
147 
148     protected void preExecute(ICS ics) {
149 
150     }
151 
152     /**
153      * Template method that is called after ics.runTag and before handleError
154      * <p/>
155      * subclasses can override
156      * 
157      * @param ics
158      */
159     protected void postExecute(ICS ics) {
160 
161     }
162 
163     /**
164      * default error handling method. This implemetation throws an
165      * TagRunnerRuntimeException.
166      * <p/>
167      * subclasses can override
168      * 
169      * @param ics
170      * @see TagRunnerRuntimeException
171      */
172     protected void handleError(ICS ics) {
173         throw new TagRunnerRuntimeException("ics.runTag(" + tagName + ") returned an errno.", ics.GetErrno(), list,
174                 ics.getComplexError(), ics.GetVar("pagename"), ics.ResolveVariables("CS.elementname"));
175 
176     }
177 }