Search in sources :

Example 1 with RemoteInvocationFactory

use of org.springframework.remoting.support.RemoteInvocationFactory in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {

        @Override
        public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            assertTrue(invocation instanceof TestRemoteInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return super.invoke(invocation, targetObject);
        }
    });
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {

        @Override
        public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
            RemoteInvocation invocation = new TestRemoteInvocation(methodInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return invocation;
        }
    });
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultRemoteInvocationExecutor(org.springframework.remoting.support.DefaultRemoteInvocationExecutor) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with RemoteInvocationFactory

use of org.springframework.remoting.support.RemoteInvocationFactory in project dubbo by alibaba.

the class RmiProtocol method doRefer.

@SuppressWarnings("unchecked")
protected <T> T doRefer(final Class<T> serviceType, final URL url) throws RpcException {
    final RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
    // RMI needs extra parameter since it uses customized remote invocation object
    if (url.getParameter(Constants.DUBBO_VERSION_KEY, Version.getVersion()).equals(Version.getVersion())) {
        // Check dubbo version on provider, this feature only support
        rmiProxyFactoryBean.setRemoteInvocationFactory(new RemoteInvocationFactory() {

            public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
                return new RmiRemoteInvocation(methodInvocation);
            }
        });
    }
    rmiProxyFactoryBean.setServiceUrl(url.toIdentityString());
    rmiProxyFactoryBean.setServiceInterface(serviceType);
    rmiProxyFactoryBean.setCacheStub(true);
    rmiProxyFactoryBean.setLookupStubOnStartup(true);
    rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
    rmiProxyFactoryBean.afterPropertiesSet();
    return (T) rmiProxyFactoryBean.getObject();
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MethodInvocation(org.aopalliance.intercept.MethodInvocation) RmiProxyFactoryBean(org.springframework.remoting.rmi.RmiProxyFactoryBean)

Example 3 with RemoteInvocationFactory

use of org.springframework.remoting.support.RemoteInvocationFactory in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {

        @Override
        public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            assertNotNull(invocation.getAttributes());
            assertEquals(1, invocation.getAttributes().size());
            assertEquals("myValue", invocation.getAttributes().get("myKey"));
            assertEquals("myValue", invocation.getAttribute("myKey"));
            return super.invoke(invocation, targetObject);
        }
    });
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {

        @Override
        public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
            RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
            invocation.addAttribute("myKey", "myValue");
            try {
                invocation.addAttribute("myKey", "myValue");
                fail("Should have thrown IllegalStateException");
            } catch (IllegalStateException ex) {
            // expected: already defined
            }
            assertNotNull(invocation.getAttributes());
            assertEquals(1, invocation.getAttributes().size());
            assertEquals("myValue", invocation.getAttributes().get("myKey"));
            assertEquals("myValue", invocation.getAttribute("myKey"));
            return invocation;
        }
    });
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultRemoteInvocationExecutor(org.springframework.remoting.support.DefaultRemoteInvocationExecutor) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)3 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)3 RemoteInvocationFactory (org.springframework.remoting.support.RemoteInvocationFactory)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServletException (javax.servlet.ServletException)2 Test (org.junit.Test)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 RemoteAccessException (org.springframework.remoting.RemoteAccessException)2 DefaultRemoteInvocationExecutor (org.springframework.remoting.support.DefaultRemoteInvocationExecutor)2 RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 RmiProxyFactoryBean (org.springframework.remoting.rmi.RmiProxyFactoryBean)1