KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > AbstractPreferencesStore


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49 package com.caucho.portal.generic;
50
51 import javax.portlet.PortletRequest;
52 import java.io.IOException JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.LinkedHashMap JavaDoc;
55 import java.util.Map JavaDoc;
56
57 /**
58  * Abstract base class for implementations that load and save preferences.
59  */

60 abstract public class AbstractPreferencesStore
61   implements PreferencesStore
62 {
63   protected static String JavaDoc[] DELETE = new String JavaDoc[] { "<delete>" };
64
65   private LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc[]> _defaultMap;
66   private HashMap JavaDoc<String JavaDoc, String JavaDoc> _nameLinkMap;
67   private Map JavaDoc<String JavaDoc, String JavaDoc> _reverseNameLinkMap;
68
69   /**
70    * Load the user preferences, called once for each connection.
71    * Implementing classes use the <i>request</i> to determine the identity
72    * of the user.
73    *
74    * @return a Map<String, String[]> of all the user preferences that are
75    * available for the user identified by <i>request</i>, null
76    * if no preferences are available.
77    */

78   abstract protected Map JavaDoc<String JavaDoc, String JavaDoc[]> load( PortletRequest request,
79                                                  String JavaDoc namespace )
80     throws IOException JavaDoc;
81
82   /**
83    * Called just before unload() if the preferences have been updated.
84    * The updateMap contains only entries that have changed. Entries with a
85    * value of DELETE are meant to be deleted from the store.
86    *
87    * @param storeMap the map returned by load()
88    *
89    * @param updateMap the map containing the updates
90    *
91    * @throws UnsupportedOperationException if the implementation does not
92    * support updates to preferences.
93    */

94   abstract protected void save( Map JavaDoc<String JavaDoc, String JavaDoc[]> storeMap,
95                                 Map JavaDoc<String JavaDoc, String JavaDoc[]> updateMap )
96     throws IOException JavaDoc;
97
98   /**
99    * Called when the connection is complete.
100    *
101    * A call to unload() is not guaranteed to occur for every connection,
102    * if an error occurs when processing a request unload() may never be called.
103    *
104    * @param isModified true if the map has been changed
105    */

106   abstract protected void unload(Map JavaDoc<String JavaDoc, String JavaDoc[]> map);
107
108   public void addDefault(String JavaDoc name, String JavaDoc value)
109   {
110     if (_defaultMap == null)
111       _defaultMap = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
112
113     String JavaDoc[] values = _defaultMap.get(name);
114
115     if (values == null)
116       values = new String JavaDoc[] { value };
117     else {
118       String JavaDoc[] newValues = new String JavaDoc[values.length + 1];
119
120       int i = 0;
121       for (; i < values.length; i++) {
122         newValues[i] = values[i];
123       }
124
125       newValues[i] = value;
126     }
127
128     _defaultMap.put(name, values);
129   }
130
131   public void addDefault(NameValuePair nameValuePair)
132   {
133     addDefault(nameValuePair.getName(), nameValuePair.getValue());
134   }
135
136   /**
137    * Add a link such that when the portlet uses the preferences <i>name</i>,
138    * the name that is used with the store is <i>link</i>.
139    */

140   public void addNameLink(String JavaDoc name, String JavaDoc link)
141   {
142     if (_nameLinkMap == null)
143       _nameLinkMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
144
145     _nameLinkMap.put(name, link);
146   }
147
148   public void addNameLink(NameLink nameLink)
149   {
150     addNameLink(nameLink.getName(), nameLink.getLink());
151   }
152
153   public Map JavaDoc<String JavaDoc, String JavaDoc[]> getPreferencesMap( PortletRequest request,
154                                                   String JavaDoc namespace )
155     throws IOException JavaDoc
156   {
157     Map JavaDoc<String JavaDoc, String JavaDoc[]> storeMap = load(request, namespace);
158
159     StoreUpdateMap<String JavaDoc, String JavaDoc[]> map
160       = new StoreUpdateMap<String JavaDoc, String JavaDoc[]>();
161     
162     if (_nameLinkMap != null) {
163       synchronized (_nameLinkMap) {
164         if (_reverseNameLinkMap == null)
165           _reverseNameLinkMap
166             = KeyLinkMap.<String JavaDoc>getReverseKeyLinkMap(_nameLinkMap);
167       }
168     }
169
170     map.start(_nameLinkMap, _reverseNameLinkMap,
171               _defaultMap, storeMap, null, DELETE);
172
173     return map;
174   }
175
176   public void finish( Map JavaDoc<String JavaDoc, String JavaDoc[]> preferencesMap )
177     throws IOException JavaDoc
178   {
179     StoreUpdateMap<String JavaDoc, String JavaDoc[]> map
180       = (StoreUpdateMap<String JavaDoc, String JavaDoc[]>) preferencesMap;
181
182     Map JavaDoc<String JavaDoc, String JavaDoc[]> storeMap = map.getStoreMap();
183     Map JavaDoc<String JavaDoc, String JavaDoc[]> updateMap = map.getUpdateMap();
184
185     map.finish();
186
187     if (updateMap != null)
188       save(storeMap, updateMap);
189
190     unload(storeMap);
191   }
192 }
193
194
Popular Tags