KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teaservlet > util > ObjectIdentifier


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

52
53 package com.go.teaservlet.util;
54
55 import java.util.*;
56 import java.lang.ref.WeakReference JavaDoc;
57 import com.go.trove.util.IdentityMap;
58
59 /******************************************************************************
60  * ObjectIdentifier assigns unique string identifiers to objects for retrieval.
61  * The string identifier is safe to use in a URL query string for referencing
62  * non-persistent objects in between stateless HTTP transactions. For example,
63  * a web page could present a list of items that, when an item is clicked,
64  * produces another page that shows detail information for the selected item.
65  *
66  * <p>The object identifiers that are assigned are not persistent and will be
67  * different if the process is restarted. This means that a web page in a
68  * browser could contain invalid object identifiers if the web server is
69  * restarted. Users of this class should attempt to gracefully handle cases
70  * where an object cannot be retrieved, or in rare circumstances, the retrieved
71  * object is of an unexpected type.
72  *
73  * <p>ObjectIdentifier is not appropriate for use on objects that already
74  * support a unique identifier, such as objects that can be retrieved from a
75  * database. In that case, use the database's unique identifier for the object
76  * instead, and possibly encrypt it.
77  *
78  * <p>Internally, ObjectIdentifier makes use of weak references so that
79  * identifiers don't prevent objects from being garbage collected.
80  *
81  * @author Brian S O'Neill
82  * @version
83  * <!--$$Revision:--> 7 <!-- $-->, <!--$$JustDate:--> 9/13/00 <!-- $-->
84  * @see java.lang.ref.WeakReference
85  *
86  * @deprecated Use of ObjectIdentifier does not work correctly when Servlet is
87  * striped against multiple processes.
88  */

89 public class ObjectIdentifier {
90     private static ObjectIdentifier cObjectIdentifer = new ObjectIdentifier();
91
92     /**
93      * Returns a URL-safe string that uniquely identifies the given object.
94      */

95     public static String JavaDoc identify(Object JavaDoc obj) {
96         return cObjectIdentifer.getIdentifer(obj);
97     }
98
99     /**
100      * Retrieves an object using an identifer string as produced by the
101      * identify method, or null if not found. Under rare circumstances, the
102      * retrieved object could be an unexpected type, in which case it should be
103      * ignored. Therefore, care must be taken when casting the retrieved
104      * object.
105      */

106     public static Object JavaDoc retrieve(String JavaDoc identifier) {
107         return cObjectIdentifer.getObject(identifier);
108     }
109
110     // Maps Object identity hashcodes to identifer Strings.
111
private Map mIdentifiers;
112
113     // Maps identifer Strings to weakly referenced Objects.
114
private Map mObjects;
115
116     private Random mRandom;
117
118     private ObjectIdentifier() {
119         mIdentifiers = new IdentityMap();
120         mObjects = new WeakHashMap();
121         mRandom = new Random();
122     }
123
124     private synchronized String JavaDoc getIdentifer(Object JavaDoc obj) {
125         String JavaDoc id = (String JavaDoc)mIdentifiers.get(obj);
126
127         if (id == null) {
128             do {
129                 id = Long.toString(mRandom.nextLong() & Long.MAX_VALUE, 36);
130             } while (mObjects.containsKey(id));
131
132             mIdentifiers.put(obj, id);
133             mObjects.put(id, new WeakReference JavaDoc(obj));
134         }
135
136         return id;
137     }
138
139     private synchronized Object JavaDoc getObject(String JavaDoc identifier) {
140         WeakReference JavaDoc ref = (WeakReference JavaDoc)mObjects.get(identifier);
141         return (ref == null) ? null : ref.get();
142     }
143 }
144
Popular Tags