KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > haclient > HANamingContextFactory


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): David Feliot
22  */

23 package fr.dyade.aaa.jndi2.haclient;
24
25 import javax.naming.spi.*;
26 import javax.naming.*;
27 import java.util.*;
28
29 import fr.dyade.aaa.jndi2.client.*;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 public class HANamingContextFactory implements InitialContextFactory {
35
36   /**
37    * @param env This contains the hostname and the port.
38    * @return A JNDI initial context.
39    * @exception NamingException Thrown if the host and port properties
40    * aren't strings, if the port string does not represent a valid number,
41    * or if an exception is thrown from the NamingContext constructor.
42    */

43   public Context getInitialContext(Hashtable env)
44     throws NamingException {
45     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
46       Trace.logger.log(
47         BasicLevel.DEBUG,
48         "NamingContextFactory.getInitialContext(" + env + ')');
49     return new fr.dyade.aaa.jndi2.client.NamingContextImpl(
50       getNamingConnection(env),
51       new CompositeName());
52   }
53
54   private static String JavaDoc getEnvProperty(Hashtable env,
55                                        String JavaDoc propName) {
56     String JavaDoc value = null;
57     if (env != null) {
58       value = (String JavaDoc) env.get(propName);
59     }
60     if (value == null) {
61       value = System.getProperty(propName, null);
62     }
63     return value;
64   }
65
66   public static NamingConnection getNamingConnection(
67     Hashtable env)
68     throws NamingException {
69     try {
70       NamingConnection namingConnection;
71
72       // URL should be as: hascn://host:port
73
String JavaDoc url = getEnvProperty(env, "scn.naming.provider.url");
74       if (url == null) {
75         url = getEnvProperty(env, Context.PROVIDER_URL);
76       }
77     
78       if (url != null) {
79         StringTokenizer tokenizer = new StringTokenizer(url, "/:,");
80         if (! tokenizer.hasMoreElements())
81           throw new NamingException("URL not valid:" + url);
82         String JavaDoc protocol = tokenizer.nextToken();
83         if (protocol.equals("hascn")) {
84           HANamingConnection haNamingConnection =
85             new HANamingConnection();
86           while (tokenizer.hasMoreElements()) {
87             haNamingConnection.addServerAddress(
88               tokenizer.nextToken(),
89               Integer.parseInt(tokenizer.nextToken()));
90           }
91           namingConnection = haNamingConnection;
92         } else {
93           throw new NamingException("Unknown protocol:" + protocol);
94         }
95       } else {
96         throw new NamingException(
97           "URL " + Context.PROVIDER_URL + " not defined");
98       }
99       return namingConnection;
100     } catch (Exception JavaDoc e) {
101       NamingException nx =
102         new NamingException(
103           "exception creating NamingContext: " +
104           e.toString());
105       nx.setRootCause(e);
106       throw nx;
107     }
108   }
109 }
110
Popular Tags