KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > 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.ejb3;
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.Name JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.RefAddr JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.StringRefAddr JavaDoc;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35 import org.jboss.naming.Util;
36
37 /**
38  * A utility class that allows one to bind a non-serializable object into a
39  * local JNDI context. The binding will only be valid for the lifetime of the
40  * VM in which the JNDI InitialContext lives. An example usage code snippet is:
41  * <p/>
42  * Internally, there is a static map that is keyed based on Context identityMap and the atom name of the target
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>.
45  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
46  * @version $Revision: 39465 $
47  * @see javax.naming.spi.ObjectFactory
48  * @see #rebind(javax.naming.Context, String, Object)
49  */

50 public class NonSerializableFactory implements ObjectFactory JavaDoc
51 {
52    private static Map JavaDoc wrapperMap = Collections.synchronizedMap(new HashMap JavaDoc());
53
54    public static void unbind(Context JavaDoc ctx, String JavaDoc strName) throws NamingException JavaDoc
55    {
56       Name JavaDoc name = ctx.getNameParser("").parse(strName);
57       int size = name.size();
58       String JavaDoc atom = name.get(size - 1);
59       Context JavaDoc parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1));
60       String JavaDoc key = parentCtx.getNameInNamespace() + "/" + atom;
61       wrapperMap.remove(key);
62       Util.unbind(ctx, strName);
63
64    }
65
66    public static void rebind(Context JavaDoc ctx, String JavaDoc strName, Object JavaDoc value) throws javax.naming.NamingException JavaDoc
67    {
68       Name JavaDoc name = ctx.getNameParser("").parse(strName);
69       int size = name.size();
70       String JavaDoc atom = name.get(size - 1);
71       Context JavaDoc parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1));
72       String JavaDoc key = parentCtx.getNameInNamespace() + "/" + atom;
73       wrapperMap.put(key, value);
74       String JavaDoc className = value.getClass().getName();
75       String JavaDoc factory = NonSerializableFactory.class.getName();
76       StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", key);
77       Reference JavaDoc memoryRef = new Reference JavaDoc(className, addr, factory, null);
78       parentCtx.rebind(atom, memoryRef);
79    }
80
81    public static void bind(Context JavaDoc ctx, String JavaDoc strName, Object JavaDoc value) throws javax.naming.NamingException JavaDoc
82    {
83       Name JavaDoc name = ctx.getNameParser("").parse(strName);
84       int size = name.size();
85       String JavaDoc atom = name.get(size - 1);
86       Context JavaDoc parentCtx = Util.createSubcontext(ctx, name.getPrefix(size - 1));
87       String JavaDoc key = parentCtx.getNameInNamespace() + "/" + atom;
88       wrapperMap.put(key, value);
89       String JavaDoc className = value.getClass().getName();
90       String JavaDoc factory = NonSerializableFactory.class.getName();
91       StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", key);
92       Reference JavaDoc memoryRef = new Reference JavaDoc(className, addr, factory, null);
93       
94       parentCtx.bind(atom, memoryRef);
95    }
96
97    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc env)
98            throws Exception JavaDoc
99    { // Get the nns value from the Reference obj and use it as the map key
100
Reference JavaDoc ref = (Reference JavaDoc) obj;
101       RefAddr JavaDoc addr = ref.get("nns");
102       String JavaDoc key = (String JavaDoc) addr.getContent();
103       return wrapperMap.get(key);
104    }
105 // --- End ObjectFactory interface methods
106
}
107
Popular Tags