001: /*
002: * Copyright 2004-2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.portletappengine.impl;
014:
015: import com.sun.portal.log.common.PortalLogger;
016: import com.sun.portal.portlet.impl.ActionRequestPool;
017: import com.sun.portal.portlet.impl.ActionResponsePool;
018: import com.sun.portal.portlet.impl.EventRequestPool;
019: import com.sun.portal.portlet.impl.EventResponsePool;
020: import com.sun.portal.portlet.impl.RenderRequestPool;
021: import com.sun.portal.portlet.impl.RenderResponsePool;
022: import com.sun.portal.portletappengine.RequestResponseFactory;
023: import com.sun.portal.portletappengine.ipc.EventRequest;
024: import com.sun.portal.portletappengine.ipc.EventResponse;
025: import com.sun.portal.portletcontainercommon.PortletContainerActionRequest;
026: import com.sun.portal.portletcontainercommon.PortletContainerActionResponse;
027: import com.sun.portal.portletcontainercommon.PortletContainerEventRequest;
028: import com.sun.portal.portletcontainercommon.PortletContainerEventResponse;
029: import com.sun.portal.portletcontainercommon.PortletContainerRenderRequest;
030: import com.sun.portal.portletcontainercommon.PortletContainerRenderResponse;
031: import com.sun.portal.portletcontainercommon.descriptor.PortletDescriptor;
032:
033: import javax.portlet.ActionRequest;
034: import javax.portlet.ActionResponse;
035: import javax.portlet.PortalContext;
036: import javax.portlet.PortletContext;
037: import javax.portlet.RenderRequest;
038: import javax.portlet.RenderResponse;
039: import javax.servlet.ServletContext;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.ByteArrayOutputStream;
043: import java.io.StringWriter;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * The request and response factory is responsible for generating the
049: * Portlet request and response objects for the Portlet Application Servlet
050: * Engine.
051: * <P>
052: * Object pooling is used in order to optimize the process and avoid messive
053: * generation of objects.There are 4 pools the request and response factory
054: * maintains:
055: * <UL>
056: * <LI>Portlet request pool
057: * <LI>Action response pool
058: * <LI>Portlet response pool
059: * </UL>
060: * <P>
061: * The get**() methods are used to get a reusable object from the pools.
062: * <P>
063: * The release**() methods are used to release objects back to the pools.
064: * Clients of this class are responsible for releasing the corresponding
065: * objects by calling these methods.
066: **/
067: public class RequestResponseFactoryImpl implements
068: RequestResponseFactory {
069:
070: // Property names for req/res factory object in servlet context
071:
072: public static final String MIN_SIZE = "minSizeParam";
073: public static final String MAX_SIZE = "maxSizeParam";
074: public static final String PARTITION = "partitionParam";
075:
076: private ActionRequestPool _actionRequestPool;
077: private RenderRequestPool _renderRequestPool;
078: private ActionResponsePool _actionResponsePool;
079: private RenderResponsePool _renderResponsePool;
080: private static Logger _logger = PortalLogger
081: .getLogger(RequestResponseFactoryImpl.class);
082:
083: private EventRequestPool eventRequestPool;
084: private EventResponsePool eventResponsePool;
085: private ServletContext sc = null;
086: //
087: // Request and response pools are initialized to size 0.
088: // items are added to to the pool as they are used
089: // and returned. The pools are allowed to expand
090: // to a very large size.
091: //
092: // We always let the pools offer instances beyond the
093: // max pool size.
094: //
095: static private final int DEFAULT_MIN_SIZE = 0;
096: static private final int DEFAULT_MAX_SIZE = 200;
097: static private final int DEFAULT_PARTITION_SIZE = 10;
098:
099: static private final int ACTION_REQUEST = 1;
100: static private final int RENDER_REQUEST = 2;
101: static private final int ACTION_RESPONSE = 3;
102: static private final int RENDER_RESPONSE = 4;
103:
104: static private final int EVENT_REQUEST = 5;
105: static private final int EVENT_RESPONSE = 6;
106:
107: /**
108: * Initializes the global variables.
109: *
110: * @param context The servlet context
111: */
112: public RequestResponseFactoryImpl(ServletContext context) {
113: sc = context;
114: String min = context.getInitParameter(
115: REQUEST_RESPONSE_FACTORY_PREFIX + "." + MIN_SIZE)
116: .trim();
117:
118: String max = context.getInitParameter(
119: REQUEST_RESPONSE_FACTORY_PREFIX + "." + MAX_SIZE)
120: .trim();
121:
122: String partition = context.getInitParameter(
123: REQUEST_RESPONSE_FACTORY_PREFIX + "." + PARTITION)
124: .trim();
125:
126: _logger.log(Level.FINEST, "PSPL_PAECSPPAI0001", min);
127: _logger.log(Level.FINEST, "PSPL_PAECSPPAI0002", max);
128: _logger.log(Level.FINEST, "PSPL_PAECSPPAI0003", partition);
129: initRequestResponsePool(context, Integer.parseInt(min), Integer
130: .parseInt(max), Integer.parseInt(partition),
131: ACTION_REQUEST);
132: initRequestResponsePool(context, Integer.parseInt(min), Integer
133: .parseInt(max), Integer.parseInt(partition),
134: RENDER_REQUEST);
135: initRequestResponsePool(context, Integer.parseInt(min), Integer
136: .parseInt(max), Integer.parseInt(partition),
137: ACTION_RESPONSE);
138: initRequestResponsePool(context, Integer.parseInt(min), Integer
139: .parseInt(max), Integer.parseInt(partition),
140: RENDER_RESPONSE);
141:
142: initRequestResponsePool(context, Integer.parseInt(min), Integer
143: .parseInt(max), Integer.parseInt(partition),
144: EVENT_REQUEST);
145:
146: initRequestResponsePool(context, Integer.parseInt(min), Integer
147: .parseInt(max), Integer.parseInt(partition),
148: EVENT_RESPONSE);
149: context.setAttribute("_actionRequestPool", _actionRequestPool);
150: context.setAttribute("_renderRequestPool", _renderRequestPool);
151: context
152: .setAttribute("_actionResponsePool",
153: _actionResponsePool);
154: context
155: .setAttribute("_renderResponsePool",
156: _renderResponsePool);
157: context.setAttribute("eventRequestPool", eventRequestPool);
158: context.setAttribute("eventResponsePool", eventResponsePool);
159: }
160:
161: /**
162: * Gets a reusable action request object from the pool.
163: *
164: * @param req The http servlet request object
165: * @param res The http servlet response object
166: *
167: * @return <code>ActionRequest</code>
168: */
169: public ActionRequest getActionRequest(HttpServletRequest req,
170: HttpServletResponse res,
171: PortletContainerActionRequest pContReq,
172: PortletContainerActionResponse pContRes,
173: PortletContext context, PortalContext portalContext,
174: PortletDescriptor pDescriptor, StringWriter writer) {
175: if (_actionRequestPool == null) {
176: _actionRequestPool = (ActionRequestPool) sc
177: .getAttribute("_actionRequestPool");
178: }
179: return _actionRequestPool.obtainObject(req, res, pContReq,
180: pContRes, context, portalContext, pDescriptor, writer);
181: }
182:
183: /**
184: * Gets a reusable render request object from the pool.
185: *
186: * @param req The http servlet request object
187: * @param res The http servlet response object
188: *
189: * @return <code>RenderRequest</code>
190: */
191: public RenderRequest getRenderRequest(HttpServletRequest req,
192: HttpServletResponse res,
193: PortletContainerRenderRequest pContReq,
194: PortletContainerRenderResponse pContRes,
195: PortletContext context, PortalContext portalContext,
196: PortletDescriptor pDescriptor) {
197: if (_renderRequestPool == null) {
198: _renderRequestPool = (RenderRequestPool) sc
199: .getAttribute("_renderRequestPool");
200: }
201: return _renderRequestPool.obtainObject(req, res, pContReq,
202: pContRes, context, portalContext, pDescriptor);
203: }
204:
205: /**
206: * Gets a reusable action response object from the pool.
207: *
208: * @return <code>ActionResponse</code>
209: */
210: public ActionResponse getActionResponse(HttpServletRequest req,
211: HttpServletResponse res, ActionRequest aReq,
212: PortletContainerActionRequest pContReq,
213: PortletContainerActionResponse pContRes) {
214: if (_actionResponsePool == null) {
215: _actionResponsePool = (ActionResponsePool) sc
216: .getAttribute("_actionResponsePool");
217: }
218: return _actionResponsePool.obtainObject(req, res, aReq,
219: pContReq, pContRes);
220:
221: }
222:
223: /**
224: * Gets a reusable portlet response object from the pool.
225: *
226: * @param res The http servlet response object
227: * @param writer The <code>StringWriter</code> that will be used as the
228: * writer when <code>RenderResponse.getWriter()</code> is called.
229: *
230: * @return <code>RenderResponse</code>
231: */
232: public RenderResponse getRenderResponse(HttpServletRequest req,
233: HttpServletResponse res, RenderRequest rReq,
234: PortletContainerRenderRequest pContReq,
235: PortletContainerRenderResponse pContRes,
236: StringWriter writer, ByteArrayOutputStream output,
237: PortletDescriptor pDescriptor) {
238: if (_renderResponsePool == null) {
239: _renderResponsePool = (RenderResponsePool) sc
240: .getAttribute("_renderResponsePool");
241: }
242: return _renderResponsePool.obtainObject(req, res, rReq,
243: pContReq, pContRes, writer, output, pDescriptor);
244: }
245:
246: /**
247: * Releases the action request object back to the pool.
248: *
249: * @param actionReq The <code>ActionRequest</code>
250: */
251: public void releaseActionRequest(ActionRequest actionReq) {
252: _actionRequestPool.releaseObject(actionReq);
253: }
254:
255: /**
256: * Releases the render request object back to the pool.
257: *
258: * @param renderReq The <code>RenderRequest</code>
259: */
260: public void releaseRenderRequest(RenderRequest renderReq) {
261: _renderRequestPool.releaseObject(renderReq);
262: }
263:
264: /**
265: * Releases the action response object back to the pool.
266: *
267: * @param actionRes The <code>ActionResponse</code>
268: */
269: public void releaseActionResponse(ActionResponse actionRes) {
270: _actionResponsePool.releaseObject(actionRes);
271: }
272:
273: /**
274: * Releases the portlet response object back to the pool.
275: *
276: * @param portletRes The <code>RenderResponse</code>
277: */
278: public void releaseRenderResponse(RenderResponse portletRes) {
279: _renderResponsePool.releaseObject(portletRes);
280: }
281:
282: public EventRequest getEventRequest(HttpServletRequest req,
283: HttpServletResponse res,
284: PortletContainerEventRequest pceRequest,
285: PortletContainerEventResponse pceResponse,
286: PortletContext context, PortalContext portalContext,
287: PortletDescriptor pDescriptor) {
288:
289: return eventRequestPool.obtainObject(req, res, pceRequest,
290: pceResponse, context, portalContext, pDescriptor);
291: }
292:
293: public EventResponse getEventResponse(
294: PortletContainerEventResponse pceResponse) {
295: return eventResponsePool.obtainObject(pceResponse);
296: }
297:
298: public void releaseEventRequest(EventRequest ereq) {
299: eventRequestPool.releaseObject(ereq);
300: }
301:
302: public void releaseEventResponse(EventResponse eres) {
303: eventResponsePool.releaseObject(eres);
304: }
305:
306: /**
307: * Initializes the portlet request pool.
308: */
309: private void initRequestResponsePool(ServletContext context,
310: int minSize, int maxSize, int partition, int type) {
311:
312: if (minSize < 0) {
313: _logger.log(Level.FINER, "PSPL_PAECSPPAI0004", new Integer(
314: DEFAULT_MIN_SIZE));
315: minSize = DEFAULT_MIN_SIZE;
316: }
317: if (maxSize < 0) {
318: _logger.log(Level.FINER, "PSPL_PAECSPPAI0005", new Integer(
319: DEFAULT_MAX_SIZE));
320: maxSize = DEFAULT_MAX_SIZE;
321: }
322: if (partition < 0) {
323: _logger.log(Level.FINER, "PSPL_PAECSPPAI0006", new Integer(
324: DEFAULT_PARTITION_SIZE));
325: partition = DEFAULT_PARTITION_SIZE;
326: }
327: if (minSize > maxSize) {
328: _logger.log(Level.FINER, "PSPL_PAECSPPAI0007", new Integer(
329: DEFAULT_MAX_SIZE));
330: minSize = DEFAULT_MAX_SIZE;
331: }
332: if (partition > maxSize) {
333: _logger.log(Level.FINER, "PSPL_PAECSPPAI0008", new Integer(
334: DEFAULT_MAX_SIZE));
335: partition = DEFAULT_MAX_SIZE;
336: }
337:
338: switch (type) {
339:
340: case ACTION_REQUEST:
341: _actionRequestPool = new ActionRequestPool(context,
342: minSize, maxSize, true, partition, _logger);
343: break;
344:
345: case RENDER_REQUEST:
346: _renderRequestPool = new RenderRequestPool(context,
347: minSize, maxSize, true, partition, _logger);
348: break;
349:
350: case ACTION_RESPONSE:
351: _actionResponsePool = new ActionResponsePool(minSize,
352: maxSize, true, partition, _logger);
353: break;
354:
355: case RENDER_RESPONSE:
356: _renderResponsePool = new RenderResponsePool(minSize,
357: maxSize, true, partition, _logger);
358: case EVENT_REQUEST:
359: eventRequestPool = new EventRequestPool(context, minSize,
360: maxSize, true, partition, _logger);
361: case EVENT_RESPONSE:
362: eventResponsePool = new EventResponsePool(minSize, maxSize,
363: true, partition, _logger);
364:
365: break;
366: }
367:
368: }
369:
370: /**
371: * Clears the pool objects.
372: */
373: public void destroy() {
374: _actionRequestPool = null;
375: _renderRequestPool = null;
376: _actionResponsePool = null;
377: _renderResponsePool = null;
378: }
379: }
|