PageRenderTime 108ms CodeModel.GetById 92ms app.highlight 12ms RepoModel.GetById 1ms 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
  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
 34
 35package bsh;
 36
 37import java.io.*;
 38import java.net.*;
 39import java.text.*;
 40/**
 41	Remote executor class.
 42*/
 43public class Remote {
 44
 45    public static void main( String args[] ) 
 46		throws Exception
 47	{
 48		if ( args.length < 2 ) {
 49			System.out.println(
 50				"usage: Remote URL(http|bsh) file [ file ] ... ");
 51			System.exit(1);
 52		}
 53		String url = args[0];
 54		String text = getFile(args[1]);
 55		int ret = eval( url, text, null );
 56		System.exit( ret );
 57		}
 58
 59	/**
 60		Evaluate text in the interpreter at url, capturing output into
 61		output and returning a possible integer return value.
 62	*/
 63	public static int eval( String url, String text, StringBuffer output )
 64		throws IOException
 65	{
 66		String returnValue = null;
 67		if ( url.startsWith( "http:" ) ) {
 68			returnValue = doHttp( url, text );
 69		} else if ( url.startsWith( "bsh:" ) ) {
 70			returnValue = doBsh( url, text );
 71		} else
 72			throw new IOException( "Unrecognized URL type."
 73				+"Scheme must be http:// or bsh://");
 74
 75		try {
 76			return Integer.parseInt( returnValue );
 77		} catch ( Exception e ) {
 78			// this convention may change...
 79			return 0;
 80		}
 81	}
 82
 83	static String doBsh( String url, String text ) 
 84	{ 
 85	    OutputStream out;
 86	    InputStream in;
 87	    String host = "";
 88	    String port = "";
 89	    String returnValue = "-1";
 90		String orgURL = url;
 91	    
 92		// Need some format checking here
 93	    try {
 94			url = url.substring(6); // remove the bsh://
 95			// get the index of the : between the host and the port is located
 96			int index = url.indexOf(":");
 97			host = url.substring(0,index);
 98			port = url.substring(index+1,url.length());
 99		} catch ( Exception ex ) {
100			System.err.println("Bad URL: "+orgURL+": "+ex  );
101			return returnValue;
102	    }
103
104	    try {
105			System.out.println("Connecting to host : " 
106				+ host + " at port : " + port);
107			Socket s = new Socket(host, Integer.parseInt(port) + 1);
108			
109			out = s.getOutputStream();
110			in = s.getInputStream();
111			
112			sendLine( text, out );
113
114			BufferedReader bin = new BufferedReader( 
115				new InputStreamReader(in));
116			  String line;
117			  while ( (line=bin.readLine()) != null )
118				System.out.println( line );
119
120			// Need to scrape a value from the last line?
121			returnValue="1";
122			return returnValue;
123	    } catch(Exception ex) {
124			System.err.println("Error communicating with server: "+ex);
125			return returnValue;
126	    }
127	}
128
129    private static void sendLine( String line, OutputStream outPipe )
130		throws IOException
131	{
132		outPipe.write( line.getBytes() );
133		outPipe.flush();
134    }
135    
136
137	static String doHttp( String postURL, String text ) 
138	{
139		String returnValue = null;
140		StringBuffer sb = new StringBuffer();
141		sb.append( "bsh.client=Remote" );
142		sb.append( "&bsh.script=" );
143		sb.append( URLEncoder.encode( text ) );
144		String formData = sb.toString(  );
145
146		try {
147		  URL url = new URL( postURL );
148		  HttpURLConnection urlcon =
149			  (HttpURLConnection) url.openConnection(  );
150		  urlcon.setRequestMethod("POST");
151		  urlcon.setRequestProperty("Content-type",
152			  "application/x-www-form-urlencoded");
153		  urlcon.setDoOutput(true);
154		  urlcon.setDoInput(true);
155		  PrintWriter pout = new PrintWriter( new OutputStreamWriter(
156			  urlcon.getOutputStream(), "8859_1"), true );
157		  pout.print( formData );
158		  pout.flush();
159
160		  // read results...
161		  int rc = urlcon.getResponseCode();
162		  if ( rc != HttpURLConnection.HTTP_OK )
163			System.out.println("Error, HTTP response: "+rc );
164
165		  returnValue = urlcon.getHeaderField("Bsh-Return");
166
167		  BufferedReader bin = new BufferedReader( 
168			new InputStreamReader( urlcon.getInputStream() ) );
169		  String line;
170		  while ( (line=bin.readLine()) != null )
171			System.out.println( line );
172
173		  System.out.println( "Return Value: "+returnValue );
174
175		} catch (MalformedURLException e) {
176		  System.out.println(e);     // bad postURL
177		} catch (IOException e2) {
178		  System.out.println(e2);    // I/O error
179		}
180
181		return returnValue;
182	}
183
184	static String getFile( String name ) 
185		throws FileNotFoundException, IOException 
186	{
187		StringBuffer sb = new StringBuffer();
188		BufferedReader bin = new BufferedReader( new FileReader( name ) );
189		String line;
190		while ( (line=bin.readLine()) != null )
191			sb.append( line ).append( "\n" );
192		return sb.toString();
193	}
194
195}