KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > pages > FlowController


1 package org.ashkelon.pages;
2
3 import org.ashkelon.db.*;
4 import org.ashkelon.util.*;
5 import org.ashkelon.*;
6
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10 import java.util.*;
11 import java.sql.*;
12 import java.io.*;
13 import java.beans.*;
14
15 import org.apache.oro.text.perl.*;
16 import org.apache.oro.text.regex.*;
17
18
19 /**
20  * @author Eitan Suez
21  */

22 public class FlowController extends HttpServlet
23 {
24    private String JavaDoc errorPage;
25    private String JavaDoc cmdNotFoundPage;
26    private ConfigInfo configInfo;
27    
28    private DBMgr dbmgr;
29    
30    private Perl5Util perlutil;
31    private Logger log = Logger.getInstance();
32    
33    private Map classPool;
34    
35    
36    public String JavaDoc getServletInfo() { return "Ashkelon Controller Servlet"; }
37    
38    public void init() throws ServletException
39    {
40       errorPage = "/error.jsp";
41       cmdNotFoundPage = "/cmd_not_found.jsp";
42       
43       try
44       {
45          configInfo = (new ConfigInfo()).load();
46       }
47       catch (Exception JavaDoc ex)
48       {
49          throw new ServletException("Unable to load config information", ex);
50       }
51       
52       dbmgr = DBMgr.getInstance();
53       dbmgr.setWebApp(getServletContext());
54          
55       log.setTraceLevel(configInfo.getTraceLevel());
56       String JavaDoc traceFile = "trace.log";
57       String JavaDoc configTraceFile = configInfo.getTraceFile();
58       if(!StringUtils.isBlank(configTraceFile))
59       {
60          try
61          {
62             if (new File(configTraceFile).canWrite())
63                traceFile = configTraceFile;
64          }
65          catch (Exception JavaDoc ex)
66          {
67            log.error("benign exception: "+ex.getMessage());
68          }
69       }
70       
71       try
72       {
73          PrintWriter writer = new PrintWriter(new FileWriter(traceFile, true));
74          log.setWriter(writer);
75       }
76       catch (IOException ex) { }
77       
78       String JavaDoc sourcepath = getServletContext().getInitParameter("sourcepath");
79       log.traceln("retrieving source path: "+sourcepath);
80       getServletContext().setAttribute("sourcepath", getPaths(sourcepath));
81       
82       perlutil = new Perl5Util();
83       
84       classPool = new HashMap(30);
85    }
86    
87    public void doGet(HttpServletRequest request, HttpServletResponse response)
88    throws ServletException, java.io.IOException JavaDoc
89    {
90       doPost(request, response);
91    }
92    
93    public void doPost(HttpServletRequest request, HttpServletResponse response)
94          throws ServletException, java.io.IOException JavaDoc
95    {
96       log.setPrefix("FlowController");
97
98       String JavaDoc cmd = ServletUtils.getCommand(request, configInfo.getDefaultCmd());
99       request.setAttribute("cmd", cmd);
100
101       Connection conn = null;
102       try
103       {
104          conn = dbmgr.getConnection();
105       }
106       catch (SQLException ex)
107       {
108          if (conn != null) dbmgr.releaseConnection(conn);
109          DBUtils.logSQLException(ex);
110          
111          request.setAttribute("javax.servlet.jsp.jspException", ex);
112          RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage);
113          rd.forward(request, response);
114          return;
115       }
116       
117       CommandInfo cmdInfo;
118       String JavaDoc pageName, className;
119       int numRedirects = 0;
120       while (!StringUtils.isBlank(cmd) && numRedirects < 3)
121       {
122          cmdInfo = (CommandInfo) configInfo.getCommandMap().get(cmd);
123          
124          if (cmdInfo == null) // not found
125
{
126             dbmgr.releaseConnection(conn);
127             RequestDispatcher rd = getServletContext().getRequestDispatcher(cmdNotFoundPage);
128             rd.forward(request, response);
129             return;
130          }
131          
132          String JavaDoc jspPathPrefix = getJspPathPrefix(request);
133          pageName = "/" + jspPathPrefix + cmdInfo.getPageName();
134          log.debug("page name: "+pageName);
135          className = configInfo.getDefaultPkg() + "." + cmdInfo.getClassName();
136          
137          try
138          {
139             Page page = (Page) classPool.get(className);
140             if (page == null)
141             {
142                page = (Page) (Class.forName(className).newInstance());
143                classPool.put(className, page);
144             }
145             
146             page.setConnection(conn);
147             page.setRequest(request);
148             page.setApplication(getServletContext());
149             
150             log.verbose("Received command: "+cmd);
151             cmd = page.handleRequest();
152             if (StringUtils.isBlank(cmd))
153             {
154                updateTrail(request, cmdInfo);
155                
156                dbmgr.releaseConnection(conn);
157
158                RequestDispatcher rd = getServletContext().getRequestDispatcher(pageName);
159                rd.forward(request, response);
160                return;
161             }
162             
163             numRedirects++;
164          }
165          catch (Exception JavaDoc ex)
166          {
167             dbmgr.releaseConnection(conn);
168             if (ex instanceof SQLException)
169             {
170                DBUtils.logSQLException((SQLException) ex);
171                dbmgr.resetConnections();
172             }
173             else
174             {
175                log.error("Exception: "+ex.getMessage());
176             }
177             
178             request.setAttribute("javax.servlet.jsp.jspException", ex);
179             RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage);
180             rd.forward(request, response);
181             return;
182          }
183       }
184       
185       dbmgr.releaseConnection(conn);
186
187       request.setAttribute("javax.servlet.jsp.jspException", new Exception JavaDoc("FlowController Says: Too many internal redirects"));
188       RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage);
189       rd.forward(request, response);
190       return;
191    }
192    
193    private void updateTrail(HttpServletRequest request, CommandInfo cmdInfo) throws SQLException
194    {
195       HttpSession session = request.getSession();
196       LinkedList trail = (LinkedList) session.getAttribute("trail");
197       if (trail == null)
198       {
199          trail = new LinkedList();
200          session.setAttribute("trail", trail);
201       }
202
203       if (!cmdInfo.isInTrail()) return;
204       
205       String JavaDoc[] uriLabelPair = new String JavaDoc[3];
206       
207       String JavaDoc uri = request.getRequestURI();
208       String JavaDoc queryString = ServletUtils.getQueryString(request);
209       
210       if (queryString != null)
211          uri += "?" + queryString;
212
213       String JavaDoc cmd = cmdInfo.getCommand();
214       String JavaDoc[] cmdparts = StringUtils.split(cmd, ".");
215       
216       uriLabelPair[0] = uri;
217       String JavaDoc caption = cmdInfo.getCaption();
218       String JavaDoc style = "";
219
220       String JavaDoc pattern = "m/(.*)\\$(\\w+)\\$(.*)/";
221       boolean matchsuccess = perlutil.match(pattern, caption);
222
223       if (matchsuccess && perlutil.groups() > 3)
224       {
225          log.debug("Match Length: "+perlutil.groups());
226          MatchResult mr = perlutil.getMatch();
227          String JavaDoc first = mr.group(1);
228          String JavaDoc field = mr.group(2);
229          String JavaDoc last = mr.group(3);
230          log.debug("Matched: 1: "+first+"; 2: "+field+"; 3: "+last);
231
232          if (cmdparts.length >= 1)
233          {
234             /*
235              * this is important to understand and relies entirely on convention:
236              * jsp request object must have same name as cmd's first part/
237              * e.g. if cmd is cls.xyz then request.getParameter("cls") must
238              * return class object in question, so that can use reflection
239              * to get class property such as getName (corresponding to a
240              * $NAME$ dyn string)
241              */

242             Object JavaDoc obj = request.getAttribute(cmdparts[0]);
243             if (obj == null)
244             {
245                // try this:
246
obj = request.getAttribute(cmdparts[0] + "_" + field.toLowerCase()); // e.g. cls_name
247
if (obj == null)
248                   caption = first + cmdparts[0] + last;
249                else
250                   caption = first + obj.toString() + last; // e.g. see cls.source jsp page
251
}
252             else if (obj instanceof JDoc)
253             {
254                String JavaDoc[] result = reflectionmagic((JDoc) obj, field.toLowerCase());
255                String JavaDoc dynvalue = result[0];
256                caption = first + dynvalue + last;
257                style = result[1];
258             }
259          } else
260          {
261             caption = first + field + last;
262          }
263          log.debug("caption is: "+caption);
264       }
265       
266       uriLabelPair[1] = caption;
267       uriLabelPair[2] = style;
268
269       if (!StringUtils.isBlank(caption))
270       {
271          if ( !trail.isEmpty() )
272          {
273             String JavaDoc[] last_pair = (String JavaDoc[]) trail.getLast();
274             String JavaDoc last_uri = last_pair[0];
275             if (!uri.equals(last_uri))
276             {
277                trail.addLast(uriLabelPair);
278                log.verbose("Added "+uriLabelPair[0]+" to trail");
279             }
280          }
281          else
282          {
283             trail.addLast(uriLabelPair);
284             log.verbose("Added "+uriLabelPair[0]+" to trail");
285          }
286
287          if (trail.size() > configInfo.getMaxTrailLength())
288             trail.removeFirst();
289       }
290
291       log.verbose("Updating trail");
292       session.setAttribute("trail", trail);
293    }
294    
295    // TODO: change name of this method to something meaningful
296
private String JavaDoc[] reflectionmagic(JDoc doc, String JavaDoc propname)
297    {
298       try
299       {
300          PropertyDescriptor d = new PropertyDescriptor(propname, doc.getClass());
301          Object JavaDoc value = d.getReadMethod().invoke(doc, null);
302          return new String JavaDoc[] {(String JavaDoc) value, doc.getStyle()};
303       }
304       catch (Exception JavaDoc ex)
305       {
306          log.error("failed to lookup value for property "+propname);
307          log.error(ex.getMessage());
308       }
309       return new String JavaDoc[] {"", ""};
310    }
311
312    /**
313     * @return either "basic" if browser does not support modern. returns "" otherwise.
314     * value is used as path prefix for jsp to fetch
315     *
316     * the calculation of browser type happens only when session is first created.
317     * afterwards stored in & retrieved from session object
318     */

