KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Set JavaDoc;
57
58 /**
59  * Abstract base class for implementations that load user attributes.
60  */

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

79   abstract protected Map JavaDoc<String JavaDoc, String JavaDoc> load( PortletRequest request )
80     throws IOException JavaDoc;
81
82   /**
83    * Called just before unload() if the attributes 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 user attributes.
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
109   public void addDefault(String JavaDoc name, String JavaDoc value)
110   {
111     if (_defaultMap == null)
112       _defaultMap = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc>();
113
114     _defaultMap.put(name, value);
115   }
116
117   public void addDefault(NameValuePair nameValuePair)
118   {
119     addDefault(nameValuePair.getName(), nameValuePair.getValue());
120   }
121
122   /**
123    * Add a link such that when the portlet uses the attribute<i>name</i>, the
124    * name that is used with the store is <i>link</i>.
125    */

126   public void addNameLink(String JavaDoc name, String JavaDoc link)
127   {
128     if (_nameLinkMap == null)
129       _nameLinkMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
130
131     _nameLinkMap.put(name, link);
132   }
133
134   public void addNameLink(NameLink nameLink)
135   {
136     addNameLink(nameLink.getName(), nameLink.getLink());
137   }
138
139   public Map JavaDoc<String JavaDoc, String JavaDoc> getUserAttributeMap( PortletRequest request,
140                                                   Set JavaDoc<String JavaDoc> names )
141     throws IOException JavaDoc
142   {
143     Map JavaDoc<String JavaDoc, String JavaDoc> storeMap = load(request);
144
145     StoreUpdateMap<String JavaDoc, String JavaDoc> map = new StoreUpdateMap<String JavaDoc, String JavaDoc>();
146     
147     if (_nameLinkMap != null) {
148       synchronized (_nameLinkMap) {
149         if (_reverseNameLinkMap == null)
150           _reverseNameLinkMap
151             = KeyLinkMap.<String JavaDoc>getReverseKeyLinkMap(_nameLinkMap);
152       }
153     }
154
155     map.start(_nameLinkMap, _reverseNameLinkMap,
156               _defaultMap, storeMap, names, DELETE);
157
158     return map;
159   }
160
161   public void finish( Map JavaDoc<String JavaDoc, String JavaDoc> userAttributeMap )
162     throws IOException JavaDoc
163   {
164     StoreUpdateMap<String JavaDoc, String JavaDoc> map
165       = (StoreUpdateMap<String JavaDoc, String JavaDoc>) userAttributeMap;
166
167     Map JavaDoc<String JavaDoc, String JavaDoc> storeMap = map.getStoreMap();
168     Map JavaDoc<String JavaDoc, String JavaDoc> updateMap = map.getUpdateMap();
169
170     map.finish();
171
172     if (updateMap != null)
173       save(storeMap, updateMap);
174
175     unload(storeMap);
176   }
177
178 }
179
180
Popular Tags