Yes, of course "inverse" - sorry.
Persistent vs RegisteredObject - not a problem but you are calling a simple class method so we don't need any super class. I used this implementation for the IRIS Class:
Class Utils.CSW1JavaFunctions { ClassMethod IrisReturn(user = "user", pass = "pass") As %Stream.GlobalBinary { try { set cswStream=##class(%Stream.GlobalBinary).%New() set cswReturn = {"user":(user), "pass":(pass) } do cswReturn.%ToJSON(cswStream) return cswStream } catch exc { write !,"Caught Exception on server: ", exc.AsSQLMessage() } } }
And this is a crude hack at the Java code - the anonymous InputStream class could use more work but it does run for this simple example. I'll leave the rest of the InputStream coding to you.
package utils; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.intersystems.jdbc.*; import java.io.*; import java.sql.SQLException; public class Reader { public static final String CACHE_CLASS_NAME = "Utils.CSW1JavaFunctions"; public IRISConnection connection; public IRIS iris; public Reader(IRISConnection connection) throws SQLException { this.connection = connection; this.iris = IRIS.createIRIS(connection); } public static void main(String[] args) throws SQLException { IRISDataSource dataSource = new IRISDataSource(); dataSource.setServerName("localhost"); dataSource.setPortNumber(51776); dataSource.setDatabaseName("USER"); dataSource.setUser("_SYSTEM"); dataSource.setPassword("SYS"); IRISConnection connection = (IRISConnection) dataSource.getConnection(); Reader reader = new Reader(connection); try { JsonNode jsonNode = reader.execute("IrisReturn", "java", "jpass"); System.out.println(jsonNode.toString()); } catch (Exception exc) { exc.printStackTrace(); } } public JsonNode execute(String method, Object... args) throws Exception { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = null; try { IRISObject data = (IRISObject) iris.classMethodObject(CACHE_CLASS_NAME, method, args[0], args[1]); InputStream is = new InputStream() { byte[] buffer; int pos = 0; int len = -1; @Override public int read() throws IOException { if (pos >= len) { getBuffer(); } if (len == -1) { return -1; } return buffer[pos++]; } void getBuffer() { pos = 0; IRISReference readLen = new IRISReference(3200); String string = (String) data.invoke("Read", readLen); if (readLen.getLong() == -1) { buffer = null; len = -1; } else { buffer = string.getBytes(); len = buffer.length; } } }; jsonNode = (JsonNode) mapper.readTree(is); return jsonNode; } catch (Exception ex) { ex.printStackTrace(); } return null; } }
Running this produces this output:
/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/bin/java -javaagent:/home/...
{"user":"java","pass":"jpass"}
Process finished with exit code 0
- Log in to post comments