Search in sources :

Example 1 with GenericException

use of com.alibaba.dubbo.rpc.service.GenericException in project dubbo by alibaba.

the class GenericServiceTest method testGenericServiceException.

@Test
public void testGenericServiceException() {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("generic-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29581));
    service.setInterface(DemoService.class.getName());
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            if ("sayName".equals(method)) {
                return "Generic " + args[0];
            }
            if ("throwDemoException".equals(method)) {
                throw new GenericException(DemoException.class.getName(), "Generic");
            }
            if ("getUsers".equals(method)) {
                return args[0];
            }
            return null;
        }
    });
    service.export();
    try {
        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setApplication(new ApplicationConfig("generic-consumer"));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://127.0.0.1:29581?generic=true");
        DemoService demoService = reference.get();
        try {
            // say name
            Assert.assertEquals("Generic Haha", demoService.sayName("Haha"));
            // get users
            List<User> users = new ArrayList<User>();
            users.add(new User("Aaa"));
            users = demoService.getUsers(users);
            Assert.assertEquals("Aaa", users.get(0).getName());
            // throw demo exception
            try {
                demoService.throwDemoException();
                Assert.fail();
            } catch (DemoException e) {
                Assert.assertEquals("Generic", e.getMessage());
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : User(com.alibaba.dubbo.config.api.User) GenericService(com.alibaba.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) DemoService(com.alibaba.dubbo.config.api.DemoService) GenericException(com.alibaba.dubbo.rpc.service.GenericException) DemoException(com.alibaba.dubbo.config.api.DemoException) Test(org.junit.Test)

Example 2 with GenericException

use of com.alibaba.dubbo.rpc.service.GenericException in project dubbo by alibaba.

the class GenericFilter method invoke.

public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    if (inv.getMethodName().equals(Constants.$INVOKE) && inv.getArguments() != null && inv.getArguments().length == 3 && !ProtocolUtils.isGeneric(invoker.getUrl().getParameter(Constants.GENERIC_KEY))) {
        String name = ((String) inv.getArguments()[0]).trim();
        String[] types = (String[]) inv.getArguments()[1];
        Object[] args = (Object[]) inv.getArguments()[2];
        try {
            Method method = ReflectUtils.findMethodByMethodSignature(invoker.getInterface(), name, types);
            Class<?>[] params = method.getParameterTypes();
            if (args == null) {
                args = new Object[params.length];
            }
            String generic = inv.getAttachment(Constants.GENERIC_KEY);
            if (StringUtils.isEmpty(generic) || ProtocolUtils.isDefaultGenericSerialization(generic)) {
                args = PojoUtils.realize(args, params, method.getGenericParameterTypes());
            } else if (ProtocolUtils.isJavaGenericSerialization(generic)) {
                for (int i = 0; i < args.length; i++) {
                    if (byte[].class == args[i].getClass()) {
                        try {
                            UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[]) args[i]);
                            args[i] = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).deserialize(null, is).readObject();
                        } catch (Exception e) {
                            throw new RpcException("Deserialize argument [" + (i + 1) + "] failed.", e);
                        }
                    } else {
                        throw new RpcException(new StringBuilder(32).append("Generic serialization [").append(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).append("] only support message type ").append(byte[].class).append(" and your message type is ").append(args[i].getClass()).toString());
                    }
                }
            } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
                for (int i = 0; i < args.length; i++) {
                    if (args[i] instanceof JavaBeanDescriptor) {
                        args[i] = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) args[i]);
                    } else {
                        throw new RpcException(new StringBuilder(32).append("Generic serialization [").append(Constants.GENERIC_SERIALIZATION_BEAN).append("] only support message type ").append(JavaBeanDescriptor.class.getName()).append(" and your message type is ").append(args[i].getClass().getName()).toString());
                    }
                }
            }
            Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
            if (result.hasException() && !(result.getException() instanceof GenericException)) {
                return new RpcResult(new GenericException(result.getException()));
            }
            if (ProtocolUtils.isJavaGenericSerialization(generic)) {
                try {
                    UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
                    ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA).serialize(null, os).writeObject(result.getValue());
                    return new RpcResult(os.toByteArray());
                } catch (IOException e) {
                    throw new RpcException("Serialize result failed.", e);
                }
            } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
                return new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD));
            } else {
                return new RpcResult(PojoUtils.generalize(result.getValue()));
            }
        } catch (NoSuchMethodException e) {
            throw new RpcException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            throw new RpcException(e.getMessage(), e);
        }
    }
    return invoker.invoke(inv);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcResult(com.alibaba.dubbo.rpc.RpcResult) UnsafeByteArrayInputStream(com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) GenericException(com.alibaba.dubbo.rpc.service.GenericException) IOException(java.io.IOException) RpcException(com.alibaba.dubbo.rpc.RpcException) GenericException(com.alibaba.dubbo.rpc.service.GenericException) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Serialization(com.alibaba.dubbo.common.serialize.Serialization) JavaBeanDescriptor(com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor) RpcException(com.alibaba.dubbo.rpc.RpcException) UnsafeByteArrayOutputStream(com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)

