001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3;
023:
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.Hashtable;
027: import java.util.Map;
028: import javax.naming.Context;
029: import javax.naming.Name;
030: import javax.naming.NamingException;
031: import javax.naming.RefAddr;
032: import javax.naming.Reference;
033: import javax.naming.StringRefAddr;
034: import javax.naming.spi.ObjectFactory;
035: import org.jboss.naming.Util;
036:
037: /**
038: * A utility class that allows one to bind a non-serializable object into a
039: * local JNDI context. The binding will only be valid for the lifetime of the
040: * VM in which the JNDI InitialContext lives. An example usage code snippet is:
041: * <p/>
042: * Internally, there is a static map that is keyed based on Context identityMap and the atom name of the target
043: *
044: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>.
045: * @author <a href="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
046: * @version $Revision: 57207 $
047: * @see javax.naming.spi.ObjectFactory
048: * @see #rebind(javax.naming.Context, String, Object)
049: */
050: public class NonSerializableFactory implements ObjectFactory {
051: private static Map wrapperMap = Collections
052: .synchronizedMap(new HashMap());
053:
054: public static void unbind(Context ctx, String strName)
055: throws NamingException {
056: Name name = ctx.getNameParser("").parse(strName);
057: int size = name.size();
058: String atom = name.get(size - 1);
059: Context parentCtx = Util.createSubcontext(ctx, name
060: .getPrefix(size - 1));
061: String key = parentCtx.getNameInNamespace() + "/" + atom;
062: wrapperMap.remove(key);
063: Util.unbind(ctx, strName);
064:
065: }
066:
067: public static void rebind(Context ctx, String strName, Object value)
068: throws javax.naming.NamingException {
069: Name name = ctx.getNameParser("").parse(strName);
070: int size = name.size();
071: String atom = name.get(size - 1);
072: Context parentCtx = Util.createSubcontext(ctx, name
073: .getPrefix(size - 1));
074: String key = parentCtx.getNameInNamespace() + "/" + atom;
075: wrapperMap.put(key, value);
076: String className = value.getClass().getName();
077: String factory = NonSerializableFactory.class.getName();
078: StringRefAddr addr = new StringRefAddr("nns", key);
079: Reference memoryRef = new Reference(className, addr, factory,
080: null);
081: parentCtx.rebind(atom, memoryRef);
082: }
083:
084: public static void bind(Context ctx, String strName, Object value)
085: throws javax.naming.NamingException {
086: Name name = ctx.getNameParser("").parse(strName);
087: int size = name.size();
088: String atom = name.get(size - 1);
089: Context parentCtx = Util.createSubcontext(ctx, name
090: .getPrefix(size - 1));
091: String key = parentCtx.getNameInNamespace() + "/" + atom;
092: wrapperMap.put(key, value);
093: String className = value.getClass().getName();
094: String factory = NonSerializableFactory.class.getName();
095: StringRefAddr addr = new StringRefAddr("nns", key);
096: Reference memoryRef = new Reference(className, addr, factory,
097: null);
098:
099: parentCtx.bind(atom, memoryRef);
100: }
101:
102: public Object getObjectInstance(Object obj, Name name,
103: Context nameCtx, Hashtable env) throws Exception { // Get the nns value from the Reference obj and use it as the map key
104: Reference ref = (Reference) obj;
105: RefAddr addr = ref.get("nns");
106: String key = (String) addr.getContent();
107: return wrapperMap.get(key);
108: }
109: // --- End ObjectFactory interface methods
110: }
|