KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > portlets > viewprocessor > VelocityViewProcessor


1 /*
2  * Copyright 2000-2001,2004 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 /*
17  * VelocityViewProcessor.java
18  *
19  *
20  * Created on January 27, 2003, 8:54 PM
21  */

22 package org.apache.jetspeed.portal.portlets.viewprocessor;
23
24 import org.apache.jetspeed.portal.Portlet;
25 import org.apache.jetspeed.portal.PortletException;
26 import org.apache.jetspeed.portal.portlets.*;
27 import org.apache.jetspeed.portal.portlets.GenericMVCContext;
28 import org.apache.jetspeed.services.TemplateLocator;
29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30 import org.apache.jetspeed.services.logging.JetspeedLogger;
31 import org.apache.jetspeed.util.JetspeedClearElement;
32
33 import org.apache.turbine.services.velocity.TurbineVelocity;
34 import org.apache.turbine.util.RunData;
35
36
37 /**
38  *
39  * @author tkuebler
40  */

41 public class VelocityViewProcessor implements ViewProcessor
42 {
43
44     /**
45      * Static initialization of the logger for this class
46      */

47     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityViewProcessor.class.getName());
48     
49     /** Creates a new instance of VelocityViewProcessor */
50     public VelocityViewProcessor()
51       {
52       }
53
54     public void init(Portlet portlet)
55               throws PortletException
56       {
57       }
58
59     /** Process the template passed in the context
60      * (context.get("template")). Invoked by the GenericMVCPortlet
61      * after action handling to process the template type
62      * in question.
63      *
64      */

65     public Object JavaDoc processView(GenericMVCContext context)
66       {
67
68         // generate the content
69
JetspeedClearElement element = null;
70         String JavaDoc template = (String JavaDoc) context.get("template");
71         logger.info("VelocityViewProcessor - processing " + template);
72
73         try
74           {
75
76             if (-1 == template.indexOf(".vm"))
77               {
78                 template = template + ".vm";
79               }
80
81             logger.info("VelocityViewProcessor - locating template - " +
82                      ((RunData) context.get("data")).toString() + template);
83
84             String JavaDoc templatePath = TemplateLocator.locatePortletTemplate(
85                                           (RunData) context.get("data"),
86                                           template);
87
88             // need to add cache support
89
Portlet portlet = (Portlet) context.get("portlet");
90             RunData rundata = (RunData) context.get("data");
91             long cachePeriod = -1;
92             AbstractPortlet abstractPortlet = null;
93             // STW: Safety net ;)
94
if(portlet instanceof AbstractPortlet)
95             {
96                 abstractPortlet =(AbstractPortlet) portlet;
97                 if(abstractPortlet.getExpirationMillis() != null)
98                 {
99                     cachePeriod = abstractPortlet.getExpirationMillis().longValue();
100                 }
101             }
102            
103             if (cachePeriod > 0 && abstractPortlet != null)
104             {
105                 String JavaDoc s = TurbineVelocity.handleRequest(context, templatePath);
106                 abstractPortlet.setExpirationMillis(
107                     cachePeriod + System.currentTimeMillis());
108                 element = new JetspeedClearElement(s);
109
110             }
111             else
112             {
113                 TurbineVelocity.handleRequest(
114                     context, templatePath, rundata.getOut());
115             }
116             
117             
118           }
119         catch (Exception JavaDoc e)
120           {
121             element = new JetspeedClearElement(e.toString());
122             logger.error("VelocityViewProcessor - had problems handling request - " + e);
123             e.printStackTrace();
124           }
125
126         TurbineVelocity.requestFinished(context);
127
128         if (element == null)
129           {
130             element = new JetspeedClearElement("");
131           }
132
133         return element;
134       }
135   }
136
Popular Tags