KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > spi > MultiOrbInitialContextFactory


1 /**
2  * Copyright (C) 2002-2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: MultiOrbInitialContextFactory.java,v 1.10 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.spi;
29
30 import java.util.Hashtable JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.spi.InitialContextFactory JavaDoc;
36
37 import org.objectweb.carol.util.configuration.CarolDefaultValues;
38 import org.objectweb.carol.util.configuration.ConfigurationRepository;
39 import org.objectweb.carol.util.configuration.Protocol;
40 import org.objectweb.carol.util.configuration.TraceCarol;
41
42 /**
43  * Class <code> MultiOrbInitialContextFactory </code> is the CAROL JNDI SPI
44  * Context Factory for multi Context management.
45  * @author Guillaume Riviere
46  * @author Florent Benoit (Refactoring)
47  * @see javax.naming.spi.InitialContextFactory
48  */

49 public class MultiOrbInitialContextFactory implements InitialContextFactory JavaDoc {
50
51     /**
52      * Creates an Initial Context for beginning name resolution. Special
53      * requirements of this context are supplied using <code>environment</code>.
54      * @param env The possibly null environment specifying information
55      * to be used in the creation of the initial context.
56      * @return A non-null initial context object that implements the Context
57      * interface.
58      * @exception NamingException If cannot create an initial context.
59      */

60     public Context JavaDoc getInitialContext(Hashtable JavaDoc env) throws NamingException JavaDoc {
61         // Need to know if we want a multiprotocol context or the context for the given PROVIDER_URL
62
String JavaDoc providerURL = null;
63         if (env != null) {
64             providerURL = (String JavaDoc) env.get(Context.PROVIDER_URL);
65         }
66
67         // No provider URL, do multi context
68
if (providerURL == null) {
69             if (TraceCarol.isDebugJndiCarol()) {
70                 TraceCarol.debugJndiCarol("No provider URL, use multiprotocol context");
71             }
72             return new MultiContext(env);
73         } else {
74             //if user has provided a PROVIDER_URL, don't do a multiprocol context
75
if (TraceCarol.isDebugJndiCarol()) {
76                 TraceCarol.debugJndiCarol("Use provider URL of the environment '" + providerURL + "'.");
77             }
78             // Need to know the factory to use if it is not provided
79
String JavaDoc initFactory = (String JavaDoc) env.get(Context.INITIAL_CONTEXT_FACTORY);
80
81             // if factory is multi factory, reset it
82
if (initFactory != null && initFactory.equals(CarolDefaultValues.MULTI_JNDI)) {
83                 initFactory = null;
84             }
85             if (initFactory == null) {
86                 String JavaDoc protocolName = CarolDefaultValues.getRMIProtocol(providerURL);
87                 // found InitialContext factory classname
88
Protocol protocol = ConfigurationRepository.getProtocol(protocolName);
89                 if (protocol == null) {
90                     throw new NamingException JavaDoc("The protocol '" + protocolName + "' is not available within carol.");
91                 }
92                 initFactory = protocol.getInitialContextFactoryClassName();
93                 env.put(Context.INITIAL_CONTEXT_FACTORY, initFactory);
94                 if (TraceCarol.isDebugJndiCarol()) {
95                     TraceCarol.debugJndiCarol("No INITIAL_CONTEXT_FACTORY, use '" + initFactory + "' as InitialContext factory.");
96                 }
97             }
98             return new InitialContext JavaDoc(env);
99         }
100     }
101
102 }
Popular Tags