01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.samples;
18:
19: import java.io.IOException;
20: import java.io.Serializable;
21:
22: import org.apache.cocoon.ProcessingException;
23: import org.apache.cocoon.caching.validity.EventValidity;
24: import org.apache.cocoon.caching.validity.NamedEvent;
25: import org.apache.cocoon.environment.ObjectModelHelper;
26: import org.apache.cocoon.environment.Request;
27: import org.apache.cocoon.generation.JXTemplateGenerator;
28: import org.apache.excalibur.source.SourceValidity;
29: import org.xml.sax.SAXException;
30:
31: /**
32: * This is a sample generator to demonstrate the event aware caching.
33: * We simply extend the JXTG.
34: * @version $Id: EventAwareGenerator.java 433543 2006-08-22 06:22:54Z crossley $
35: */
36: public class EventAwareGenerator extends JXTemplateGenerator {
37:
38: /**
39: * Generate the unique key for the cache.
40: *
41: * This key must be unique inside the space of this XSP page, it is used
42: * to find the page contents in the cache (if getValidity says that the
43: * contents are still valid).
44: *
45: * This method will be invoked before the getValidity() method.
46: *
47: * @return The generated key or null if the component
48: * is currently not cacheable.
49: */
50: public Serializable getKey() {
51: final Request request = ObjectModelHelper
52: .getRequest(this .objectModel);
53: // for our test, pages having the same value of "pageKey" will share
54: // the same cache location
55: String key = request.getParameter("pageKey");
56: return ((key == null || "".equals(key)) ? "one" : key);
57: }
58:
59: /**
60: * Generate the validity object, tells the cache how long to
61: * keep contents having this key around. In this case, it will
62: * be until an Event is retrieved matching the NamedEvent created below.
63: *
64: * Before this method can be invoked the getKey() method
65: * will be invoked.
66: *
67: * @return The generated validity object or null if the
68: * component is currently not cacheable.
69: */
70: public SourceValidity getValidity() {
71: final Request request = ObjectModelHelper
72: .getRequest(this .objectModel);
73: String key = request.getParameter("pageKey");
74: return new EventValidity(new NamedEvent((key == null || ""
75: .equals(key)) ? "one" : key));
76: }
77:
78: /* (non-Javadoc)
79: * @see org.apache.cocoon.generation.Generator#generate()
80: */
81: public void generate() throws IOException, SAXException,
82: ProcessingException {
83: super .generate();
84: // slowdown page generation.
85: long DELAY_SECS = this .parameters.getParameterAsLong(
86: "DELAY_SECS", 2);
87: try {
88: Thread.sleep(DELAY_SECS * 1000L);
89: } catch (InterruptedException ie) {
90: // Not much that can be done...
91: }
92: }
93: }
|