KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > naming > NonSerializableFactory


1  /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.naming;
23
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.NameAlreadyBoundException JavaDoc;
32 import javax.naming.NameNotFoundException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.RefAddr JavaDoc;
36 import javax.naming.StringRefAddr JavaDoc;
37 import javax.naming.spi.ObjectFactory JavaDoc;
38
39 /**
40  * A utility class that allows one to bind a non-serializable object into a
41  * local JNDI context. The binding will only be valid for the lifetime of the
42  * VM in which the JNDI InitialContext lives. An example usage code snippet is:
43  *
44 <pre>
45     // The non-Serializable object to bind
46     Object nonserializable = ...;
47     // An arbitrary key to use in the StringRefAddr. The best key is the jndi
48     // name that the object will be bound under.
49     String key = ...;
50     // This places nonserializable into the NonSerializableFactory hashmap under key
51     NonSerializableFactory.rebind(key, nonserializable);
52
53     Context ctx = new InitialContext();
54     // Bind a reference to nonserializable using NonSerializableFactory as the ObjectFactory
55     String className = nonserializable.getClass().getName();
56     String factory = NonSerializableFactory.class.getName();
57     StringRefAddr addr = new StringRefAddr("nns", key);
58     Reference memoryRef = new Reference(className, addr, factory, null);
59     ctx.rebind(key, memoryRef);
60 </pre>
61  *
62  * Or you can use the {@link #rebind(Context, String, Object)} convenience
63  * method to simplify the number of steps to:
64  *
65 <pre>
66     Context ctx = new InitialContext();
67     // The non-Serializable object to bind
68     Object nonserializable = ...;
69     // The jndiName that the object will be bound into ctx with
70     String jndiName = ...;
71     // This places nonserializable into the NonSerializableFactory hashmap under key
72     NonSerializableFactory.rebind(ctx, jndiName, nonserializable);
73 </pre>
74  *
75  * To unbind the object, use the following code snippet:
76  *
77 <pre>
78     new InitialContext().unbind(key);
79     NonSerializableFactory.unbind(key);
80 </pre>
81  *
82  * @see javax.naming.spi.ObjectFactory
83  * @see #rebind(Context, String, Object)
84  *
85  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
86  * @version $Revision: 1958 $
87 */

