KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > GenericPortalServlet


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49 package com.caucho.portal.generic;
50
51 import javax.portlet.PortletException;
52 import javax.servlet.ServletConfig JavaDoc;
53 import javax.servlet.ServletException JavaDoc;
54 import javax.servlet.http.HttpServlet JavaDoc;
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import java.io.IOException JavaDoc;
58 import java.lang.reflect.Constructor JavaDoc;
59 import java.lang.reflect.Modifier JavaDoc;
60 import java.util.logging.Logger JavaDoc;
61
62 /**
63  * This servlet supports the following configuration items.
64  * Items marked with a * can be set as init-param.
65  *
66  * <dl>
67  * <dd>portal
68  * <dt>an instance of {@link Portal}, default is an instance of
69  * {@link GenericPortal}.
70  * <dt>portal-class*
71  * <dd>a class name, an alternative to <code>portal</code>
72  * <dd>layout
73  * <dt>an instance of {@link GenericLayoutWindow}, required
74  * <dt>layout-class*
75  * <dd>a class name, an alternative to <code>layout</code>
76  *
77  * </dl>
78  * </pre>
79  */

80 public class GenericPortalServlet
81   extends HttpServlet JavaDoc
82 {
83   static protected final Logger JavaDoc log =
84     Logger.getLogger(GenericPortalServlet.class.getName());
85
86   private Portal _portal;
87   private HttpPortletContext _portletContext;
88
89   private GenericLayoutWindow _layout;
90
91   /**
92    * Default is an instance of {@link GenericPortal}.
93    */

94   public void setPortal(Portal portal)
95   {
96     if (_portal != null)
97       throw new IllegalArgumentException JavaDoc("`portal' already set");
98
99     _portal = portal;
100   }
101
102   /**
103    * An alternative to {@link #setPortal(Portal)}, specify the class
104    * name of an object to instantiate
105    */

106   public void setPortalClass(String JavaDoc className)
107   {
108     setPortal( (Portal) newInstance(Portal.class, className) );
109   }
110
111   /**
112    * The layout, required. This method can be called in derived classes,
113    * or through the use of dependency injection on containers that support it,
114    * or indirectly using the init param `layout-class'.
115    */

116   public void setLayout(GenericLayoutWindow layout)
117   {
118     if (_layout != null)
119       throw new IllegalArgumentException JavaDoc("`layout' is already set");
120
121     _layout = layout;
122   }
123
124
125   /**
126    * An alternative to {@link #setLayout(GenericLayoutWindow)}, specify the
127    * class name of an object to instantiate
128    */

129   public void setLayoutClass(String JavaDoc className)
130   {
131     setLayout( (GenericLayoutWindow) newInstance( GenericLayoutWindow.class,
132                                                   className ) );
133   }
134
135   public void init(ServletConfig JavaDoc servletConfig)
136     throws ServletException JavaDoc
137   {
138     super.init(servletConfig);
139
140     String JavaDoc p;
141
142     p = super.getInitParameter("portal-class");
143     if (p != null)
144       setPortalClass( p );
145
146     if (_portal == null)
147       _portal = new GenericPortal();
148
149     p = super.getInitParameter("layout-class");
150     if (p != null)
151       setLayoutClass( p );
152
153     if (_layout == null)
154       throw new ServletException JavaDoc("`layout' is required");
155
156     _portletContext = new HttpPortletContext(getServletContext());
157
158     try {
159       _layout.init(_portletContext);
160     }
161     catch (PortletException ex) {
162       throw new ServletException JavaDoc(ex);
163     }
164   }
165
166   protected Object JavaDoc newInstance(Class JavaDoc targetClass, String JavaDoc className)
167     throws IllegalArgumentException JavaDoc
168   {
169     Class JavaDoc cl = null;
170
171     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
172
173     try {
174       cl = Class.forName(className, false, loader);
175     } catch (ClassNotFoundException JavaDoc e) {
176     }
177
178     if (cl == null)
179       throw new IllegalArgumentException JavaDoc(
180           "`" + className + "' is not a known class");
181
182     if (!targetClass.isAssignableFrom(cl))
183       throw new IllegalArgumentException JavaDoc(
184           "'" + className + "' must implement " + targetClass.getName());
185
186     if (Modifier.isAbstract(cl.getModifiers()))
187       throw new IllegalArgumentException JavaDoc(
188           "'" + className + "' must not be abstract.");
189
190     if (!Modifier.isPublic(cl.getModifiers()))
191       throw new IllegalArgumentException JavaDoc(
192           "'" + className + "' must be public.");
193
194     Constructor JavaDoc []constructors = cl.getDeclaredConstructors();
195
196     Constructor JavaDoc zeroArg = null;
197     for (int i = 0; i < constructors.length; i++) {
198       if (constructors[i].getParameterTypes().length == 0) {
199         zeroArg = constructors[i];
200         break;
201       }
202     }
203
204     if (zeroArg == null || !Modifier.isPublic(zeroArg.getModifiers()))
205       throw new IllegalArgumentException JavaDoc(
206           "'" + className + "' must have a public zero arg constructor");
207
208     Object JavaDoc obj = null;
209
210     try {
211       obj = cl.newInstance();
212     }
213     catch (Exception JavaDoc ex) {
214       throw new IllegalArgumentException JavaDoc(
215           "error instantiating `" + className + "': " + ex.toString(), ex);
216     }
217
218     return obj;
219   }
220
221   protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
222     throws ServletException JavaDoc, IOException JavaDoc
223   {
224     doRequest(req, res);
225   }
226
227   protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
228     throws ServletException JavaDoc, IOException JavaDoc
229   {
230     doRequest(req, res);
231   }
232
233   protected void doRequest(HttpServletRequest JavaDoc httpRequest,
234                            HttpServletResponse JavaDoc httpResponse)
235     throws ServletException JavaDoc, IOException JavaDoc
236   {
237     HttpPortletConnection connection = new HttpPortletConnection();
238     connection.start(_portal, _portletContext, httpRequest, httpResponse, true);
239
240     try {
241       _layout.processAction(connection);
242       _layout.render(connection);
243       connection.checkForFailure();
244     }
245     catch (PortletException ex) {
246       throw new ServletException JavaDoc(ex);
247     }
248     finally {
249       connection.finish();
250     }
251   }
252
253   protected void doRequest(PortletConnection connection)
254     throws PortletException, IOException JavaDoc
255   {
256   }
257   public void destroy()
258   {
259     if (_layout != null)
260       _layout.destroy();
261   }
262 }
263
264
Popular Tags