home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / cacheable.xsp < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  5.3 KB  |  149 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       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.  
  18. <!--
  19.     XSP caching sample.
  20.  
  21.     WARNING - another sample depends on this!
  22.     The output of this is used by the FOP cache test - if you modify this page, please make sure that
  23.     the caching samples found at http://localhost:8888/samples/fop/welcome (or equivalent)
  24.     still work .
  25.  
  26.     $Id: cacheable.xsp,v 1.2 2004/04/05 12:25:30 antonio Exp $
  27.  -->
  28.  
  29.  
  30. <xsp:page language="java"
  31.           xmlns:xsp="http://apache.org/xsp"
  32.           xmlns:xsp-request="http://apache.org/xsp/request/2.0">
  33.  
  34. <xsp:structure>
  35.     <xsp:include>org.apache.excalibur.source.SourceValidity</xsp:include>
  36.     <xsp:include>org.apache.excalibur.source.impl.validity.ExpiresValidity</xsp:include>
  37.     <xsp:include>java.io.Serializable</xsp:include>
  38. </xsp:structure>
  39.  
  40. <xsp:logic>
  41.  
  42.     // artificial slowdown to make the effects of the cache visible
  43.     final int DELAY_SECS = 2;
  44.  
  45.     // request parameter "validity" contains number of seconds to cache
  46.     private int getValidityFromRequest() {
  47.         int result = 15;
  48.         try {
  49.             result = Integer.valueOf(request.getParameter("validity")).intValue();
  50.         } catch(Exception e) {
  51.             // keep default value
  52.         }
  53.         return result;
  54.     }
  55.  
  56.     /**
  57.     * Generate the unique key for the cache.
  58.     *
  59.     * This key must be unique inside the space of this XSP page, it is used
  60.     * to find the page contents in the cache (if getValidity says that the
  61.     * contents are still valid).
  62.     *
  63.     * This method will be invoked before the getValidity() method.
  64.     *
  65.     * @return The generated key or null if the component
  66.     *         is currently not cacheable.
  67.     */
  68.     public Serializable getKey()
  69.     {
  70.        // for our test, pages having the same value of "pageKey" will share
  71.        // the same cache location
  72.        return "" + request.getParameter("pageKey");
  73.     }
  74.  
  75.     /**
  76.     * Generate the validity object, tells the cache how long to
  77.     * keep contents having this key around.
  78.     *
  79.     * Before this method can be invoked the getKey() method
  80.     * will be invoked.
  81.     *
  82.     * @return The generated validity object or null if the
  83.     *         component is currently not cacheable.
  84.     */
  85.     public SourceValidity getValidity() {
  86.        // keep in cache for our validity time
  87.        return new ExpiresValidity(getValidityFromRequest()*1000);
  88.     }
  89.  </xsp:logic>
  90.  
  91.  
  92.   <page>
  93.     <title>A Cacheable XSP Page</title>
  94.     <content>
  95.         <para>
  96.             Hi there! I'm a simple dynamic page generated by XSP (eXtensible Server Pages).
  97.         </para>
  98.  
  99.         <para>
  100.             I need <xsp:expr>DELAY_SECS</xsp:expr> seconds to be generated, so you can tell
  101.             if I'm being served from the cache or not.
  102.             <br/>
  103.             What you see here has been generated on <b><xsp:expr>new java.util.Date()</xsp:expr></b>.
  104.         </para>
  105.  
  106.         <para>
  107.             I'm cached for every different value of request parameter 'pageKey'.
  108.             <br/>
  109.             Here the value is:
  110.             <b><xsp-request:get-parameter name="pageKey"/></b>.
  111.             <br/>
  112.             If this is not the same as the 'pageKey' parameter in the page URL, we have a problem.
  113.         </para>
  114.  
  115.         <para>
  116.           All other request parameters do not influence cache status, but
  117.             my validity will expire after <xsp:expr>getValidityFromRequest()</xsp:expr> seconds
  118.             (set by 'validity' URL parameter when page is generated).
  119.         </para>
  120.  
  121.         <para>
  122.             Value of parameter 'other' is: <b><xsp:expr>String.valueOf(request.getParameter("other"))</xsp:expr></b>.
  123.             <br/>
  124.             This might be different than the URL parameter 'other', in case the version of the page you're
  125.             seeing was cached from a request having the same 'pageKey' but a different value of 'other'.
  126.  
  127.         </para>
  128.  
  129.         <xsp:logic>
  130.           // slowdown page generation.
  131.             try {
  132.               Thread.sleep(DELAY_SECS * 1000L);
  133.             } catch (InterruptedException ie) {
  134.               // Not much that can be done...
  135.             }
  136.         </xsp:logic>
  137.  
  138.         <para>Test links:
  139.             <ul>
  140.                 <li><a target="_new" href="cacheable?pageKey=one">pageKey=one</a></li>
  141.                 <li><a target="_new" href="cacheable?pageKey=two">pageKey=two</a></li>
  142.                 <li><a target="_new" href="cacheable?pageKey=three&other=abc">pageKey=three, other=abc</a></li>
  143.                 <li><a target="_new" href="cacheable?pageKey=three&other=xyz">pageKey=three, other=xyz</a></li>
  144.                 <li><a target="_new" href="cacheable?pageKey=three&other=wow&validity=5">pageKey=three, other=wow and 5 seconds of cache validity</a></li>
  145.             </ul>
  146.         </para>
  147.     </content>
  148.   </page>
  149. </xsp:page>