1 48 49 package com.caucho.portal.generic; 50 51 import javax.portlet.PortletException; 52 import javax.servlet.ServletConfig ; 53 import javax.servlet.ServletException ; 54 import javax.servlet.http.HttpServlet ; 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import java.io.IOException ; 58 import java.lang.reflect.Constructor ; 59 import java.lang.reflect.Modifier ; 60 import java.util.logging.Logger ; 61 62 80 public class GenericPortalServlet 81 extends HttpServlet 82 { 83 static protected final Logger log = 84 Logger.getLogger(GenericPortalServlet.class.getName()); 85 86 private Portal _portal; 87 private HttpPortletContext _portletContext; 88 89 private GenericLayoutWindow _layout; 90 91 94 public void setPortal(Portal portal) 95 { 96 if (_portal != null) 97 throw new IllegalArgumentException ("`portal' already set"); 98 99 _portal = portal; 100 } 101 102 106 public void setPortalClass(String className) 107 { 108 setPortal( (Portal) newInstance(Portal.class, className) ); 109 } 110 111 116 public void setLayout(GenericLayoutWindow layout) 117 { 118 if (_layout != null) 119 throw new IllegalArgumentException ("`layout' is already set"); 120 121 _layout = layout; 122 } 123 124 125 129 public void setLayoutClass(String className) 130 { 131 setLayout( (GenericLayoutWindow) newInstance( GenericLayoutWindow.class, 132 className ) ); 133 } 134 135 public void init(ServletConfig servletConfig) 136 throws ServletException 137 { 138 super.init(servletConfig); 139 140 String 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 ("`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 (ex); 163 } 164 } 165 166 protected Object newInstance(Class targetClass, String className) 167 throws IllegalArgumentException 168 { 169 Class cl = null; 170 171 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 172 173 try { 174 cl = Class.forName(className, false, loader); 175 } catch (ClassNotFoundException e) { 176 } 177 178 if (cl == null) 179 throw new IllegalArgumentException ( 180 "`" + className + "' is not a known class"); 181 182 if (!targetClass.isAssignableFrom(cl)) 183 throw new IllegalArgumentException ( 184 "'" + className + "' must implement " + targetClass.getName()); 185 186 if (Modifier.isAbstract(cl.getModifiers())) 187 throw new IllegalArgumentException ( 188 "'" + className + "' must not be abstract."); 189 190 if (!Modifier.isPublic(cl.getModifiers())) 191 throw new IllegalArgumentException ( 192 "'" + className + "' must be public."); 193 194 Constructor []constructors = cl.getDeclaredConstructors(); 195 196 Constructor 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 ( 206 "'" + className + "' must have a public zero arg constructor"); 207 208 Object obj = null; 209 210 try { 211 obj = cl.newInstance(); 212 } 213 catch (Exception ex) { 214 throw new IllegalArgumentException ( 215 "error instantiating `" + className + "': " + ex.toString(), ex); 216 } 217 218 return obj; 219 } 220 221 protected void doGet(HttpServletRequest req, HttpServletResponse res) 222 throws ServletException , IOException 223 { 224 doRequest(req, res); 225 } 226 227 protected void doPost(HttpServletRequest req, HttpServletResponse res) 228 throws ServletException , IOException 229 { 230 doRequest(req, res); 231 } 232 233 protected void doRequest(HttpServletRequest httpRequest, 234 HttpServletResponse httpResponse) 235 throws ServletException , IOException 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 (ex); 247 } 248 finally { 249 connection.finish(); 250 } 251 } 252 253 protected void doRequest(PortletConnection connection) 254 throws PortletException, IOException 255 { 256 } 257 public void destroy() 258 { 259 if (_layout != null) 260 _layout.destroy(); 261 } 262 } 263 264 | Popular Tags |