/server/src/main/java/vimclojure/nailgun/NGContext.java

https://bitbucket.org/kotarak/vimclojure/ · Java · 261 lines · 92 code · 37 blank · 132 comment · 4 complexity · 2243afd5a1f293ad0c9333d1a9b3f3de MD5 · raw file

  1. /*
  2. Copyright 2004, Martian Software, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vimclojure.nailgun;
  14. import java.io.InputStream;
  15. import java.io.PrintStream;
  16. import java.net.InetAddress;
  17. import java.net.NetworkInterface;
  18. import java.util.Properties;
  19. /**
  20. * <p>Provides quite a bit of potentially useful information to classes
  21. * specifically written for NailGun. The <a href="NGServer.html">NailGun server</a> itself, its
  22. * <a href="AliasManager.html">AliasManager</a>, the remote client's environment variables, and other
  23. * information is available via this class. For all intents and purposes,
  24. * the NGContext represents a single connection from a NailGun client.</p>
  25. *
  26. * If a class is written with a
  27. *
  28. * <pre><code>
  29. * public static void nailMain(NGContext context)
  30. * </code></pre>
  31. *
  32. * method, that method will be called by NailGun instead of the traditional
  33. * <code>main(String[])</code> method normally used for programs. A fully populated <code>NGContext</code>
  34. * object will then be provided to <code>nailMain()</code>.
  35. *
  36. * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb </a>
  37. */
  38. public class NGContext {
  39. /**
  40. * The remote host's environment variables
  41. */
  42. private Properties remoteEnvironment = null;
  43. /**
  44. * The remote host's address
  45. */
  46. private InetAddress remoteHost = null;
  47. /**
  48. * The port on the remote host that is communicating with NailGun
  49. */
  50. private int remotePort = 0;
  51. /**
  52. * Command line arguments for the nail
  53. */
  54. private String[] args = null;
  55. /**
  56. * A stream to which a client exit code can be printed
  57. */
  58. private PrintStream exitStream = null;
  59. /**
  60. * The NGServer that accepted this connection
  61. */
  62. private NGServer server = null;
  63. /**
  64. * The command that was issued for this connection
  65. */
  66. private String command = null;
  67. private String workingDirectory = null;
  68. /**
  69. * The client's stdin
  70. */
  71. public InputStream in = null;
  72. /**
  73. * The client's stdout
  74. */
  75. public PrintStream out = null;
  76. /**
  77. * The client's stderr
  78. */
  79. public PrintStream err = null;
  80. /**
  81. * Creates a new, empty NGContext
  82. */
  83. NGContext() {
  84. super();
  85. }
  86. void setExitStream(PrintStream exitStream) {
  87. this.exitStream = exitStream;
  88. }
  89. void setPort(int remotePort) {
  90. this.remotePort = remotePort;
  91. }
  92. void setCommand(String command) {
  93. this.command = command;
  94. }
  95. /**
  96. * Returns the command that was issued by the client (either an alias or the name of a class).
  97. * This allows multiple aliases to point to the same class but result in different behaviors.
  98. * @return the command issued by the client
  99. */
  100. public String getCommand() {
  101. return (command);
  102. }
  103. void setWorkingDirectory(String workingDirectory) {
  104. this.workingDirectory = workingDirectory;
  105. }
  106. /**
  107. * Returns the current working directory of the client, as reported by the client.
  108. * This is a String that will use the client's <code>File.separator</code> ('/' or '\'),
  109. * which may differ from the separator on the server.
  110. * @return the current working directory of the client
  111. */
  112. public String getWorkingDirectory() {
  113. return (workingDirectory);
  114. }
  115. void setEnv(Properties remoteEnvironment) {
  116. this.remoteEnvironment = remoteEnvironment;
  117. }
  118. void setInetAddress(InetAddress remoteHost) {
  119. this.remoteHost = remoteHost;
  120. }
  121. void setArgs(String[] args) {
  122. this.args = args;
  123. }
  124. void setNGServer(NGServer server) {
  125. this.server = server;
  126. }
  127. /**
  128. * Returns a <code>java.util.Properties</code> object containing a copy
  129. * of the client's environment variables
  130. * @see java.util.Properties
  131. * @return a <code>java.util.Properties</code> object containing a copy
  132. * of the client's environment variables
  133. */
  134. public Properties getEnv() {
  135. return (remoteEnvironment);
  136. }
  137. /**
  138. * Returns the file separator ('/' or '\\') used by the client's os.
  139. * @return the file separator ('/' or '\\') used by the client's os.
  140. */
  141. public String getFileSeparator() {
  142. return (remoteEnvironment.getProperty("NAILGUN_FILESEPARATOR"));
  143. }
  144. /**
  145. * Returns the path separator (':' or ';') used by the client's os.
  146. * @return the path separator (':' or ';') used by the client's os.
  147. */
  148. public String getPathSeparator() {
  149. return (remoteEnvironment.getProperty("NAILGUN_PATHSEPARATOR"));
  150. }
  151. /**
  152. * Returns the address of the client at the other side of this connection.
  153. * @return the address of the client at the other side of this connection.
  154. */
  155. public InetAddress getInetAddress() {
  156. return (remoteHost);
  157. }
  158. /**
  159. * Returns the command line arguments for the command
  160. * implementation (nail) on the server.
  161. * @return the command line arguments for the command
  162. * implementation (nail) on the server.
  163. */
  164. public String[] getArgs() {
  165. return (args);
  166. }
  167. /**
  168. * Returns the NGServer that accepted this connection
  169. * @return the NGServer that accepted this connection
  170. */
  171. public NGServer getNGServer() {
  172. return (server);
  173. }
  174. /**
  175. * Sends an exit command with the specified exit code to
  176. * the client. The client will exit immediately with
  177. * the specified exit code; you probably want to return
  178. * from nailMain immediately after calling this.
  179. *
  180. * @param exitCode the exit code with which the client
  181. * should exit
  182. */
  183. public void exit(int exitCode) {
  184. exitStream.println(exitCode);
  185. }
  186. /**
  187. * Returns the port on the client connected to the NailGun
  188. * server.
  189. * @return the port on the client connected to the NailGun
  190. * server.
  191. */
  192. public int getPort() {
  193. return (remotePort);
  194. }
  195. /**
  196. * Throws a <code>java.lang.SecurityException</code> if the client is not
  197. * connected via the loopback address.
  198. */
  199. public void assertLoopbackClient() {
  200. if (!getInetAddress().isLoopbackAddress()) {
  201. throw (new SecurityException("Client is not at loopback address."));
  202. }
  203. }
  204. /**
  205. * Throws a <code>java.lang.SecurityException</code> if the client is not
  206. * connected from the local machine.
  207. */
  208. public void assertLocalClient() {
  209. NetworkInterface iface = null;
  210. try {
  211. iface = NetworkInterface.getByInetAddress(getInetAddress());
  212. } catch (java.net.SocketException se) {
  213. throw (new SecurityException("Unable to determine if client is local. Assuming he isn't."));
  214. }
  215. if ((iface == null) && (!getInetAddress().isLoopbackAddress())) {
  216. throw (new SecurityException("Client is not local."));
  217. }
  218. }
  219. }