All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ibizsys.model.engine.cloud.sysutil.CloudClientPSSysUtilEngineBase Maven / Gradle / Ivy

The newest version!
package net.ibizsys.model.engine.cloud.sysutil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.node.ObjectNode;

import net.ibizsys.model.IPSModelObjectRuntime;
import net.ibizsys.model.PSModelEnums.ServiceType;
import net.ibizsys.model.engine.PSModelEngineException;
import net.ibizsys.model.engine.cloud.service.ICloudPSSubSysServiceAPIEngine;
import net.ibizsys.model.engine.cloud.service.client.ICloudDevOpsClient;
import net.ibizsys.model.engine.cloud.service.client.ICloudOSSClient;
import net.ibizsys.model.engine.cloud.service.client.ICloudPortalClient;
import net.ibizsys.model.engine.service.IPSSubSysServiceAPIEngine;
import net.ibizsys.model.engine.service.client.IWebClient;
import net.ibizsys.model.engine.sysutil.PSSysUtilEngineBase;
import net.ibizsys.model.engine.util.IAction;
import net.ibizsys.model.engine.util.JsonUtils;
import net.ibizsys.model.res.IPSSysUtil;
import net.ibizsys.model.service.IPSSubSysServiceAPI;
import net.ibizsys.model.service.PSSubSysServiceAPIImpl;

public abstract class CloudClientPSSysUtilEngineBase extends PSSysUtilEngineBase implements ICloudClientPSSysUtilEngine {

	private static final org.apache.commons.logging.Log log = LogFactory.getLog(CloudClientPSSysUtilEngineBase.class);

	private IPSSubSysServiceAPIEngine iPSSubSysServiceAPIEngine = null;

	private Map serviceUrlMap = new ConcurrentHashMap();

	private Map> rawServiceClientMap = new ConcurrentHashMap>();
	private Map> serviceClientMap = new ConcurrentHashMap>();

	private ICloudDevOpsClient iCloudDevOpsClient = null;
	private ICloudPortalClient iCloudPortalClient = null;
	private ICloudOSSClient iCloudOSSClient = null;

	@Override
	protected void onInit() throws Exception {

		super.onInit();

	}

