Search code examples
javajakarta-eesap-erpjcobapi

How to use JCo connection without creating *.JcoDestination file


I'm trying to connect to SAP ECC 6.0 using JCo. I'm following this tutorial. However, there is a Note saying:

For this example the destination configuration is stored in a file that is called by the program. In practice you should avoid this for security reasons.

And that is reasonable and understood. But, there is no explenation how to set up secure destination provider. I found solution in this thread that created custom implementation of DestinationDataProvider and that works on my local machine. But when I deploy it on Portal I get an error saying that there is already registered DestinationDataProvider. So my question is: How to store destination data in SAP Java EE application?

Here is my code to further clarify what I'm trying to do.

public static void main(String... args) throws JCoException {
        CustomDestinationProviderMap provider = new CustomDestinationProviderMap();
        com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(provider);

        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "host.sap.my.domain.com");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, "user");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");

        provider.addDestination(DESTINATION_NAME1, connectProperties);
        connect();
    }

    public static void connect() throws JCoException {
        String FUNCTION_NAME = "BAPI_EMPLOYEE_GETDATA";
        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
        JCoContext.begin(destination);

        JCoFunction function = destination.getRepository().getFunction(FUNCTION_NAME);
        if (function == null) {
            throw new RuntimeException(FUNCTION_NAME + " not found in SAP.");
        }

        //function.getImportParameterList().setValue("EMPLOYEE_ID", "48");
        function.getImportParameterList().setValue("FSTNAME_M", "ANAKIN");
        function.getImportParameterList().setValue("LASTNAME_M", "SKYWALKER");

        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }

        JCoTable table = function.getTableParameterList().getTable("PERSONAL_DATA");
        for (int i = 0; i < table.getNumRows(); i++) {
            table.setRow(i);
            System.out.println(table.getString("PERNO") + '\t' + table.getString("FIRSTNAME") + '\t' + table.getString("LAST_NAME")
            +'\t' + table.getString("BIRTHDATE")+'\t' + table.getString("GENDER"));
        }

        JCoContext.end(destination);
    }

Solution

  • Ok, so I got this up and going and thought I'd share my research. You need to add your own destination in Portal. To achieve that you need to go to NetWeaver Administrator, located at: host:port/nwa. So it'll be something like sapportal.your.domain.com:50000/nwa.

    Then you go to Configuration-> Infrastructure-> Destinations and add your destination there. You can leave empty most of the fields like Message Server. The important part is Destination name as it is how you will retrieve it and destination type which should be set to RFC Destination in my case. Try pinging your newly created destination to check if its up and going.

    Finally you should be able to get destination by simply calling: JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME); as it is added to your Portal environment and managed from there.