View Javadoc

1   /*
2    * Copyright 2003,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  
18   */
19  
20  package org.apache.pluto.portalImpl;
21  
22  import java.io.IOException;
23  import java.util.Properties;
24  
25  import javax.portlet.PortletException;
26  import javax.servlet.ServletConfig;
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.pluto.PortletContainerException;
33  import org.apache.pluto.services.log.Logger;
34  import org.apache.pluto.om.window.PortletWindow;
35  import org.apache.pluto.portalImpl.aggregation.RootFragment;
36  import org.apache.pluto.portalImpl.core.PortalControlParameter;
37  import org.apache.pluto.portalImpl.core.PortalEnvironment;
38  import org.apache.pluto.portalImpl.core.PortalURL;
39  import org.apache.pluto.portalImpl.core.PortletContainerEnvironment;
40  import org.apache.pluto.portalImpl.core.PortletContainerFactory;
41  import org.apache.pluto.portalImpl.services.ServiceManager;
42  import org.apache.pluto.portalImpl.services.log.Log;
43  import org.apache.pluto.portalImpl.services.factorymanager.FactoryManager;
44  import org.apache.pluto.portalImpl.factory.FactoryAccess;
45  import org.apache.pluto.portalImpl.services.config.Config;
46  import org.apache.pluto.portalImpl.services.pageregistry.PageRegistry;
47  import org.apache.pluto.portalImpl.servlet.ServletObjectAccess;
48  
49  
50  public class Servlet extends HttpServlet
51  {
52  
53      private static String CONTENT_TYPE = "text/html";
54  
55      private Logger log = null;
56  
57      public String getServletInfo()
58      {
59          return "portalImpl - Pluto Driver";
60      }
61  
62      public void init (ServletConfig config) throws ServletException
63      {
64          super.init (config);
65  
66          String charset = config.getInitParameter("charset");
67          if (charset != null && charset.length() > 0) {
68              CONTENT_TYPE = "text/html; charset=" + charset;
69          }
70  
71          try
72          {
73              ServiceManager.init (config);
74          }
75          catch (Throwable exc)
76          {
77              log ("Initialization failed!", exc);
78  
79              throw (new javax.servlet.UnavailableException ("Initialization of one or more services failed."));
80          }
81  
82          try {
83  
84              ServiceManager.postInit(config);
85  
86          } catch (Throwable expos) {
87              
88              log ("Post initialization failed!", expos);
89  
90              throw (new javax.servlet.UnavailableException ("Post initialization of one or more services failed."));
91          }
92  
93          log = Log.getService().getLogger(getClass());
94  
95          if (!PortletContainerFactory.getPortletContainer().isInitialized()) {
96              String uniqueContainerName =
97                 Config.getParameters().getString("portletcontainer.uniquename", "pluto");
98  
99              if(log.isInfoEnabled())
100                 log.info("Initializing PortletContainer ["
101                           +uniqueContainerName+"]...");
102     
103             PortletContainerEnvironment environment
104                 = new PortletContainerEnvironment();
105 
106             environment.addContainerService(Log.getService());
107             environment.addContainerService(FactoryManager.getService());
108             environment.addContainerService(FactoryAccess.getInformationProviderContainerService());
109             environment.addContainerService(FactoryAccess.getDynamicTitleContainerService());
110     
111     		Properties properties = new Properties();
112     		
113             try
114             {
115                 PortletContainerFactory.
116                     getPortletContainer().
117                         init(uniqueContainerName, config, environment, properties);
118             }
119             catch (PortletContainerException exc)
120             {
121                 log.error("Initialization of the portlet container failed!", exc);
122                 throw (new javax.servlet.UnavailableException ("Initialization of the portlet container failed."));
123             }
124         } else if(log.isInfoEnabled()) {
125             log.info("PortletContainer already initialized");
126         }
127 
128         log.debug("Ready to serve you.");
129     }
130 
131     public void destroy()
132     {
133         if(log.isInfoEnabled())
134             log.info("Shutting down portlet container. . .");
135         try
136         {
137             PortletContainerFactory.
138                 getPortletContainer().
139                     shutdown();
140 
141             // destroy all services
142 
143             ServiceManager.destroy (getServletConfig ());
144 
145             System.gc ();
146         }
147         catch (Throwable t)
148         {
149             log ("Destruction failed!", t);
150         }
151     }
152 
153     public void doGet (HttpServletRequest servletRequest,
154                              HttpServletResponse servletResponse) throws IOException, ServletException
155     {
156         servletResponse.setContentType(CONTENT_TYPE);
157 
158         PortalEnvironment env =
159             new PortalEnvironment(servletRequest,
160                                   servletResponse,
161                                   getServletConfig());
162 
163         PortalURL currentURL = env.getRequestedPortalURL();
164         PortalControlParameter control = new PortalControlParameter(currentURL);
165         PortletWindow actionWindow = control.getPortletWindowOfAction();
166         if (actionWindow!=null)
167         {
168             try {
169                 PortletContainerFactory.
170                     getPortletContainer().
171                         processPortletAction(actionWindow,
172                                              ServletObjectAccess.getServletRequest(servletRequest, actionWindow),
173                                              ServletObjectAccess.getServletResponse(servletResponse));
174             }
175             catch (PortletException e)
176             {
177                 e.printStackTrace(System.err);
178             }
179             catch (PortletContainerException e)
180             {
181                 e.printStackTrace(System.err);
182             }
183             // This catch block is for compliance
184             // of TCK's Portlet.ProcessActionIOExceptionTest
185             catch (Exception e)
186             {
187                 e.printStackTrace(System.err);
188             }
189 
190             return; // we issued an redirect, so return directly
191         }
192 
193         try
194         {
195             RootFragment root = PageRegistry.getRootFragment();
196             root.service(servletRequest, servletResponse);
197         }
198         catch (Throwable t)
199         {
200             ;
201         }
202         finally
203         {
204 
205         }
206 
207     }
208 
209     public void doPost (HttpServletRequest request,
210                               HttpServletResponse response) throws IOException, ServletException
211     {
212         doGet (request, response);
213     }
214 
215 }