KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > client > NamingContextFactory


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): Sofiane Chibani
22  * Contributor(s): David Feliot, Nicolas Tachker
23  */

24 package fr.dyade.aaa.jndi2.client;
25
26 import javax.naming.spi.*;
27 import javax.naming.*;
28 import java.util.*;
29
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32
33 public class NamingContextFactory implements InitialContextFactory {
34
35   /**
36    * This property which defines the listener port must be passed
37    * when creating an initial context using this factory.
38    */

39   public final static String JavaDoc JAVA_PORT_PROPERTY = "java.naming.factory.port";
40
41   /**
42    * This property which defines the host name must be passed
43    * when creating an initial context using this factory.
44    */

45   public final static String JavaDoc JAVA_HOST_PROPERTY = "java.naming.factory.host";
46
47   /**
48    * Specific property. It is useful when several naming provider use
49    * the same property.
50    */

51   public final static String JavaDoc SCN_PORT_PROPERTY = "scn.naming.factory.port";
52
53   /**
54    * Specific property. It is useful when several naming provider use
55    * the same property.
56    */

57   public final static String JavaDoc SCN_HOST_PROPERTY = "scn.naming.factory.host";
58
59   /**
60    * Specific property. It is useful when several naming provider use
61    * the same property.
62    */

63   public final static String JavaDoc SCN_PROVIDER_URL = "scn.naming.provider.url";
64
65   /**
66    * @param env This contains the hostname and the port.
67    * @return A JNDI initial context.
68    * @exception NamingException Thrown if the host and port properties
69    * aren't strings, if the port string does not represent a valid number,
70    * or if an exception is thrown from the NamingContext constructor.
71    */

72   public Context getInitialContext(Hashtable env)
73     throws NamingException {
74     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
75       Trace.logger.log(
76         BasicLevel.DEBUG,
77         "NamingContextFactory.getInitialContext(" + env + ')');
78     return new fr.dyade.aaa.jndi2.client.NamingContextImpl(
79       getNamingConnection(env),
80       new CompositeName());
81   }
82
83   public static NamingConnection getNamingConnection(
84     Hashtable env)
85     throws NamingException {
86     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
87       Trace.logger.log(
88         BasicLevel.DEBUG,
89         "NamingContextFactory.getNamingConnection(" + env + ')');
90     try {
91       NamingConnection namingConnection;
92       // The URL format is scn://host:port
93
String JavaDoc url = (String JavaDoc) env.get(SCN_PROVIDER_URL);
94       if (url == null) url = (String JavaDoc) env.get(Context.PROVIDER_URL);
95       if (url != null && !url.equals("")) {
96         StringTokenizer tokenizer = new StringTokenizer(url, "/:,");
97         if (! tokenizer.hasMoreElements())
98           throw new NamingException("URL not valid:" + url);
99         String JavaDoc protocol = tokenizer.nextToken();
100         if (protocol.equals("scn")) {
101           String JavaDoc host = tokenizer.nextToken();
102           String JavaDoc portStr = tokenizer.nextToken();
103           int port = Integer.parseInt(portStr);
104           namingConnection = new SimpleNamingConnection(host, port, env);
105         } else {
106           throw new NamingException("Unknown protocol:" + protocol);
107         }
108       } else {
109         String JavaDoc host = (String JavaDoc) env.get(SCN_HOST_PROPERTY);
110         if (host == null) host = (String JavaDoc) System.getProperty(SCN_HOST_PROPERTY);
111         if (host == null) host = (String JavaDoc) env.get(JAVA_HOST_PROPERTY);
112         if (host == null) host = (String JavaDoc) System.getProperty(JAVA_HOST_PROPERTY);
113         if (host == null) host = "localhost";
114
115         String JavaDoc portStr = (String JavaDoc) env.get(SCN_PORT_PROPERTY);
116         if (portStr == null) portStr = (String JavaDoc) System.getProperty(SCN_PORT_PROPERTY);
117         if (portStr == null) portStr = (String JavaDoc) env.get(JAVA_PORT_PROPERTY);
118         if (portStr == null) portStr = (String JavaDoc) System.getProperty(JAVA_PORT_PROPERTY);
119         if (portStr == null) portStr = "16400";
120
121         int port = Integer.parseInt(portStr);
122         namingConnection = new SimpleNamingConnection(
123           host, port, env);
124       }
125       return namingConnection;
126     } catch (NumberFormatException JavaDoc e) {
127       NamingException nx = new NamingException();
128       nx.setRootCause(e);
129       throw nx;
130     } catch (Exception JavaDoc e) {
131       NamingException nx = new NamingException();
132       nx.setRootCause(e);
133       throw nx;
134     }
135   }
136 }
137
Popular Tags