⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bclogin.java

📁 与SaaS软件提供的WebService的接口进行连通
💻 JAVA
字号:
package jgd.ebridgechina.bizComp;

import javax.xml.rpc.ServiceException;
import com.sforce.soap.enterprise.*;
import com.sforce.soap.enterprise.fault.ExceptionCode;
import com.sforce.soap.enterprise.fault.LoginFault;

/**
 * The bcLogin model is used to get access to SF easily.
 * @author guowei
 *
 */
public class bcLogin {
	private String username;
	private String password;
	private SoapBindingStub binding;
	
	/**
	 * You cannot get access to SF unless userName and passWord is submitted.
	 *
	 */
	public bcLogin(){
		if(bcConstant.isDebug){
			System.out.println("Sorry,you should give the User Name and Password " +
				"to access Salesforce.com!");
		}
	}
	
	/**
	 * Get access to SF by username and password
	 * @param userName
	 * @param passWord
	 */
	public bcLogin(String userName,String passWord){
		this.username = userName;
		this.password = passWord;		
	}

	/**
	 * Exit and disable binding.
	 *
	 */
	public void bvExit(){
		this.username = null;
		this.password = null;
		binding.clearAttachments();
		binding.clearHeaders();
		if(bcConstant.isDebug){
			System.out.println(binding.getHeaders().toString());
		}
		binding.setMaintainSession(false);
	}
	
	/**
	 * User can only call this function to login. The username
	 * and password is passed during bcLogin initalization process.
	 * See above.
	 * 
	 * @return boolean
	 * @throws ServiceException
	 */
	public boolean login(bcBindingStub tmpBinding)throws ServiceException{
        tmpBinding.sbs = (SoapBindingStub)new SforceServiceLocator().getSoap();
		return bcloginProcess(tmpBinding);
	}
	
	/**
	 * The login process model which is called by login model to 
	 * access to SF.
	 * 
	 * @param tmpBinding binding element for only one transmission process
	 * @return
	 * @throws ServiceException
	 */
	private boolean bcloginProcess(bcBindingStub tmpBinding)
	throws ServiceException{
		binding = tmpBinding.sbs;
		binding.setTimeout(bcConstant.timeOut);
		LoginResult loginResult;
		try{
			if(bcConstant.isDebug){
				System.out.println("LOGGING IN NOW...");
			}
			loginResult = binding.login(this.username,this.password);
		}
		catch(LoginFault ex){
			if(bcConstant.isDebug){
				System.out.println("Sorry, you cannot login!");
			}
			ExceptionCode exCode = ex.getExceptionCode();
			if (exCode == ExceptionCode.FUNCTIONALITY_NOT_ENABLED ||
				exCode == ExceptionCode.INVALID_CLIENT ||
				exCode == ExceptionCode.INVALID_LOGIN ||
				exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_DOMAIN ||
			    exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_TIME ||
			    exCode == ExceptionCode.ORG_LOCKED ||
			    exCode == ExceptionCode.PASSWORD_LOCKOUT ||
			    exCode == ExceptionCode.SERVER_UNAVAILABLE ||
			    exCode == ExceptionCode.TRIAL_EXPIRED ||
			    exCode == ExceptionCode.UNSUPPORTED_CLIENT) {
				if(bcConstant.isDebug){
					System.out.println("Please be sure that you have a valid username and password.");
				}
		}else{
			if(bcConstant.isDebug){
				System.out.println(ex.getExceptionCode());
				System.out.println("An unexpected error has occurred." + ex.getMessage());
			}
			}
			return false;
		} catch (Exception ex) {
			if(bcConstant.isDebug){
				System.out.println("An unexpected error has occurred: " + ex.getMessage());
			}
			    ex.printStackTrace();
			    return false;
			}
		if(loginResult.isPasswordExpired()){
			if(bcConstant.isDebug){
			    System.out.println("An error has occurred. Your password has expired.");
			}
				return false;
		}
		
		binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY,
				loginResult.getServerUrl());
		
		SessionHeader sh = new SessionHeader();
		sh.setSessionId(loginResult.getSessionId());
		binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),
				"SessionHeader",sh);
		
		if(bcConstant.isDebug){
			System.out.println("LOGGING IN SUCCESSFULLY!");
		}
		
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -