001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005: package org.jasig.portal.container.services.property;
006:
007: import java.util.Map;
008: import java.util.Properties;
009: import java.util.WeakHashMap;
010:
011: import javax.portlet.RenderResponse;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.apache.pluto.om.entity.PortletEntity;
018: import org.apache.pluto.om.portlet.PortletDefinition;
019: import org.apache.pluto.om.window.PortletWindow;
020: import org.apache.pluto.services.property.PropertyManagerService;
021:
022: /**
023: * The {@link PropertyManagerServiceImpl} is used to pass properties to the
024: * portlet so it can read them via it's {@link javax.portlet.PortletRequest#getProperty(java.lang.String)}
025: * methods and so properties set by the portlet via it's {@link javax.portlet.PortletResponse#setProperty(java.lang.String, java.lang.String)}
026: * methods can be read by uPortal.
027: *
028: * Currently all properties set by the portlet are saved into a {@link WeakHashMap}
029: * using the {@link org.apache.pluto.om.window.PortletWindow} as the key. This
030: * should ensure that the old properties aren't stored beyond the life of the
031: * user's session.
032: *
033: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
034: * @version $Revision: 36027 $
035: */
036: public class PropertyManagerServiceImpl implements
037: PropertyManagerService {
038: private final Map propertyMapping = new WeakHashMap();
039: protected Log log = LogFactory.getLog(getClass());
040:
041: /**
042: * Stores the properties in a {@link WeakHashMap} that is keyed off the
043: * {@link PortletWindow} so the properties are removed when the user's
044: * session with the portlet is done.
045: *
046: * @see org.apache.pluto.services.property.PropertyManagerService#setResponseProperties(org.apache.pluto.om.window.PortletWindow, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.util.Map)
047: */
048: public void setResponseProperties(final PortletWindow window,
049: final HttpServletRequest request,
050: final HttpServletResponse response, final Map properties) {
051: synchronized (propertyMapping) {
052: propertyMapping.put(window, properties);
053: }
054: }
055:
056: /**
057: * Gets the properties that have been set by the portlet. This will pass
058: * any properties that the portlet has set in the response back into the
059: * portlet's request.
060: *
061: * This code also provides a uPortal extension that provides the current
062: * cache expiration time for the portlet via the {@link RenderResponse#EXPIRATION_CACHE}
063: * constant.
064: *
065: * @see org.apache.pluto.services.property.PropertyManagerService#getRequestProperties(org.apache.pluto.om.window.PortletWindow, javax.servlet.http.HttpServletRequest)
066: */
067: public Map getRequestProperties(final PortletWindow window,
068: final HttpServletRequest request) {
069: final Map properties = new Properties();
070: Map savedProps = null;
071:
072: synchronized (propertyMapping) {
073: savedProps = (Map) propertyMapping.get(window);
074: }
075:
076: //Copy all the properties into a new map to return.
077: if (savedProps != null)
078: properties.putAll(savedProps);
079:
080: //Make sure the EXPIRATION_CACHE property is set to whatever the current
081: //cache timeout for the portlet is. This is not a required property
082: final String[] exprTime = (String[]) properties
083: .get(RenderResponse.EXPIRATION_CACHE);
084: if (exprTime == null) {
085: final PortletEntity pe = window.getPortletEntity();
086: final PortletDefinition pd = pe.getPortletDefinition();
087: String value = pd.getExpirationCache();
088: try {
089: Integer.parseInt(value);
090: } catch (NumberFormatException e) {
091: log.warn("Expriation cache \"" + value
092: + "\" must be an integer in window " + window
093: + ". " + "Using -1 instead.", e);
094: value = "-1";
095: }
096:
097: //Values MUST be String[]
098: properties.put(RenderResponse.EXPIRATION_CACHE,
099: new String[] { value });
100: }
101:
102: return properties;
103: }
104: }
|