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:
057: /**
058: * Abstract base class for implementations that load and save preferences.
059: */
060: abstract public class AbstractPreferencesStore implements
061: PreferencesStore {
062: protected static String[] DELETE = new String[] { "<delete>" };
063:
064: private LinkedHashMap<String, String[]> _defaultMap;
065: private HashMap<String, String> _nameLinkMap;
066: private Map<String, String> _reverseNameLinkMap;
067:
068: /**
069: * Load the user preferences, called once for each connection.
070: * Implementing classes use the <i>request</i> to determine the identity
071: * of the user.
072: *
073: * @return a Map<String, String[]> of all the user preferences that are
074: * available for the user identified by <i>request</i>, null
075: * if no preferences are available.
076: */
077: abstract protected Map<String, String[]> load(
078: PortletRequest request, String namespace)
079: throws IOException;
080:
081: /**
082: * Called just before unload() if the preferences 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 preferences.
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: String[] values = _defaultMap.get(name);
111:
112: if (values == null)
113: values = new String[] { value };
114: else {
115: String[] newValues = new String[values.length + 1];
116:
117: int i = 0;
118: for (; i < values.length; i++) {
119: newValues[i] = values[i];
120: }
121:
122: newValues[i] = value;
123: }
124:
125: _defaultMap.put(name, values);
126: }
127:
128: public void addDefault(NameValuePair nameValuePair) {
129: addDefault(nameValuePair.getName(), nameValuePair.getValue());
130: }
131:
132: /**
133: * Add a link such that when the portlet uses the preferences <i>name</i>,
134: * the name that is used with the store is <i>link</i>.
135: */
136: public void addNameLink(String name, String link) {
137: if (_nameLinkMap == null)
138: _nameLinkMap = new HashMap<String, String>();
139:
140: _nameLinkMap.put(name, link);
141: }
142:
143: public void addNameLink(NameLink nameLink) {
144: addNameLink(nameLink.getName(), nameLink.getLink());
145: }
146:
147: public Map<String, String[]> getPreferencesMap(
148: PortletRequest request, String namespace)
149: throws IOException {
150: Map<String, String[]> storeMap = load(request, namespace);
151:
152: StoreUpdateMap<String, String[]> map = new StoreUpdateMap<String, String[]>();
153:
154: if (_nameLinkMap != null) {
155: synchronized (_nameLinkMap) {
156: if (_reverseNameLinkMap == null)
157: _reverseNameLinkMap = KeyLinkMap
158: .<String> getReverseKeyLinkMap(_nameLinkMap);
159: }
160: }
161:
162: map.start(_nameLinkMap, _reverseNameLinkMap, _defaultMap,
163: storeMap, null, DELETE);
164:
165: return map;
166: }
167:
168: public void finish(Map<String, String[]> preferencesMap)
169: throws IOException {
170: StoreUpdateMap<String, String[]> map = (StoreUpdateMap<String, String[]>) preferencesMap;
171:
172: Map<String, String[]> storeMap = map.getStoreMap();
173: Map<String, String[]> updateMap = map.getUpdateMap();
174:
175: map.finish();
176:
177: if (updateMap != null)
178: save(storeMap, updateMap);
179:
180: unload(storeMap);
181: }
182: }
|