0

I want to access an RFC function on sap from my java program.

When I call getDestination("connect") I can connect and run my function without problems.

But I will have more than one config file because I will be connecting to different SAP servers.

I want to collect all of these files in one folder, but when I write getDestination("src/JcoDestinations/connect") it does not read my config file that I put in the folder and does not connect.

How do I get all my connect jcoDestination config files together and run?

The error I get is this :

(106) JCO_ERROR_RESOURCE: Destination 'connect' does not exist. –

I tried:

File file = new File("src/JcoDestinations/connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

and:

File file = new File("C:\\connect");
JCoDestination destination = JCoDestinationManager.getDestination(file.toString());

these did not work for me.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • Also the error i get is this : (106) JCO_ERROR_RESOURCE: Destination 'connect' does not exist. – Yiğit Tunç Feb 13 '23 at 14:56
  • Does this answer your question? [Implementing different destinations in applications on the Tomcat server](https://stackoverflow.com/questions/46280376/implementing-different-destinations-in-applications-on-the-tomcat-server) – Suncatcher Aug 20 '23 at 23:53

1 Answers1

2

you can define a Custom DestinationDataProvider to hold more than one Connection Profile. We dont use Property - Files here ,but I think this example should work.

Here a DataProvider to hold different Property Files, stored in a HashMap. The method JCoDestinationManager.getDestination calls the method getDestinationProperties inside the DataProvider and expects to get a Properties Object back. The method addConnectionProperties adds new Properties Objects to the HashMap.

Here you can also implement a method to read one big Property file. But I'm too lazy for that ;)

Here an example

public class SapSystemDestinationDataProvider implements DestinationDataProvider {

private DestinationDataEventListener el;
private final HashMap<String, Properties> connectionProperties = new HashMap<>();


   @Override
   public Properties getDestinationProperties(String destinationName) {
      if (connectionProperties.size() > 0) {
         Properties con = connectionProperties.get(destinationName.toLowerCase().trim());
         if (con != null) {
            return con;
          }
      }
      return null;
   }


   @Override
   public boolean supportsEvents() {
       return true;
   }

    @Override
    public void setDestinationDataEventListener(DestinationDataEventListener dl) {
        this.el = dl;
    }

    public void addConnectionProperties(String destName, Properties properties) {
        connectionProperties.put(destName.toLowerCase().trim(), properties);
    }
}

Then register the provider in your main programm

SapSystemDestinationDataProvider sapSystemProvider = new SapSystemDestinationDataProvider();
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(sapSystemProvider );

Now read all your property files from disk and place the files in the Custom DestinationDataProvider.

InputStream input = new FileInputStream("path/to/connection_1.properties")) {
Properties prop = new Properties();
prop.load(input);
InputStream input2 = new FileInputStream("path/to/connection2.properties")) {
Properties prop2 = new Properties();
prop2.load(input2);

sapSystemProvider.addConnectionProperties("CONNECTION_1",prop);
sapSystemProvider.addConnectionProperties("CONNECTION_2", prop2);

Now you have 2 connections. You can access the connections via JCoDestinationManager.getDestination.

JCoDestination dest1 = JCoDestinationManager.getDestination("CONNECTION_1");
JCoDestination dest2 = JCoDestinationManager.getDestination("CONNECTION_2");

I never tried it, cause i use an external database to build up the Propertie Objects, but i think it should work. Let me know, how it works ;)