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.naming.client.java;
023:
024: import java.util.Hashtable;
025: import java.lang.reflect.InvocationHandler;
026: import java.lang.reflect.Method;
027: import java.lang.reflect.Proxy;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import javax.naming.Context;
031: import javax.naming.Name;
032: import javax.naming.NamingException;
033: import javax.naming.InitialContext;
034: import javax.naming.OperationNotSupportedException;
035: import javax.naming.NameParser;
036: import javax.naming.spi.ObjectFactory;
037:
038: import org.jboss.corba.ORBFactory;
039:
040: /** The external client java URL context factory. This is used in conjunction
041: * with j2ee application clients to implement the java:comp/env
042: * enterprise naming context (ENC).
043: *
044: * @see javax.naming.spi.ObjectFactory
045: *
046: * @author Scott.Stark@jboss.org
047: * @version $Revision: 57209 $
048: */
049: public class javaURLContextFactory implements ObjectFactory {
050: public static final String J2EE_CLIENT_NAME_PROP = "j2ee.clientName";
051:
052: // ObjectFactory implementation ----------------------------------
053: public Object getObjectInstance(Object obj, Name name,
054: Context nameCtx, Hashtable env) throws Exception {
055: // Get the j2ee.clientName value
056: String clientName = (String) env.get(J2EE_CLIENT_NAME_PROP);
057: if (clientName == null) {
058: // Look for the name as a system property
059: clientName = (String) AccessController
060: .doPrivileged(new PrivilegedAction() {
061: public Object run() {
062: try {
063: return System
064: .getProperty(J2EE_CLIENT_NAME_PROP);
065: } catch (SecurityException e) {
066: return null;
067: }
068: }
069: });
070: if (clientName == null)
071: throw new NamingException(
072: "Failed to find j2ee.clientName in jndi env");
073: }
074:
075: Object result = null;
076:
077: if (nameCtx == null)
078: nameCtx = new InitialContext(env);
079: if (obj == null) {
080: // Create a context for resolving the java: url
081: InvocationHandler handler = new EncContextProxy(nameCtx,
082: clientName);
083: ClassLoader loader = Thread.currentThread()
084: .getContextClassLoader();
085: Class[] ifaces = { Context.class };
086: result = Proxy.newProxyInstance(loader, ifaces, handler);
087: }
088: return result;
089: }
090:
091: private static class EncContextProxy implements InvocationHandler {
092: Context lookupCtx;
093: String clientName;
094:
095: EncContextProxy(Context lookupCtx, String clientName) {
096: this .lookupCtx = lookupCtx;
097: this .clientName = clientName;
098: }
099:
100: /**
101: */
102: public Object invoke(Object proxy, Method method, Object[] args)
103: throws Throwable {
104: String methodName = method.getName();
105: if (methodName.equals("toString") == true)
106: return "Client ENC(" + clientName + ")";
107:
108: if (methodName.equals("lookup") == false)
109: throw new OperationNotSupportedException(
110: "Only lookup is supported, op=" + method);
111: NameParser parser = lookupCtx.getNameParser("");
112: Name name = null;
113: if (args[0] instanceof String)
114: name = parser.parse((String) args[0]);
115: else
116: name = (Name) args[0];
117:
118: // Lookup the client application context from the server
119: Context clientCtx = (Context) lookupCtx.lookup(clientName);
120: // Check for special objects not in the env
121: if (name.size() < 2
122: || "java:comp".equals(name.get(0)) == false
123: || "env".equals(name.get(1)) == false)
124: return getSpecialObject(name);
125: // Strip the comp/env prefix
126: Name bindingName = name.getSuffix(2);
127: Object binding = clientCtx.lookup(bindingName);
128: return binding;
129: }
130:
131: public Object getSpecialObject(Name name)
132: throws NamingException {
133: if (name.size() > 0 && "java:comp".equals(name.get(0))) {
134: if (name.size() == 2 && "ORB".equals(name.get(1)))
135: return ORBFactory.getORB();
136: else if (name.size() == 2
137: && "HandleDelegate".equals(name.get(1)))
138: return HandleDelegateFactory
139: .getHandleDelegateSingleton();
140: }
141: throw new NamingException("Name not found " + name);
142: }
143: }
144: }
|