KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > CofaxPage


1 /*
2  * CofaxPage 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  * You 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/CofaxPage.java,v 1.9.2.1 2006/12/11 16:28:44 fxrobin Exp $
21  */

22
23 package org.cofax;
24
25 import java.util.*;
26 import javax.servlet.http.*;
27
28 /**
29  * This is the container class of Cofax. CofaxPage contains data and metadata to
30  * be used within the Cofax framework, for example a result page's or template's
31  * data.
32  *
33  * @author Rajiv Pant
34  * @author Karl Martino
35  * @author Sam Cohen
36  * @author Lee Bolding
37  * @created April 19, 2002
38  */

39
40 public final class CofaxPage {
41
42     /** Storage for templating strings */
43     private StringBuffer JavaDoc contents = new StringBuffer JavaDoc();
44
45     public byte[] contentBytes = null;
46
47     /** Usually used for storing headers users added to templates */
48     private HashMap headers = new HashMap();
49
50     /** the URL which identified this page */
51     private String JavaDoc pageId;
52
53     /** FX : boolean to tell the cache system not to cache the result */
54     private boolean cacheThisPage = true;
55
56     /** a parameter to store an error message */
57     private String JavaDoc errorMsg = "";
58
59     /** system time stamp for last changed time */
60     private long lastModified;
61
62     /** system time stamp for last cached time */
63     private long cachedTime;
64
65     /** system time stamp for last time this page started to be built */
66     private long startBuildTime;
67
68     /** system time stamp for the time taken to build this page */
69     private long endBuildTime;
70
71     /** flag for whether this page is in cache or not */
72     private boolean isCached;
73
74     /**
75      * Used to keep track of how often this object has been accessed. Typically
76      * from CofaxCache
77      */

78     private int hitCount = 0;
79
80     /**
81      * A code to signify the status of the object. commonly used to store HTTP
82      * codes
83      */

84     private int status;
85
86     /** A Glossary object that normally contains request data. */
87     private Glossary glossary;
88
89     /** Used to locate templates */
90     private TemplateLoader templateLoader;
91
92     /** Used to connect/disconnect and utilize a Cofax DataStore object */
93     private DataStore dataStore;
94
95     /** Used to identify the page that is being served */
96     private String JavaDoc servingPage;
97
98     /** Used to identify the client IP address */
99     private String JavaDoc servingClient;
100
101     private final float SECOND = 1000;
102
103     private final float MINUTE = 60 * SECOND;
104
105     private final float HOUR = 60 * MINUTE;
106
107     private long Start;
108
109     /**
110      * Default constructor which initializes the status field to OK, resets the
111      * contents stringbuffer, starts the page build timer and the saves in the
112      * headers collection a Content-Type of "text/html"
113      */

114     public CofaxPage() {
115         // start fresh
116
contents.setLength(0);
117
118         Start = System.currentTimeMillis();
119         status = HttpServletResponse.SC_OK;
120         headers.put("Content-Type", "text/html");
121     }
122
123     /**
124      * Sets the DataStore object.
125      *
126      * @param dataStore
127      * The Cofax DataStore object.
128      */

129     public void setDataStore(DataStore dataStore) {
130         this.dataStore = dataStore;
131     }
132
133     /**
134      * Connects to the DataStore, if not connected, and returns the DataStore
135      * object.
136      *
137      * @return The DataStore object.
138      */

139     public DataStore getDataStore() {
140         if ((dataStore == null) || (!dataStore.isConnected())) {
141             dataStore.connect();
142         }
143         return dataStore;
144     }
145
146     /**
147      * Disconnects from the DataStore.
148      */

149     public void disconnectDataStore() {
150         dataStore.disConnect();
151     }
152
153     /**
154      * Sets the TemplateLoader object
155      *
156      * @param templateLoader
157      * The new templateLoader value
158      */

159     public void setTemplateLoader(TemplateLoader templateLoader) {
160         this.templateLoader = templateLoader;
161     }
162
163     /**
164      * Gets the TemplateLoader object
165      *
166      * @return The templateLoader value
167      */

168     public TemplateLoader getTemplateLoader() {
169         return templateLoader;
170     }
171
172     /**
173      * Sets the Glossary object
174      *
175      * @param glossary
176      * The new glossary value
177      */

178     public void setGlossary(Glossary glossary) {
179         this.glossary = glossary;
180     }
181
182     /**
183      * Gets the Glossary object
184      *
185      * @return The glossary object
186      */

187     public Glossary getGlossary() {
188         return glossary;
189     }
190
191     /**
192      * Adds a passed in HashMap to Glossary object
193      *
194      * @param newGlossaryValues
195      * The HashMap to add to the Glossary
196      */

197     public void addToGlossary(HashMap newGlossaryValues) {
198         glossary.addToGlossary(newGlossaryValues);
199         return;
200     }
201
202     /**
203      * Puts a single String value in the Glossary
204      *
205      * @param key
206      * The key to associate with the value.
207      * @param value
208      * The value.
209      */

210     public void putGlossaryValue(String JavaDoc key, String JavaDoc value) {
211         glossary.putString(key, value);
212     }
213
214     /**
215      * Gets a single String Glossary value
216      *
217      * @param key
218      * The key associated with the value.
219      * @return The glossary value
220      */

221     public String JavaDoc getGlossaryValue(String JavaDoc key) {
222         return glossary.getString(key);
223     }
224
225     /**
226      * Applies the Glossary to a string. Replaces key names, pre and post fixed
227      * with backtics, with Glossary values.
228      *
229      * @param inString
230      * The string with embedded glossary keys to be replaced with
231      * values.
232      * @return The parsed string.
233      */

234     public String JavaDoc applyGlossary(String JavaDoc inString) {
235         return glossary.applyGlossary(inString) + "";
236     }
237
238     /**
239      * Applies a passed in HashMap to a string. Replaces key names, pre and post
240      * fixed with backtics, with he HashMap's values.
241      *
242      * @param inString
243      * The string with embedded HashMap keys to be replaced by
244      * values.
245      * @param params
246      * The HashMap to utilize.
247      * @return The parsed string.
248      */

249     public static String JavaDoc applyGlossaryWithHashMap(String JavaDoc inString, HashMap params) {
250         Glossary tmpGlossary = new Glossary();
251         tmpGlossary.setKeyValues(params);
252         return tmpGlossary.applyGlossary(inString) + "";
253     }
254
255     /**
256      * Calls the Glossary's printGlossary() routine.
257      *
258      * @return A string containing an output of the glossary's keys and values.
259      */

260     public String JavaDoc printGlossary() {
261         return glossary.printGlossary();
262     }
263
264     /**
265      * Prints the Glossary where key item start with specific pattern name.
266      *
267      * @param startWithName
268      * The key to start printing from.
269      * @return A string containing an output of the glossary's keys and values.
270      */

271     public String JavaDoc printGlossary(String JavaDoc startWithName) {
272         return glossary.printGlossary(startWithName);
273     }
274
275     /**
276      * Adds a header to the header Collection.
277      *
278      * @param headerName
279      * The header name.
280      * @param headerValue
281      * The header value.
282      */

283     public void putHeader(String JavaDoc headerName, String JavaDoc headerValue) {
284         headers.put(headerName, headerValue + "");
285     }
286
287     /**
288      * Gets a header from the header Collection.
289      *
290      * @param headerName
291      * The header name.
292      * @return The header value.
293      */

294     public String JavaDoc getHeader(String JavaDoc headerName) {
295         return headers.get(headerName) + "";
296     }
297
298     /**
299      * Returns the headers Collection.
300      *
301      * @return The headers Collection.
302      */

303     public HashMap getHeaders() {
304         return headers;
305     }
306
307     /**
308      * Verifies whether a header exists.
309      *
310      * @param headerName
311      * The header name.
312      * @return True if the header exists, false if the header does not.
313      */

314     public boolean doesHeaderExist(String JavaDoc headerName) {
315         if (headers.containsKey(headerName)) {
316             return true;
317         } else {
318             return false;
319         }
320     }
321
322     /**
323      * Set's the last modified time.
324      *
325      * @param theTime
326      * The new lastModified value
327      */

328     public void setLastModified(long theTime) {
329         lastModified = theTime;
330     }
331
332     /**
333      * Get's the last modified time.
334      *
335      * @return The lastModified value
336      */

337     public long getLastModified() {
338         return lastModified;
339     }
340
341     // Developer note: toString and getContents do the SAME THING.
342
// This is for java bean compatibility
343
/**
344      * Get's the content's field's data.
345      *
346      * @return The contents field's value.
347      */

348     public String JavaDoc toString() {
349         return contents.toString();
350     }
351
352     /**
353      * Gets the content field's data.
354      *
355      * @return The contents field's value.
356      */

357     public String JavaDoc getContents() {
358         return contents.toString();
359     }
360
361     /**
362      * Gets the lenght of the content field.
363      *
364      * @return The length of the content field.
365      */

366     public int getContentsLength() {
367         return contents.length();
368     }
369
370     /**
371      * Appends to the content field.
372      *
373      * @param s
374      * The string to be appended to the content field
375      */

376     public void append(String JavaDoc s) {
377         contents.append(s);
378     }
379
380     /**
381      * Sets this page's status
382      *
383      * @param status
384      * The new status value
385      */

386     public void setStatus(int status) {
387         this.status = status;
388     }
389
390     /**
391      * Gets the status.
392      *
393      * @return The status value
394      */

395     public int getStatus() {
396         return status;
397     }
398
399     /**
400      * Sets the error message.
401      *
402      * @param errorMsg
403      * The new errorMsg value
404      */

405     public void setErrorMsg(String JavaDoc errorMsg) {
406         this.errorMsg = this.errorMsg + "\n" + errorMsg;
407     }
408
409     /**
410      * Gets the error message.
411      *
412      * @return The errorMsg value
413      */

414     public String JavaDoc getErrorMsg() {
415         return errorMsg;
416     }
417
418     /**
419      * Resets the length of page's contents.
420      */

421     public void reset() {
422         contents.setLength(0);
423         contents = new StringBuffer JavaDoc(); // fx : new Object for clean operations
424
contentBytes = null; // fx : deleting the bytes in memory
425
}
426
427     /**
428      * Converts the contents StringBuffer to bytes
429      */

430     public void bytes() {
431         contentBytes = contents.toString().getBytes();
432         contents.setLength(0);
433     }
434
435     /**
436      * Sets the hit count.
437      *
438      * @param value
439      * The new hitCount value
440      * @return Description of the Return Value
441      */

442     public boolean setHitCount(int value) {
443         this.hitCount = value;
444         return true;
445     }
446
447     /**
448      * Gets the hit count.
449      *
450      * @return The hitCount value
451      */

452     public int getHitCount() {
453         return hitCount;
454     }
455
456     /**
457      * Sets whether this page is cached or not
458      *
459      * @param status
460      * The new pageCached value
461      * @return Description of the Return Value
462      */

463     public boolean setPageCached(boolean status) {
464         this.isCached = status;
465         return true;
466     }
467
468     /**
469      * Tell's whether this page is in cache memory or not. This is normally
470      * flagged by CofaxCache. Notes: whether this page thinks it is in cache or
471      * not is up to this page object but the caching class actualy has to
472      * manipulate it in and out of cache
473      *
474      * @return The pageCached value
475      */

476     public boolean isPageCached() {
477         return this.isCached;
478     }
479
480     /**
481      * Sets the time that this page was cached
482      *
483      * @param time
484      * The new cachedTime value
485      * @return Always returns true
486      */

487     public boolean setCachedTime(long time) {
488         this.cachedTime = time;
489         return true;
490     }
491
492     /**
493      * Adds one to the number of times this page has been hit
494      */

495     public void incrementHitCount() {
496         this.hitCount++;
497     }
498
499     public String JavaDoc cacheTime(long requestStart) {
500         String JavaDoc procTime = processTime(this.endBuildTime - requestStart);
501         String JavaDoc buildString = "<!-- Served from Cache in : " + procTime + " -->\r\n";
502         return buildString;
503     }
504
505     public String JavaDoc buildTime(long requestStart) {
506         String JavaDoc procTime = processTime(this.endBuildTime - requestStart);
507         String JavaDoc buildString = "<!-- Page Built in : " + procTime + " -->\r\n";
508         return buildString;
509     }
510
511     public String JavaDoc pageAge() {
512         long End = System.currentTimeMillis();
513         String JavaDoc procTime = processTime(End - Start);
514         String JavaDoc ageString = "<!-- Page Built : " + procTime + " ago -->\r\n";
515         return ageString;
516     }
517
518     /**
519      * Sets the time that this is starting to be built
520      *
521      * @return Description of the Return Value
522      */

523
524     public void setStartBuildTime() {
525         this.startBuildTime = System.currentTimeMillis();
526         return;
527     }
528
529     public void setEndBuildTime() {
530         this.endBuildTime = System.currentTimeMillis();
531         return;
532     }
533
534     /**
535      * Gets the time that this is starting to be built
536      *
537      * @return The startBuildTime value
538      */

539     public long getStartBuildTime() {
540
541         return this.startBuildTime;
542     }
543
544     public long getEndBuildTime() {
545
546         return this.endBuildTime;
547     }
548
549     /**
550      * Sets the servingPage attribute
551      *
552      * @param inServingPage
553      * The new servingPage value
554      */

555     public void setServingPage(String JavaDoc inServingPage) {
556         servingPage = inServingPage;
557     }
558
559     /**
560      * Gets the servingPage attribute.
561      *
562      * @return The servingPage value
563      */

564     public String JavaDoc getServingPage() {
565         return servingPage;
566     }
567
568     /**
569      * Sets the servingClient attribute
570      *
571      * @param inServingClient
572      * The new servingClient value
573      */

574     public void setServingClient(String JavaDoc inServingClient) {
575         servingClient = inServingClient;
576     }
577
578     /**
579      * Gets the servingClient attribute of the WysiwygTemplate object
580      *
581      * @return The servingClient value
582      */

583     public String JavaDoc getServingClient() {
584         return servingClient;
585     }
586
587     public String JavaDoc processTime(long time) {
588         StringBuffer JavaDoc timeString = new StringBuffer JavaDoc();
589
590         if (time <= SECOND) {
591             timeString.append(time);
592             timeString.append(" ms");
593         } else if (time <= MINUTE) {
594             timeString.append(time / SECOND);
595             timeString.append(" secs");
596         } else if (time <= HOUR) {
597             timeString.append(time / MINUTE);
598             timeString.append(" mins");
599         } else {
600             timeString.append(time / HOUR);
601             timeString.append(" hours");
602         }
603
604         return timeString.toString();
605     }
606
607     // ** FX : methods to access the boolean flag about cacing the pages
608
public void putInCache(boolean c) {
609         this.cacheThisPage = c;
610     }
611
612     public boolean mustBeCached() {
613         return this.cacheThisPage;
614     }
615
616     // FX : getter and setter for pageId
617
public void setPageId(String JavaDoc pid) {
618         pageId = pid;
619     }
620
621     public String JavaDoc getPageId() {
622         return pageId;
623     }
624 }
625 // end class CofaxPage
626

627
Popular Tags