View Javadoc

1   /*
2    * WebFlow Navigation Manager: webflow definiton, server side navigation history and automatic session cleaning.
3    * Distributed under LGPL license at web site http://wfnm.sourceforge.net .
4    */
5   package net.sf.wfnm.web.taglib;
6   
7   import net.sf.wfnm.AttributeContainer;
8   import net.sf.wfnm.Config;
9   import net.sf.wfnm.NavigationManager;
10  import net.sf.wfnm.NavigationManagerFactory;
11  import net.sf.wfnm.Page;
12  import net.sf.wfnm.Webflow;
13  import net.sf.wfnm.web.HttpSessionWrapper;
14  
15  import java.io.IOException;
16  
17  import java.util.Enumeration;
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.Stack;
23  import java.util.StringTokenizer;
24  
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  
29  /***
30   * A tag that display the status of the framework.
31   *
32   * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
33   * @version 1.0.6
34   */
35  public class StatusTag extends TagSupport {
36      /*** 
37       * The default webflows background colors
38       */
39      private static String[] DEFAULT_COLORS = new String[] { "#CFFAFA", "#FAFA9A", "#FACF9A", "#DAFADA" };
40  
41      /*** 
42       * The map that bind a webflow name with a background color
43       */
44      private Map webflow2color = new HashMap();
45  
46      /*** 
47       * The page color
48       */
49      private String pageColor;
50  
51      /*** 
52       * The height of the status table
53       */
54      private int height;
55  
56      /*** 
57       * The width of the status table
58       */
59      private int width;
60  
61      /***
62       * Set the height of the status table
63       *
64       * @param height The height to set.
65       */
66      public void setHeight(int height) {
67          this.height = height;
68      }
69  
70      /***
71       * Sets the page color
72       *
73       * @param pageColor The pageColor to set.
74       */
75      public void setPageColor(String pageColor) {
76          this.pageColor = pageColor;
77      }
78  
79      /***
80       * Sets the colors of webflows background
81       *
82       * @param colors The colors to set in the form. For example 'A=#CFFAFA; B=#FAFA9A; C=#FACF9A; D=#DAFADA'
83       */
84      public void setWebflowColors(String colors) {
85          StringTokenizer strTok = new StringTokenizer(colors, " ,;");
86  
87          while (strTok.hasMoreTokens()) {
88              String token = strTok.nextToken();
89              int pos = token.indexOf('=');
90  
91              if (pos > 0) {
92                  String webflow = token.substring(0, pos);
93                  String color = token.substring(pos + 1);
94                  webflow2color.put(webflow, color);
95              }
96          }
97      }
98  
99      /***
100      * Set the width of the status table
101      *
102      * @param width The width to set.
103      */
104     public void setWidth(int width) {
105         this.width = width;
106     }
107 
108     /***
109      * Implements the start of the tag
110      *
111      * @return always SKIP_BODY in order to skip the body
112      *
113      * @throws JspException if something fails
114      */
115     public int doStartTag() throws JspException {
116         StringBuffer buffer1 = new StringBuffer();
117         buffer1.append("<table width='");
118         buffer1.append(String.valueOf(width));
119         buffer1.append("' bgcolor='#C0C0C0' ");
120         buffer1.append("cellSpacing='0' cellPadding='0' border='1' bordercolor='#000000'>\n");
121 
122         if (!Config.getInstance().isEnabled()) {
123             buffer1.append("<tr><td bordercolor='#C0C0C0' align='center'>\n");
124             buffer1.append("Framework disabled\n");
125         } else {
126             AttributeContainer ac = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(pageContext.getSession());
127             NavigationManager manager = NavigationManagerFactory.getInstance(ac);
128 
129             Set sessionSet = new HashSet();
130 
131             for (Enumeration e = pageContext.getSession().getAttributeNames(); e.hasMoreElements();) {
132                 String key = (String) e.nextElement();
133                 sessionSet.add(key);
134             }
135 
136             buffer1.append("<tr valign='bottom'><td bordercolor='#C0C0C0' align='center'>\n");
137             buffer1.append("<table width='100%' cellSpacing='1' cellPadding='1' bgcolor='#C0C0C0'>\n");
138 
139             StringBuffer buffer2 = new StringBuffer();
140             int count = 1;
141 
142             Stack webflowStack = manager.getWebflowStack();
143 
144             for (int i = webflowStack.size() - 1; i >= 0; i--) {
145                 count++;
146 
147                 Webflow webflow = (Webflow) webflowStack.elementAt(i);
148                 String wfBgColor = getColor(webflow.getName(), i);
149 
150                 buffer2.append("<tr valign='bottom'><td>\n");
151                 buffer2.append("<table bgcolor='");
152                 buffer2.append(wfBgColor);
153                 buffer2.append("' width='100%' cellSpacing='0' cellPadding='0' border='1' bordercolor='#C0C0C0'>\n");
154                 buffer2.append("<tr><td align='center' bordercolor='#000000'>\n");
155 
156                 buffer2.append("<table width='100%' cellSpacing='2' cellPadding='2' border='1' bordercolor='");
157                 buffer2.append(wfBgColor);
158                 buffer2.append("' >\n");
159 
160                 Stack pageStack = webflow.getPageStack();
161 
162                 for (int j = pageStack.size() - 1; j >= 0; j--) {
163                     count++;
164 
165                     Page page = (Page) pageStack.elementAt(j);
166 
167                     buffer2.append("<tr><td align='center' bgcolor='");
168                     buffer2.append(pageColor);
169                     buffer2.append("' bordercolor='#000000' >\n");
170                     buffer2.append("Page <b>");
171                     buffer2.append(page.getUrl());
172                     buffer2.append("</b> owns: ");
173                     buffer2.append(page.getOwnedObjectSet().toString());
174                     buffer2.append("\n");
175                     buffer2.append("<tr><td>\n");
176 
177                     sessionSet.removeAll(page.getOwnedObjectSet());
178                 }
179 
180                 buffer2.append("</table>\n");
181 
182                 buffer2.append("Webflow <b>");
183                 buffer2.append(webflow.getName());
184                 buffer2.append("</b> owns: ");
185                 buffer2.append(webflow.getOwnedObjectSet().toString());
186                 buffer2.append("\n");
187                 buffer2.append("</tr></td>\n");
188                 buffer2.append("</table>\n");
189                 buffer2.append("</tr></td>\n");
190 
191                 sessionSet.removeAll(webflow.getOwnedObjectSet());
192             }
193 
194             int emptyHeight = this.height - (count * 33);
195 
196             if (emptyHeight > 0) {
197                 buffer1.append("<tr valign='bottom'><td>\n");
198                 buffer1.append("<table width='100%' height='");
199                 buffer1.append(emptyHeight);
200                 buffer1.append("'>\n");
201                 buffer1.append("<tr><td>&nbsp;</tr></td>\n");
202                 buffer1.append("</table>\n");
203                 buffer1.append("</tr></td>\n");
204             }
205 
206             buffer1.append(buffer2);
207 
208             buffer1.append("</table>\n");
209             buffer1.append("</tr></td>\n");
210             buffer1.append("<tr valign='bottom'><td align='center' bordercolor='#C0C0C0'>\n");
211             buffer1.append("<b>Session</b> owns: ");
212             buffer1.append(sessionSet.toString());
213             buffer1.append("\n");
214         }
215 
216         buffer1.append("</tr></td>\n");
217         buffer1.append("</table>\n");
218 
219         try {
220             pageContext.getOut().print(buffer1.toString());
221         } catch (IOException ioe) {
222             throw new JspException("Unable to print out the status", ioe);
223         }
224 
225         return SKIP_BODY;
226     }
227 
228     /***
229      * Return the background color of a webflow
230      *
231      * @param webflowName the webflow name
232      * @param index the inxex of the webflow
233      *
234      * @return the background color of the webflow
235      */
236     private String getColor(String webflowName, int index) {
237         String color = (String) webflow2color.get(webflowName);
238 
239         if (color == null) {
240             color = DEFAULT_COLORS[index % DEFAULT_COLORS.length];
241         }
242 
243         return color;
244     }
245 }