KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > client > java > javaURLContextFactory


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.naming.client.java;
23
24 import java.util.Hashtable JavaDoc;
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.security.AccessController JavaDoc;
29 import java.security.PrivilegedAction JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.OperationNotSupportedException JavaDoc;
35 import javax.naming.NameParser JavaDoc;
36 import javax.naming.spi.ObjectFactory JavaDoc;
37
38 import org.jboss.corba.ORBFactory;
39
40 /** The external client java URL context factory. This is used in conjunction
41  * with j2ee application clients to implement the java:comp/env
42  * enterprise naming context (ENC).
43  *
44  * @see javax.naming.spi.ObjectFactory
45  *
46  * @author Scott.Stark@jboss.org
47  * @version $Revision: 56438 $
48  */

49 public class javaURLContextFactory
50    implements ObjectFactory JavaDoc
51 {
52    public static final String JavaDoc J2EE_CLIENT_NAME_PROP = "j2ee.clientName";
53
54    // ObjectFactory implementation ----------------------------------
55
public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
56       Hashtable JavaDoc env)
57       throws Exception JavaDoc
58    {
59       // Get the j2ee.clientName value
60
String JavaDoc clientName = (String JavaDoc) env.get(J2EE_CLIENT_NAME_PROP);
61       if (clientName == null)
62       {
63          // Look for the name as a system property
64
clientName = (String JavaDoc) AccessController.doPrivileged(
65             new PrivilegedAction JavaDoc()
66             {
67                public Object JavaDoc run()
68                {
69                   try
70                   {
71                      return System.getProperty(J2EE_CLIENT_NAME_PROP);
72                   }
73                   catch (SecurityException JavaDoc e)
74                   {
75                      return null;
76                   }
77                }
78             }
79          );
80          if (clientName == null)
81             throw new NamingException JavaDoc("Failed to find j2ee.clientName in jndi env");
82       }
83
84       Object JavaDoc result = null;
85
86       if (nameCtx == null)
87          nameCtx = new InitialContext JavaDoc(env);
88       if (obj == null)
89       {
90          // Create a context for resolving the java: url
91
InvocationHandler JavaDoc handler = new EncContextProxy(nameCtx, clientName);
92          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
93          Class JavaDoc[] ifaces = {Context JavaDoc.class};
94          result = Proxy.newProxyInstance(loader, ifaces, handler);
95       }
96       return result;
97    }
98
99    private static class EncContextProxy implements InvocationHandler JavaDoc
100    {
101       Context JavaDoc lookupCtx;
102       String JavaDoc clientName;
103
104       EncContextProxy(Context JavaDoc lookupCtx, String JavaDoc clientName)
105       {
106          this.lookupCtx = lookupCtx;
107          this.clientName = clientName;
108       }
109
110       /**
111        */

112       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
113          throws Throwable JavaDoc
114       {
115          String JavaDoc methodName = method.getName();
116          if (methodName.equals("toString") == true)
117             return "Client ENC(" + clientName + ")";
118
119          if (methodName.equals("lookup") == false)
120             throw new OperationNotSupportedException JavaDoc("Only lookup is supported, op=" + method);
121          NameParser JavaDoc parser = lookupCtx.getNameParser("");
122          Name JavaDoc name = null;
123          if (args[0] instanceof String JavaDoc)
124             name = parser.parse((String JavaDoc) args[0]);
125          else
126             name = (Name JavaDoc) args[0];
127
128          // Check for special objects not in the env
129
if (name.size() < 2 || "java:comp".equals(name.get(0)) == false || "env".equals(name.get(1)) == false)
130             return getSpecialObject(name);
131          // Lookup the client application context from the server
132
Context JavaDoc clientCtx = (Context JavaDoc) lookupCtx.lookup(clientName);
133          // Strip the comp/env prefix
134
Name JavaDoc bindingName = name.getSuffix(2);
135          Object JavaDoc binding = clientCtx.lookup(bindingName);
136          return binding;
137       }
138       
139       public Object JavaDoc getSpecialObject(Name JavaDoc name) throws NamingException JavaDoc
140       {
141          if (name.size() > 0 && "java:comp".equals(name.get(0)))
142          {
143             if (name.size() == 2 && "ORB".equals(name.get(1)))
144                return ORBFactory.getORB();
145             else if (name.size() == 2 && "HandleDelegate".equals(name.get(1)))
146                return HandleDelegateFactory.getHandleDelegateSingleton();
147          }
148          throw new NamingException JavaDoc("Name not found " + name);
149       }
150    }
151 }
152
Popular Tags