1/*2 * Copyright 2000-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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.controllers;
1819// Turbine stuff20import org.apache.turbine.modules.ActionLoader;
21import org.apache.turbine.services.velocity.TurbineVelocity;
22import org.apache.turbine.services.pull.TurbinePull;
23import org.apache.turbine.util.RunData;
2425// Jetspeed stuff26import org.apache.jetspeed.services.TemplateLocator;
27import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28import org.apache.jetspeed.services.logging.JetspeedLogger;
2930// Ecs stuff31import org.apache.ecs.ConcreteElement;
32import org.apache.ecs.StringElement;
3334// Velocity Stuff35import org.apache.velocity.context.Context;
3637/***38 * A Velocity based portlet controller implementation39 * 40 * @author <a href="mailto:re_carrasco@bco011.sonda.cl">Roberto Carrasco</a>41 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>42 *43 * @version $Id: VelocityPortletController.java,v 1.12 2004/02/23 03:25:06 jford Exp $44 */45publicclassVelocityPortletControllerextendsAbstractPortletController46 {
4748/***49 * Static initialization of the logger for this class50 */51privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityPortletController.class.getName());
5253public ConcreteElement getContent( RunData rundata )
54 {
55// create a blank context and with all the global application56// Pull Tools inside57 Context context = TurbineVelocity.getContext();
5859 context.put( "data", rundata );
60 context.put( "controller", this );
61 context.put( "portlets", this.getPortlets().toArray() );
62 context.put( "config", this.getConfig() );
63 context.put( "skin", this.getPortlets().getPortletConfig().getPortletSkin() );
64 context.put( "template", getConfig().getInitParameter("template") );
6566// Put the request and session based contexts67 TurbinePull.populateContext(context, rundata);
6869// allow subclass to insert specific objects in the context70 buildContext(rundata, context);
7172 String actionName = getConfig().getInitParameter("action");
7374if (actionName != null)
75 {
76// store the context so that the action can retrieve it77 rundata.getTemplateInfo().setTemplateContext( "VelocityControllerContext", context );
7879// if there is an action with the same name in modules/actions/portlets exec it80try81 {
82 ActionLoader.getInstance().exec( rundata, actionName );
83 }
84catch( Exception e)
85 {
86 logger.error("Exception", e);
87 }
88 }
8990// either the action selected the template, or use the default template 91// defined in the registry92 String template = (String)context.get( "template" );
9394// generate the content95 String s = "";
9697try98 {
99if (-1 == template.indexOf(".vm"))
100 {
101 template = template + ".vm";
102 }
103104 String templatePath = TemplateLocator.locateControllerTemplate(rundata, template);
105 TurbineVelocity.handleRequest(context, templatePath, rundata.getOut());
106 }
107catch( Exception e)
108 {
109 logger.error( "Error generating content: ", e );
110 s= e.toString();
111 }
112113 TurbineVelocity.requestFinished(context);
114115returnnew StringElement( s );
116 }
117118publicvoid buildContext(RunData data, Context context)
119 {
120// nothing special121 }
122 }
123