KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsServlet


1 /*
2  * CofaxToolsServlet is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * 7You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsServlet.java,v 1.36.2.1 2006/12/11 16:28:22 fxrobin Exp $
21  */

22
23 package org.cofax.cms;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39
40 import org.cofax.DataStore;
41 import org.cofax.XMLConfig;
42 import org.cofax.cms.login.ILoginHandler;
43
44 /**
45  * Cofax Tools Servlet: Main driver for Cofax Tools. Initializes connection
46  * pooling, loads templates, loads package tags, then collects all queries for
47  * proper distribution and processing.
48  *
49  * @author Charles Harvey
50  * @author Hung Dao
51  * @created April 29, 2002
52  * @version 1.8
53  */

54
55 public class CofaxToolsServlet extends HttpServlet JavaDoc {
56     // todo - hack - remove
57
/**
58      * Description of the Field
59      */

60     public static String JavaDoc toolsPath ;
61
62     public static String JavaDoc staticFolder;
63
64     // FX :
65
public static String JavaDoc articleRessourceFolder;
66
67     public static String JavaDoc userImageFolder;
68
69     public static HashMap JavaDoc configGlossary = new HashMap JavaDoc();
70
71     // location of the xml config file for tools
72
static XMLConfig configFile;
73
74     // server context path
75
static String JavaDoc contextPath;
76     
77     // context url
78
public static String JavaDoc contextUrl;
79
80     // config location for datastore
81
static String JavaDoc configLocation;
82
83     // name of datastore
84
static String JavaDoc dataStoreName;
85
86     // datastore
87
static DataStore dataStore;
88
89     static String JavaDoc adminGroupID;
90
91     // path to the templates folder
92
public static String JavaDoc templatePath;
93
94     // if or if not to log
95
static String JavaDoc log;
96
97     // whether or not to write to disk or system
98
static String JavaDoc logLocation;
99
100     // max size in bits for log data
101
static long logMaxSize = 0;
102
103     // if or if not to log
104
static String JavaDoc dataLog;
105
106     // whether or not to write to disk or system
107
static String JavaDoc dataLogLocation;
108
109     // max size in bits for log data
110
static long dataLogMaxSize = 0;
111
112     // http string to append page location to when clearing cache
113
static String JavaDoc removePageCache;
114
115     // http string to append template location to when clearing cache
116
static String JavaDoc removeTemplateCache;
117
118     // name of dataStore to use
119
static String JavaDoc dataStoreClass;
120
121     // name of db config file
122
static String JavaDoc dbProperties;
123
124     // determine if there are additional config files
125
static String JavaDoc lookInContext;
126
127     // determine if we have already gotten the package tags from the database
128
static boolean hasPackageTags = false;
129
130     // Determine if the tools app is running on the 'beta server'
131
static boolean betaServer = false;
132
133     /**
134      * Location of folder with 777 permissions on this hard drive for writing
135      * temp during file transfers.
136      */

137     public static String JavaDoc fileTransferFolder;
138
139     /**
140      * Physical path to the servlet.
141      */

142     public String JavaDoc servletPath;
143
144     /**
145      * Path info for the servlet.
146      */

147     public String JavaDoc pathInfo;
148
149     /**
150      * Handle for any queryStrings passed.
151      */

152     public String JavaDoc queryString;
153
154     /**
155      * Used for host information.
156      */

157     public Object JavaDoc tmp;
158
159     /**
160      * Name of the host.
161      */

162     public String JavaDoc hostName;
163
164     /**
165      * Handle for user sessions.
166      */

167     public HttpSession JavaDoc session;
168
169     /**
170      * The html color that appears on highlighted tabs for tools pages.
171      */

172     public static String JavaDoc hilite = "CCCCCC";
173
174     /**
175      * The html body color for tools pages.
176      */

177     public static String JavaDoc bodyColor = "FFFFFF";
178
179     /**
180      * The html table color for tools pages.
181      */

182     public static String JavaDoc tableColor = "FFFFFF";
183
184     /**
185      * The html color that appears on tabs for tools pages.
186      */

187     public static String JavaDoc tabColor = "0066CC";
188
189     public static String JavaDoc adminEmail = "";
190
191     public static String JavaDoc aliasPath = "";
192
193     public static Locale JavaDoc lcl;
194
195     public static int numrows = 10;
196
197     public static String JavaDoc logFolder = "";
198
199     public static int uploadMaxSize = 2; // 2 Mo
200

201     /**
202      * Setup CofaxTools with database access upon startup and initialize all
203      * values. Load memory with all package tags, determine the status of
204      * logging, and load templates.
205      *
206      * @param config
207      * Description of the Parameter
208      * @exception ServletException
209      * Description of the Exception
210      */

211     public final void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
212         super.init(config);
213
214         // Get the servlet's context information.
215
ServletContext JavaDoc context = getServletContext();
216         contextPath = context.getRealPath("/");
217
218         // staticFolder is the location to save static resources
219
// to.
220
// fileTransferFolder is for temporary storage of files
221
// uploaded to the system. These files are moved to their
222
// appropriate folder within staticFolder.
223
// Sometimes fileTransferFolder will require cleanup.
224
staticFolder = contextPath + "static";
225         fileTransferFolder = staticFolder + File.separator + "tmp";
226
227         // Initializing default templates
228
templatePath = "/" + config.getInitParameter("templatePath"); // fx:
229
// modified
230

231         if (templatePath != null && !templatePath.endsWith("/"))
232             templatePath = templatePath + "/"; // FX : adding a backslash
233

234         // Initializing log path
235
logFolder = contextPath;
236         try {
237             logFolder = config.getInitParameter("logFolder");
238             if (!(logFolder.endsWith(File.separator)))
239                 logFolder += File.separator;
240         } catch (Exception JavaDoc e) {
241             logFolder = contextPath;
242             System.err.println("[WARNING] logFolder parameter not initialized. 'contextPath' value used instead");
243         }
244
245         // Initializing upload max size
246
try {
247             String JavaDoc strUploadMaxSize = config.getInitParameter("uploadMaxSize");
248             uploadMaxSize = Integer.parseInt(strUploadMaxSize);
249         } catch (Exception JavaDoc e) {
250             uploadMaxSize = 2;
251             System.err.println("[WARNING] uploadMaxSize parameter not initialized. Default value is 2Mo.");
252         }
253
254         adminGroupID = config.getInitParameter("adminGroupID");
255         removePageCache = config.getInitParameter("removePageCache");
256         removeTemplateCache = config.getInitParameter("removeTemplateCache");
257         log = config.getInitParameter("log");
258         logLocation = logFolder + "CofaxTools.log";
259
260         // FX : uploaded files are now set as parameters of the servlet
261
userImageFolder = config.getInitParameter("configGlossary:userImageFolder");
262         if (userImageFolder == null)
263             userImageFolder = "images";
264
265         articleRessourceFolder = config.getInitParameter("configGlossary:articleRessourceFolder");
266         if (articleRessourceFolder == null)
267             articleRessourceFolder = "images";
268
269         for (Enumeration JavaDoc params = config.getInitParameterNames(); params.hasMoreElements();) {
270             String JavaDoc paramName = (String JavaDoc) params.nextElement();
271             if (paramName.indexOf("configGlossary:") > -1) {
272                 configGlossary.put(paramName, config.getInitParameter(paramName));
273             }
274         }
275         // FX : end
276

277         if (!config.getInitParameter("logMaxSize").equals("")) {
278             logMaxSize = Long.parseLong(config.getInitParameter("logMaxSize"));
279         }
280         dataLog = config.getInitParameter("dataLog");
281         dataLogLocation = logFolder + "dataLog.log";
282
283         if (!config.getInitParameter("dataLogMaxSize").equals("")) {
284             logMaxSize = Long.parseLong(config.getInitParameter("dataLogMaxSize"));
285         }
286
287         dataStoreName = (String JavaDoc) getServletContext().getAttribute("dataStoreName");
288         dataStoreClass = (String JavaDoc) getServletContext().getAttribute("dataStoreClass");
289         dataStore = (DataStore) getServletContext().getAttribute("dataStore");
290
291         // get the login handler
292
CofaxToolsLogin.setLoginHandler((ILoginHandler) getServletContext().getAttribute("loginHandler"));
293
294         adminEmail = (String JavaDoc) getServletContext().getAttribute("adminEmail");
295         getServletContext().setAttribute("toolsPath", toolsPath);
296
297         // Init the thread of life cycle publications
298
try {
299             CofaxToolsLifeCycleThread th = new CofaxToolsLifeCycleThread();
300             th.setDataStore(dataStore);
301             th.setLogFile(logFolder + "CofaxToolsLifeCycle.log");
302             th.start();
303         } catch (Exception JavaDoc e) {
304             System.err.println("[ERROR] LifeCycleThread : Exception :" + e);
305         }
306
307     }
308
309     /**
310      * All navigation & page returns flow through Get. Sends all requests to
311      * CofaxToolNavigate where correct action is determined and results are
312      * returned.
313      *
314      * @param req
315      * Description of the Parameter
316      * @param res
317      * Description of the Parameter
318      * @exception ServletException
319      * Description of the Exception
320      * @exception IOException
321      * Description of the Exception
322      */

