| Line | Hits | Source |
|---|---|---|
| 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 | */ | |
| 51 | 5 | private Properties remoteEnvironment = null; |
| 52 | ||
| 53 | /** | |
| 54 | * The remote host's address | |
| 55 | */ | |
| 56 | 5 | private InetAddress remoteHost = null; |
| 57 | ||
| 58 | /** | |
| 59 | * The port on the remote host that is communicating with NailGun | |
| 60 | */ | |
| 61 | 5 | private int remotePort = 0; |
| 62 | ||
| 63 | /** | |
| 64 | * Command line arguments for the nail | |
| 65 | */ | |
| 66 | 5 | private String[] args = null; |
| 67 | ||
| 68 | /** | |
| 69 | * A stream to which a client exit code can be printed | |
| 70 | */ | |
| 71 | 5 | private PrintStream exitStream = null; |
| 72 | ||
| 73 | /** | |
| 74 | * The NGServer that accepted this connection | |
| 75 | */ | |
| 76 | 5 | private NGServer server = null; |
| 77 | ||
| 78 | /** | |
| 79 | * The command that was issued for this connection | |
| 80 | */ | |
| 81 | 5 | private String command = null; |
| 82 | ||
| 83 | 5 | private String workingDirectory = null; |
| 84 | ||
| 85 | /** | |
| 86 | * The client's stdin | |
| 87 | */ | |
| 88 | 5 | public InputStream in = null; |
| 89 | ||
| 90 | /** | |
| 91 | * The client's stdout | |
| 92 | */ | |
| 93 | 5 | public PrintStream out = null; |
| 94 | ||
| 95 | /** | |
| 96 | * The client's stderr | |
| 97 | */ | |
| 98 | 5 | public PrintStream err = null; |
| 99 | ||
| 100 | ||
| 101 | /** | |
| 102 | * Creates a new, empty NGContext | |
| 103 | */ | |
| 104 | NGContext() { | |
| 105 | 5 | super(); |
| 106 | 5 | } |
| 107 | ||
| 108 | void setExitStream(PrintStream exitStream) { | |
| 109 | 1 | this.exitStream = exitStream; |
| 110 | 1 | } |
| 111 | ||
| 112 | void setPort(int remotePort) { | |
| 113 | 1 | this.remotePort = remotePort; |
| 114 | 1 | } |
| 115 | ||
| 116 | void setCommand(String command) { | |
| 117 | 1 | this.command = command; |
| 118 | 1 | } |
| 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() { | |
| 126 | 1 | return (command); |
| 127 | } | |
| 128 | ||
| 129 | void setWorkingDirectory(String workingDirectory) { | |
| 130 | 1 | this.workingDirectory = workingDirectory; |
| 131 | 1 | } |
| 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() { | |
| 140 | 1 | return (workingDirectory); |
| 141 | } | |
| 142 | ||
| 143 | void setEnv(Properties remoteEnvironment) { | |
| 144 | 1 | this.remoteEnvironment = remoteEnvironment; |
| 145 | 1 | } |
| 146 | ||
| 147 | void setInetAddress(InetAddress remoteHost) { | |
| 148 | 3 | this.remoteHost = remoteHost; |
| 149 | 3 | } |
| 150 | ||
| 151 | void setArgs(String[] args) { | |
| 152 | 1 | this.args = args; |
| 153 | 1 | } |
| 154 | ||
| 155 | void setNGServer(NGServer server) { | |
| 156 | 1 | this.server = server; |
| 157 | 1 | } |
| 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() { | |
| 167 | 1 | 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() { | |
| 175 | 0 | 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() { | |
| 183 | 0 | 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() { | |
| 191 | 9 | 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() { | |
| 201 | 1 | 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 | 1 | 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 | 1 | exitStream.println(exitCode); |
| 223 | 1 | } |
| 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 | 1 | 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 | 3 | if (!getInetAddress().isLoopbackAddress()) { |
| 241 | 2 | throw (new SecurityException("Client is not at loopback address.")); |
| 242 | } | |
| 243 | 1 | } |
| 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 | 3 | NetworkInterface iface = null; |
| 251 | try { | |
| 252 | 3 | iface = NetworkInterface.getByInetAddress(getInetAddress()); |
| 253 | 0 | } catch (java.net.SocketException se) { |
| 254 | 0 | throw (new SecurityException("Unable to determine if client is local. Assuming he isn't.")); |
| 255 | 3 | } |
| 256 | ||
| 257 | 3 | if ((iface == null) && (!getInetAddress().isLoopbackAddress())) { |
| 258 | 1 | throw (new SecurityException("Client is not local.")); |
| 259 | } | |
| 260 | 2 | } |
| 261 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |