001: /*
002: * CoadunationClient: The client libraries for Coadunation. (RMI/CORBA)
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * CoadunationInitialContextFactory.java
020: *
021: * This Context Factory is responsible for initializing the context of for the
022: * client side. So that RMI connection can be made to Coadunation.
023: *
024: * Revision: $ID
025: */
026:
027: package com.rift.coad.client.naming;
028:
029: // java imports
030: import java.util.Hashtable;
031: import java.util.Properties;
032: import javax.naming.Context;
033: import javax.naming.NamingException;
034: import javax.naming.spi.InitialContextFactory;
035: import org.omg.CORBA.ORB;
036:
037: // Coadunation imports
038: import com.rift.coad.client.interceptor.iiop.ClientInterceptorIntializer;
039: import com.rift.coad.lib.interceptor.credentials.Login;
040:
041: /**
042: * This Context Factory is responsible for initializing the context of for the
043: * client side. So that RMI connection can be made to Coadunation.
044: *
045: * @author Brett Chaldecott
046: */
047: public class CoadunationInitialContextFactory implements
048: InitialContextFactory {
049:
050: // class conconstants
051: public final static String USERNAME = "com.rift.coad.username";
052: public final static String PASSWORD = "com.rift.coad.password";
053:
054: // the static reference to the user login object.
055: public static ThreadLocal userLogin = new ThreadLocal();
056:
057: /**
058: *
059: * Creates a new instance of CoadunationInitialContextFactory
060: */
061: public CoadunationInitialContextFactory() {
062: }
063:
064: /**
065: * This method is responsible for instanciating the initial context using
066: * the supplied environment.
067: *
068: * @return The context to return.
069: * @param environment The environment to use to instanciate the url context.
070: * @exception NamingException
071: */
072: public Context getInitialContext(Hashtable environment)
073: throws NamingException {
074: if (environment.containsKey(USERNAME)
075: && environment.containsKey(PASSWORD)) {
076: userLogin.set(new Login((String) environment.get(USERNAME),
077: (String) environment.get(PASSWORD)));
078: } else {
079: userLogin.set(null);
080: }
081:
082: // setup the orb
083: System.setProperty(
084: "org.omg.PortableInterceptor.ORBInitializerClass."
085: + ClientInterceptorIntializer.class.getName(),
086: "");
087: Properties properties = new Properties();
088: properties.setProperty("org.omg.CORBA.ORBClass",
089: "org.jacorb.orb.ORB");
090: properties.setProperty("org.omg.CORBA.ORBSingletonClass",
091: "org.jacorb.orb.ORBSingleton");
092: ORB orb = ORB.init(new String[0], properties);
093:
094: // add the extra environmental variables
095: environment.put(Context.INITIAL_CONTEXT_FACTORY,
096: "com.sun.jndi.cosnaming.CNCtxFactory");
097: environment.put("java.naming.corba.orb", orb);
098:
099: if (environment.containsKey(Context.PROVIDER_URL)) {
100: String url = "corbaloc:iiop:"
101: + (String) environment.get(Context.PROVIDER_URL)
102: + "/StandardNS/NameServer-POA/_root";
103: environment.put(Context.PROVIDER_URL, url);
104: }
105:
106: // instanciate the context
107: return new CoadunationContext(orb, environment);
108: }
109: }
|