JCO Demo

2 篇文章 0 订阅

 来源于 JCO 包下的示例调整测试

sapjco3.dll 进windows目录下,sapjco3.jar导入,JCO有负载均衡参数,NCO好像没有。

package com.demo.jco;
import java.util.HashMap;
import java.util.Properties;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DataProviderException;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;

/**
 * Each application using Java Connector 3 deals with destinations. A
 * destination represents a logical address of an ABAP system and thus separates
 * the destination configuration from application logic. JCo retrieves the
 * destination parameters required at runtime from DestinationDataProvider and
 * ServerDataProvider registered in the runtime environment. If no provider is
 * registered, JCo uses the default implementation that reads the configuration
 * from a properties file. It is expected that each environment provides a
 * suitable implementation that meets security and other requirements.
 * Furthermore, only one DestinationDataProvider and only one ServerDataProvider
 * can be registered per process. The reason behind this design decision is the
 * following: the provider implementations are part of the environment
 * infrastructure. The implementation should not be application specific, and in
 * particular must be separated from application logic.
 * 
 * This example demonstrates a simple implementation of the
 * DestinationDataProvider interface and shows how to register it. A real world
 * implementation should save the configuration data in a secure way.
 */

public class JCOProvider {
	// The custom destination data provider implements DestinationDataProvider and
	// provides an implementation for at least getDestinationProperties(String).
	// Whenever possible the implementation should support events and notify the JCo
	// runtime
	// if a destination is being created, changed, or deleted. Otherwise JCo runtime
	// will check regularly if a cached destination configuration is still valid
	// which incurs
	// a performance penalty.
	static class MyDestinationDataProvider implements DestinationDataProvider {
		private DestinationDataEventListener eL;
		private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();

		// The custom destination data provider implements DestinationDataProvider and
		// provides an implementation for at least getDestinationProperties(String).
		// Whenever possible the implementation should support events and notify the JCo
		// runtime
		// if a destination is being created, changed, or deleted. Otherwise JCo runtime
		// will check regularly if a cached destination configuration is still valid
		// which incurs
		// a performance penalty.
		@Override
		public Properties getDestinationProperties(String destinationName) {
			try {
				// read the destination from DB
				Properties p = secureDBStorage.get(destinationName);

				if (p != null) {
					// check if all is correct, for example
					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);
			}
		}

		// An implementation supporting events has to retain the eventListener instance
		// provided
		// by the JCo runtime. This listener instance shall be used to notify the JCo
		// runtime
		// about all changes in destination configurations.
		@Override
		public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
			this.eL = eventListener;

		}

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

		// implementation that saves the properties in a very secure way
		public void changePropertiesForABAP_AS(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
				}
			}
		}

		/**
		 * Test Destination
		 * 
		 * @param destName
		 */
		void executeCalls(String destName) {
			JCoDestination dest;
			try {
				dest = JCoDestinationManager.getDestination(destName);
				dest.ping();
				System.out.println("Destination " + destName + " works");
			} catch (JCoException e) {
				e.printStackTrace();
				System.out.println("Execution on destination " + destName + " failed");
			}
		}

		/**
		 * @param clnt
		 * @param user
		 * @param pwd
		 * @return
		 */
		static Properties getDestinationPropertiesFromUI(String dest, String clnt, String user, String pwd) {
			Properties connectProperties = new Properties();
			String ip = "";

			// A - ABAP system with direct application server host login
			connectProperties.setProperty(DestinationDataProvider.JCO_TYPE, "A");
			switch (dest) {
			case "DEV":
				ip = "IP或域名";
				break;
			case "QAS":
				ip = "IP或域名";
				break;
			case "PRD":
				// B - ABAP system with message server driven load balanced login
				connectProperties.setProperty(DestinationDataProvider.JCO_TYPE, "B");
				// SAP message server host, mandatory for a login load balanced connection.
				connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, "负载均衡服务器IP或域名");
				connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, "PUBLIC");
				break;
			default:
				return null;
			}
			connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, ip);
			connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
			connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, dest);
			connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, clnt);
			connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH");

			//SNC - Single Sign On
			if (user.lastIndexOf("@") > 0) {
				connectProperties.setProperty(DestinationDataProvider.JCO_SNC_MODE, "1");
				connectProperties.setProperty(DestinationDataProvider.JCO_SNC_MYNAME, "p:CN="+user); 
				//System.getenv("USERNAME")@System.getenv("USERDOMAIN")
				connectProperties.setProperty(DestinationDataProvider.JCO_SNC_PARTNERNAME, "p:CN=SNC名称");
				//connectProperties.setProperty(JCO_R3NAME, dest);
				//connectProperties.setProperty(DestinationDataProvider.JCO_USE_SAPGUI,"1");
			} else {
				connectProperties.setProperty(DestinationDataProvider.JCO_USER, user);
				connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, pwd);
			}
			return connectProperties;
		}
	}

	/**
	 * JAR With Parameters java -Ddest=LSD -Duser=LL125202 -Dpwd=123456 -Dclnt=300
	 * -jar JCO.jar
	 * 
	 * Java Console java -jar JCO.jar
	 **/
	public static void main(String args[]) {
		MyDestinationDataProvider provider = new MyDestinationDataProvider();

		try {
			Environment.registerDestinationDataProvider(provider);
		} catch (IllegalStateException providerAlreadyRegisteredException) {
			// somebody else registered its implementation,
			// stop the execution
			throw new Error(providerAlreadyRegisteredException);
		}

		String destName = "ABAP_AS";
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String user = System.getProperty("user");
		String pwd = System.getProperty("pwd");
		String clnt = System.getProperty("clnt");
		String dest = System.getProperty("dest");

		// UserName & Password
		if (user == "" | user == null) {
			try {
				System.out.println("Input System ID for DEV/QAS/PRD");
				dest = br.readLine().trim().toUpperCase();
				System.out.println("Input Client");
				clnt = br.readLine().trim();
				System.out.println("Input UserName");
				user = br.readLine().trim().toUpperCase();
				Console con = System.console();
				if (con != null) {
					pwd = new String(con.readPassword("[%s]", "Input Password:"));
				} else {
					System.out.println("Input PassWord");
					pwd = br.readLine().trim();
				}

			} catch (IOException ex) {
				System.out.println("Error:\n" + ex.getMessage());
			}
		}

		// set properties for the destination and ...
		provider.changePropertiesForABAP_AS(destName,
				MyDestinationDataProvider.getDestinationPropertiesFromUI(dest, clnt, user, pwd));
		// ... work with it
		provider.executeCalls(destName);

		// now remove the properties and ...
		provider.changePropertiesForABAP_AS(destName, null);

	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值