webendservicelocator.java

来自「《j2ee开发全程实录》随书源码」· Java 代码 · 共 62 行

JAVA
62
字号
package com.cownew.PIS.framework.web.helper;

import java.net.MalformedURLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import com.cownew.PIS.framework.client.RemoteServerException;
import com.cownew.PIS.framework.common.SysConstants;
import com.cownew.ctk.common.StringUtils;

public class WebEndServiceLocator
{
	public static Object getService(HttpServletRequest request,
			Class serviceIntfClass)
	{
		HttpSession webSession = request.getSession();
		String sessionId = (String) webSession
				.getAttribute(WebConstant.SESSIONID);
		return getService(sessionId, serviceIntfClass);
	}

	public static Object getService(String sessionId,
			Class serviceIntfClass)
	{
		WebConfig webConfig = WebConfig.getInstance();
		HttpInvokerProxyFactoryBean proxyFactory = new HttpInvokerProxyFactoryBean();
		CommonsHttpInvokerRequestExecutor reqExecutor = new CommonsHttpInvokerRequestExecutor();
		proxyFactory.setHttpInvokerRequestExecutor(reqExecutor);
		try
		{
			String serviceIntfName = serviceIntfClass.getName();
			String serviceURL = webConfig.getServerURL().toString()
					+ serviceIntfName;
			if (!StringUtils.isEmpty(sessionId))
			{
				StringBuffer sb = new StringBuffer();
				sb.append(serviceURL).append("?")
						.append(SysConstants.SESSIONID).append("=").append(
								sessionId);
				proxyFactory.setServiceUrl(sb.toString());
			} else
			{
				proxyFactory.setServiceUrl(serviceURL);
			}

			proxyFactory.setServiceInterface(serviceIntfClass);
			proxyFactory.afterPropertiesSet();
			Object service = proxyFactory.getObject();
			return service;
		} catch (MalformedURLException e)
		{
			throw new RemoteServerException(
					RemoteServerException.CONNECTSERVERERROR, e);
		}
	}

}

⌨️ 快捷键说明

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