Source code: net/jxta/util/endpoint/WeakMessengerCache.java
1 /************************************************************************
2 *
3 * $Id: WeakMessengerCache.java,v 1.4 2004/11/09 21:24:18 bondolo Exp $
4 *
5 * Copyright (c) 2003 Sun Microsystems, Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Sun Microsystems, Inc. for Project JXTA."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
27 * must not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact Project JXTA at http://www.jxta.org.
30 *
31 * 5. Products derived from this software may not be called "JXTA",
32 * nor may "JXTA" appear in their name, without prior written
33 * permission of Sun.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of Project JXTA. For more
52 * information on Project JXTA, please see
53 * <http://www.jxta.org/>.
54 *
55 * This license is based on the BSD license adopted by the Apache Foundation.
56 *********************************************************************************/
57
58 package net.jxta.util.endpoint;
59
60 import java.util.Map;
61 import java.util.WeakHashMap;
62 import java.lang.ref.WeakReference;
63
64 import net.jxta.endpoint.EndpointAddress;
65 import net.jxta.endpoint.Messenger;
66
67 /**
68 * This class implements a cache of Messenger, indexed by
69 * their EndpointAddress based on WeakReference.
70 *
71 * <p/>The life time of an Messenger in the cache is the same as the
72 * life time of the Messenger itself.
73 *
74 * <p/>This allow applications and services to reuse an existing Messenger
75 * for sending a new message.
76 *
77 * @deprecated The new endpoint implemenation introduced in JXTA J2SE 2.3
78 * makes this cache largely unnecessary. It should not be used in new code.
79 */
80
81 public class WeakMessengerCache extends WeakHashMap {
82
83 /**
84 * Default Constructor
85 **/
86 public WeakMessengerCache () {
87 super();
88 }
89
90 /**
91 * Constructor which includes a capacity hint.
92 **/
93 public WeakMessengerCache (int initialCapacity) {
94 super(initialCapacity);
95 }
96
97 /**
98 * Constructor which includes a capacity hint and a required loading factor.
99 **/
100 public WeakMessengerCache (int initialCapacity, float loadFactor) {
101 super(initialCapacity, loadFactor);
102 }
103
104 /**
105 * Constructor which includes an initial map content.
106 **/
107 public WeakMessengerCache (Map t) {
108 super(t);
109 }
110
111 /**
112 * Cache an Messenger. If there was already an Messenger cached
113 * for the same EndpointAddress, the old cached EndpointMeseenger it returned,
114 * and messenger Messenger is cached.
115 * This method is not thread-safe: if required, synchronization must
116 * be performed by the caller.
117 *
118 * @param messenger Messenger to be cached.
119 * @return messenger A previously cached Messenger or null.
120 */
121 public Messenger putMessenger (Messenger messenger) {
122
123 if (messenger == null) {
124 throw new RuntimeException ("null Messenger");
125 }
126 WeakReference oldRef = (WeakReference) put (messenger.getDestinationAddressObject(),
127 new WeakReference (messenger));
128
129 if (oldRef != null) {
130 return (Messenger) oldRef.get();
131 }
132
133 return null;
134 }
135
136 /**
137 * Return, if any, a cached Messenger.
138 * This method is not thread-safe: if required, synchronization must
139 * be performed by the caller.
140 *
141 * @param addr Destination EndpointAddress
142 * @return Messenger cached Messenger, or null.
143 **/
144 public Messenger getMessenger (EndpointAddress addr) {
145
146 WeakReference ref = (WeakReference) get (addr);
147
148 if (ref == null) {
149 // No cached Messenger for addr
150 return null;
151 }
152 return (Messenger) ref.get();
153 }
154 }
155
156
157