Spring 第一个解决方案的CustomDestinationDataProvider.java代码: 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; } }
同样,此解决方案仅在一个应用程序中有效。如果将此代码直接实现到多个应用程序中,则调用此代码的第一个应用程序将获得资源,而另一个应用程序将出错 我提出的第二个解决方案允许多个java应用程序同时使用CustomDestinationDataProvider类。我将CustomDestinationDataProvider类从应用程序代码中分离出来,并为其创建了一个单独的JavaSpring应用程序(不是web应用程序),目的是创建一个jar。然后,我将MyDestinationDataProvider内部类转换为单例。以下是CustomDestinationDataProvider的单例版本的代码:Spring 第一个解决方案的CustomDestinationDataProvider.java代码: 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; } },spring,spring-mvc,tomcat,jco,sap-r3,Spring,Spring Mvc,Tomcat,Jco,Sap R3,同样,此解决方案仅在一个应用程序中有效。如果将此代码直接实现到多个应用程序中,则调用此代码的第一个应用程序将获得资源,而另一个应用程序将出错 我提出的第二个解决方案允许多个java应用程序同时使用CustomDestinationDataProvider类。我将CustomDestinationDataProvider类从应用程序代码中分离出来,并为其创建了一个单独的JavaSpring应用程序(不是web应用程序),目的是创建一个jar。然后,我将MyDestinationDataProvid
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;
}
}
我还将这个jar文件的副本放在工作站上的“C:\Users\userid\Documents\jars”文件夹中,以便测试应用程序代码可以看到jar中的代码并进行编译。然后,我在两个测试应用程序的pom.xml文件中引用了jar文件的这个副本:
<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>
第二个测试应用程序中调用JCOConnector.jar共享库的代码如下所示:
shared.loader=\
${catalina.home}/shared/lib\*.jar,${catalina.home}/shared/lib
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();
}
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();
}
我知道我省略了在工作站和服务器上安装SAP JCO 3库的许多步骤。我确实希望这能帮助至少另一个人克服困难,让多个spring mvc java应用程序在同一台服务器上与SAP对话。如果我理解正确,你是说我应该在我的第一个应用程序中重用CustomDestinationProvider实现。我会从我的第二个应用程序调用它,对destName使用不同的值(而不是“ABAP_AS”)。这是正确的吗?我在第二个应用程序中的代码是什么样子的?我想我对它的工作原理有点困惑。
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();
}
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();
}