Implementing different destinations in applications on the Tomcat server

匆匆过客 提交于 2021-01-28 05:09:24

问题


Earlier this year I developed an implementation of the SAP JCO CustomDestinationProvider for one of my Spring MVC tomcat applications. In my application, I use this implementation to call a BAPI in my SAP R/3 system to retrieve data.

I am now working on a second Spring MVC tomcat application that I want to have call a BAPI in my SAP R/3 system to retrieve data. It will be a different BAPI that I will be calling, thus it will be different data that I will be retrieving. Since this is a different application calling a different BAPI, I want to use a different SAP system user in my configurations. This new application will be running on the same physical tomcat server as the first application.

My question is should I develop another implementation of the SAP JCO CustomDestinationProvider for this new application or should I somehow reuse the first implementation? If the answer is that I should develop another implementation for this new application, I would expect then that I would develop another implementation for each new Spring MVC tomcat application that I develop that needs to talk to SAP. Is this correct thinking?

If I do a different implementation for this new application of mine, should I be using the same destination name in the code, or should I use a different name? Below is the code for my first implementation of CustomDestinationDataProvider:

public class CustomDestinationDataProvider {
public class MyDestinationDataProvider implements DestinationDataProvider {
    private DestinationDataEventListener eL;
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();

    public Properties getDestinationProperties(String destinationName) {
        try {
            Properties p = secureDBStorage.get(destinationName);
            if(p!=null) {
                if(p.isEmpty())
                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
                return p;
            }
            return null;
        } catch(RuntimeException re) {
            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
        }
    }
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
        this.eL = eventListener;
    }
    public boolean supportsEvents() {
        return true;
    }
    public void changeProperties(String destName, Properties properties) {
        synchronized(secureDBStorage) {
            if(properties==null) {
                if(secureDBStorage.remove(destName)!=null)
                    eL.deleted(destName);
            } else {
                secureDBStorage.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }
}

public ArrayList<String> executeSAPCall(Properties connectProperties, ArrayList<String> partnumbers) throws Exception {
    String destName = "ABAP_AS";
    SAPDAO sapDAO = new SAPDAO(); 
    ArrayList<MaterialBean> searchResults = new ArrayList<MaterialBean>();
    MyDestinationDataProvider myProvider = new MyDestinationDataProvider();
    boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
    JCoDestination dest;
    try {
        if (!destinationDataProviderRegistered) {
            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
            myProvider.changeProperties(destName, connectProperties);
        }
    } catch(IllegalStateException providerAlreadyRegisteredException) {
        logger.error("executeSAPCall: providerAlreadyRegisteredException!");
    }
    try {
        dest = JCoDestinationManager.getDestination(destName);
        searchResults = sapDAO.searchSAP(dest, partnumbers);
    } catch(JCoException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return searchResults;
}  
}

If the answer is that I should not need to implement another CustomDestinationDataProvider for my second application, what other considerations do I need to keep in mind?


回答1:


You can only register one DestinationDataProvider so the one you set must be able to handle both (or more) different connections. In order to do this, you need unique names for each connection, i.e. destName can't be the fixed value ABAP_AS, you need to create one for each connection.

Your current implementation of the provider looks good for me, but your method when calling the RFC is mixing the creation of the connection and the actual RFC-calling too much in my eyes. IMHO you should separate the former into its own method, so you can call it from other parts of your application to e.g. do other things than RFC-calling.




回答2:


I've figured it out! I discovered two different ways to implement CustomDestinationDataProvider so that I could use multiple destinations.

Something that I did that helped out with both of my different solutions was change out the method in CustomDestinationDataProvider that instantiates the MyDestinationDataProvider inner class so that instead of returning ArrayList, it returns JCoDestination. I changed the name of this method from executeSAPCall to getDestination.

The first way that I discovered that allowed me to use multiple destinations, successfully changing out destinations, was to introduce a class variable for MyDestinationDataProvider so that I could keep my instantiated version. Please note that for this solution, the CustomDestinationDataProvider class is still embedded within my java application code.

I found that this solution only worked for one application. I was not able to use this mechanism in multiple applications on the same tomcat server, but at least I was finally able to successfully switch destinations. Here is the code for CustomDestinationDataProvider.java for this first solution:

public class CustomDestinationDataProvider {

private MyDestinationDataProvider gProvider;    // class version of MyDestinationDataProvider

public class MyDestinationDataProvider implements DestinationDataProvider {
    private DestinationDataEventListener eL;
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
    public Properties getDestinationProperties(String destinationName) {
        try {
            Properties p = secureDBStorage.get(destinationName);
            if(p!=null) {
                if(p.isEmpty())
                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
                return p;
            }

            return null;
        } catch(RuntimeException re) {
            System.out.println("getDestinationProperties: Exception detected!!! message = " + re.getMessage());
            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
        }
    }
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
        this.eL = eventListener;
    }
    public boolean supportsEvents() {
        return true;
    }
    public void changeProperties(String destName, Properties properties) {
        synchronized(secureDBStorage) {
            if(properties==null) {
                if(secureDBStorage.remove(destName)!=null) {
                    eL.deleted(destName);
                }
            } else {
                secureDBStorage.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }
}
public JCoDestination getDestination(String destName, Properties connectProperties) {
    MyDestinationDataProvider myProvider = new MyDestinationDataProvider();
    boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
    if (!destinationDataProviderRegistered) {
        try {
            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
            gProvider = myProvider; // save our destination data provider in the class var
        } catch(IllegalStateException providerAlreadyRegisteredException) {
            throw new Error(providerAlreadyRegisteredException);
        }
    } else {
        myProvider = gProvider; // get the destination data provider from the class var.
    }
    myProvider.changeProperties(destName, connectProperties);
    JCoDestination dest = null;
    try {
        dest = JCoDestinationManager.getDestination(destName);
    } catch(JCoException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    return dest;
}
}

This is the code in my servlet class that I use to instantiate and call CustomDestinationDataProvider within my application code:

    CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
    SAPDAO sapDAO = new SAPDAO();

    Properties p1 = getProperties("SAPSystem01");
    Properties p2 = getProperties("SAPSystem02");
    try {
        JCoDestination dest = cddp.getDestination("SAP_R3_USERID_01", p1);  // establish the first destination
        sapDAO.searchEmployees(dest, searchCriteria);   // call the first BAPI
        dest = cddp.getDestination("SAP_R3_USERID_02", p2); // establish the second destination
        sapDAO.searchAvailability(dest);    // call the second BAPI
    } catch (Exception e) {
        e.printStackTrace();
    }

Again, this solution only works within one application. If you implement this code directly into more than one application, the first app that calls this code gets the resource and the other one will error out.

The second solution that I came up with allows multiple java applications to use the CustomDestinationDataProvider class at the same time. I broke the CustomDestinationDataProvider class out of my application code and created a separate java spring application for it (not a web application) for the purpose of creating a jar. I then transformed the MyDestinationDataProvider inner class into a singleton. Here's the code for the singleton version of CustomDestinationDataProvider:

public class CustomDestinationDataProvider {
public static class MyDestinationDataProvider implements DestinationDataProvider {

////////////////////////////////////////////////////////////////////
    // The following lines convert MyDestinationDataProvider into a singleton. Notice 
    // that the MyDestinationDataProvider class has now been declared as static.
    private static MyDestinationDataProvider myDestinationDataProvider = null;
    private MyDestinationDataProvider() {
    }
    public static MyDestinationDataProvider getInstance() {
        if (myDestinationDataProvider == null) {
            myDestinationDataProvider = new MyDestinationDataProvider();
        }
        return myDestinationDataProvider;
    }
    ////////////////////////////////////////////////////////////////////

    private DestinationDataEventListener eL;
    private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
    public Properties getDestinationProperties(String destinationName) {
        try {
            Properties p = secureDBStorage.get(destinationName);
            if(p!=null) {
                if(p.isEmpty())
                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
                return p;
            }
            return null;
        } catch(RuntimeException re) {
            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
        }
    }
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
        this.eL = eventListener;
    }
    public boolean supportsEvents() {
        return true;
    }
    public void changeProperties(String destName, Properties properties) {
        synchronized(secureDBStorage) {
            if(properties==null) {
                if(secureDBStorage.remove(destName)!=null) {
                    eL.deleted(destName);
                }
            } else {
                secureDBStorage.put(destName, properties);
                eL.updated(destName); // create or updated
            }
        }
    }
}
public JCoDestination getDestination(String destName, Properties connectProperties) throws Exception {
    MyDestinationDataProvider myProvider = MyDestinationDataProvider.getInstance();
    boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
    if (!destinationDataProviderRegistered) {
        try {
            com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
        } catch(IllegalStateException providerAlreadyRegisteredException) {
            throw new Error(providerAlreadyRegisteredException);
        }
    }
    myProvider.changeProperties(destName, connectProperties);
    JCoDestination dest = null;
    try {
        dest = JCoDestinationManager.getDestination(destName);
    } catch(JCoException ex) {
        ex.printStackTrace();
        throw ex;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
    return dest;
}
}

After putting this code into the jar file application and creating the jar file (I call it JCOConnector.jar), I put the jar file on the shared library classpath of my tomcat server and restarted the tomcat server. In my case, this was /opt/tomcat/shared/lib. Check your /opt/tomcat/conf/catalina.properties file for the shared.loader line for the location of your shared library classpath. Mine looks like this:

shared.loader=\
${catalina.home}/shared/lib\*.jar,${catalina.home}/shared/lib

I also put a copy of this jar file in the "C:\Users\userid\Documents\jars" folder on my workstation so that the test application code could see the code in the jar and compile. I then referenced this copy of the jar file in my pom.xml file in both of my test applications:

    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>jcoconnector</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>C:\Users\userid\Documents\jars\JCOConnector.jar</systemPath>
    </dependency>

After adding this to the pom.xml file, I right clicked on each project, selected Maven -> Update Project..., and I then right clicked again on each project and selected 'Refresh'. Something very important that I learned was to not add a copy of JCOConnector.jar directly to either of my test projects. The reason for this is because I want the code from the jar file in /opt/tomcat/shared/lib/JCOConnector.jar to be used. I then built and deployed each of my test apps to the tomcat server.

The code that calls my JCOConnector.jar shared library in my first test application looks like this:

    CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
    JCoDestination dest = null;
    SAPDAO sapDAO = new SAPDAO();

    Properties p1 = getProperties("SAPSystem01");
    try {
        dest = cddp.getDestination("SAP_R3_USERID_01", p1);
        sapDAO.searchEmployees(dest);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

The code in my second test application that calls my JCOConnector.jar shared library looks like this:

    CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
    JCoDestination dest = null;
    SAPDAO sapDAO = new SAPDAO();

    Properties p2 = getProperties("SAPSystem02");
    try {
        dest = cddp.getDestination("SAP_R3_USERID_02", p2);
        sapDAO.searchAvailability(dest);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

I know that I've left out a lot of the steps involved in first getting the SAP JCO 3 library installed on your workstation and server. I do hope that this helps out at least one other person of getting over the hill of trying to get multiple spring mvc java spplications talking to SAP on the same server.



来源:https://stackoverflow.com/questions/46280376/implementing-different-destinations-in-applications-on-the-tomcat-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!