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