KickJava   Java API By Example, From Geeks To Geeks.

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


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.client;
24
25 import java.net.*;
26 import java.io.*;
27 import java.util.*;
28 import javax.naming.*;
29
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32
33 import fr.dyade.aaa.jndi2.msg.*;
34
35 public class SimpleNamingConnection
36     implements NamingConnection {
37
38   private String JavaDoc hostName;
39
40   private int port;
41
42   private Hashtable env;
43
44   private IOControl ioCtrl;
45
46   public SimpleNamingConnection(String JavaDoc hostName,
47                                 int port,
48                                 Hashtable env) {
49     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
50       Trace.logger.log(BasicLevel.DEBUG,
51                        "SimpleNamingConnection.<init>(" +
52                        hostName + ',' +
53                        port + ',' +
54                        env + ')');
55     this.hostName = hostName;
56     this.port = port;
57     this.env = env;
58   }
59
60   public final String JavaDoc getHostName() {
61     return hostName;
62   }
63
64   public final int getPort() {
65     return port;
66   }
67
68   /**
69    * An invoke opens a connection and closes it
70    * when the result has been returned. The overhead
71    * of the connection opening could be avoided
72    * if the server could close connections. Such a
73    * protocol would change the client as well.
74    */

75   public synchronized JndiReply invoke(JndiRequest request) throws NamingException {
76     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
77       Trace.logger.log(BasicLevel.DEBUG,
78                        "NamingConnection.invoke(" + request + ')');
79     open();
80     try {
81       ioCtrl.writeObject(request);
82       return (JndiReply)ioCtrl.readObject();
83     } catch (IOException ioe) {
84       if (Trace.logger.isLoggable(BasicLevel.ERROR))
85         Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", ioe);
86       NamingException ne = new NamingException(ioe.getMessage());
87       ne.setRootCause(ioe);
88       throw ne;
89     } catch (ClassNotFoundException JavaDoc cnfe) {
90       if (Trace.logger.isLoggable(BasicLevel.ERROR))
91         Trace.logger.log(BasicLevel.ERROR, "NamingConnection.receive()", cnfe);
92       NamingException ne = new NamingException(cnfe.getMessage());
93       ne.setRootCause(cnfe);
94       throw ne;
95     } finally {
96       ioCtrl.close();
97     }
98   }
99
100   private static String JavaDoc TIMEOUT_PROPERTY =
101       "fr.dyade.aaa.jndi2.client.SimpleNamingConnection.timeout";
102
103   private void open() throws NamingException {
104     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
105       Trace.logger.log(BasicLevel.DEBUG,
106                        "SimpleNamingConnection.open()");
107     try {
108       int timeout = Integer.getInteger(TIMEOUT_PROPERTY, 0).intValue();
109       Socket socket = new Socket();
110       socket.connect(new InetSocketAddress(hostName, port), timeout);
111       ioCtrl = new IOControl(socket);
112     } catch (IOException exc) {
113       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
114         Trace.logger.log(BasicLevel.DEBUG, "NamingConnection.open()", exc);
115       NamingException exc2 = new NamingException(exc.getMessage());
116       exc2.setRootCause(exc);
117       throw exc2;
118     }
119   }
120
121   public Hashtable getEnvironment() {
122     return env;
123   }
124
125   public NamingConnection cloneConnection() {
126     return new SimpleNamingConnection(hostName, port, env);
127   }
128
129   public String JavaDoc toString() {
130     return '(' + super.toString() +
131       ",hostname=" + hostName +
132       ",port=" + port +
133       ",env=" + env + ')';
134   }
135 }
136
Popular Tags