88 public class NonSerializableFactory implements ObjectFactory JavaDoc
89 {
90     private static Map JavaDoc wrapperMap = Collections.synchronizedMap(new HashMap JavaDoc());
91
92     /** Place an object into the NonSerializableFactory namespace for subsequent
93     access by getObject. There cannot be an already existing binding for key.
94
95     @param key, the name to bind target under. This should typically be the
96     name that will be used to bind target in the JNDI namespace, but it does
97     not have to be.
98     @param target, the non-Serializable object to bind.
99     @throws NameAlreadyBoundException thrown if key already exists in the
100      NonSerializableFactory map
101     */

102     public static synchronized void bind(String JavaDoc key, Object JavaDoc target) throws NameAlreadyBoundException JavaDoc
103     {
104         if( wrapperMap.containsKey(key) == true )
105             throw new NameAlreadyBoundException JavaDoc(key+" already exists in the NonSerializableFactory map");
106         wrapperMap.put(key, target);
107     }
108     /** Place or replace an object in the NonSerializableFactory namespce
109      for subsequent access by getObject. Any existing binding for key will be
110      replaced by target.
111
112     @param key, the name to bind target under. This should typically be the
113     name that will be used to bind target in the JNDI namespace, but it does
114     not have to be.
115     @param target, the non-Serializable object to bind.
116     */

117     public static void rebind(String JavaDoc key, Object JavaDoc target)
118     {
119         wrapperMap.put(key, target);
120     }
121
122     /** Remove a binding from the NonSerializableFactory map.
123
124     @param key, the key into the NonSerializableFactory map to remove.
125     @throws NameNotFoundException thrown if key does not exist in the
126      NonSerializableFactory map
127     */

128     public static void unbind(String JavaDoc key) throws NameNotFoundException JavaDoc
129     {
130         if( wrapperMap.remove(key) == null )
131             throw new NameNotFoundException JavaDoc(key+" was not found in the NonSerializableFactory map");
132     }
133     /** Remove a binding from the NonSerializableFactory map.
134
135     @param name, the name for the key into NonSerializableFactory map to remove.
136      The key is obtained as name.toString().
137     @throws NameNotFoundException thrown if key does not exist in the
138      NonSerializableFactory map
139     */

140     public static void unbind(Name JavaDoc name) throws NameNotFoundException JavaDoc
141     {
142         String JavaDoc key = name.toString();
143         if( wrapperMap.remove(key) == null )
144             throw new NameNotFoundException JavaDoc(key+" was not found in the NonSerializableFactory map");
145     }
146
147     /** Lookup a value from the NonSerializableFactory map.
148     @return the object bound to key is one exists, null otherwise.
149     */

150     public static Object JavaDoc lookup(String JavaDoc key)
151     {
152         Object JavaDoc value = wrapperMap.get(key);
153         return value;
154     }
155     /** Lookup a value from the NonSerializableFactory map.
156     @return the object bound to key is one exists, null otherwise.
157     */

158     public static Object JavaDoc lookup(Name JavaDoc name)
159     {
160         String JavaDoc key = name.toString();
161         Object JavaDoc value = wrapperMap.get(key);
162         return value;
163     }
164
165     /** A convience method that simplifies the process of rebinding a
166         non-zerializable object into a JNDI context.
167
168     @param ctx, the JNDI context to rebind to.
169     @param key, the key to use in both the NonSerializableFactory map and JNDI. It
170         must be a valid name for use in ctx.bind().
171     @param target, the non-Serializable object to bind.
172     @throws NamingException thrown on failure to rebind key into ctx.
173     */

174     public static synchronized void rebind(Context JavaDoc ctx, String JavaDoc key, Object JavaDoc target) throws NamingException JavaDoc
175     {
176         NonSerializableFactory.rebind(key, target);
177         // Bind a reference to target using NonSerializableFactory as the ObjectFactory
178
String JavaDoc className = target.getClass().getName();
179         String JavaDoc factory = NonSerializableFactory.class.getName();
180         StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", key);
181         Reference JavaDoc memoryRef = new Reference JavaDoc(className, addr, factory, null);
182         ctx.rebind(key, memoryRef);
183     }
184
185    /** A convience method that simplifies the process of rebinding a
186     non-zerializable object into a JNDI context. This version binds the
187     target object into the default IntitialContext using name path.
188
189    @param name, the name to use as JNDI path name. The key into the
190     NonSerializableFactory map is obtained from the toString() value of name.
191     The name parameter cannot be a 0 length name.
192     Any subcontexts between the root and the name must exist.
193    @param target, the non-Serializable object to bind.
194    @throws NamingException thrown on failure to rebind key into ctx.
195    */

196    public static synchronized void rebind(Name JavaDoc name, Object JavaDoc target) throws NamingException JavaDoc
197    {
198       rebind(name, target, false);
199    }
200
201    /** A convience method that simplifies the process of rebinding a
202     non-zerializable object into a JNDI context. This version binds the
203     target object into the default IntitialContext using name path.
204
205    @param name, the name to use as JNDI path name. The key into the
206     NonSerializableFactory map is obtained from the toString() value of name.
207     The name parameter cannot be a 0 length name.
208    @param target, the non-Serializable object to bind.
209    @param createSubcontexts, a flag indicating if subcontexts of name should
210     be created if they do not already exist.
211    @throws NamingException thrown on failure to rebind key into ctx.
212    */

213    public static synchronized void rebind(Name JavaDoc name, Object JavaDoc target,
214       boolean createSubcontexts) throws NamingException JavaDoc
215    {
216        String JavaDoc key = name.toString();
217        InitialContext JavaDoc ctx = new InitialContext JavaDoc();
218        if( createSubcontexts == true && name.size() > 1 )
219        {
220           int size = name.size() - 1;
221           Util.createSubcontext(ctx, name.getPrefix(size));
222        }
223        rebind(ctx, key, target);
224    }
225
226 // --- Begin ObjectFactory interface methods
227
/** Transform the obj Reference bound into the JNDI namespace into the
228     actual non-Serializable object.
229
230     @param obj, the object bound in the JNDI namespace. This must be an implementation
231     of javax.naming.Reference with a javax.naming.RefAddr of type "nns" whose
232     content is the String key used to location the non-Serializable object in the
233     NonSerializableFactory map.
234     @param name, ignored.
235     @param nameCtx, ignored.
236     @param env, ignored.
237
238     @return the non-Serializable object associated with the obj Reference if one
239     exists, null if one does not.
240     */

241     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc env)
242         throws Exception JavaDoc
243     { // Get the nns value from the Reference obj and use it as the map key
244
Reference JavaDoc ref = (Reference JavaDoc) obj;
245         RefAddr JavaDoc addr = ref.get("nns");
246         String JavaDoc key = (String JavaDoc) addr.getContent();
247         Object JavaDoc target = wrapperMap.get(key);
248         return target;
249     }
250 // --- End ObjectFactory interface methods
251
}
252
Popular Tags