319    private String JavaDoc getJspPathPrefix(HttpServletRequest request)
320    {
321       HttpSession session = request.getSession();
322       Object JavaDoc uiObj = (String JavaDoc) session.getAttribute("ui");
323       String JavaDoc ui = "";
324       log.debug("session ui value is "+uiObj);
325       if (uiObj == null)
326       {
327          String JavaDoc ua = request.getHeader("User-Agent");
328          if (ua == null) ua = "";
329          boolean new_explorer = ua.indexOf("MSIE 6.") > -1 || ua.indexOf("MSIE 5.") > -1;
330          boolean new_mozilla = ua.indexOf("Gecko") > -1;
331          if (new_explorer || new_mozilla) // don't yet serve xul for mozilla as it's
332
// far from being in a working state -- allow config page for manual
333
// switch to xul.
334
{
335             ui = "";
336          }
337          else
338          {
339             ui = "basic/";
340          }
341          session.setAttribute("ui", ui);
342       }
343       else
344       {
345          ui = (String JavaDoc) uiObj;
346       }
347       log.debug("decided to use ui "+ui);
348       return ui;
349    }
350
351
352    private List getPaths(String JavaDoc pathstring)
353    {
354       log.debug("path string is: "+pathstring);
355       String JavaDoc[] paths = StringUtils.split(pathstring, File.pathSeparator);
356       return Arrays.asList(paths);
357    }
358    
359    public void destroy() {}
360    
361 }
362
363
Popular Tags