KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > NGContext


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17  */

18
19 package com.martiansoftware.nailgun;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.NetworkInterface JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 /**
28  * <p>Provides quite a bit of potentially useful information to classes
29  * specifically written for NailGun. The <a HREF="NGServer.html">NailGun server</a> itself, its
30  * <a HREF="AliasManager.html">AliasManager</a>, the remote client's environment variables, and other
31  * information is available via this class. For all intents and purposes,
32  * the NGContext represents a single connection from a NailGun client.</p>
33  *
34  * If a class is written with a
35  *
36  * <pre><code>
37  * public static void nailMain(NGContext context)
38  * </code></pre>
39  *
40  * method, that method will be called by NailGun instead of the traditional
41  * <code>main(String[])</code> method normally used for programs. A fully populated <code>NGContext</code>
42  * object will then be provided to <code>nailMain()</code>.
43  *
44  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb </a>
45  */

46 public class NGContext {
47
48     /**
49      * The remote host's environment variables
50      */

51     private Properties JavaDoc remoteEnvironment = null;
52
53     /**
54      * The remote host's address
55      */

56     private InetAddress JavaDoc remoteHost = null;
57
58     /**
59      * The port on the remote host that is communicating with NailGun
60      */

61     private int remotePort = 0;
62
63     /**
64      * Command line arguments for the nail
65      */

66     private String JavaDoc[] args = null;
67
68     /**
69      * A stream to which a client exit code can be printed
70      */

71     private PrintStream JavaDoc exitStream = null;
72
73     /**
74      * The NGServer that accepted this connection
75      */

76     private NGServer server = null;
77
78     /**
79      * The command that was issued for this connection
80      */

81     private String JavaDoc command = null;
82
83     private String JavaDoc workingDirectory = null;
84     
85     /**
86      * The client's stdin
87      */

88     public InputStream JavaDoc in = null;
89
90     /**
91      * The client's stdout
92      */

93     public PrintStream JavaDoc out = null;
94
95     /**
96      * The client's stderr
97      */

98     public PrintStream JavaDoc err = null;
99
100     
101     /**
102      * Creates a new, empty NGContext
103      */

104     NGContext() {
105         super();
106     }
107     
108     void setExitStream(PrintStream JavaDoc exitStream) {
109         this.exitStream = exitStream;
110     }
111
112     void setPort(int remotePort) {
113         this.remotePort = remotePort;
114     }
115
116     void setCommand(String JavaDoc command) {
117         this.command = command;
118     }
119
120     /**
121      * Returns the command that was issued by the client (either an alias or the name of a class).
122      * This allows multiple aliases to point to the same class but result in different behaviors.
123      * @return the command issued by the client
124      */

125     public String JavaDoc getCommand() {
126         return (command);
127     }
128     
129     void setWorkingDirectory(String JavaDoc workingDirectory) {
130         this.workingDirectory = workingDirectory;
131     }
132     
133     /**
134      * Returns the current working directory of the client, as reported by the client.
135      * This is a String that will use the client's <code>File.separator</code> ('/' or '\'),
136      * which may differ from the separator on the server.
137      * @return the current working directory of the client
138      */

139     public String JavaDoc getWorkingDirectory() {
140         return (workingDirectory);
141     }
142     
143     void setEnv(Properties JavaDoc remoteEnvironment) {
144         this.remoteEnvironment = remoteEnvironment;
145     }
146
147     void setInetAddress(InetAddress JavaDoc remoteHost) {
148         this.remoteHost = remoteHost;
149     }
150
151     void setArgs(String JavaDoc[] args) {
152         this.args = args;
153     }
154
155     void setNGServer(NGServer server) {
156         this.server = server;
157     }
158
159     /**
160      * Returns a <code>java.util.Properties</code> object containing a copy
161      * of the client's environment variables
162      * @see java.util.Properties
163      * @return a <code>java.util.Properties</code> object containing a copy
164      * of the client's environment variables
165      */

166     public Properties JavaDoc getEnv() {
167         return (remoteEnvironment);
168     }
169
170     /**
171      * Returns the file separator ('/' or '\\') used by the client's os.
172      * @return the file separator ('/' or '\\') used by the client's os.
173      */

174     public String JavaDoc getFileSeparator() {
175         return (remoteEnvironment.getProperty("NAILGUN_FILESEPARATOR"));
176     }
177     
178     /**
179      * Returns the path separator (':' or ';') used by the client's os.
180      * @return the path separator (':' or ';') used by the client's os.
181      */

182     public String JavaDoc getPathSeparator() {
183         return (remoteEnvironment.getProperty("NAILGUN_PATHSEPARATOR"));
184     }
185     
186     /**
187      * Returns the address of the client at the other side of this connection.
188      * @return the address of the client at the other side of this connection.
189      */

190     public InetAddress JavaDoc getInetAddress() {
191         return (remoteHost);
192     }
193
194     /**
195      * Returns the command line arguments for the command
196      * implementation (nail) on the server.
197      * @return the command line arguments for the command
198      * implementation (nail) on the server.
199      */

200     public String JavaDoc[] getArgs() {
201         return (args);
202     }
203
204     /**
205      * Returns the NGServer that accepted this connection
206      * @return the NGServer that accepted this connection
207      */

208     public NGServer getNGServer() {
209         return (server);
210     }
211
212     /**
213      * Sends an exit command with the specified exit code to
214      * the client. The client will exit immediately with
215      * the specified exit code; you probably want to return
216      * from nailMain immediately after calling this.
217      *
218      * @param exitCode the exit code with which the client
219      * should exit
220      */

221     public void exit(int exitCode) {
222         exitStream.println(exitCode);
223     }
224
225     /**
226      * Returns the port on the client connected to the NailGun
227      * server.
228      * @return the port on the client connected to the NailGun
229      * server.
230      */

231     public int getPort() {
232         return (remotePort);
233     }
234     
235     /**
236      * Throws a <code>java.lang.SecurityException</code> if the client is not
237      * connected via the loopback address.
238      */

239     public void assertLoopbackClient() {
240         if (!getInetAddress().isLoopbackAddress()) {
241             throw (new SecurityException JavaDoc("Client is not at loopback address."));
242         }
243     }
244     
245     /**
246      * Throws a <code>java.lang.SecurityException</code> if the client is not
247      * connected from the local machine.
248      */

249     public void assertLocalClient() {
250         NetworkInterface JavaDoc iface = null;
251         try {
252             iface = NetworkInterface.getByInetAddress(getInetAddress());
253         } catch (java.net.SocketException JavaDoc se) {
254             throw (new SecurityException JavaDoc("Unable to determine if client is local. Assuming he isn't."));
255         }
256         
257         if ((iface == null) && (!getInetAddress().isLoopbackAddress())) {
258             throw (new SecurityException JavaDoc("Client is not local."));
259         }
260     }
261 }
Popular Tags