View Javadoc

1   package org.apache.jetspeed.services.jsp.tags;
2   
3   /*
4    * Copyright 2000-2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import javax.servlet.jsp.JspException;
20  import javax.servlet.jsp.PageContext;
21  import javax.servlet.jsp.tagext.TagSupport;
22  
23  // Turbine Classes
24  import org.apache.turbine.services.jsp.JspService;
25  
26  import org.apache.ecs.ConcreteElement;
27  
28  // Jetspeed classes
29  import org.apache.jetspeed.services.PortletFactory;
30  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31  import org.apache.jetspeed.services.logging.JetspeedLogger;
32  import org.apache.jetspeed.services.rundata.JetspeedRunData;
33  import org.apache.jetspeed.om.profile.Entry;
34  import org.apache.jetspeed.om.profile.ProfileLocator;
35  import org.apache.jetspeed.om.profile.Profile;
36  import org.apache.jetspeed.services.Profiler;
37  
38  /***
39   * Supporting class for the portlet tag.
40   * Builds the output of a portlet (with conrol) and insert it within the
41   * current JSP page
42   *
43   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
44   * @version $Id: JetspeedPortletTag.java,v 1.10 2004/02/23 03:59:40 jford Exp $
45   */
46  public class JetspeedPortletTag extends TagSupport
47  {
48      /***
49       * Static initialization of the logger for this class
50       */    
51      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortletTag.class.getName());
52      
53      private String name = null;
54      private String psml = null;
55  
56      public void setName(String name)
57      {
58          this.name = name;
59      }
60  
61      public String getName()
62      {
63          return this.name;
64      }
65  
66      public void setPsml(String psml)
67      {
68          this.psml = psml;
69      }
70  
71      public String getPsml()
72      {
73          return this.psml;
74      }
75  
76      /***
77       * Method called when the tag is encountered to send attributes to the
78       * output stream
79       *
80       * @return SKIP_BODY, as it is intended to be a single tag.
81       */
82      public int doStartTag() throws JspException
83      {
84          JetspeedRunData data = (JetspeedRunData)pageContext.getAttribute(JspService.RUNDATA, PageContext.REQUEST_SCOPE);
85  
86          // if called without arguments, use the default request parameter
87          if (this.name == null)
88          {
89              this.name = data.getPortlet();
90          }
91  
92          try
93          {
94              pageContext.getOut().flush();
95  
96              ConcreteElement result = new ConcreteElement();
97              Entry entry = null;
98  
99              if (this.psml != null)
100             {
101                 ProfileLocator baseLocator = Profiler.createLocator();
102                 baseLocator.createFromPath(this.psml);
103                 Profile baseProfile = Profiler.getProfile(baseLocator);
104                 if (baseProfile != null)
105                 {
106                     entry = baseProfile.getDocument().getEntry(name);
107                     if ( logger.isDebugEnabled() )
108                     {
109                         logger.debug("JetspeedPortletTag: retrieved [" + entry + "] from psml [" + this.psml);
110                     }
111                 }
112             }
113             else
114             {
115                 entry  = data.getProfile().getDocument().getEntry(name);
116                 if ( logger.isDebugEnabled() )
117                 {
118                     logger.debug("JetspeedPortletTag: retrieved [" + entry + "] from current psml");
119                 }
120             }
121 
122             if (entry != null)
123             {
124                 result = PortletFactory.getPortlet(entry).getContent(data);
125             }
126 
127             // Check whether this is an "old" screen (that returns a ConcreteElement)
128             // or a "new" one that returns null.
129             if ( result != null )
130             {
131                 //The ECS element must serialize in the character encoding
132                 // of the response
133                 result.setCodeSet( data.getResponse().getCharacterEncoding() );
134 
135                 result.output( data.getResponse().getWriter() );
136             }
137 
138         }
139         catch (Exception e)
140         {
141             String message = "Error processing name '" + name + "'.";
142             logger.error(message, e);
143             try
144             {
145                 data.getOut().print("Error processing portlet '" + name + "'. See log for more information.");
146             }
147             catch(java.io.IOException ioe) {}
148         }
149         return SKIP_BODY;
150     }
151 }