Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Example showing how to use an OScript Socket to write data to a background Java ServerSocket.
- 1. Compile and package the Java source (note the dependencies).
- 2. Copy the resulting jar file and the Apache Commons IO library to your (OTHome | module) ojlib directory.
- 3. Create a new script file in OScript using the source below.
- 4. Run the OScript script.
- DISCLAIMER: Obviously, this code is more educational than production quality.
- Java source library dependencies:
- * Apache Commons IO
- * OpenText oml.jar file from %OTHOME%\ojlib\oml.jar
- //// OSCRIPT SOURCE CODE BEGIN ////
- Integer counter, port, start
- Socket sock
- Dynamic joResult
- String path, clazz, methServerSocketForFile, crlf
- List parms
- Boolean ok
- start = Date.Tick()
- crlf = Str.Ascii(13) + Str.Ascii(10)
- path = "C:\TEMP\output.txt"
- clazz = "com.capspire.cs.example.SocketListenerExample"
- methServerSocketForFile = "getServerSocketForFile"
- parms = { path }
- joResult = JavaObject.InvokeStaticMethod(clazz, methServerSocketForFile, parms)
- if (IsError(joResult))
- echo(joResult)
- else
- port = joResult
- sock = Socket.Create()
- Socket.Connect( sock, "localhost", port )
- for counter = 1 to 1000000
- ok = Socket.Write( sock, Str.Format("This is line %1%2", counter, crlf) )
- if (!ok)
- echo("Failed to write to socket!")
- break
- end
- if ( counter % 1000 == 0)
- echo(" counter: ", counter)
- end
- end
- Socket.Flush( sock )
- Socket.Close( sock)
- end
- echo("duration: ", Date.Tick() - start)
- //// OSCRIPT SOURCE CODE END ////
- //// JAVA SOURCE CODE BEGIN ////
- package com.capspire.cs.example;
- import com.opentext.livelink.oml.OScriptObject;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import org.apache.commons.io.IOUtils;
- public class SocketListenerExample {
- /**
- * OScript will call this, passing a string path.
- */
- public static synchronized int getServerSocketForFile(final String path) {
- return getServerSocketForFile(new File(path));
- }
- public static synchronized int getServerSocketForFile(final File output) {
- final Integer port;
- try {
- final ServerSocket listener = new ServerSocket(0);
- //if connection not established within ten seconds, kill socket listener
- listener.setSoTimeout(10000);
- port = listener.getLocalPort();
- echo(String.format("[SocketListenerExample.getServerSocketForFile] listener established on port %s", port));
- final Runnable job = new Runnable() {
- public void run() {
- Socket socket = null;
- try {
- socket = listener.accept();
- final InputStream is = socket.getInputStream();
- final OutputStream os = new FileOutputStream(output);
- boolean done;
- do {
- int piped = IOUtils.copy(is, os);
- done = (piped == 0);
- } while (!done);
- os.close();
- is.close();
- } catch (Exception e) {
- System.err.println(e.getLocalizedMessage());
- } finally {
- if ((socket != null) && (!socket.isConnected())) {
- try {
- socket.close();
- } catch (IOException ioe) {
- //do nothing, throwing it away anyway
- }
- }
- }
- }
- };
- final Thread t = new Thread(job);
- t.start();
- } catch (Throwable t) {
- throw new RuntimeException(String.format(
- "Encountered error while generating a ServerSocket. Error: %s",
- t.getLocalizedMessage()));
- }
- return port;
- }
- private static void echo(String msg) {
- try {
- OScriptObject.runScript("echo", msg);
- } catch (Throwable e) {
- System.out.println(msg);
- }
- }
- }
- //// JAVA SOURCE CODE END ////
Advertisement
Add Comment
Please, Sign In to add comment