Coverage details for com.martiansoftware.nailgun.NGContext

LineHitsSource
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;
22 import java.io.PrintStream;
23 import java.net.InetAddress;
24 import java.net.NetworkInterface;
25 import java.util.Properties;
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      */
515    private Properties remoteEnvironment = null;
52  
53     /**
54      * The remote host's address
55      */
565    private InetAddress remoteHost = null;
57  
58     /**
59      * The port on the remote host that is communicating with NailGun
60      */
615    private int remotePort = 0;
62  
63     /**
64      * Command line arguments for the nail
65      */
665    private String[] args = null;
67  
68     /**
69      * A stream to which a client exit code can be printed
70      */
715    private PrintStream exitStream = null;
72  
73     /**
74      * The NGServer that accepted this connection
75      */
765    private NGServer server = null;
77  
78     /**
79      * The command that was issued for this connection
80      */
815    private String command = null;
82  
835    private String workingDirectory = null;
84     
85     /**
86      * The client's stdin
87      */
885    public InputStream in = null;
89  
90     /**
91      * The client's stdout
92      */
935    public PrintStream out = null;
94  
95     /**
96      * The client's stderr
97      */
985    public PrintStream err = null;
99  
100     
101     /**
102      * Creates a new, empty NGContext
103      */
104     NGContext() {
1055        super();
1065    }
107     
108     void setExitStream(PrintStream exitStream) {
1091        this.exitStream = exitStream;
1101    }
111  
112     void setPort(int remotePort) {
1131        this.remotePort = remotePort;
1141    }
115  
116     void setCommand(String command) {
1171        this.command = command;
1181    }
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 getCommand() {
1261        return (command);
127     }
128     
129     void setWorkingDirectory(String workingDirectory) {
1301        this.workingDirectory = workingDirectory;
1311    }
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 getWorkingDirectory() {
1401        return (workingDirectory);
141     }
142     
143     void setEnv(Properties remoteEnvironment) {
1441        this.remoteEnvironment = remoteEnvironment;
1451    }
146  
147     void setInetAddress(InetAddress remoteHost) {
1483        this.remoteHost = remoteHost;
1493    }
150  
151     void setArgs(String[] args) {
1521        this.args = args;
1531    }
154  
155     void setNGServer(NGServer server) {
1561        this.server = server;
1571    }
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 getEnv() {
1671        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 getFileSeparator() {
1750        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 getPathSeparator() {
1830        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 getInetAddress() {
1919        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[] getArgs() {
2011        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() {
2091        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) {
2221        exitStream.println(exitCode);
2231    }
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() {
2321        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() {
2403        if (!getInetAddress().isLoopbackAddress()) {
2412            throw (new SecurityException("Client is not at loopback address."));
242         }
2431    }
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() {
2503        NetworkInterface iface = null;
251         try {
2523            iface = NetworkInterface.getByInetAddress(getInetAddress());
2530        } catch (java.net.SocketException se) {
2540            throw (new SecurityException("Unable to determine if client is local. Assuming he isn't."));
2553        }
256         
2573        if ((iface == null) && (!getInetAddress().isLoopbackAddress())) {
2581            throw (new SecurityException("Client is not local."));
259         }
2602    }
261 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.