KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > comanche > lib > Application


1 /*====================================================================
2  
3  OpenCCM: The Open CORBA Component Model Platform
4  Copyright (C) 2004 INRIA - USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6  
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21  
22  Initial developer(s): Philippe Merle.
23  Contributor(s): Areski Flissi.
24  
25  ====================================================================
26  $Id: Application.java,v 1.1 2004/10/12 14:39:36 rouvoy Exp $
27  ====================================================================*/

28
29 package org.objectweb.comanche.lib;
30
31 // Package dependencies.
32
import java.io.InputStreamReader JavaDoc;
33 import java.io.PrintStream JavaDoc;
34 import java.io.LineNumberReader JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import java.net.InetAddress JavaDoc;
41 import java.net.ServerSocket JavaDoc;
42 import java.net.UnknownHostException JavaDoc;
43 import java.net.Socket JavaDoc;
44
45 import org.objectweb.util.cmdline.lib.ApplicationBase;
46 import org.objectweb.util.cmdline.lib.DefaultCommandLine;
47 import org.objectweb.util.cmdline.api.OptionArgument;
48 import org.objectweb.util.cmdline.lib.DefaultOptionArgument;
49
50 /**
51  * Comanche is a micro HTTP server.
52  */

53 public class Application
54      extends ApplicationBase
55 {
56     // ==================================================================
57
//
58
// Internal state.
59
//
60
// ==================================================================
61

62     /** The command line argument for the root directory. */
63     private OptionArgument rootDirectoryArgument_;
64     
65     /** Th command line argument for the TCP/IP port. */
66     private OptionArgument portArgument_;
67     
68     /** Th command line argument for the InetAddress group. */
69     private OptionArgument groupAddress_;
70     
71     // ==================================================================
72
//
73
// Constructor.
74
//
75
// ==================================================================
76

77     /** The default constructor. */
78     public Application()
79     {
80         // Call the ApplicationBase constructor.
81
super(
82                 // Init the command line.
83
new DefaultCommandLine("comanche", "",
84                         "Run the OpenCCM Comanche HTTP server.", true)
85         );
86         
87         // Set zero argument to the command line.
88
getCommandLine().setArguments(new String JavaDoc[0]);
89         
90         // Add the command line argument for the root directory.
91
rootDirectoryArgument_ =
92             new DefaultOptionArgument("--rootdir",
93                     "directory",
94                     "Set the root directory, default is ./",
95                     "."
96             );
97         getCommandLine().addOption(rootDirectoryArgument_);
98         
99         // Add the command line argument for the TCP/IP port.
100
portArgument_ =
101             new DefaultOptionArgument("--port", "port",
102                     "Set the TCP/IP port, default is 8080.", "8080"
103             );
104         getCommandLine().addOption(portArgument_);
105         
106         // Add the command line argument for the groupAddress argument.
107
groupAddress_ =
108             new DefaultOptionArgument("--group", "groupAddress",
109                     "Set the address group, default is 224.0.0.100.",
110                     "224.0.0.100");
111         getCommandLine().addOption(groupAddress_);
112     }
113     
114     // ==================================================================
115
//
116
// Public methods for org.objectweb.util.api.Identifiable
117
//
118
// ==================================================================
119

120     // ==================================================================
121
//
122
// Public methods for org.objectweb.util.cmdline.api.Application
123
//
124
// ==================================================================
125

126     /**
127      * Starts the main function.
128      * Must be defined into subclasses.
129      * @param args The command line arguments.
130      * @return The status of the application.
131      */

132     public int start(String JavaDoc[] args)
133     {
134         // Obtain the root directory for the HTTP and Multicast server.
135
String JavaDoc rootDirectory = rootDirectoryArgument_.getArgument();
136         
137         // Starting the Multicast Server
138
String JavaDoc addgroup = groupAddress_.getArgument();
139         MulticastServer mcastserver = new MulticastServer(addgroup,rootDirectory);
140         mcastserver.start();
141         System.err.println("The Comanche Server is waiting Multicast requests on mcast://" + addgroup);
142         
143         // Obtain the TCP/IP port for the HTTP server.
144
int port = 0;
145         try {
146             port = Integer.parseInt(portArgument_.getArgument());
147         } catch(NumberFormatException JavaDoc ex) {
148             report_exception(ex);
149             return -1;
150         }
151         
152         // Create the TCP/IP socket for the HTTP server.
153
ServerSocket JavaDoc server = null;
154         try {
155             server = new ServerSocket JavaDoc(port);
156         } catch(IOException JavaDoc ex) {
157             report_exception(ex);
158             return -1;
159         }
160         
161         // Obtain the URL to access the HTTP server.
162
String JavaDoc url = null;
163         try {
164             url = "http://" + InetAddress.getLocalHost().getHostName()
165             + ':' + server.getLocalPort();
166         } catch(UnknownHostException JavaDoc ex) {
167             report_exception(ex);
168             return -1;
169         }
170         
171         System.err.println("The OpenCCM Comanche Server is waiting HTTP requests on " + url);
172         
173         // Infinite loop of the HTTP server.
174
while (true) {
175             try {
176                 getConsole().message("Waiting requests on " + url);
177                 // Accept a new client.
178
Socket JavaDoc client = server.accept();
179                 
180                 InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(client.getInputStream());
181                 PrintStream JavaDoc out = new PrintStream JavaDoc(client.getOutputStream());
182                 
183                 // Read the client request.
184
String JavaDoc request = new LineNumberReader JavaDoc(in).readLine();
185                 getConsole().message(request);
186                 
187                 if (request.startsWith("GET ")) {
188                     File JavaDoc f = new File JavaDoc(rootDirectory + request.substring(4, request.indexOf(' ', 4)));
189                     if (f.exists() && !f.isDirectory()) {
190                         InputStream JavaDoc is = new FileInputStream JavaDoc(f);
191                         byte[] data = new byte[is.available()];
192                         is.read(data);
193                         is.close();
194                         out.print("HTTP/1.0 200 OK\n\n");
195                         out.write(data);
196                     } else {
197                         out.print("HTTP/1.0 404 Not Found\n\n");
198                         out.print("<html>Document not found.</html>");
199                     }
200                 }
201                 out.close();
202                 client.close();
203             } catch(Exception JavaDoc ex) {
204                 report_exception(ex);
205             }
206         }
207     }
208     
209     // ==================================================================
210
//
211
// Public methods.
212
//
213
// ==================================================================
214

215     /**
216      * The main bootstrap method.
217      * @param args The command line arguments.
218      */

219     public static void
220     main(String JavaDoc[] args) {
221         Application application = new Application();
222         application.runMain(args);
223     }
224 }
225
Popular Tags