001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import javax.portlet.PortletRequest;
052: import java.io.IOException;
053: import java.util.HashMap;
054: import java.util.LinkedHashMap;
055: import java.util.Map;
056: import java.util.Set;
057:
058: /**
059: * Abstract base class for implementations that load user attributes.
060: */
061: abstract public class AbstractUserAttributeStore implements
062: UserAttributeStore {
063: protected static String DELETE = "<delete>";
064:
065: private Map<String, String> _defaultMap;
066: private Map<String, String> _nameLinkMap;
067: private Map<String, String> _reverseNameLinkMap;
068:
069: /**
070: * Load the user attributes, called once for each connection.
071: * Implementing classes use the <i>request</i> to determine the identity
072: * of the user.
073: *
074: * @return a Map<String, String> of all the user attribute names that are
075: * available for the user identified by <i>request</i>, null
076: * if no attributes are available.
077: */
078: abstract protected Map<String, String> load(PortletRequest request)
079: throws IOException;
080:
081: /**
082: * Called just before unload() if the attributes have been updated.
083: * The updateMap contains only entries that have changed. Entries with a
084: * value of DELETE are meant to be deleted from the store.
085: *
086: * @param storeMap the map returned by load()
087: *
088: * @param updateMap the map containing the updates
089: *
090: * @throws UnsupportedOperationException if the implementation does not
091: * support updates to user attributes.
092: */
093: abstract protected void save(Map<String, String> storeMap,
094: Map<String, String> updateMap) throws IOException;
095:
096: /**
097: * Called when the connection is complete.
098: *
099: * A call to unload() is not guaranteed to occur for every connection,
100: * if an error occurs when processing a request unload() may never be called.
101: *
102: * @param isModified true if the map has been changed
103: */
104: abstract protected void unload(Map<String, String> map);
105:
106: public void addDefault(String name, String value) {
107: if (_defaultMap == null)
108: _defaultMap = new LinkedHashMap<String, String>();
109:
110: _defaultMap.put(name, value);
111: }
112:
113: public void addDefault(NameValuePair nameValuePair) {
114: addDefault(nameValuePair.getName(), nameValuePair.getValue());
115: }
116:
117: /**
118: * Add a link such that when the portlet uses the attribute<i>name</i>, the
119: * name that is used with the store is <i>link</i>.
120: */
121: public void addNameLink(String name, String link) {
122: if (_nameLinkMap == null)
123: _nameLinkMap = new HashMap<String, String>();
124:
125: _nameLinkMap.put(name, link);
126: }
127:
128: public void addNameLink(NameLink nameLink) {
129: addNameLink(nameLink.getName(), nameLink.getLink());
130: }
131:
132: public Map<String, String> getUserAttributeMap(
133: PortletRequest request, Set<String> names)
134: throws IOException {
135: Map<String, String> storeMap = load(request);
136:
137: StoreUpdateMap<String, String> map = new StoreUpdateMap<String, String>();
138:
139: if (_nameLinkMap != null) {
140: synchronized (_nameLinkMap) {
141: if (_reverseNameLinkMap == null)
142: _reverseNameLinkMap = KeyLinkMap
143: .<String> getReverseKeyLinkMap(_nameLinkMap);
144: }
145: }
146:
147: map.start(_nameLinkMap, _reverseNameLinkMap, _defaultMap,
148: storeMap, names, DELETE);
149:
150: return map;
151: }
152:
153: public void finish(Map<String, String> userAttributeMap)
154: throws IOException {
155: StoreUpdateMap<String, String> map = (StoreUpdateMap<String, String>) userAttributeMap;
156:
157: Map<String, String> storeMap = map.getStoreMap();
158: Map<String, String> updateMap = map.getUpdateMap();
159:
160: map.finish();
161:
162: if (updateMap != null)
163: save(storeMap, updateMap);
164:
165: unload(storeMap);
166: }
167:
168: }
|