KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > BridgeNamingContextFactory


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;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.NameNotFoundException JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import org.jnp.interfaces.NamingContextFactory;
34
35 /** A naming provider InitialContextFactory implementation that combines
36  two Naming services to allow for delegation of lookups from one to the
37  other. The default naming service is specified via the standard
38  Context.PROVIDER_URL property while the secondary service is specified
39  using an org.jboss.naming.provider.url2 property. An example of where
40  this would be used is to bridge between the local JNDI service and the
41  HAJNDI service. Lookups into the local JNDI service that fail would then
42  try the HAJNDI service.
43
44  @see javax.naming.spi.InitialContextFactory
45  
46  @author Scott.Stark@jboss.org
47  @version $Revision: 37459 $
48  */

49 public class BridgeNamingContextFactory extends NamingContextFactory
50 {
51    // InitialContextFactory implementation --------------------------
52
public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
53       throws NamingException JavaDoc
54    {
55       Context JavaDoc primaryCtx = super.getInitialContext(env);
56       Context JavaDoc bridgeCtx = primaryCtx;
57       Object JavaDoc providerURL2 = env.get("org.jboss.naming.provider.url2");
58       if( providerURL2 != null )
59       {
60          // A second provider url was given, create a secondary naming context
61
Hashtable JavaDoc env2 = (Hashtable JavaDoc) env.clone();
62          env2.put(Context.PROVIDER_URL, providerURL2);
63          Context JavaDoc secondaryCtx = super.getInitialContext(env2);
64          InvocationHandler JavaDoc h = new BridgeContext(primaryCtx, secondaryCtx);
65          Class JavaDoc[] interfaces = {Context JavaDoc.class};
66          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
67          bridgeCtx = (Context JavaDoc) Proxy.newProxyInstance(loader, interfaces, h);
68       }
69       return bridgeCtx;
70    }
71
72    /** This class is the Context interface handler and performs the
73        failed lookup delegation from the primary to secondary naming
74        Context.
75    */

76    static class BridgeContext implements InvocationHandler JavaDoc
77    {
78       private Context JavaDoc primaryCtx;
79       private Context JavaDoc secondaryCtx;
80
81       BridgeContext(Context JavaDoc primaryCtx, Context JavaDoc secondaryCtx)
82       {
83          this.primaryCtx = primaryCtx;
84          this.secondaryCtx = secondaryCtx;
85       }
86
87       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
88             throws Throwable JavaDoc
89       {
90          Object JavaDoc value = null;
91          // First try the primary context
92
try
93          {
94             value = method.invoke(primaryCtx, args);
95          }
96          catch(InvocationTargetException JavaDoc e)
97          {
98             Throwable JavaDoc t = e.getTargetException();
99             // Try the secondary if this is a failed lookup
100
if( t instanceof NameNotFoundException JavaDoc && method.getName().equals("lookup") )
101             {
102                try
103                {
104                   value = method.invoke(secondaryCtx, args);
105                }
106                catch (InvocationTargetException JavaDoc e1)
107                {
108                   throw e1.getTargetException();
109                }
110             }
111             else
112             {
113                throw t;
114             }
115          }
116          return value;
117       }
118    }
119 }
120
Popular Tags