KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > jndi > JNDIConnection


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JNDIConnection.java 10:47:20 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.jndi;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.net.InetSocketAddress JavaDoc;
29 import java.net.Socket JavaDoc;
30
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.ServiceUnavailableException JavaDoc;
33
34 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
35 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
36 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse.ResponseType;
37
38 /**
39  * Connection to a RegistryServer
40  *
41  * @author ddesjardins - eBMWebsourcing
42  */

43 public class JNDIConnection implements Serializable JavaDoc {
44
45     private static final long serialVersionUID = 1L;
46
47     /**
48      * Port of the RegistryServer
49      */

50     protected int port;
51
52     /**
53      * Host of the RegistryServer
54      */

55     protected String JavaDoc host;
56
57     public JNDIConnection(int port, String JavaDoc host) {
58         super();
59         this.port = port;
60         this.host = host;
61     }
62
63     /**
64      * Send a request to a Registry server
65      *
66      * @param request
67      * request to send
68      * @return return of the request
69      * @throws NamingException
70      * if a NamingException is thrown on the server
71      */

72     public Object JavaDoc send(RegistryRequest request) throws NamingException JavaDoc {
73         Object JavaDoc out = null;
74         Socket JavaDoc socket = new Socket JavaDoc();
75         try {
76             socket.connect(new InetSocketAddress JavaDoc(host, port), 1000);
77             ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(
78                 socket.getOutputStream());
79             objectOutputStream.writeObject(request);
80             objectOutputStream.flush();
81
82             ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(socket
83                 .getInputStream());
84             RegistryResponse response = (RegistryResponse) objectInputStream
85                 .readObject();
86
87             if (response.getType() == ResponseType.exception) {
88                 throw (NamingException JavaDoc) response.getArg1();
89             }
90             out = response.getArg1();
91         } catch (IOException JavaDoc e) {
92             throw new ServiceUnavailableException JavaDoc(
93                 "Problem while sending request to server " + host + ":" + port);
94         } catch (ClassNotFoundException JavaDoc e) {
95             throw new ServiceUnavailableException JavaDoc(
96                 "Problem while sending request to server " + host + ":" + port);
97         }
98         return out;
99     }
100
101 }
102
Popular Tags