You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ki...@apache.org on 2002/03/23 23:18:17 UTC
cvs commit: jakarta-jetspeed/webapp/WEB-INF/psml/anon/html/en default.psml
kimptoc 02/03/23 14:18:17
Modified: src/java/org/apache/jetspeed/portal/portlets
VelocityPortlet.java
webapp/WEB-INF/conf demo-portlets.xreg
webapp/WEB-INF/psml/anon/html default.psml
webapp/WEB-INF/psml/anon/html/en default.psml
Log:
VelocityPortlet is now Cacheable and has the facility to cache its content
Revision Changes Path
1.14 +60 -9 jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/VelocityPortlet.java
Index: VelocityPortlet.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/VelocityPortlet.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- VelocityPortlet.java 13 Mar 2002 05:41:15 -0000 1.13
+++ VelocityPortlet.java 23 Mar 2002 22:18:17 -0000 1.14
@@ -64,6 +64,7 @@
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.portal.portlets.AbstractPortlet;
import org.apache.jetspeed.portal.PortletException;
+import org.apache.jetspeed.portal.PortletConfig;
import org.apache.jetspeed.services.TemplateLocator;
import org.apache.jetspeed.util.template.JetspeedTemplateLink;
import org.apache.jetspeed.services.JetspeedSecurity;
@@ -88,19 +89,59 @@
public class VelocityPortlet extends AbstractPortlet
{
+
+ /**
+ How long in milliseconds will the content of this portlet be cached,
+ that is, how long before regenerating it.
+ The default is zero - never cache the content.
+ This is specified in the portlet config parameter "cache-period-milliseconds"
+ **/
+ private long cachePeriod = 0;
+
/**
- * By default the data is non cacheable
+ * Initialize this portlet by looking up the cache period (default is zero)
+ * @throws PortletException Initialization failed
*/
- public void init( ) throws PortletException
+ public void init() throws PortletException {
+
+ PortletConfig config = this.getPortletConfig();
+
+ try
+ {
+ cachePeriod = Long.parseLong( config.getInitParameter("cache-period-milliseconds","0") );
+ } catch (NumberFormatException e) {
+ cachePeriod = 0;
+ }
+ }
+
+ /**
+ This methods outputs the content of the portlet for a given
+ request.
+
+ @param data the RunData object for the request
+ @return the content to be displayed to the user-agent
+ */
+ public ConcreteElement getContent( RunData data )
{
- setCacheable( false );
+ //if caching is turned off or no expiration time set, generate and return the content
+ if (cachePeriod == 0 || null == getExpirationMillis())
+ return getVelocityContent(data);
+
+ //is the cached content still valid, if not, generate and return the content
+ if (getExpirationMillis().longValue() <= System.currentTimeMillis())
+ return getVelocityContent(data);
+
+ //else, the cached content is fine to be returned
+ return getContent( data, null , true );
}
+
/**
* @param rundata The RunData object for the current request
*/
- public ConcreteElement getContent( RunData rundata )
+ public ConcreteElement getVelocityContent( RunData rundata )
{
+ Log.debug("VelocityPortlet::getVelocityContent");
// create a blank context and with all the global application
// Pull Tools inside
@@ -155,26 +196,36 @@
}
// generate the content
- String s = "";
+ ClearElement element = null;
try
{
if (-1 == template.indexOf(".vm"))
template = template + ".vm";
String templatePath = TemplateLocator.locatePortletTemplate(rundata, template);
- TurbineVelocity.handleRequest(context, templatePath, rundata.getOut());
+ if (cachePeriod > 0)
+ {
+ String s = TurbineVelocity.handleRequest(context, templatePath);
+ setExpirationMillis(cachePeriod + System.currentTimeMillis());
+ element = new ClearElement( s );
+ clearContent(); // doing this because setContent() is not overwriting current content.
+ setContent(element);
+ } else {
+ TurbineVelocity.handleRequest(context, templatePath, rundata.getOut());
+ }
}
catch( Exception e)
{
- s= e.toString();
+ element = new ClearElement( e.toString() );
}
TurbineVelocity.requestFinished(context);
- if (s == null) s = "";
+ if (element == null) element = new ClearElement("");
- return new ClearElement( s );
+ return element;
}
+
}
1.8 +12 -0 jakarta-jetspeed/webapp/WEB-INF/conf/demo-portlets.xreg
Index: demo-portlets.xreg
===================================================================
RCS file: /home/cvs/jakarta-jetspeed/webapp/WEB-INF/conf/demo-portlets.xreg,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- demo-portlets.xreg 25 Feb 2002 05:39:55 -0000 1.7
+++ demo-portlets.xreg 23 Mar 2002 22:18:17 -0000 1.8
@@ -1,5 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<registry>
+ <portlet-entry name="HelloVelocityCached" hidden="false" type="ref"
+ parent="CustomizerVelocity" application="false">
+ <meta-info>
+ <title>HelloVelocityCached</title>
+ <description>Simple Cached Velocity Portlet Example</description>
+ </meta-info>
+ <parameter name="template" value="hello" hidden="false"/>
+ <parameter name="cache-period-milliseconds" value="60000" hidden="false"/>
+ <parameter name="action" value="portlets.HelloAction" hidden="false"/>
+ <parameter name="text" value="Hello - only changes occasionally due to content caching" hidden="false"/>
+ <media-type ref="html"/>
+ </portlet-entry>
<portlet-entry name="HelloVelocity" hidden="false" type="ref"
parent="CustomizerVelocity" application="false">
<meta-info>
1.8 +2 -1 jakarta-jetspeed/webapp/WEB-INF/psml/anon/html/default.psml
Index: default.psml
===================================================================
RCS file: /home/cvs/jakarta-jetspeed/webapp/WEB-INF/psml/anon/html/default.psml,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- default.psml 31 Jan 2002 12:24:28 -0000 1.7
+++ default.psml 23 Mar 2002 22:18:17 -0000 1.8
@@ -94,7 +94,8 @@
<controller name="RowColumnPortletController"/>
<entry parent="HelloVelocity"/>
+ <entry parent="HelloVelocityCached"/>
</portlets>
</portlets>
-</portlets>
\ No newline at end of file
+</portlets>
1.10 +9 -1 jakarta-jetspeed/webapp/WEB-INF/psml/anon/html/en/default.psml
Index: default.psml
===================================================================
RCS file: /home/cvs/jakarta-jetspeed/webapp/WEB-INF/psml/anon/html/en/default.psml,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- default.psml 31 Jan 2002 12:24:28 -0000 1.9
+++ default.psml 23 Mar 2002 22:18:17 -0000 1.10
@@ -95,10 +95,18 @@
</portlets>
<portlets>
- <entry parent="HelloJSP"/>
+ <entry parent="HelloVelocityCached"/>
<layout>
<property name="row" value="1"/>
<property name="column" value="1"/>
+ </layout>
+ </portlets>
+
+ <portlets>
+ <entry parent="HelloJSP"/>
+ <layout>
+ <property name="row" value="1"/>
+ <property name="column" value="0"/>
</layout>
</portlets>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>