	@Override
	protected void prepareSetting() throws Exception {

		// 准备Cloud服务路径
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_CONF, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_CONF);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_WF, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_WF);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_UAA, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_UAA);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_OSS, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_OSS);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_SAAS, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_SAAS);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_OU, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_OU);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_DEVOPS, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_DEVOPS);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_LOG, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_LOG);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_PORTAL, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_PORTAL);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_NOTIFY, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_NOTIFY);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_TASK, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_TASK);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_OPEN, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_OPEN);
		// setServiceUrl(ICloudUtilRuntime.CLOUDSERVICE_AI, "lb://" +
		// ICloudUtilRuntime.CLOUDSERVICEURL_AI);

		// 优先读取系统自身设置
		this.setServiceUrl(this.getPSModelEngineHolder().getParam(this.getConfigFolder() + ".serviceurl", null));
		if (StringUtils.hasLength(this.getServiceUrl())) {
			this.setClientId(this.getPSModelEngineHolder().getParam(this.getConfigFolder() + ".clientid", this.getPSModelObject().getAuthClientId()));
			this.setClientSecret(this.getPSModelEngineHolder().getParam(this.getConfigFolder() + ".clientsecret", this.getPSModelObject().getAuthClientSecret()));
			this.setAuthMode(this.getPSModelEngineHolder().getParam(this.getConfigFolder() + ".authmode", this.getPSModelObject().getAuthMode()));
			this.setAccessTokenUrl(this.getPSModelEngineHolder().getParam(this.getConfigFolder() + ".accesstokenurl", null));
		} else {
			this.setServiceUrl(this.getPSModelEngineHolder().getParam(CLOUDCLIENTUTIL_CONFIGFOLDER + ".serviceurl", null));
			this.setClientId(this.getPSModelEngineHolder().getParam(CLOUDCLIENTUTIL_CONFIGFOLDER + ".clientid", this.getPSModelObject().getAuthClientId()));
			this.setClientSecret(this.getPSModelEngineHolder().getParam(CLOUDCLIENTUTIL_CONFIGFOLDER + ".clientsecret", this.getPSModelObject().getAuthClientSecret()));
			this.setAuthMode(this.getPSModelEngineHolder().getParam(CLOUDCLIENTUTIL_CONFIGFOLDER + ".authmode", this.getPSModelObject().getAuthMode()));
			this.setAccessTokenUrl(this.getPSModelEngineHolder().getParam(CLOUDCLIENTUTIL_CONFIGFOLDER + ".accesstokenurl", null));
		}
	}

	@Override
	protected void onInstall() throws Exception {
		super.onInstall();

		// if (this.getPSSubSysServiceAPIEngine(true) == null) {
		// preparePSSubSysServiceAPIEngine();
		// if (this.getPSSubSysServiceAPIEngine(true) == null) {
		// throw new Exception("未指定Cloud服务客户端对象");
		// }
		// }
	}

	protected void setPSSubSysServiceAPIEngine(IPSSubSysServiceAPIEngine iPSSubSysServiceAPIEngine) {
		this.iPSSubSysServiceAPIEngine = iPSSubSysServiceAPIEngine;
	}

	@Override
	public IPSSubSysServiceAPIEngine getPSSubSysServiceAPIEngine() {
		try {
			return this.getPSSubSysServiceAPIEngine(false);
		} catch (Exception ex) {
			throw new PSModelEngineException(this, ex.getMessage(), ex);
		}
	}

	protected IPSSubSysServiceAPIEngine getPSSubSysServiceAPIEngine(boolean bTryMode) throws Exception {
		if (this.iPSSubSysServiceAPIEngine != null || bTryMode) {
			return this.iPSSubSysServiceAPIEngine;
		}

		this.preparePSSubSysServiceAPIEngine();
		if (this.iPSSubSysServiceAPIEngine != null) {
			return this.iPSSubSysServiceAPIEngine;
		}

		throw new Exception("未指定Cloud服务客户端对象");
	}

	protected synchronized void preparePSSubSysServiceAPIEngine() throws Exception {
		if (this.getPSSubSysServiceAPIEngine(true) != null) {
			return;
		}
		this.executeAction("准备Cloud服务客户端", new IAction() {
			@Override
			public Object execute(Object[] args) throws Throwable {
				onPreparePSSubSysServiceAPIEngine();
				return true;
			}
		}, null, Boolean.class);
	}

	protected void onPreparePSSubSysServiceAPIEngine() throws Throwable {
		// 判断系统是否已经定义对应的接口
		java.util.List psSubSysServiceAPIs = this.getPSModelEngineHolder().getPSSystemService().getPSSystem().getAllPSSubSysServiceAPIs();
		IPSSubSysServiceAPI iPSSubSysServiceAPI = null;
		if (psSubSysServiceAPIs != null) {
			for (IPSSubSysServiceAPI item : psSubSysServiceAPIs) {
				if (ICloudPSSubSysServiceAPIEngine.APITAG_CLOUDCLIENT.equalsIgnoreCase(item.getAPITag())) {
					iPSSubSysServiceAPI = item;
					break;
				}
			}
			if (iPSSubSysServiceAPI == null) {
				for (IPSSubSysServiceAPI item : psSubSysServiceAPIs) {
					if (ICloudPSSubSysServiceAPIEngine.APITAG_CLOUDCLIENT.equalsIgnoreCase(item.getCodeName())) {
						iPSSubSysServiceAPI = item;
						break;
					}
				}
			}
		}
		if (iPSSubSysServiceAPI == null) {

			if (!StringUtils.hasLength(this.getServiceUrl())) {
				throw new Exception(String.format("未定义Cloud服务路径"));
			}

			String strFullModelPath = "/net/ibizsys/model/engine/cloud/sysmodel/PSSUBSYSSERVICEAPIS/CloudClient.json";
			ObjectNode objectNode = (ObjectNode) JsonUtils.getMapper().readTree(this.getClass().getResourceAsStream(strFullModelPath));

			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETSERVICEPATH, this.getServiceUrl());
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETAUTHCLIENTID, this.getClientId());
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETAUTHCLIENTSECRET, this.getClientSecret());
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETAUTHMODE, this.getAuthMode());
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETAUTHACCESSTOKENURL, this.getAccessTokenUrl());
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETAPITAG, ICloudPSSubSysServiceAPIEngine.APITAG_CLOUDCLIENT);
			objectNode.put(PSSubSysServiceAPIImpl.ATTR_GETSERVICETYPE, ServiceType.IBIZCLOUD.value);

			iPSSubSysServiceAPI = this.getPSModelEngineHolder().getPSSystemService().createAndInitPSModelObject((IPSModelObjectRuntime) this.getPSModelEngineHolder().getPSSystemService().getPSSystem(), IPSSubSysServiceAPI.class, objectNode);

			this.setPSSubSysServiceAPIEngine(this.getPSModelEngineHolder().getPSModelEngine(iPSSubSysServiceAPI, IPSSubSysServiceAPIEngine.class));
		} else {
			this.setPSSubSysServiceAPIEngine(this.getPSModelEngineHolder().getPSModelEngine(iPSSubSysServiceAPI, IPSSubSysServiceAPIEngine.class));
		}
	}

	@Override
	public IWebClient getRawServiceClient(String serviceType) {
		IWebClient iWebClient = rawServiceClientMap.get(serviceType);
		if (iWebClient == null) {
			String serviceUrl = this.getServiceUrl(serviceType);
			iWebClient = this.executeAction("准备直接服务客户端", new IAction() {
				@Override
				public IWebClient execute(Object[] args) throws Throwable {
					return onGetRawServiceClient(serviceType, serviceUrl);
				}
			}, null, IWebClient.class);
			rawServiceClientMap.put(serviceType, iWebClient);
		}
		return iWebClient;
	}

	protected IWebClient onGetRawServiceClient(String serviceType, String serviceUrl) throws Throwable {
		IWebClient iWebClient = this.getPSModelEngineHolder().getPSModelEngineAddin(this, serviceUrl, IWebClient.class);
		return iWebClient;
	}

	@Override
	public IWebClient getServiceClient(String serviceType) {
		IWebClient iWebClient = serviceClientMap.get(serviceType);
		if (iWebClient == null) {
			String serviceUrl = this.getServiceUrl(serviceType);
			iWebClient = this.executeAction("准备服务客户端", new IAction() {
				@Override
				public IWebClient execute(Object[] args) throws Throwable {
					return onGetServiceClient(serviceType, serviceUrl);
				}
			}, IWebClient.class);
			serviceClientMap.put(serviceType, iWebClient);
			// 要求认证
			this.getPSSubSysServiceAPIEngine().requestTokenIf(true);
		}
		return iWebClient;
	}

	protected IWebClient onGetServiceClient(String serviceType, String serviceUrl) throws Throwable {
		IWebClient iWebClient = this.getPSModelEngineHolder().getPSModelEngineAddin(this.getPSSubSysServiceAPIEngine(), serviceUrl, IWebClient.class);
		return iWebClient;
	}

	@Override
	public  T getServiceClient(String serviceType, Class cls) {
		IWebClient iWebClient = this.getServiceClient(serviceType);
		return iWebClient.getProxyClient(cls);
	}

	public String getServiceUrl(String serviceType) {
		String serviceUrl = serviceUrlMap.get(serviceType);
		if (StringUtils.hasLength(serviceUrl)) {
			return serviceUrl;
		}
		if ((serviceType.indexOf("http://") == 0) || (serviceType.indexOf("https://") == 0) || (serviceType.indexOf("lb://") == 0)) {
			return serviceType;
		}
		log.warn(String.format("未定义Cloud服务[%1$s]路径,使用默认路径", serviceType));
		return this.getServiceUrl();
	}

	protected void setServiceUrl(String serviceType, String serviceUrl) {
		serviceUrlMap.put(serviceType, serviceUrl);
	}

	@Override
	public ICloudDevOpsClient getCloudDevOpsClient() {
		getPSSubSysServiceAPIEngine().requestTokenIf(true);
		try {
			return this.getCloudDevOpsClient(false);
		} catch (Exception ex) {
			throw new PSModelEngineException(this, ex.getMessage(), ex);
		}
	}
	
	protected void setCloudDevOpsClient(ICloudDevOpsClient iCloudDevOpsClient) {
		this.iCloudDevOpsClient = iCloudDevOpsClient;
	}
	
	protected ICloudDevOpsClient getCloudDevOpsClient(boolean bTryMode) throws Exception {
		if (this.iCloudDevOpsClient != null || bTryMode) {
			return this.iCloudDevOpsClient;
		}

		this.prepareCloudDevOpsClient();
		if (this.iCloudDevOpsClient != null) {
			return this.iCloudDevOpsClient;
		}

		throw new Exception("未指定CloudDevOps客户端对象");
	}
	
	protected void prepareCloudDevOpsClient() throws Exception{
		ICloudDevOpsClient iCloudDevOpsClient = this.getPSModelEngineHolder().getPSModelEngineAddin(this, this.getPSSubSysServiceAPIEngine().getWebClient(), ICloudDevOpsClient.class);
		this.setCloudDevOpsClient(iCloudDevOpsClient);
	}
	

	@Override
	public ICloudOSSClient getCloudOSSClient() {
		getPSSubSysServiceAPIEngine().requestTokenIf(true);
		try {
			return this.getCloudOSSClient(false);
		} catch (Exception ex) {
			throw new PSModelEngineException(this, ex.getMessage(), ex);
		}
	}
	
	protected void setCloudOSSClient(ICloudOSSClient iCloudOSSClient) {
		this.iCloudOSSClient = iCloudOSSClient;
	}
	
	protected ICloudOSSClient getCloudOSSClient(boolean bTryMode) throws Exception {
		if (this.iCloudOSSClient != null || bTryMode) {
			return this.iCloudOSSClient;
		}

		this.prepareCloudOSSClient();
		if (this.iCloudOSSClient != null) {
			return this.iCloudOSSClient;
		}

		throw new Exception("未指定CloudOSS客户端对象");
	}
	
	protected void prepareCloudOSSClient() throws Exception{
		ICloudOSSClient iCloudOSSClient = this.getPSModelEngineHolder().getPSModelEngineAddin(this, this.getPSSubSysServiceAPIEngine().getWebClient(), ICloudOSSClient.class);
		this.setCloudOSSClient(iCloudOSSClient);
	}
	
	
	
	@Override
	public ICloudPortalClient getCloudPortalClient() {
		getPSSubSysServiceAPIEngine().requestTokenIf(true);
		try {
			return this.getCloudPortalClient(false);
		} catch (Exception ex) {
			throw new PSModelEngineException(this, ex.getMessage(), ex);
		}
	}
	
	protected void setCloudPortalClient(ICloudPortalClient iCloudPortalClient) {
		this.iCloudPortalClient = iCloudPortalClient;
	}
	
	protected ICloudPortalClient getCloudPortalClient(boolean bTryMode) throws Exception {
		if (this.iCloudPortalClient != null || bTryMode) {
			return this.iCloudPortalClient;
		}

		this.prepareCloudPortalClient();
		if (this.iCloudPortalClient != null) {
			return this.iCloudPortalClient;
		}

		throw new Exception("未指定CloudPortal客户端对象");
	}
	
	protected void prepareCloudPortalClient() throws Exception{
		ICloudPortalClient iCloudPortalClient = this.getPSModelEngineHolder().getPSModelEngineAddin(this, this.getPSSubSysServiceAPIEngine().getWebClient(), ICloudPortalClient.class);
		this.setCloudPortalClient(iCloudPortalClient);
	}
	
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy