ossiejhmoore

OScript / Java | Socket Communication

Dec 21st, 2015
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. Example showing how to use an OScript Socket to write data to a background Java ServerSocket.
  2.  
  3. 1. Compile and package the Java source (note the dependencies).
  4. 2. Copy the resulting jar file and the Apache Commons IO library to your (OTHome | module) ojlib directory.
  5. 3. Create a new script file in OScript using the source below.
  6. 4. Run the OScript script.
  7.  
  8. DISCLAIMER: Obviously, this code is more educational than production quality.
  9.  
  10. Java source library dependencies:
  11. * Apache Commons IO
  12. * OpenText oml.jar file from %OTHOME%\ojlib\oml.jar
  13.  
  14. //// OSCRIPT SOURCE CODE BEGIN ////
  15. Integer counter, port, start
  16. Socket sock
  17. Dynamic joResult
  18. String path, clazz, methServerSocketForFile, crlf
  19. List parms
  20. Boolean ok
  21.  
  22.  
  23. start = Date.Tick()
  24.  
  25. crlf = Str.Ascii(13) + Str.Ascii(10)
  26. path = "C:\TEMP\output.txt"
  27.  
  28. clazz = "com.capspire.cs.example.SocketListenerExample"
  29. methServerSocketForFile = "getServerSocketForFile"
  30. parms = { path }
  31.  
  32. joResult = JavaObject.InvokeStaticMethod(clazz, methServerSocketForFile, parms)
  33. if (IsError(joResult))
  34.     echo(joResult)
  35. else
  36.     port = joResult
  37.     sock = Socket.Create()
  38.     Socket.Connect( sock, "localhost", port )
  39.     for counter = 1 to 1000000
  40.         ok = Socket.Write( sock, Str.Format("This is line %1%2", counter, crlf) )
  41.         if (!ok)
  42.             echo("Failed to write to socket!")
  43.             break
  44.         end
  45.         if ( counter % 1000 == 0)
  46.             echo("  counter: ", counter)
  47.         end
  48.     end
  49.     Socket.Flush( sock )
  50.     Socket.Close( sock)
  51. end
  52.  
  53. echo("duration: ", Date.Tick() - start)
  54. //// OSCRIPT SOURCE CODE END ////
  55.  
  56. //// JAVA SOURCE CODE BEGIN ////
  57. package com.capspire.cs.example;
  58.  
  59. import com.opentext.livelink.oml.OScriptObject;
  60. import java.io.File;
  61. import java.io.FileOutputStream;
  62. import java.io.IOException;
  63. import java.io.InputStream;
  64. import java.io.OutputStream;
  65. import java.net.ServerSocket;
  66. import java.net.Socket;
  67. import org.apache.commons.io.IOUtils;
  68.  
  69. public class SocketListenerExample {
  70.     /**
  71.      * OScript will call this, passing a string path.
  72.      */
  73.     public static synchronized int getServerSocketForFile(final String path) {
  74.         return getServerSocketForFile(new File(path));
  75.     }
  76.  
  77.     public static synchronized int getServerSocketForFile(final File output) {
  78.         final Integer port;
  79.         try {
  80.             final ServerSocket listener = new ServerSocket(0);
  81.             //if connection not established within ten seconds, kill socket listener
  82.             listener.setSoTimeout(10000);
  83.             port = listener.getLocalPort();
  84.             echo(String.format("[SocketListenerExample.getServerSocketForFile] listener established on port %s", port));
  85.             final Runnable job = new Runnable() {
  86.                 public void run() {
  87.                     Socket socket = null;
  88.                     try {
  89.                         socket = listener.accept();
  90.                         final InputStream is = socket.getInputStream();
  91.                         final OutputStream os = new FileOutputStream(output);
  92.                         boolean done;
  93.                         do {
  94.                             int piped = IOUtils.copy(is, os);
  95.                             done = (piped == 0);
  96.                         } while (!done);
  97.                         os.close();
  98.                         is.close();
  99.                     } catch (Exception e) {
  100.                         System.err.println(e.getLocalizedMessage());
  101.                     } finally {
  102.                         if ((socket != null) && (!socket.isConnected())) {
  103.                             try {
  104.                                 socket.close();
  105.                             } catch (IOException ioe) {
  106.                                 //do nothing, throwing it away anyway
  107.                             }
  108.                         }
  109.                     }
  110.                 }
  111.             };
  112.             final Thread t = new Thread(job);
  113.             t.start();
  114.         } catch (Throwable t) {
  115.             throw new RuntimeException(String.format(
  116.                     "Encountered error while generating a ServerSocket. Error: %s",
  117.                     t.getLocalizedMessage()));
  118.         }
  119.         return port;
  120.     }
  121.  
  122.     private static void echo(String msg) {
  123.         try {
  124.             OScriptObject.runScript("echo", msg);
  125.         } catch (Throwable e) {
  126.             System.out.println(msg);
  127.         }
  128.     }
  129. }
  130. //// JAVA SOURCE CODE END ////
Advertisement
Add Comment
Please, Sign In to add comment