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.portlets;
18  
19  //standard java stuff
20  import java.util.Enumeration;
21  import java.util.Hashtable;
22  import java.util.Iterator;
23  
24  //Element Construction Set
25  import org.apache.ecs.html.Comment;
26  import org.apache.ecs.ConcreteElement;
27  import org.apache.ecs.ElementContainer;
28  
29  //standard Jetspeed stuff
30  import org.apache.jetspeed.portal.PortletConfig;
31  import org.apache.jetspeed.portal.PortletException;
32  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
33  import org.apache.jetspeed.services.logging.JetspeedLogger;
34  
35  //turbine stuff
36  import org.apache.turbine.util.RunData;
37  import org.apache.turbine.modules.ScreenLoader;
38  
39  /***
40   This portlet will attempt to render the Turbine Screen within a portlet.
41   Parameters are passed to the screen via portlet config information:
42   <PRE>
43  
44   <entry type="abstract" name="TurbineScreen">
45       <classname>portlets.TurbineScreenPortlet</classname>
46   </entry>
47  
48   <entry type="ref" parent="TurbineScreen" name="TestPortal">
49       <parameter name="display.screen" value="TurbineScreenName" />
50       <parameter name="TurbineScreenName.param.MYPARAM" value="MYVALUE" />
51       <metainfo>
52            <title>Place title here</title>
53            <description>Place description here</description>
54       </metainfo>
55   </entry>
56  
57   </PRE>
58  
59   In the above example, the parameter MYPARAM=MYVALUE will be set in the rundata that calls the screen.
60  
61  @author <a href="mailto:ekkerbj@netscape.net">Jeff Breckke</a>
62  @version $Id: TurbineScreenPortlet.java,v 1.13 2004/02/23 04:03:34 jford Exp $
63  */
64  public class TurbineScreenPortlet extends AbstractPortlet
65  {
66      /***
67       * Static initialization of the logger for this class
68       */    
69      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(TurbineScreenPortlet.class.getName());
70      
71      private String screen = null;
72      private String bgcolor = null;
73      private String classes = null;
74      private Hashtable paramSet = null;
75  
76      static final public String BGCOLOR = "bgcolor";
77      static final public String CLASSES = "classes";
78      static final public String SCREEN = "display.screen";
79  
80      /***
81      */
82      public ConcreteElement getContent( RunData rundata )
83      {
84          ElementContainer content = new ElementContainer();
85          try
86          {
87              rundata = setParams( rundata );
88              content.addElement( new Comment( "BEGIN TurbineScreenPortlet" ) );
89              content.addElement( ScreenLoader.getInstance().eval( rundata, screen ) );
90              content.addElement( new Comment( "END TurbineScreenPortlet" ) );
91          }
92          catch ( Exception e )
93          {
94              String message = "TurbineScreenPortlet: " + e.getMessage();
95              logger.error( message, e );
96              content.addElement( message );
97          }
98          return ( content );
99      }
100 
101     /***
102     */
103     public void init() throws PortletException
104     {
105         PortletConfig pc = getPortletConfig();
106         ConcreteElement myContent = null;
107         try
108         {
109             screen = ( String ) pc.getInitParameter( SCREEN );
110             //if it is null here it should be in the parameters
111             if ( screen == null )
112             {
113                 throw new IllegalArgumentException("Missing screen parameter");
114             }
115 
116             //if it is still null something is wrong
117             if ( screen == null )
118             {
119                 throw new PortletException( "You need to specify a " + SCREEN + " parameter for this portlet" );
120             }
121 
122             /* Save the parameters if any */
123             String param = null;
124             String value = null;
125             java.util.Map dict = pc.getInitParameters();
126             Iterator en = dict.keySet().iterator();
127             int index = -1;
128             String tParam = screen + ".param";
129             String newParam = null;
130             paramSet = new Hashtable();
131             while ( en.hasNext() )
132             {
133                 param = ( String ) en.next();
134                 index = param.indexOf( tParam );
135                 if ( index != -1 )
136                 {
137                     value = ( String ) dict.get( param );
138                     if ( value == null )
139                     {
140                         throw new PortletException( "Could not retrieve value for " + param );
141                     }
142                     newParam = param.substring( index + tParam.length() + 1 );
143                     paramSet.put( newParam, value );
144                 }
145             }
146 
147             bgcolor = this.getPortletConfig().getPortletSkin().getBackgroundColor();
148             classes = "WEB-INF/classes";
149         }
150         catch ( Exception e )
151         {
152             String message = "TurbineScreenPortlet: " + e.getMessage();
153             logger.error( message, e );
154         }
155     }
156 
157     /***
158     */
159     public boolean getAllowEdit( RunData rundata )
160     {
161         return false;
162     }
163 
164     /***
165     */
166     public boolean getAllowMaximize( RunData rundata )
167     {
168         return true;
169     }
170 
171     /***
172     */
173     private RunData setParams( RunData data )
174     {
175         data.getParameters().add( BGCOLOR, bgcolor );
176         data.getParameters().add( CLASSES, classes );
177 
178         Enumeration en = paramSet.keys();
179         String param = null;
180         while ( en.hasMoreElements() )
181         {
182             param = ( String ) en.nextElement();
183             data.getParameters().add( param, ( String ) paramSet.get( param ) );
184         }
185         return ( data );
186     }
187 }