View Javadoc

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 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  package org.apache.jetspeed.portal.controllers;
18  
19  // Turbine stuff
20  import org.apache.turbine.modules.ActionLoader;
21  import org.apache.turbine.services.velocity.TurbineVelocity;
22  import org.apache.turbine.services.pull.TurbinePull;
23  import org.apache.turbine.util.RunData;
24  
25  // Jetspeed stuff
26  import org.apache.jetspeed.services.TemplateLocator;
27  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28  import org.apache.jetspeed.services.logging.JetspeedLogger;
29  
30  // Ecs stuff
31  import org.apache.ecs.ConcreteElement;
32  import org.apache.ecs.StringElement;
33  
34  // Velocity Stuff
35  import org.apache.velocity.context.Context;
36  
37  /***
38   * A Velocity based portlet controller implementation
39   * 
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   */
45  public class VelocityPortletController extends AbstractPortletController
46  {
47      
48      /***
49       * Static initialization of the logger for this class
50       */    
51      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityPortletController.class.getName());    
52      
53      public ConcreteElement getContent( RunData rundata )
54      {
55          // create a blank context and with all the global application
56          // Pull Tools inside
57          Context context = TurbineVelocity.getContext();
58          
59          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") );
65          
66          // Put the request and session based contexts
67          TurbinePull.populateContext(context, rundata);
68          
69          // allow subclass to insert specific objects in the context
70          buildContext(rundata, context);
71          
72          String actionName = getConfig().getInitParameter("action");
73          
74          if (actionName != null)
75          {
76              // store the context so that the action can retrieve it
77              rundata.getTemplateInfo().setTemplateContext( "VelocityControllerContext", context );
78  
79              // if there is an action with the same name in modules/actions/portlets exec it
80              try
81              {
82                  ActionLoader.getInstance().exec( rundata, actionName );
83              }
84              catch( Exception e)
85              {
86                 logger.error("Exception",  e);
87              }
88          }
89   
90          // either the action selected the template, or use the default template 
91          // defined in the registry
92          String template = (String)context.get( "template" );
93          
94          // generate the content
95          String s = "";
96  
97          try
98          {
99              if (-1 == template.indexOf(".vm"))
100             {
101                 template = template + ".vm";
102             }
103             
104             String templatePath = TemplateLocator.locateControllerTemplate(rundata, template);
105             TurbineVelocity.handleRequest(context, templatePath, rundata.getOut());
106         }
107         catch( Exception e)
108         {
109             logger.error( "Error generating content: ", e );
110             s= e.toString();
111         }
112         
113         TurbineVelocity.requestFinished(context);
114 
115         return new StringElement( s );
116     }
117 
118     public void buildContext(RunData data, Context context)
119     {
120         // nothing special
121     }
122 }
123