KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > EventAwareGenerator


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.samples;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20
21 import org.apache.cocoon.ProcessingException;
22 import org.apache.cocoon.caching.validity.EventValidity;
23 import org.apache.cocoon.caching.validity.NamedEvent;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Request;
26 import org.apache.cocoon.generation.JXTemplateGenerator;
27 import org.apache.excalibur.source.SourceValidity;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * This is a sample generator to demonstrate the event aware caching.
32  * We simply extend the JXTG.
33  * @version $Id: EventAwareGenerator.java 155005 2005-02-23 13:02:09Z cziegeler $
34  */

35 public class EventAwareGenerator extends JXTemplateGenerator {
36
37     /**
38      * Generate the unique key for the cache.
39      *
40      * This key must be unique inside the space of this XSP page, it is used
41      * to find the page contents in the cache (if getValidity says that the
42      * contents are still valid).
43      *
44      * This method will be invoked before the getValidity() method.
45      *
46      * @return The generated key or null if the component
47      * is currently not cacheable.
48      */

49     public Serializable JavaDoc getKey() {
50         final Request request = ObjectModelHelper.getRequest(this.objectModel);
51         // for our test, pages having the same value of "pageKey" will share
52
// the same cache location
53
String JavaDoc key = request.getParameter("pageKey") ;
54         return ((key==null||"".equals(key)) ? "one" : key);
55     }
56
57     /**
58      * Generate the validity object, tells the cache how long to
59      * keep contents having this key around. In this case, it will
60      * be until an Event is retrieved matching the NamedEvent created below.
61      *
62      * Before this method can be invoked the getKey() method
63      * will be invoked.
64      *
65      * @return The generated validity object or null if the
66      * component is currently not cacheable.
67      */

68     public SourceValidity getValidity() {
69         final Request request = ObjectModelHelper.getRequest(this.objectModel);
70         String JavaDoc key = request.getParameter("pageKey") ;
71         return new EventValidity(
72                    new NamedEvent(
73                        (key==null||"".equals(key)) ? "one" : key));
74     }
75
76
77     /* (non-Javadoc)
78      * @see org.apache.cocoon.generation.Generator#generate()
79      */

80     public void generate()
81     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
82         super.generate();
83         // slowdown page generation.
84
long DELAY_SECS = this.parameters.getParameterAsLong("DELAY_SECS", 2);
85         try {
86           Thread.sleep(DELAY_SECS * 1000L);
87         } catch (InterruptedException JavaDoc ie) {
88           // Not much that can be done...
89
}
90     }
91 }
92
Popular Tags