01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.naming;
23:
24: import java.util.Hashtable;
25:
26: import javax.naming.Context;
27: import javax.naming.InitialContext;
28: import javax.naming.Name;
29: import javax.naming.spi.ObjectFactory;
30:
31: import org.jboss.logging.Logger;
32: import org.jboss.util.id.GUID;
33:
34: /**
35: * An object factory that allows different objects to be used
36: * in the local virtual machine versus remote virtual machines
37: *
38: * @author Adrian Brock (adrian@jboss.com)
39: * @version $Revision: 57209 $
40: */
41: public class LinkRefPairObjectFactory implements ObjectFactory {
42: // Constants -----------------------------------------------------
43:
44: /** The logger */
45: private static final Logger log = Logger
46: .getLogger(LinkRefPairObjectFactory.class);
47:
48: /** Our class name */
49: static final String className = LinkRefPairObjectFactory.class
50: .getName();
51:
52: /** The guid used to determine whether we in the same VM */
53: static final String guid = new GUID().asString();
54:
55: // Attributes ----------------------------------------------------
56:
57: // Static --------------------------------------------------------
58:
59: // Constructors --------------------------------------------------
60:
61: // Public --------------------------------------------------------
62:
63: // ObjectFactory implementation ----------------------------------
64:
65: public Object getObjectInstance(Object obj, Name name,
66: Context nameCtx, Hashtable environment) throws Exception {
67: LinkRefPair pair = (LinkRefPair) obj;
68: String jndiName;
69:
70: // Local or remote?
71: boolean local = false;
72: if (guid.equals(pair.getGUID())) {
73: jndiName = pair.getLocalLinkName();
74: local = true;
75: } else
76: jndiName = pair.getRemoteLinkName();
77:
78: InitialContext ctx;
79: if (local || environment == null)
80: ctx = new InitialContext();
81: else
82: ctx = new InitialContext(environment);
83:
84: return ctx.lookup(jndiName);
85: }
86:
87: // Y overrides ---------------------------------------------------
88:
89: // Package protected ---------------------------------------------
90:
91: // Protected -----------------------------------------------------
92:
93: // Private -------------------------------------------------------
94:
95: // Inner classes -------------------------------------------------
96: }
|