Example 3 with GenericException

use of com.alibaba.dubbo.rpc.service.GenericException in project dubbo by alibaba.

the class GenericServiceTest method testGenericImplementationWithBeanSerialization.

@Test
public void testGenericImplementationWithBeanSerialization() throws Exception {
    final AtomicReference reference = new AtomicReference();
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("bean-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29581));
    service.setInterface(DemoService.class.getName());
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            if ("getUsers".equals(method)) {
                GenericParameter arg = new GenericParameter();
                arg.method = method;
                arg.parameterTypes = parameterTypes;
                arg.arguments = args;
                reference.set(arg);
                return args[0];
            }
            if ("sayName".equals(method)) {
                return null;
            }
            return args;
        }
    });
    service.export();
    ReferenceConfig<DemoService> ref = null;
    try {
        ref = new ReferenceConfig<DemoService>();
        ref.setApplication(new ApplicationConfig("bean-consumer"));
        ref.setInterface(DemoService.class);
        ref.setUrl("dubbo://127.0.0.1:29581?scope=remote&generic=bean");
        DemoService demoService = ref.get();
        User user = new User();
        user.setName("zhangsan");
        List<User> users = new ArrayList<User>();
        users.add(user);
        List<User> result = demoService.getUsers(users);
        Assert.assertEquals(users.size(), result.size());
        Assert.assertEquals(user.getName(), result.get(0).getName());
        GenericParameter gp = (GenericParameter) reference.get();
        Assert.assertEquals("getUsers", gp.method);
        Assert.assertEquals(1, gp.parameterTypes.length);
        Assert.assertEquals(ReflectUtils.getName(List.class), gp.parameterTypes[0]);
        Assert.assertEquals(1, gp.arguments.length);
        Assert.assertTrue(gp.arguments[0] instanceof JavaBeanDescriptor);
        JavaBeanDescriptor descriptor = (JavaBeanDescriptor) gp.arguments[0];
        Assert.assertTrue(descriptor.isCollectionType());
        Assert.assertEquals(ArrayList.class.getName(), descriptor.getClassName());
        Assert.assertEquals(1, descriptor.propertySize());
        descriptor = (JavaBeanDescriptor) descriptor.getProperty(0);
        Assert.assertTrue(descriptor.isBeanType());
        Assert.assertEquals(User.class.getName(), descriptor.getClassName());
        Assert.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty());
        Assert.assertNull(demoService.sayName("zhangsan"));
    } finally {
        if (ref != null) {
            ref.destroy();
        }
        service.unexport();
    }
}
Also used : User(com.alibaba.dubbo.config.api.User) GenericService(com.alibaba.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) DemoService(com.alibaba.dubbo.config.api.DemoService) AtomicReference(java.util.concurrent.atomic.AtomicReference) GenericException(com.alibaba.dubbo.rpc.service.GenericException) JavaBeanDescriptor(com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with GenericException

use of com.alibaba.dubbo.rpc.service.GenericException in project dubbo by alibaba.

the class ConfigTest method testGenericServiceConfig.

@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry) collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Registry(com.alibaba.dubbo.registry.Registry) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) GenericException(com.alibaba.dubbo.rpc.service.GenericException) URL(com.alibaba.dubbo.common.URL) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) Test(org.junit.Test)

Example 5 with GenericException

use of com.alibaba.dubbo.rpc.service.GenericException in project dubbo by alibaba.

the class ConfigTest method testReferGenericExport.

@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);
    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());
    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeansException(org.springframework.beans.BeansException) RpcException(com.alibaba.dubbo.rpc.RpcException) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) Test(org.junit.Test)

Aggregations

GenericException (com.alibaba.dubbo.rpc.service.GenericException)6 GenericService (com.alibaba.dubbo.rpc.service.GenericService)4 Test (org.junit.Test)4 JavaBeanDescriptor (com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor)3 RpcException (com.alibaba.dubbo.rpc.RpcException)3 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)2 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)2 ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)2 DemoService (com.alibaba.dubbo.config.api.DemoService)2 User (com.alibaba.dubbo.config.api.User)2 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)2 Result (com.alibaba.dubbo.rpc.Result)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 RpcResult (com.alibaba.dubbo.rpc.RpcResult)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)2 URL (com.alibaba.dubbo.common.URL)1 UnsafeByteArrayInputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream)1 UnsafeByteArrayOutputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)1