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);
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations