PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/Remote.java

#
Java | 195 lines | 126 code | 25 blank | 44 comment | 12 complexity | 523c98a1ababbbf7a8ec19e9ddfb32a2 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. import java.io.*;
  35. import java.net.*;
  36. import java.text.*;
  37. /**
  38. Remote executor class.
  39. */
  40. public class Remote {
  41. public static void main( String args[] )
  42. throws Exception
  43. {
  44. if ( args.length < 2 ) {
  45. System.out.println(
  46. "usage: Remote URL(http|bsh) file [ file ] ... ");
  47. System.exit(1);
  48. }
  49. String url = args[0];
  50. String text = getFile(args[1]);
  51. int ret = eval( url, text, null );
  52. System.exit( ret );
  53. }
  54. /**
  55. Evaluate text in the interpreter at url, capturing output into
  56. output and returning a possible integer return value.
  57. */
  58. public static int eval( String url, String text, StringBuffer output )
  59. throws IOException
  60. {
  61. String returnValue = null;
  62. if ( url.startsWith( "http:" ) ) {
  63. returnValue = doHttp( url, text );
  64. } else if ( url.startsWith( "bsh:" ) ) {
  65. returnValue = doBsh( url, text );
  66. } else
  67. throw new IOException( "Unrecognized URL type."
  68. +"Scheme must be http:// or bsh://");
  69. try {
  70. return Integer.parseInt( returnValue );
  71. } catch ( Exception e ) {
  72. // this convention may change...
  73. return 0;
  74. }
  75. }
  76. static String doBsh( String url, String text )
  77. {
  78. OutputStream out;
  79. InputStream in;
  80. String host = "";
  81. String port = "";
  82. String returnValue = "-1";
  83. String orgURL = url;
  84. // Need some format checking here
  85. try {
  86. url = url.substring(6); // remove the bsh://
  87. // get the index of the : between the host and the port is located
  88. int index = url.indexOf(":");
  89. host = url.substring(0,index);
  90. port = url.substring(index+1,url.length());
  91. } catch ( Exception ex ) {
  92. System.err.println("Bad URL: "+orgURL+": "+ex );
  93. return returnValue;
  94. }
  95. try {
  96. System.out.println("Connecting to host : "
  97. + host + " at port : " + port);
  98. Socket s = new Socket(host, Integer.parseInt(port) + 1);
  99. out = s.getOutputStream();
  100. in = s.getInputStream();
  101. sendLine( text, out );
  102. BufferedReader bin = new BufferedReader(
  103. new InputStreamReader(in));
  104. String line;
  105. while ( (line=bin.readLine()) != null )
  106. System.out.println( line );
  107. // Need to scrape a value from the last line?
  108. returnValue="1";
  109. return returnValue;
  110. } catch(Exception ex) {
  111. System.err.println("Error communicating with server: "+ex);
  112. return returnValue;
  113. }
  114. }
  115. private static void sendLine( String line, OutputStream outPipe )
  116. throws IOException
  117. {
  118. outPipe.write( line.getBytes() );
  119. outPipe.flush();
  120. }
  121. static String doHttp( String postURL, String text )
  122. {
  123. String returnValue = null;
  124. StringBuffer sb = new StringBuffer();
  125. sb.append( "bsh.client=Remote" );
  126. sb.append( "&bsh.script=" );
  127. sb.append( URLEncoder.encode( text ) );
  128. String formData = sb.toString( );
  129. try {
  130. URL url = new URL( postURL );
  131. HttpURLConnection urlcon =
  132. (HttpURLConnection) url.openConnection( );
  133. urlcon.setRequestMethod("POST");
  134. urlcon.setRequestProperty("Content-type",
  135. "application/x-www-form-urlencoded");
  136. urlcon.setDoOutput(true);
  137. urlcon.setDoInput(true);
  138. PrintWriter pout = new PrintWriter( new OutputStreamWriter(
  139. urlcon.getOutputStream(), "8859_1"), true );
  140. pout.print( formData );
  141. pout.flush();
  142. // read results...
  143. int rc = urlcon.getResponseCode();
  144. if ( rc != HttpURLConnection.HTTP_OK )
  145. System.out.println("Error, HTTP response: "+rc );
  146. returnValue = urlcon.getHeaderField("Bsh-Return");
  147. BufferedReader bin = new BufferedReader(
  148. new InputStreamReader( urlcon.getInputStream() ) );
  149. String line;
  150. while ( (line=bin.readLine()) != null )
  151. System.out.println( line );
  152. System.out.println( "Return Value: "+returnValue );
  153. } catch (MalformedURLException e) {
  154. System.out.println(e); // bad postURL
  155. } catch (IOException e2) {
  156. System.out.println(e2); // I/O error
  157. }
  158. return returnValue;
  159. }
  160. static String getFile( String name )
  161. throws FileNotFoundException, IOException
  162. {
  163. StringBuffer sb = new StringBuffer();
  164. BufferedReader bin = new BufferedReader( new FileReader( name ) );
  165. String line;
  166. while ( (line=bin.readLine()) != null )
  167. sb.append( line ).append( "\n" );
  168. return sb.toString();
  169. }
  170. }