KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > Servlet


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 JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import javax.portlet.PortletException;
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc
51 {
52
53     private static String JavaDoc CONTENT_TYPE = "text/html";
54
55     private Logger log = null;
56
57     public String JavaDoc getServletInfo()
58     {
59         return "portalImpl - Pluto Driver";
60     }
61
62     public void init (ServletConfig JavaDoc config) throws ServletException JavaDoc
63     {
64         super.init (config);
65
66         String JavaDoc 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 JavaDoc exc)
76         {
77             log ("Initialization failed!", exc);
78
79             throw (new javax.servlet.UnavailableException JavaDoc ("Initialization of one or more services failed."));
80         }
81
82         try {
83
84             ServiceManager.postInit(config);
85
86         } catch (Throwable JavaDoc expos) {
87             
88             log ("Post initialization failed!", expos);
89
90             throw (new javax.servlet.UnavailableException JavaDoc ("Post initialization of one or more services failed."));
91         }
92
93         log = Log.getService().getLogger(getClass());
94
95         if (!PortletContainerFactory.getPortletContainer().isInitialized()) {
96             String JavaDoc 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 JavaDoc properties = new Properties JavaDoc();
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 JavaDoc ("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 JavaDoc t)
148         {
149             log ("Destruction failed!", t);
150         }
151     }
152
153     public void doGet (HttpServletRequest JavaDoc servletRequest,
154                              HttpServletResponse JavaDoc servletResponse) throws IOException JavaDoc, ServletException JavaDoc
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 JavaDoc 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 JavaDoc t)
199         {
200             // nothing to do
201
}
202    
203     }
204
205     public void doPost (HttpServletRequest JavaDoc request,
206                               HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
207     {
208         doGet (request, response);
209     }
210
211 }
212
Popular Tags