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

📄 serviceinvoker.java

📁 单点登陆
💻 JAVA
字号:
package net.s3o.core.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.HessianOutput;
import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.server.HessianSkeleton;

public class ServiceInvoker implements InitializingBean {

	private String serviceInterfaceName;

	private Object serviceImpl;

	private String objectInterfaceName;

	private Object objectImpl;

	private Class _serviceInterface;

	private Class _objectInterface;

	private HessianSkeleton _homeSkeleton;

	private HessianSkeleton _objectSkeleton;

	private SerializerFactory _serializerFactory;

	public SerializerFactory getSerializerFactory() {
		if (_serializerFactory == null)
			_serializerFactory = new SerializerFactory();

		return _serializerFactory;
	}

	public void setSerializerFactory(SerializerFactory factory) {
		_serializerFactory = factory;
	}

	public void setSendCollectionType(boolean sendType) {
		getSerializerFactory().setSendCollectionType(sendType);
	}

	public void afterPropertiesSet() throws Exception {
		
		Assert.notNull(serviceInterfaceName, "必须设置 属性 serviceInterfaceName ");
		Assert.notNull(serviceImpl, "必须设置 属性 serviceImpl ");
		init();

	}

	private void init() throws Exception {

		try {


			_serviceInterface = loadClass(serviceInterfaceName);

			if (_serviceInterface == null) {
				_serviceInterface = topClassAPI(serviceImpl.getClass());
			}

			
			if (objectInterfaceName!=null){
				 _objectInterface = loadClass(objectInterfaceName);
				 if (_objectInterface==null && objectImpl!=null){
					 _objectInterface= topClassAPI(objectImpl.getClass());
				 }
			}
			
			_homeSkeleton = new HessianSkeleton(serviceImpl, _serviceInterface);
			if (_objectInterface != null)
				_homeSkeleton.setObjectClass(_objectInterface);

			if (objectImpl != null) {
				_objectSkeleton = new HessianSkeleton(objectImpl,
						_objectInterface);
				_objectSkeleton.setHomeClass(_serviceInterface);
			} else {
				_objectSkeleton = _homeSkeleton;
			}

		} catch (Exception e) {
			throw e;
		}
	}

	public void invok(InputStream is, OutputStream os, String objectId)
			throws Throwable {

		Hessian2Input in = new Hessian2Input(is);
		AbstractHessianOutput out;

		SerializerFactory serializerFactory = getSerializerFactory();

		in.setSerializerFactory(serializerFactory);

		int code = in.read();

		if (code != 'c') {
			// XXX: deflate
			throw new IOException("expected 'c' in hessian input at " + code);
		}

		int major = in.read();
		int minor = in.read();

		if (major >= 2 && (true || minor<0) )
			out = new Hessian2Output(os);
		else
			out = new HessianOutput(os);

		out.setSerializerFactory(serializerFactory);

		if (objectId != null)
			_objectSkeleton.invoke(in, out);
		else
			_homeSkeleton.invoke(in, out);

		out.close();
	}

	
	  public static Class topClassAPI(Class implClass) {
		if (implClass == null) {
			return null;
		}

		Class[] interfaces = implClass.getInterfaces();

		if (interfaces.length == 1)
			return interfaces[0];
		
		Class topClass=topClassAPI(implClass.getSuperclass());
		if (topClass==null){
			return implClass;
		}

		return topClass;
	}
	  
	  public static Class loadClass(String className) throws ClassNotFoundException {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();

		if (loader != null)
			return Class.forName(className, false, loader);
		else
			return Class.forName(className);
	}

	public Object getObjectImpl() {
		return objectImpl;
	}

	public void setObjectImpl(Object objectImpl) {
		this.objectImpl = objectImpl;
	}

	public String getObjectInterfaceName() {
		return objectInterfaceName;
	}

	public void setObjectInterfaceName(String objectInterfaceName) {
		this.objectInterfaceName = objectInterfaceName;
	}

	public Object getServiceImpl() {
		return serviceImpl;
	}

	public void setServiceImpl(Object serviceImpl) {
		this.serviceImpl = serviceImpl;
	}

	public String getServiceInterfaceName() {
		return serviceInterfaceName;
	}

	public void setServiceInterfaceName(String serviceInterfaceName) {
		this.serviceInterfaceName = serviceInterfaceName;
	}

}

⌨️ 快捷键说明

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