323     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
324
325         res.setContentType("text/html");
326
327         // get a data connection and fulfill the client's request
328
PrintWriter JavaDoc out = res.getWriter();
329         servletPath = req.getServletPath();
330         contextUrl = req.getContextPath(); // FX
331
pathInfo = req.getPathInfo();
332         aliasPath = req.getContextPath();
333         if (!(aliasPath.startsWith("/")))
334             aliasPath = "/" + aliasPath;
335         toolsPath = aliasPath + "/tools/";
336         CofaxToolsUtil.log("toolsPath :"+toolsPath);
337         lcl = req.getLocale();
338         queryString = req.getQueryString();
339         tmp = req.getHeader("host");
340         session = req.getSession(true);
341         String JavaDoc page = "";
342         DataStore db = null;
343
344         try {
345             db = (DataStore) (Class.forName(dataStoreClass)).newInstance();
346         } catch (ClassNotFoundException JavaDoc e) {
347             CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: Data store class not found: " + e);
348         } catch (InstantiationException JavaDoc e) {
349             CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR loading data store class.: " + e);
350         } catch (IllegalAccessException JavaDoc e) {
351             CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR loading data store class.: " + e);
352         }
353
354         if (db == null) {
355             CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR " + "instantiating dataStore: DataStore is null.");
356         }
357         // Fullfill the package tags hash so we don't have to get them from
358
// the database every time a call is made.
359
// Other than the search function (which is a build dynamic)
360
// this is the only place database calls appear in the code.
361
if (hasPackageTags == false) {
362             if (db.connect()) {
363                 hasPackageTags = true;
364             } else {
365                 if (db.getError() == true) {
366                     CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "can't connect to db " + dataStoreName + "at CofaxToolsServlet: "
367                             + db.getLastError());
368                 }
369             }
370
371             // instantiate logging for the DataStore
372
DataStore.dataLog = dataLog;
373             DataStore.dataLogLocation = dataLogLocation;
374             DataStore.dataLogMaxSize = dataLogMaxSize;
375
376             CofaxToolsDbUtils.st_selectPackagetags = db.selectPackagetags();
377             CofaxToolsDbUtils.st_selectPackagetagsCache = db.selectPackagetagsCache();
378
379             dataStore.setCache(CofaxToolsDbUtils.getValueValuePackageHash(db, CofaxToolsDbUtils.st_selectPackagetags, "TAG_NAME", "TAG_VALUE", true),
380                     DataStore.PACKAGE_TAG_PROCESSED);
381             dataStore.setCache(CofaxToolsDbUtils.getValueValuePackageHash(db, CofaxToolsDbUtils.st_selectPackagetagsCache, "TAG_VALUE", "CACHE", true),
382                     DataStore.PACKAGE_TAG_CACHE_COMMAND);
383
384             if (db.disConnect() != true) {
385                 CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "cannot disconnect from Servlet database " + dataStoreName);
386             }
387         }
388
389         if (db.connect()) {
390
391             try {
392                 page = CofaxToolsNavigation.navigate(db, pathInfo, queryString, session, req, res);
393             } catch (Exception JavaDoc e) {
394                 if (db.getError() == true)
395                   {
396                     page = "<html><body></pre>";
397                     page += "CofaxToolsServlet doGet ERROR: " + "occurred during a database transaction\n";
398                     page += db.getLastError();
399                     page += "<html><body></pre>";
400                     e.printStackTrace(System.err);
401                 } else {
402                     page = ("CofaxToolsServlet doGet ERROR : " + e + "<BR>Please report this error and the actions that led to it.");
403                     e.printStackTrace(System.err);
404                 }
405             } finally {
406                 if (db.disConnect() != true) {
407                     CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "cannot disconnect from Servlet database " + dataStoreName);
408                 }
409             }
410
411         } else {
412             CofaxToolsUtil.log("CofaxToolsServlet doGet ERROR: " + "can't connect to db " + dataStoreName + " at CofaxToolsServlet: " + db.getLastError());
413         }
414         boolean transferFolder = CofaxToolsUtil.checkFileTransferFolder(fileTransferFolder);
415         if (transferFolder == false) {
416             page = ("The admin of this servlet has not set the file transfer folder and "
417                     + "this servlet will not function correctly until it is set. Please have the admin read tools.xml "
418                     + "and set the appropriate folder with permissions of 777.");
419         }
420
421         out.println(page);
422
423     }
424
425     /**
426      * Sends all requests to Get.
427      *
428      * @param req
429      * Description of the Parameter
430      * @param res
431      * Description of the Parameter
432      * @exception ServletException
433      * Description of the Exception
434      * @exception IOException
435      * Description of the Exception
436      */

437     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
438         doGet(req, res);
439     }
440
441     /**
442      * Creates a database connection to be passed through all CofaxTools
443      * classes.
444      *
445      * @return Description of the Return Value
446      */

447     public static DataStore connect() {
448
449         // Create and initialize cofax database connection
450
DataStore db = null;
451
452         try {
453             db = (DataStore) (Class.forName(dataStoreClass)).newInstance();
454         } catch (ClassNotFoundException JavaDoc e) {
455             CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "Data store class not found.: " + e);
456         } catch (InstantiationException JavaDoc e) {
457             CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "loading data store class.: " + e);
458         } catch (IllegalAccessException JavaDoc e) {
459             CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "loading data store class.: " + e);
460         }
461
462         if (db == null) {
463             CofaxToolsUtil.log("CofaxToolsServlet connect ERROR: " + "Could not create DataStore object db.");
464         }
465         return db;
466     }
467
468 }
469
Popular Tags