基于Burlap协议调用java方法

35 篇文章 0 订阅
10 篇文章 0 订阅
public interface TestService {
    public void test(String name);
}

 

public class TestServiceImpl implements TestService {
    @Override
    public void test(String name) {
        System.out.println("test:" + name);
    }
}

 

public class BurlapSkeletonTest {

	private static TestServiceImpl testService;

	private static BurlapSkeleton skeleton;

	@BeforeClass
	public static void initialize() {
		testService = new TestServiceImpl();

		skeleton = new BurlapSkeleton(testService, TestService.class);
	}
	
	private Document initBurlapRequest() throws BurlapException {
		DocumentBuilder domBuilder = null;
		try {
			domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new BurlapException(e);
		}
		return domBuilder.newDocument();
	}
	
	private InputStream getBurlapInputStream(Document doc) throws BurlapException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		Transformer transformer = null;
		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			transformer = transFactory.newTransformer();
		} catch (Exception e) {
			throw new BurlapException(e);
		}
		
		transformer.setOutputProperty("encoding", "gb2312");
		transformer.setOutputProperty("indent", "yes");
		
		// omit xml declaration
		// <?xml version="1.0" encoding="GB2312" standalone="no"?>
		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		
		
		DOMSource source = new DOMSource();
		source.setNode(doc);
		StreamResult result = new StreamResult();
		result.setOutputStream(bos);
		try {
			transformer.transform(source, result);
		} catch (Exception e) {
			throw new BurlapException(e);
		}
		
		System.out.println(new String(bos.toByteArray()));

		return new ByteArrayInputStream(bos.toByteArray());
	}

	/**
	 * <burlap:call>
	 *   <method>test</method>
	 *   <string>helloworld</string>
	 * </burlap:call>
	 * 
	 * @throws IOException
	 */
	@Test
	public void invoke() throws IOException {
		Document doc = null;
		try {
			doc = initBurlapRequest();
		} catch (BurlapException e) {
			throw new IOException(e);
		}
		
		Element root = doc.createElement("burlap:call");
		
		Element element = doc.createElement("method");
		element.setTextContent("test");
		root.appendChild(element);
		
		element = doc.createElement("string");
		element.setTextContent("helloworld");
		root.appendChild(element);
		
		doc.appendChild(root);


		InputStream isToUse;
		try {
			isToUse = getBurlapInputStream(doc);
		} catch (BurlapException e) {
			throw new IOException(e);
		}
		OutputStream osToUse = new ByteArrayOutputStream();

		BurlapInput in = new BurlapInput(isToUse);
		BurlapOutput out = new BurlapOutput(osToUse);
		try {
			skeleton.invoke(in, out);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值