Search in sources :

Example 6 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilder method schema.

/**
 * Describe a Java interface in Json schema.
 *
 * @return Service description
 */
public static String schema(final Class<?> clazz) {
    ServiceDefinition sd = build(clazz);
    Gson gson = new Gson();
    return gson.toJson(sd);
}
Also used : Gson(com.google.gson.Gson) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition)

Example 7 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class AbstractAbstractWritableMetadataService method getServiceDefinition.

protected String getServiceDefinition(URL exportedURL) {
    String interfaceName = exportedURL.getParameter(INTERFACE_KEY);
    String json = null;
    try {
        if (StringUtils.isNotEmpty(interfaceName) && !isGeneric(exportedURL.getParameter(GENERIC_KEY))) {
            Class interfaceClass = forName(interfaceName);
            ServiceDefinition serviceDefinition = buildFullDefinition(interfaceClass, exportedURL.getParameters());
            json = JSON.toJSONString(serviceDefinition);
        }
    } catch (ClassNotFoundException e) {
        // ignore error
        if (logger.isErrorEnabled()) {
            logger.error("The interface class[name : " + interfaceName + "] can't be found , providerUrl: " + exportedURL.toFullString());
        }
    }
    return json;
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 8 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class InMemoryWritableMetadataService method publishServiceDefinition.

@Override
public void publishServiceDefinition(URL providerUrl) {
    try {
        if (!ProtocolUtils.isGeneric(providerUrl.getParameter(GENERIC_KEY))) {
            String interfaceName = providerUrl.getParameter(INTERFACE_KEY);
            if (StringUtils.isNotEmpty(interfaceName)) {
                Class interfaceClass = Class.forName(interfaceName);
                ServiceDefinition serviceDefinition = ServiceDefinitionBuilder.build(interfaceClass);
                String data = JSON.toJSONString(serviceDefinition);
                serviceDefinitions.put(providerUrl.getServiceKey(), data);
                return;
            }
            logger.error("publishProvider interfaceName is empty . providerUrl: " + providerUrl.toFullString());
        } else if (CONSUMER_SIDE.equalsIgnoreCase(providerUrl.getParameter(SIDE_KEY))) {
            // to avoid consumer generic invoke style error
            return;
        }
    } catch (ClassNotFoundException e) {
        // ignore error
        logger.error("publishProvider getServiceDescriptor error. providerUrl: " + providerUrl.toFullString(), e);
    }
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 9 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class MetadataTest method testExtendsMap.

@Test
public void testExtendsMap() {
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    TypeDefinition td = builder.build(ClassExtendsMap.class, ClassExtendsMap.class);
    System.out.println(">> testExtendsMap: " + new Gson().toJson(td));
    Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap", td.getType());
    Assertions.assertEquals(MapTypeBuilder.class.getName(), td.getTypeBuilderName());
    Assertions.assertEquals(0, td.getProperties().size());
    ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
    System.out.println(">> testExtendsMap: " + new Gson().toJson(sd));
    Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
    Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
    boolean containsType = false;
    for (TypeDefinition type : sd.getTypes()) {
        if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ClassExtendsMap")) {
            containsType = true;
            break;
        }
    }
    Assertions.assertFalse(containsType);
}
Also used : TestService(org.apache.dubbo.metadata.definition.common.TestService) Gson(com.google.gson.Gson) MapTypeBuilder(org.apache.dubbo.metadata.definition.builder.MapTypeBuilder) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

Example 10 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class MetadataTest method testEnum.

@Test
public void testEnum() {
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    TypeDefinition td = builder.build(ColorEnum.class, ColorEnum.class);
    System.out.println(">> testEnum: " + new Gson().toJson(td));
    Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ColorEnum", td.getType());
    Assertions.assertEquals(EnumTypeBuilder.class.getName(), td.getTypeBuilderName());
    Assertions.assertEquals(3, td.getEnums().size());
    Assertions.assertTrue(td.getEnums().contains("RED"));
    Assertions.assertTrue(td.getEnums().contains("YELLOW"));
    Assertions.assertTrue(td.getEnums().contains("BLUE"));
    ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
    System.out.println(">> testEnum: " + new Gson().toJson(sd));
    Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
    Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
    boolean containsType = false;
    for (TypeDefinition type : sd.getTypes()) {
        if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ColorEnum")) {
            containsType = true;
            break;
        }
    }
    Assertions.assertTrue(containsType);
}
Also used : TestService(org.apache.dubbo.metadata.definition.common.TestService) Gson(com.google.gson.Gson) EnumTypeBuilder(org.apache.dubbo.metadata.definition.builder.EnumTypeBuilder) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)14 Test (org.junit.jupiter.api.Test)8 Gson (com.google.gson.Gson)5 TypeDefinition (org.apache.dubbo.metadata.definition.model.TypeDefinition)5 TestService (org.apache.dubbo.metadata.definition.common.TestService)4 HashMap (java.util.HashMap)2 URL (org.apache.dubbo.common.URL)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)2 MapTypeBuilder (org.apache.dubbo.metadata.definition.builder.MapTypeBuilder)2 FullServiceDefinition (org.apache.dubbo.metadata.definition.model.FullServiceDefinition)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)2 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)2 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 TypeElement (javax.lang.model.element.TypeElement)1 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)1