Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gsoft/xervlet/BaseXervlet.java


1   /*************************************************************************
2    Copyright (C) 2003  Steve Gee
3    stevesgee@cox.net
4   
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License
7    as published by the Free Software Foundation; either version 2
8    of the License, or (at your option) any later version.
9   
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *************************************************************************/
19  
20  package gsoft.xervlet;
21  
22  import javax.servlet.http.*;
23  import java.io.*;
24  import java.util.*;
25  
26  public abstract class BaseXervlet
27          extends HttpServlet {
28  
29      private String INIFILE;
30      private ServletSettings iniHash;
31      private TagReader tReader;
32      private PrintWriter out;
33      private long lastFileTime = 0l;
34      private File iniFile = null;
35      private boolean resetIHash = false;
36  
37  //-- The controlling abstract methods for obtaining configuration settings
38      public abstract String getIniFileName();
39      public abstract String getHashMap();
40  
41      private void construct() {
42          try {
43              INIFILE = getInitParameter("inifile");
44              if (INIFILE == null) {
45                  INIFILE = getIniFileName();
46              } //end if
47  
48              iniFile = new File(INIFILE);
49              long thisFileTime = iniFile.lastModified();
50              if (thisFileTime > lastFileTime) {
51                  tReader = new TagReader(INIFILE);
52                  resetIHash = true;
53                  lastFileTime = thisFileTime;
54              }
55          } catch (Exception e) {
56              System.out.println("ErrorReadingInFileTypes::BaseXervlet ::construct:: " + e);
57              e.printStackTrace();
58          } //end try-catch
59      } //end construct
60  
61      public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
62          out = res.getWriter();
63          HttpSession session = req.getSession(true);
64          this.construct();
65  
66  /********** LOAD SESSION DATA ************/
67          if (session != null) {
68              String className = "";
69  /************ load inihash settings ************/
70              if (session.isNew()) {
71                  iniHash = new ServletSettings(session);
72                  iniHash.setTags(tReader);
73                  iniHash.setResponse(res);
74                  iniHash.setRequest(req);
75                  session.setAttribute("INIHASH", iniHash);
76              } else {
77                  iniHash = (ServletSettings) session.getAttribute("INIHASH");
78                  if (resetIHash) { iniHash.setTags(tReader); }
79              } //end newSession
80  
81  /*************************************************************************/
82  /***************** Load the Keys and Parameters ******************/
83  /*************************************************************************/
84              Enumeration values = req.getParameterNames();
85              Hashtable params = new Hashtable();
86              String key = null;
87              boolean noKeyFound = true;
88              while (values.hasMoreElements()) {
89                  key = (String) values.nextElement();
90                  params.put(key, req.getParameterValues(key));
91                  if (key.startsWith("comm") && noKeyFound) {
92                      noKeyFound = false;
93                      params.put("$COMMAND%KEY$", key);
94                      String cname = key.substring(4);
95                      try {
96                          className = tReader.getTagValue("execute", cname);
97                      } catch (Exception missingClassnameException) {
98                          res.setContentType("text/html");
99                          out.println("<font color=red><h3><center>An error has occured loading server settings:USEDBSOURCE::<br>" +
100                                 missingClassnameException.toString() + "</center></h3>");
101                     } //end try-catch for boolean serversettings
102                 }
103             } //end while
104             session.setAttribute("PARAMS", params);
105             /***********************************************************************/
106 //************* Load the class and send it off ******************
107             try {
108                 Xervlet s = (Xervlet) Class.forName(className).newInstance();
109                 if (s == null) {
110                     System.out.println("ServletCommand is NULL");
111                 }
112                 s.init(req, res);
113             } catch (Exception e) {
114                 res.setContentType("text/html");
115                 out.println("<p>Error Loading Classfile in BaseXervlet :: " + e);
116                 out.println("<br>Last parameter read in was: " + key);
117                 try {
118                     out.println("<p>Could not instantiate: " + className);
119                 } catch (Exception ex) {
120                 }
121                 try {
122                     System.out.println("Could not instantiate: " + className);
123                 } catch (Exception ex) {
124                 }
125                 System.out.println("There has been an Error dynamically initializing the object: " + e);
126                 e.printStackTrace();
127             } //end try-catch
128         } else {
129             if (out == null) {
130                 out = res.getWriter();
131             }
132             res.setContentType("text/html");
133             out.println(
134                     "<font color=red><h3><center>Warning -- Your session has been expired</h3>"
135                     + "<b>There are a number of reasons that may cause this to happen"
136                     + "<br>1) The session time limit has expired"
137                     + "<br>2) The has been a network error<b></center>");
138         } //end if-else
139 
140         try {
141             out.close();
142         } catch (Exception ex) {
143         }
144 
145     } //end service
146 }