diff --git a/java/src/main/java/com/genexus/sap/DestinationProvider.java b/java/src/main/java/com/genexus/sap/DestinationProvider.java index 22d3e0d66..939459e31 100644 --- a/java/src/main/java/com/genexus/sap/DestinationProvider.java +++ b/java/src/main/java/com/genexus/sap/DestinationProvider.java @@ -23,8 +23,8 @@ public static DestinationProvider Instance() return _instance; } - private String SAP_SERVER = "SAP_SERVER"; - private String SAP_DOC_SERVER = "DMS_SERVER"; + private static final String SAP_SERVER = "SAP_SERVER"; + private static final String SAP_DOC_SERVER = "DMS_SERVER"; private Properties connectionProperties; private Hashtable connectionList = new Hashtable(); private DestinationDataEventListener eventListener; @@ -33,16 +33,8 @@ public static DestinationProvider Instance() public Properties getDestinationProperties(String sessionName) { if (sessionName != null) - { - Properties val = connectionList.get(sessionName); - if (val != null) - { - return val; - } - else - { - return null; - } + { + return connectionList.get(sessionName); } else { @@ -65,16 +57,8 @@ public void setDestinationDataEventListener(DestinationDataEventListener eventLi @Override public Properties getServerProperties(String serverName) { if (serverName != null) - { - Properties val = connectionList.get(serverName); - if (val != null) - { - return val; - } - else - { - return null; - } + { + return connectionList.get(serverName); } else { @@ -118,7 +102,7 @@ public void setServerProperties( String serverName, Properties properties) } else { - Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + Integer.toString(connectionList.size())); + Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + connectionList.size()); connectionProperties = properties; connectionList.put( serverName, connectionProperties); if (eventListener != null) @@ -160,7 +144,7 @@ public void setConnectionProperties( String sessionName, Properties properties) } else { - Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + Integer.toString(connectionList.size())); + Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + connectionList.size()); connectionProperties = properties; connectionList.put( sessionName, connectionProperties); if (eventListener != null) diff --git a/java/src/main/java/com/genexus/sap/DocumentClient.java b/java/src/main/java/com/genexus/sap/DocumentClient.java index e996cdbe9..c76c5667d 100644 --- a/java/src/main/java/com/genexus/sap/DocumentClient.java +++ b/java/src/main/java/com/genexus/sap/DocumentClient.java @@ -1,24 +1,16 @@ package com.genexus.sap; -import java.io.*; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import com.sap.conn.jco.AbapClassException; -import com.sap.conn.jco.AbapException; - import com.sap.conn.jco.JCoFunction; import com.sap.conn.jco.JCoParameterList; import com.sap.conn.jco.JCoTable; -import com.sap.conn.jco.server.JCoServer; -import com.sap.conn.jco.server.JCoServerContext; -import com.sap.conn.jco.server.JCoServerContextInfo; -import com.sap.conn.jco.server.JCoServerErrorListener; -import com.sap.conn.jco.server.JCoServerExceptionListener; +import com.sap.conn.jco.server.*; -import com.sap.conn.jco.server.JCoServerFunctionHandler; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.Path; public class DocumentClient { private static final int BLOB_LENGTH = 1022; @@ -43,7 +35,7 @@ public void serverErrorOccurred(JCoServer server, String connectionID, JCoServer static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler { @Override - public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException { + public void handleRequest(JCoServerContext serverCtx, JCoFunction function) { String fname; int length; JCoTable blob; @@ -67,14 +59,18 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro length -= BLOB_LENGTH; hasNextRow = blob.nextRow(); } - else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")"); + else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")"); } if (length > 0){ if (hasNextRow) out.write(blob.getByteArray(0), 0, length); - else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")"); + else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")"); } } catch (IOException e) { + + // Unfortunately there is no way of transmitting error details back to SAP, so we better log it here, + // if we want to keep the chance of troubleshooting later, what exactly went wrong... + e.printStackTrace(); function.getExportParameterList().setValue("ERROR", 3); } @@ -92,19 +88,19 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler { @Override - public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException { + public void handleRequest(JCoServerContext serverCtx, JCoFunction function) { String fname; fname = function.getImportParameterList().getString("FNAME"); fname = fname.replace("#",""); - - try (InputStream source = Files.newInputStream(Paths.get(fname));) { + Path inputFile = Paths.get(fname); + try (InputStream source = Files.newInputStream(inputFile)) { byte[] file2 = new byte[BLOB_LENGTH]; int bytesread; int totallenght =0; - InputStream source2 = Files.newInputStream(Paths.get(fname)); + InputStream source2 = Files.newInputStream(inputFile); JCoTable blobtable = function.getTableParameterList().getTable("BLOB"); while((bytesread = source2.read(file2,0,file2.length)) >0 ) @@ -124,7 +120,4 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro } } } - - - } \ No newline at end of file diff --git a/java/src/main/java/com/genexus/sap/DocumentReceiver.java b/java/src/main/java/com/genexus/sap/DocumentReceiver.java new file mode 100644 index 000000000..0a568e737 --- /dev/null +++ b/java/src/main/java/com/genexus/sap/DocumentReceiver.java @@ -0,0 +1,122 @@ +package com.genexus.sap; + +import com.genexus.ModelContext; +import com.sap.conn.jco.JCoException; + +import com.sap.conn.jco.JCoMetaData; +import com.sap.conn.jco.JCoRecordMetaData; +import com.sap.conn.jco.server.JCoServer; +import com.sap.conn.jco.server.DefaultServerHandlerFactory; + +import com.sap.conn.jco.server.JCoServerFactory; +import com.sap.conn.jco.JCoCustomRepository; +import com.sap.conn.jco.JCoFunctionTemplate; +import com.sap.conn.jco.JCoListMetaData; +import com.sap.conn.jco.JCo; +import com.genexus.diagnostics.Log; + +public class DocumentReceiver +{ + private String serverName; + private String repositoryName; + private ModelContext _context; + JCoServer receiver = null; + + + public DocumentReceiver(String server, String repository, ModelContext context) + { + this.serverName = server; + this.repositoryName = repository; + this._context = context; + } + + public void start() + { + Log.info("GX SAP - DMS Receiver Start " + serverName + " " + repositoryName); + try { + receiver = setupServerDownload(serverName, repositoryName); + receiver.start(); + } + catch (JCoException ex) + { + Log.error("GX SAP - Error Starting " + ex) ; + } + } + + public void stop() + { + + String receiverName = ""; + if (serverName == null || serverName.equals("")) + { + Object receiverObj = _context.getContextProperty("SAPReceiverServerName"); + if (receiverObj != null) + { + receiverName = (String)receiverObj; + } + } + else + { + receiverName = serverName; + } + + try { + if (!receiverName.equals("")) + { + receiver = JCoServerFactory.getServer(receiverName); + if (receiver != null) receiver.stop(); + Log.info("GX SAP - DMS Receiver stop " + receiverName ); + } + else + { + Log.error("GX SAP - Error Stopping receiver." ); + } + } + catch (JCoException ex) + { + Log.error("GX SAP - Error Stopping " + ex) ; + } + + } + + public JCoServer setupServerDownload(String serverName, String repositoryName) throws JCoException + { + JCoServer server = JCoServerFactory.getServer(serverName); + JCoCustomRepository repo = JCo.createCustomRepository(repositoryName); + JCoListMetaData imports, exports, tables; + JCoFunctionTemplate FTP_R3_TO_CLIENT; + + imports = JCo.createListMetaData("IMPORTING"); + imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER); + imports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.IMPORT_PARAMETER); + imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER); + imports.lock(); + + exports = JCo.createListMetaData("EXPORTING"); + exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); + exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); + exports.lock(); + + JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB"); + tabLine.add("LINE", JCoMetaData.TYPE_BYTE, EnterpriseConnect.BLOB_LENGTH, 0, EnterpriseConnect.BLOB_LENGTH, 0); + tabLine.lock(); + tables = JCo.createListMetaData("TABLES"); + tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0); + tables.lock(); + + FTP_R3_TO_CLIENT = JCo.createFunctionTemplate("FTP_R3_TO_CLIENT", imports, exports, null, tables, null); + repo.addFunctionTemplateToCache(FTP_R3_TO_CLIENT); + server.setRepository(repo); + + DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory(); + handlerFactory.registerHandler("FTP_R3_TO_CLIENT", new DocumentClient.FTP_R3_TO_CLIENTHandler()); + server.setCallHandlerFactory(handlerFactory); + + DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler(); + server.addServerErrorListener(hdl); + server.addServerExceptionListener(hdl); + + return server; + } + +} \ No newline at end of file diff --git a/java/src/main/java/com/genexus/sap/DocumentSender.java b/java/src/main/java/com/genexus/sap/DocumentSender.java new file mode 100644 index 000000000..72e8617fc --- /dev/null +++ b/java/src/main/java/com/genexus/sap/DocumentSender.java @@ -0,0 +1,123 @@ + + + +package com.genexus.sap; + +import com.genexus.ModelContext; +import com.sap.conn.jco.JCoException; +import com.sap.conn.jco.JCoMetaData; +import com.sap.conn.jco.JCoRecordMetaData; +import com.sap.conn.jco.server.JCoServer; +import com.sap.conn.jco.server.DefaultServerHandlerFactory; + +import com.sap.conn.jco.server.JCoServerFactory; +import com.sap.conn.jco.JCoCustomRepository; +import com.sap.conn.jco.JCoFunctionTemplate; +import com.sap.conn.jco.JCoListMetaData; +import com.sap.conn.jco.JCo; +import com.genexus.diagnostics.Log; + +public class DocumentSender +{ + private String serverName; + private String repositoryName; + private ModelContext _context; + JCoServer sender = null; + + + public DocumentSender(String server, String repository, ModelContext context) + { + this.serverName = server; + this.repositoryName = repository; + this._context = context; + } + + public void start() + { + Log.info("GX SAP - DMS Sender Start " + serverName + " " + repositoryName); + + try { + sender = setupServerUpload(serverName, repositoryName); + sender.start(); + } + catch (JCoException ex) + { + Log.error("GX SAP - Error Starting " + ex) ; + } + } + + public void stop() + { + String senderName = ""; + if (serverName == null || serverName.equals("")) + { + Object senderObj = _context.getContextProperty("SAPSenderServerName"); + if (senderObj != null) + { + senderName = (String)senderObj; + } + } + else + { + senderName = serverName; + } + try { + if (!senderName.equals("")) + { + sender = JCoServerFactory.getServer(senderName); + if (sender != null) sender.stop(); + Log.info("GX SAP - DMS Sender stop " + senderName ); + } + else + { + Log.error("GX SAP - Error Stopping sender" ); + } + } + catch (JCoException ex) + { + Log.error("GX SAP - Error Stopping " + ex) ; + } + } + + public JCoServer setupServerUpload(String serverName, String repositoryName) throws JCoException + { + JCoServer server = JCoServerFactory.getServer(serverName); + + JCoCustomRepository repo = JCo.createCustomRepository(repositoryName); + JCoListMetaData imports, exports, tables; + JCoFunctionTemplate FTP_CLIENT_TO_R3; + + imports = JCo.createListMetaData("IMPORTING"); + imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER); + imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER); + imports.lock(); + + exports = JCo.createListMetaData("EXPORTING"); + exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); + exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); + exports.lock(); + + JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB"); + tabLine.add("LINE", JCoMetaData.TYPE_BYTE, EnterpriseConnect.BLOB_LENGTH, 0, EnterpriseConnect.BLOB_LENGTH, 0); + tabLine.lock(); + + tables = JCo.createListMetaData("TABLES"); + tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0); + tables.lock(); + + FTP_CLIENT_TO_R3 = JCo.createFunctionTemplate("FTP_CLIENT_TO_R3", imports, exports, null, tables, null); + repo.addFunctionTemplateToCache(FTP_CLIENT_TO_R3); + server.setRepository(repo); + + DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory(); + handlerFactory.registerHandler("FTP_CLIENT_TO_R3", new DocumentClient.FTP_CLIENT_TO_R3Handler()); + server.setCallHandlerFactory(handlerFactory); + + DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler(); + server.addServerErrorListener(hdl); + server.addServerExceptionListener(hdl); + + return server; + } + +} \ No newline at end of file diff --git a/java/src/main/java/com/genexus/sap/EnterpriseConnect.java b/java/src/main/java/com/genexus/sap/EnterpriseConnect.java index 14f555bc0..898954951 100644 --- a/java/src/main/java/com/genexus/sap/EnterpriseConnect.java +++ b/java/src/main/java/com/genexus/sap/EnterpriseConnect.java @@ -1,6 +1,7 @@ package com.genexus.sap; import java.math.BigDecimal; +import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Iterator; @@ -25,7 +26,8 @@ public class EnterpriseConnect { - static String DESTINATION_NAME = "SAP_SERVER"; + static final String DESTINATION_NAME = "SAP_SERVER"; + static final int BLOB_LENGTH = 1022; JCoFunction function = null; String destinationName = DESTINATION_NAME; @@ -47,14 +49,7 @@ public void setValue(String parameterName, String value) setvalue = true; function.getImportParameterList().setValue(parameterName, value); } - if (setvalue) - { - function.getImportParameterList().setActive(parameterName, true); - } - else - { - function.getImportParameterList().setActive(parameterName, false); - } + function.getImportParameterList().setActive(parameterName, setvalue); } public void setValue(String parameterName, int value) @@ -84,7 +79,7 @@ public void setValue(String parameterName, double value) public void setValue(String parameterName, Date value) { GregorianCalendar calendar = new GregorianCalendar(); - calendar.set(0, 0, 0); + calendar.set(0, Calendar.JANUARY, 0); Date baseDate = calendar.getTime(); if (value != null && value.after(baseDate)) { @@ -99,7 +94,7 @@ public void setValue(String parameterName, Date value) // I/O parameter - public void setValue(String parameterName, GXSimpleCollection[] value, Boolean inOut) + public void setValue(String parameterName, GXSimpleCollection[] value, Boolean inOut) { if (value != null && value.length > 0) { @@ -107,8 +102,7 @@ public void setValue(String parameterName, GXSimpleCollection[] value, Boolean i } } - - public void setValue(String parameterName, GXSimpleCollection[] value) + public void setValue(String parameterName, GXSimpleCollection[] value) { if (value != null && value.length > 0) { @@ -125,16 +119,16 @@ public void setValue(String parameterName, IGxJSONAble[] value) } // ------ - public void setValue(String parameterName, GXSimpleCollection value) + public void setValue(String parameterName, GXSimpleCollection value) { setValue(parameterName, value, false); } - public void setValue(String parameterName, GXSimpleCollection value, Boolean inOut) + public void setValue(String parameterName, GXSimpleCollection value, Boolean inOut) { JCoTable jTable = function.getTableParameterList().getTable(parameterName); - Boolean setValues = false; + boolean setValues = false; try{ for (int i = 1; i <= value.getItemCount(); i++) { @@ -150,9 +144,20 @@ public void setValue(String parameterName, GXSimpleCollection value, Boolean inO { String key = (String)keys.next(); int jcoType = jTable.getRecordMetaData().getType(key); + //int len = jTable.getRecordMetaData().getLength(key); + int dec = jTable.getRecordMetaData().getDecimals(key); + if( jObj.get(key) instanceof String ) { - jTable.setValue(key, jObj.getString(key)); + String obj_value = jObj.getString(key); + if (jcoType == JCoMetaData.TYPE_NUM && dec == 0 && ( ! obj_value.trim().equals("") )) + { + String sValue = new DecimalFormat("#").format(new BigDecimal(obj_value)); + jTable.setValue(key, sValue); + } + else { + jTable.setValue(key, obj_value); + } } else if (jcoType == JCoMetaData.TYPE_NUM || jcoType == JCoMetaData.TYPE_INT) { @@ -173,7 +178,7 @@ else if (jcoType == JCoMetaData.TYPE_INT2 || jcoType == JCoMetaData.TYPE_INT1 } else { - System.out.println( key + " Invalid Type " + Integer.toString(jcoType)); + System.out.println( key + " Invalid Type " + jcoType); } } } @@ -230,7 +235,7 @@ else if (jcoType == JCoMetaData.TYPE_INT2 || jcoType == JCoMetaData.TYPE_INT1 } else { - System.out.println( key + " Invalid Type " + Integer.toString(jcoType)); + System.out.println( key + " Invalid Type " + jcoType); } } function.getImportParameterList().setActive(parameterName, true); @@ -249,11 +254,11 @@ else if (jcoType == JCoMetaData.TYPE_INT2 || jcoType == JCoMetaData.TYPE_INT1 /* --- Get Return Values --- */ - public void getValue(String parameterName, GXSimpleCollection[] value) + public void getValue(String parameterName, GXSimpleCollection[] value) { if (value.length != 0) { - GXSimpleCollection col = value[0]; + GXSimpleCollection col = value[0]; col.clear(); JCoTable tbl = function.getTableParameterList().getTable(parameterName); JSONArray jCol = new JSONArray(); @@ -375,7 +380,7 @@ public void executeStart(String functionName) } catch (JCoException e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException("JCoException: " + e); } } @@ -391,12 +396,12 @@ public void executeStart(String functionName, boolean isTransaction) throws JCoE function = destination.getRepository().getFunction(functionName); if (function == null) { - throw new RuntimeException( functionName + " not found in SAP"); + throw new RuntimeException( "GeneXus cannot find SAP function " + functionName); } } catch (AbapException e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException(" ABAP Exception: " + e); } } @@ -408,7 +413,7 @@ public int executeFunction(String functionName) } catch (JCoException e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException("JCoException excecuting: " + e); } } @@ -427,7 +432,7 @@ public int executeFunction(String functionName, boolean isTransaction) throws JC } catch (AbapException e) { - throw new RuntimeException(e.toString()); + throw new RuntimeException( "ABAP exception executing: " + e); } return 0; } diff --git a/java/src/main/java/com/genexus/sap/ServerConnection.java b/java/src/main/java/com/genexus/sap/ServerConnection.java new file mode 100644 index 000000000..0641d787b --- /dev/null +++ b/java/src/main/java/com/genexus/sap/ServerConnection.java @@ -0,0 +1,136 @@ +package com.genexus.sap; + +import java.util.Properties; +import com.genexus.ModelContext; +import com.genexus.diagnostics.Log; +import com.sap.conn.jco.AbapException; +import com.sap.conn.jco.JCoContext; +import com.sap.conn.jco.JCoDestination; +import com.sap.conn.jco.JCoDestinationManager; +import com.sap.conn.jco.JCoException; +import com.sap.conn.jco.JCoFunction; +import com.sap.conn.jco.ext.DestinationDataProvider; +import com.sap.conn.jco.ext.ServerDataProvider; + +public class ServerConnection +{ + ModelContext _context; + SessionManager _manager; + DestinationProvider destinationProvider = null; + private Integer errorCode = 0; + private String errorMessage = ""; + + private String hashedSession = ""; + + public ServerConnection(SessionManager manager, ModelContext context) + { + _manager = manager; + _context = context; + } + + public void connect(Properties connectionProperties) + { + + if (destinationProvider == null) { + destinationProvider = DestinationProvider.Instance(); + } + + if (_manager.getMessageHost() == null || _manager.getMessageHost().equals("")) { + connectionProperties.setProperty(DestinationDataProvider.JCO_ASHOST, _manager.getRouterString() + _manager.getAppServer()); + } + else { + if (_manager.getPort() == null || _manager.getPort().equals("")) { + connectionProperties.setProperty(DestinationDataProvider.JCO_MSHOST, _manager.getMessageHost()); + connectionProperties.setProperty(DestinationDataProvider.JCO_MSSERV, _manager.getMessageSrv()); + } + else { + connectionProperties.setProperty(DestinationDataProvider.JCO_MSHOST, _manager.getMessageHost() + ":" + _manager.getPort()); + if (!_manager.getMessageSrv().equals("")) + { + connectionProperties.setProperty(DestinationDataProvider.JCO_MSSERV, _manager.getMessageSrv() + ":" + _manager.getPort()); + } + } + } + + // Document Transfer server + //connectionProperties.setProperty(DestinationDataProvider.JCO_GWHOST, gatewayHost); + //connectionProperties.setProperty(DestinationDataProvider.JCO_GWSERV, gatewayService); + // Set Destination Properties + + connectionProperties.setProperty(DestinationDataProvider.JCO_R3NAME, _manager.getSystemId()); + connectionProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, _manager.getSAPRouter()); + connectionProperties.setProperty(DestinationDataProvider.JCO_GROUP, _manager.getGroup()); + + connectionProperties.setProperty(ServerDataProvider.JCO_GWHOST, _manager.getGatewayHost()); + connectionProperties.setProperty(ServerDataProvider.JCO_GWSERV, _manager.getGatewaySrv()); + connectionProperties.setProperty(ServerDataProvider.JCO_PROGID, _manager.getProgramID()); + connectionProperties.setProperty(ServerDataProvider.JCO_CONNECTION_COUNT, _manager.getRegistrationCount()); + // + connectionProperties.setProperty(DestinationDataProvider.JCO_SYSNR, _manager.getInstanceNumber()); + connectionProperties.setProperty(DestinationDataProvider.JCO_CLIENT, _manager.getClientNumber()); + connectionProperties.setProperty(DestinationDataProvider.JCO_USER, _manager.getUserName()); + connectionProperties.setProperty(DestinationDataProvider.JCO_PASSWD, _manager.getPassword()); + + connectionProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, _manager.getPoolCapacity()); + connectionProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, _manager.getPeekLimit()); + + if ( ! _manager.getLanguage().equals("")) + { + connectionProperties.setProperty(DestinationDataProvider.JCO_LANG, _manager.getLanguage()); + } + + // Hash session name + hashedSession = _manager.getSessionName() + "H" + Integer.toString(connectionProperties.values().toString().hashCode()).replace("-","0"); + + Log.info("GX SAP - Connecting " + _manager.getSessionName()); + destinationProvider.setConnectionProperties( hashedSession, connectionProperties); + destinationProvider.setServerProperties(_manager.getServerName(), connectionProperties); + + _context.setContextProperty("SAPSessionName", hashedSession); + + _context.setContextProperty("SAPReceiverServerName", _manager.getServerName()); + + _context.setContextProperty("SAPReceiverRepositoryName", _manager.getRepositoryName()); + _context.setContextProperty("SAPSenderServerName", _manager.getServerName()); + _context.setContextProperty("SAPSenderRepositoryName", _manager.getRepositoryName()); + + try + { + JCoDestination destination = JCoDestinationManager.getDestination(hashedSession); + destination.ping(); + } + catch (AbapException ex) + { + errorCode = ex.getGroup(); + errorMessage = ex.toString(); + Log.warning("GX SAP - Error Connecting " + _manager.getSessionName() + " " + ex.toString()) ; + } + catch (JCoException ex) + { + if(ex.getGroup() == JCoException.JCO_ERROR_INTERNAL) + { + Log.error("GX SAP - Error Connecting " + _manager.getSessionName() + " " + ex.toString()) ; + throw new RuntimeException(ex.toString()); + } + errorCode = ex.getGroup(); + errorMessage = ex.toString(); + Log.warning("GX SAP - Error Connecting " + _manager.getSessionName() + " " + ex.toString()) ; + } + catch (Exception ex) + { + Log.error("GX SAP - Error Connecting " + _manager.getSessionName() + " " + ex.toString()) ; + throw new RuntimeException(ex.toString()); + } + } + + public Integer getErrorCode() + { + return errorCode; + } + + public String getErrorMessage() + { + return errorMessage; + } + +} \ No newline at end of file diff --git a/java/src/main/java/com/genexus/sap/SessionManager.java b/java/src/main/java/com/genexus/sap/SessionManager.java index efccadb15..a6415aa6f 100644 --- a/java/src/main/java/com/genexus/sap/SessionManager.java +++ b/java/src/main/java/com/genexus/sap/SessionManager.java @@ -9,22 +9,10 @@ import com.sap.conn.jco.JCoDestinationManager; import com.sap.conn.jco.JCoException; import com.sap.conn.jco.JCoFunction; -import com.sap.conn.jco.JCoMetaData; -import com.sap.conn.jco.JCoRecordMetaData; -import com.sap.conn.jco.server.JCoServer; -import com.sap.conn.jco.server.DefaultServerHandlerFactory; - -import com.sap.conn.jco.ext.DestinationDataProvider; -import com.sap.conn.jco.ext.ServerDataProvider; -import com.sap.conn.jco.server.JCoServerFactory; -import com.sap.conn.jco.JCoCustomRepository; -import com.sap.conn.jco.JCoFunctionTemplate; -import com.sap.conn.jco.JCoListMetaData; -import com.sap.conn.jco.JCo; public class SessionManager { - private static final int BLOB_LENGTH = 1022; + private String msHost = ""; private String msServ = ""; private String group = ""; @@ -40,7 +28,7 @@ public class SessionManager private String clientNumber = ""; private String systemId = ""; private String sessionName = ""; - private String hashedSession = ""; + private String sapGUI = ""; private Integer errorCode = 0; private String errorMessage = ""; @@ -54,10 +42,10 @@ public class SessionManager private ModelContext _context = null; - DestinationProvider destinationProvider = null; Properties connectionProperties=null; - JCoServer receiver = null; - JCoServer sender = null; + DocumentReceiver documentReceiver=null; + DocumentSender documentSender=null; + public SessionManager(ModelContext context) { @@ -76,291 +64,102 @@ public void Disconnect() //_context.setContextProperty("SAPSessionName", ""); } - public void Connect() + public void ConnectSession(String destination, String scope) { + Log.info("GX SAP - Connecting to Destination" + destination); + ConnectInternal(destination, destination, scope); + } + + public void ConnectInternal(String session, String sessionName, String scope) + { errorCode = 0; - errorMessage = ""; - connectionProperties = new Properties(); - if (destinationProvider == null) { - destinationProvider = DestinationProvider.Instance(); - } - - if (msHost == null || msHost.equals("")) { - connectionProperties.setProperty(DestinationDataProvider.JCO_ASHOST, routerString + appServer); - } - else { - if (port == null || port.equals("")) { - connectionProperties.setProperty(DestinationDataProvider.JCO_MSHOST, msHost); - connectionProperties.setProperty(DestinationDataProvider.JCO_MSSERV, msServ); - } - else { - connectionProperties.setProperty(DestinationDataProvider.JCO_MSHOST, msHost + ":" + port); - if (!msServ.equals("")) - { - connectionProperties.setProperty(DestinationDataProvider.JCO_MSSERV, msServ + ":" + port); - } - } - } - // Document Transfer server - //connectionProperties.setProperty(DestinationDataProvider.JCO_GWHOST, gatewayHost); - //connectionProperties.setProperty(DestinationDataProvider.JCO_GWSERV, gatewayService); - connectionProperties.setProperty(DestinationDataProvider.JCO_R3NAME, systemId); - connectionProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapRouter); - connectionProperties.setProperty(DestinationDataProvider.JCO_GROUP, group); - - connectionProperties.setProperty(ServerDataProvider.JCO_GWHOST,gatewayHost); - connectionProperties.setProperty(ServerDataProvider.JCO_GWSERV, gatewayService); - connectionProperties.setProperty(ServerDataProvider.JCO_PROGID, programID); - connectionProperties.setProperty(ServerDataProvider.JCO_CONNECTION_COUNT, registrationCount); - // - connectionProperties.setProperty(DestinationDataProvider.JCO_SYSNR, instanceNumber); - connectionProperties.setProperty(DestinationDataProvider.JCO_CLIENT, clientNumber); - connectionProperties.setProperty(DestinationDataProvider.JCO_USER, userName); - connectionProperties.setProperty(DestinationDataProvider.JCO_PASSWD, password); - - connectionProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, poolCapacity); - connectionProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, peekLimit); - - if ( ! language.equals("")) - { - connectionProperties.setProperty(DestinationDataProvider.JCO_LANG, language); - } - - // Hash session name - hashedSession = sessionName + "H" + Integer.toString(connectionProperties.values().toString().hashCode()).replace("-","0"); - - Log.info("GX SAP - Connecting " + sessionName); - destinationProvider.setConnectionProperties( hashedSession, connectionProperties); - destinationProvider.setServerProperties(serverName, connectionProperties); - - _context.setContextProperty("SAPSessionName", hashedSession); - - _context.setContextProperty("SAPReceiverServerName", serverName); - _context.setContextProperty("SAPReceiverRepositoryName", repositoryName); - _context.setContextProperty("SAPSenderServerName", serverName); - _context.setContextProperty("SAPSenderRepositoryName", repositoryName); - - + errorMessage = ""; + _context.setContextProperty("SAPSessionName", session); + _context.setContextProperty("SAPSessionScope", scope); try { - JCoDestination destination = JCoDestinationManager.getDestination(hashedSession); + JCoDestination destination = null; + if ( scope == null || scope.length() == 0 ) + destination = JCoDestinationManager.getDestination(session); + else + destination = JCoDestinationManager.getDestination(session, scope); destination.ping(); } catch (AbapException ex) { - errorCode = ex.getGroup(); + errorCode = ex.getGroup(); errorMessage = ex.toString(); - Log.warning("GX SAP - Error Connecting " + sessionName + " " + ex.toString()) ; - + Log.warning("GX SAP - Error Connecting " + sessionName + " " + ex) ; } catch (JCoException ex) - { + { if(ex.getGroup() == JCoException.JCO_ERROR_INTERNAL) { - Log.error("GX SAP - Error Connecting " + sessionName + " " + ex.toString()) ; + Log.error("GX SAP - Error Connecting " + sessionName + " " + ex) ; throw new RuntimeException(ex.toString()); } errorCode = ex.getGroup(); errorMessage = ex.toString(); - Log.warning("GX SAP - Error Connecting " + sessionName + " " + ex.toString()) ; - } + Log.warning("GX SAP - Error Connecting " + sessionName + " " + ex) ; + } catch (Exception ex) - { - Log.error("GX SAP - Error Connecting " + sessionName + " " + ex.toString()) ; - throw new RuntimeException(ex.toString()); - } - } - - - public JCoServer setupServerDownload(String serverName, String repositoryName) throws JCoException - { - JCoServer server = JCoServerFactory.getServer(serverName); - JCoCustomRepository repo = JCo.createCustomRepository(repositoryName); - JCoListMetaData imports, exports, tables; - JCoFunctionTemplate FTP_R3_TO_CLIENT; - - - imports = JCo.createListMetaData("IMPORTING"); - imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER); - imports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.IMPORT_PARAMETER); - imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER); - imports.lock(); - - exports = JCo.createListMetaData("EXPORTING"); - exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); - exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); - exports.lock(); - - JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB"); - tabLine.add("LINE", JCoMetaData.TYPE_BYTE, BLOB_LENGTH, 0, BLOB_LENGTH, 0); - tabLine.lock(); - tables = JCo.createListMetaData("TABLES"); - tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0); - tables.lock(); - - FTP_R3_TO_CLIENT = JCo.createFunctionTemplate("FTP_R3_TO_CLIENT", imports, exports, null, tables, null); - repo.addFunctionTemplateToCache(FTP_R3_TO_CLIENT); - server.setRepository(repo); - - DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory(); - handlerFactory.registerHandler("FTP_R3_TO_CLIENT", new DocumentClient.FTP_R3_TO_CLIENTHandler()); - server.setCallHandlerFactory(handlerFactory); - - DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler(); - server.addServerErrorListener(hdl); - server.addServerExceptionListener(hdl); - - return server; + { + Log.error("GX SAP - Error Connecting " + sessionName + " " + ex) ; + throw new RuntimeException(ex.toString()); + } } - - public JCoServer setupServerUpload(String serverName, String repositoryName) throws JCoException + public void Connect() { - JCoServer server = JCoServerFactory.getServer(serverName); - - JCoCustomRepository repo = JCo.createCustomRepository(repositoryName); - JCoListMetaData imports, exports, tables; - JCoFunctionTemplate FTP_CLIENT_TO_R3; - - imports = JCo.createListMetaData("IMPORTING"); - imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER); - imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER); - imports.lock(); - - exports = JCo.createListMetaData("EXPORTING"); - exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); - exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER); - exports.lock(); - - JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB"); - tabLine.add("LINE", JCoMetaData.TYPE_BYTE, BLOB_LENGTH, 0, BLOB_LENGTH, 0); - tabLine.lock(); - - tables = JCo.createListMetaData("TABLES"); - tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0); - tables.lock(); - - FTP_CLIENT_TO_R3 = JCo.createFunctionTemplate("FTP_CLIENT_TO_R3", imports, exports, null, tables, null); - repo.addFunctionTemplateToCache(FTP_CLIENT_TO_R3); - server.setRepository(repo); - - DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory(); - handlerFactory.registerHandler("FTP_CLIENT_TO_R3", new DocumentClient.FTP_CLIENT_TO_R3Handler()); - server.setCallHandlerFactory(handlerFactory); - - DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler(); - server.addServerErrorListener(hdl); - server.addServerExceptionListener(hdl); + errorCode = 0; + errorMessage = ""; + connectionProperties = new Properties(); - return server; + ServerConnection connection = new ServerConnection(this, _context); + connection.connect(connectionProperties); } - + public void DocumentReceiverStart() { - Log.info("GX SAP - DMS Receiver Start " + serverName + " " + repositoryName); - try { - receiver = setupServerDownload(serverName, repositoryName); - receiver.start(); - } - catch (JCoException ex) - { - Log.error("GX SAP - Error Starting " + ex.toString()) ; - } + documentReceiver = new DocumentReceiver(serverName, repositoryName, _context); + documentReceiver.start(); } public void DocumentReceiverStop() { - String receiverName = ""; - if (serverName == null || serverName.equals("")) - { - Object receiverObj = _context.getContextProperty("SAPReceiverServerName"); - if (receiverObj != null) - { - receiverName = (String)receiverObj; - } - } - else - { - receiverName = serverName; - } - - try { - if (!receiverName.equals("")) - { - receiver = JCoServerFactory.getServer(receiverName); - if (receiver != null) receiver.stop(); - Log.info("GX SAP - DMS Receiver stop " + receiverName ); - } - else - { - Log.error("GX SAP - Error Stopping receiver." ); - } - } - catch (JCoException ex) - { - Log.error("GX SAP - Error Stopping " + ex.toString()) ; - } + documentReceiver.stop(); } public void DocumentSenderStart() - { - Log.info("GX SAP - DMS Sender Start " + serverName + " " + repositoryName); - - try { - sender = setupServerUpload(serverName, repositoryName); - sender.start(); - } - catch (JCoException ex) - { - Log.error("GX SAP - Error Starting " + ex.toString()) ; - } + { + documentSender = new DocumentSender(serverName, repositoryName, _context); + documentSender.start(); } public void DocumentSenderStop() { - String senderName = ""; - if (serverName == null || serverName.equals("")) - { - Object senderObj = _context.getContextProperty("SAPSenderServerName"); - if (senderObj != null) - { - senderName = (String)senderObj; - } - } - else - { - senderName = serverName; - } - try { - if (!senderName.equals("")) - { - sender = JCoServerFactory.getServer(senderName); - if (sender != null) sender.stop(); - Log.info("GX SAP - DMS Sender stop " + senderName ); - } - else - { - Log.error("GX SAP - Error Stopping sender" ); - } - } - catch (JCoException ex) - { - Log.error("GX SAP - Error Stopping " + ex.toString()) ; - } + documentSender.stop(); } public void TransactionBegin() { Object destinationObj = _context.getContextProperty("SAPSessionName"); + Object scopeObj = _context.getContextProperty("SAPSessionScope"); String destinationName = ""; + String scopeName = ""; if (destinationObj !=null) { try { destinationName = (String)destinationObj; + scopeName = (String)scopeObj; Log.info("GX SAP - Begin Transaction " + destinationName); - JCoDestination destination = JCoDestinationManager.getDestination(destinationName); + JCoDestination destination = null; + if (scopeName.length() == 0) + destination = JCoDestinationManager.getDestination(destinationName); + else + destination = JCoDestinationManager.getDestination(destinationName, scopeName); JCoContext.begin(destination); } catch (JCoException e) @@ -373,13 +172,22 @@ public void TransactionBegin() public void TransactionCommit() { Object destinationObj = _context.getContextProperty("SAPSessionName"); + Object scopeObject = _context.getContextProperty("SAPSessionScope"); String destinationName = ""; + String scopeName = ""; if (destinationObj !=null) { try { - destinationName = (String)destinationObj; - JCoDestination destination = JCoDestinationManager.getDestination(destinationName); + JCoDestination destination = null; + destinationName = (String)destinationObj; + if ( scopeObject == null || ((String)scopeObject).length() == 0 ) { + destination = JCoDestinationManager.getDestination(destinationName); + } + else { + scopeName = (String)scopeObject; + destination = JCoDestinationManager.getDestination(destinationName, scopeName); + } Log.info("GX SAP - Commit Transaction " + destinationName); JCoFunction commitFnc = destination.getRepository().getFunction("BAPI_TRANSACTION_COMMIT"); commitFnc.execute(destination); @@ -434,59 +242,59 @@ public String getGroup( ) } public String getUserName( ) - { + { return userName; - } + } public String getPassword() - { + { return password; - } + } public String getInstanceNumber( ) - { + { return instanceNumber; - } + } public String getAppServer( ) { return appServer; - } + } public String getRouterString() - { + { return routerString; - } + } public String getClientNumber( ) - { + { return clientNumber; - } + } public String getSystemId() - { + { return systemId; - } + } public String getSessionName( ) - { + { return sessionName; - } + } public String getSAPGUI() - { + { return sapGUI; - } + } public String getPeekLimit() - { + { return peekLimit; - } + } public String getPoolCapacity() - { + { return poolCapacity; - } + } public String getLanguage() { @@ -518,13 +326,11 @@ public String getServerName() return serverName; } - public String getRepositoryName() { return repositoryName; } - /* Setters */ public void setPort( String value ) @@ -567,39 +373,39 @@ public void setUserName( String value ) } public void setPassword( String value ) - { + { password = value; - } + } public void setInstanceNumber( String value ) - { + { instanceNumber = value; - } + } public void setAppServer( String value ) - { + { appServer = value; - } + } public void setRouterString( String value ) - { + { routerString = value; - } + } public void setClientNumber( String value ) - { + { clientNumber = value; - } + } public void setSystemId( String value ) - { + { systemId = value; - } + } public void setSessionName( String value ) { sessionName = value; - } + } public void setSAPGUI( String value ) { @@ -622,12 +428,12 @@ public void setLanguage(String value) } public void setErrorCode( Integer value ) - { + { errorCode = value; - } + } public void setErrorMessage( String value ) - { + { errorMessage = value; } @@ -650,5 +456,4 @@ public void setRepositoryName(String value) { repositoryName = value; } - } \ No newline at end of file