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.om.window.PortletWindow;
34  import org.apache.pluto.portalImpl.aggregation.RootFragment;
35  import org.apache.pluto.portalImpl.core.PortalControlParameter;
36  import org.apache.pluto.portalImpl.core.PortalEnvironment;
37  import org.apache.pluto.portalImpl.core.PortalURL;
38  import org.apache.pluto.portalImpl.core.PortletContainerEnvironment;
39  import org.apache.pluto.portalImpl.core.PortletContainerFactory;
40  import org.apache.pluto.portalImpl.factory.FactoryAccess;
41  import org.apache.pluto.portalImpl.services.ServiceManager;
42  import org.apache.pluto.portalImpl.services.config.Config;
43  import org.apache.pluto.portalImpl.services.factorymanager.FactoryManager;
44  import org.apache.pluto.portalImpl.services.log.Log;
45  import org.apache.pluto.portalImpl.services.pageregistry.PageRegistry;
46  import org.apache.pluto.portalImpl.servlet.ServletObjectAccess;
47  import org.apache.pluto.services.log.Logger;
48  
49  
50  public class Servlet extends HttpServlet
51  {
52  
53      private static String CONTENT_TYPE = "text/html";
54      
55      /***
56       * Stores whether the portlet container used supports buffering
57       */
58      private static String CONTAINER_SUPPORTS_BUFFERING = "portletcontainer.supportsBuffering";
59  
60      private Logger log;
61  
62      public String getServletInfo()
63      {
64          return "portalImpl - Pluto Driver";
65      }
66  
67      public void init (ServletConfig config) throws ServletException
68      {
69          super.init (config);
70          
71          String charset = config.getInitParameter("charset");
72          if (charset != null && charset.length() > 0) {
73              CONTENT_TYPE = "text/html; charset=" + charset;
74          }
75  
76          try
77          {
78              ServiceManager.init (config);
79          }
80          catch (Throwable exc)
81          {
82              log ("Initialization failed!", exc);
83  
84              throw (new javax.servlet.UnavailableException ("Initialization of one or more services failed."));
85          }
86  
87          try {
88  
89              ServiceManager.postInit(config);
90  
91          } catch (Throwable expos) {
92              
93              log ("Post initialization failed!", expos);
94  
95              throw (new javax.servlet.UnavailableException ("Post initialization of one or more services failed."));
96          }
97  
98          log = Log.getService().getLogger(getClass());
99  
100         if (!PortletContainerFactory.getPortletContainer().isInitialized()) {
101             String uniqueContainerName =
102                Config.getParameters().getString("portletcontainer.uniquename", "pluto");
103 
104             if(log.isInfoEnabled())
105                 log.info("Initializing PortletContainer ["
106                           +uniqueContainerName+"]...");
107     
108             PortletContainerEnvironment environment
109                 = new PortletContainerEnvironment();
110 
111             environment.addContainerService(Log.getService());
112             environment.addContainerService(FactoryManager.getService());
113             environment.addContainerService(FactoryAccess.getInformationProviderContainerService());
114             environment.addContainerService(FactoryAccess.getDynamicTitleContainerService());
115     
116             String allowSetBufferSizeString = Config.getParameters().getString(CONTAINER_SUPPORTS_BUFFERING);
117             boolean allowSetBufferSize = false;
118             if (allowSetBufferSizeString == null) {
119             	log.warn("org.apache.pluto.portalImpl.Servlet#init(): " +
120             			"Couldn't read property \"pluto.allowSetBufferSize\" " +
121             			"from config file ConfigService.properties");
122             } else {
123             	allowSetBufferSize = allowSetBufferSizeString.equalsIgnoreCase("yes");
124             }
125             Properties containerProperties = new Properties();
126 
127             containerProperties.put(CONTAINER_SUPPORTS_BUFFERING, new Boolean(allowSetBufferSize));
128             
129 //    		Properties properties = new Properties();
130     		
131             try
132             {
133                 PortletContainerFactory.
134                     getPortletContainer().
135 //                        init(uniqueContainerName, config, environment, properties);
136                     	init(uniqueContainerName, config, environment, containerProperties);
137             }
138             catch (PortletContainerException exc)
139             {
140                 log.error("Initialization of the portlet container failed!", exc);
141                 throw (new javax.servlet.UnavailableException ("Initialization of the portlet container failed."));
142             }
143         } else if(log.isInfoEnabled()) {
144             log.info("PortletContainer already initialized");
145         }
146 
147         log.debug("Ready to serve you.");
148     }
149 
150     public void destroy()
151     {
152         if(log.isInfoEnabled())
153             log.info("Shutting down portlet container. . .");
154         try
155         {
156             PortletContainerFactory.
157                 getPortletContainer().
158                     shutdown();
159 
160             // destroy all services
161 
162             ServiceManager.destroy (getServletConfig ());
163 
164             System.gc ();
165         }
166         catch (Throwable t)
167         {
168             log ("Destruction failed!", t);
169         }
170     }
171 
172     public void doGet (HttpServletRequest servletRequest,
173                              HttpServletResponse servletResponse) throws IOException, ServletException
174     {
175         servletResponse.setContentType(CONTENT_TYPE);
176 
177         //start hot deploy patch
178         String theService = servletRequest.getParameter("hotDeploy");
179      	if (theService != null) {
180             if(log.isInfoEnabled()) {
181                 log.info("Hot deploying portlet. . .");
182             }
183 
184      		//run registry services to load new portlet info from the registry files
185      		String[] svcs = {
186      				"org.apache.pluto.portalImpl.services.portletdefinitionregistry.PortletDefinitionRegistryService",
187      				"org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService",
188      				"org.apache.pluto.portalImpl.services.pageregistry.PageRegistryService"};
189      		int len = svcs.length;
190      		for (int i = 0; i < len; i++) {				
191 	 			try {
192 					ServiceManager.hotInit(getServletConfig(), svcs[i]);
193 	 			} catch (Throwable e) {
194 	 				String svc = svcs[i].substring(svcs[i].lastIndexOf('.') + 1);
195 	 				String msg = "Initialization of " + svc + " service for hot deployment failed!"; 
196 	 				log.error(msg, e);
197 	 				break;
198 //	 				throw new javax.servlet.UnavailableException(msg);
199 	 			}
200 	 	
201 	 			try {
202 					ServiceManager.postHotInit(getServletConfig(), svcs[i]);
203 	 			} catch (Throwable e) {
204 	 				String svc = svcs[i].substring(svcs[i].lastIndexOf('.') + 1);
205 	 				String msg = "Post initialization of " + svc + " service for hot deployment failed!"; 
206 	 				log.error(msg, e);
207 	 				break;
208 //	 				throw new javax.servlet.UnavailableException(msg);
209 	 			}
210 			}
211 //			servletResponse.getOutputStream().println("Service reloaded.");
212 // 			return;
213  		}
214         //end hot deploy patch
215         
216         PortalEnvironment env =
217             new PortalEnvironment(servletRequest,
218                                   servletResponse,
219                                   getServletConfig());
220 
221         PortalURL currentURL = env.getRequestedPortalURL();
222         PortalControlParameter control = new PortalControlParameter(currentURL);
223         PortletWindow actionWindow = control.getPortletWindowOfAction();
224         if (actionWindow!=null)
225         {
226             try {
227                 PortletContainerFactory.
228                     getPortletContainer().
229                         processPortletAction(actionWindow,
230                                              ServletObjectAccess.getServletRequest(servletRequest, actionWindow),
231                                              ServletObjectAccess.getServletResponse(servletResponse));
232             }
233             catch (PortletException e)
234             {
235                 log.error(e);
236             }
237             catch (PortletContainerException e)
238             {
239                 log.error(e);
240             }
241             // This catch block is for compliance
242             // of TCK's Portlet.ProcessActionIOExceptionTest
243             catch (Exception e)
244             {
245                 log.error(e);
246             }
247 
248             return; // we issued an redirect, so return directly
249         }
250 
251         try
252         {
253             RootFragment root = PageRegistry.getRootFragment();
254             root.service(servletRequest, servletResponse);
255         }
256         catch (Throwable t)
257         {
258             log.error(t);
259             // nothing to do
260         }
261    
262     }
263 
264     public void doPost (HttpServletRequest request,
265                               HttpServletResponse response) throws IOException, ServletException
266     {
267         doGet (request, response);
268     }
269 
270 }