KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jtests > conform > basic > server > BasicServer


1 /**
2  * Copyright (C) 2002,2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: BasicServer.java,v 1.10 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jtests.conform.basic.server;
29
30 import java.io.IOException JavaDoc;
31 import java.net.ServerSocket JavaDoc;
32 import java.net.Socket JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.objectweb.carol.util.configuration.ConfigurationException;
40 import org.objectweb.carol.util.configuration.ConfigurationRepository;
41
42 /**
43  * Class <code>BasicServer</code> is a Server for Junit tests Test The
44  * InitialContext and the PortableRemoteObject situation with remote object
45  */

46 public class BasicServer {
47
48     /**
49      * Name of the basic remote object (in all name services)
50      */

51     private static String JavaDoc basicObjectName = "basicname";
52
53     /**
54      * Name of the basic multi remote object (in all name services)
55      */

56     private static String JavaDoc basicMultiObjectName = "basicmultiname";
57
58     /**
59      * Name of the basic object ref
60      */

61     private static String JavaDoc basicObjectRefName = "basicrefname";
62
63     /**
64      * Initial context
65      */

66     private Context JavaDoc ic = null;
67
68     /**
69      * Instance of basic object
70      */

71     private BasicObjectItf ba;
72
73     /**
74      * Instance of basic multi object
75      */

76     private BasicMultiObjectItf bma;
77
78     /**
79      * Instance of basic object ref
80      */

81     private BasicObjectRef bref;
82
83     /**
84      * Port number
85      */

86     private int port;
87
88     /**
89      * Start was successful ?
90      */

91     private boolean startedSuccessfully;
92
93     /**
94      * This method binds all the names in the registry.
95      * @param args arguments for launching the test
96      */

97     public static void main(String JavaDoc[] args) {
98         if (args.length != 1) {
99             throw new IllegalArgumentException JavaDoc("expected the port number, but got: " + Arrays.asList(args));
100         }
101         new BasicServer(Integer.parseInt(args[0])).advertiseReadiness();
102     }
103
104     /**
105      * Constructor
106      * @param port port number to use
107      */

108     private BasicServer(int port) {
109         this.port = port;
110         startedSuccessfully = true;
111
112         try {
113             ConfigurationRepository.init();
114         } catch (ConfigurationException ex) {
115             System.err.println("carol is misconfigured");
116             ex.printStackTrace();
117             startedSuccessfully = false;
118         }
119
120         if (startedSuccessfully) {
121             try {
122                 ba = new BasicObject();
123                 bma = new BasicMultiObject();
124                 bref = new BasicObjectRef("string");
125             } catch (Exception JavaDoc ex) {
126                 System.err.println("error creating basic objects");
127                 ex.printStackTrace();
128                 startedSuccessfully = false;
129             }
130         }
131         if (startedSuccessfully) {
132             try {
133                 ic = new InitialContext JavaDoc();
134                 ic.rebind(basicObjectName, ba);
135                 ic.rebind(basicMultiObjectName, bma);
136                 ic.rebind(basicObjectRefName, bref);
137             } catch (NamingException JavaDoc ex) {
138                 ex.printStackTrace();
139             }
140         }
141     }
142
143     /**
144      * Advertise readiness to the external world by binding to the specified TCP
145      * port.
146      * <p>
147      * The purpose of this method is to indicate to Ant's <wait>task that this
148      * BasicServer has registered all the necessary objects in the registry and
149      * it is ok for the clients to start making remote calls.
150      * </p>
151      */

152     private void advertiseReadiness() {
153         ServerSocket JavaDoc serverSocket;
154         try {
155             serverSocket = new ServerSocket JavaDoc(port);
156         } catch (IOException JavaDoc ex) {
157             throw new RuntimeException JavaDoc("Couldn't bind to " + port);
158         }
159
160         while (true) {
161             try {
162                 Socket JavaDoc socket = serverSocket.accept();
163                 socket.close();
164             } catch (IOException JavaDoc ex) {
165                 throw new RuntimeException JavaDoc("Error accepting a connection", ex);
166             }
167         }
168     }
169 }
Popular Tags