|
1 |
| -package com.googlecode.jsonrpc4j.spring; |
2 |
| - |
3 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
4 |
| -import com.googlecode.jsonrpc4j.JsonRpcService; |
5 |
| -import org.slf4j.Logger; |
6 |
| -import org.slf4j.LoggerFactory; |
7 |
| -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
8 |
| -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
9 |
| -import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
10 |
| -import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
11 |
| -import org.springframework.context.ApplicationContext; |
12 |
| -import org.springframework.context.ApplicationContextAware; |
13 |
| -import org.springframework.core.io.Resource; |
14 |
| -import org.springframework.core.type.AnnotationMetadata; |
15 |
| -import org.springframework.core.type.ClassMetadata; |
16 |
| -import org.springframework.core.type.classreading.MetadataReader; |
17 |
| -import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; |
18 |
| - |
19 |
| -import java.io.IOException; |
20 |
| -import java.net.MalformedURLException; |
21 |
| -import java.net.URL; |
22 |
| - |
23 |
| -import static java.lang.String.format; |
24 |
| -import static org.springframework.util.ClassUtils.convertClassNameToResourcePath; |
25 |
| -import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX; |
26 |
| - |
27 |
| -/** |
28 |
| - * Auto-creates proxies for service interfaces annotated with {@link JsonRpcService}. |
29 |
| - */ |
30 |
| -@SuppressWarnings("unused") |
31 |
| -public class AutoJsonRpcClientProxyCreator implements BeanFactoryPostProcessor, ApplicationContextAware { |
32 |
| - |
33 |
| - private static final Logger logger = LoggerFactory.getLogger(AutoJsonRpcClientProxyCreator.class); |
34 |
| - private ApplicationContext applicationContext; |
35 |
| - private String scanPackage; |
36 |
| - private URL baseUrl; |
37 |
| - private ObjectMapper objectMapper; |
38 |
| - private String contentType; |
39 |
| - |
40 |
| - @Override |
41 |
| - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
42 |
| - SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext); |
43 |
| - DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; |
44 |
| - String resolvedPath = resolvePackageToScan(); |
45 |
| - logger.debug("Scanning '{}' for JSON-RPC service interfaces.", resolvedPath); |
46 |
| - try { |
47 |
| - for (Resource resource : applicationContext.getResources(resolvedPath)) { |
48 |
| - if (resource.isReadable()) { |
49 |
| - MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); |
50 |
| - ClassMetadata classMetadata = metadataReader.getClassMetadata(); |
51 |
| - AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); |
52 |
| - String jsonRpcPathAnnotation = JsonRpcService.class.getName(); |
53 |
| - if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) { |
54 |
| - String className = classMetadata.getClassName(); |
55 |
| - String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("value"); |
56 |
| - logger.debug("Found JSON-RPC service to proxy [{}] on path '{}'.", className, path); |
57 |
| - registerJsonProxyBean(defaultListableBeanFactory, className, path); |
58 |
| - } |
59 |
| - } |
60 |
| - } |
61 |
| - } catch (IOException e) { |
62 |
| - throw new IllegalStateException(format("Cannot scan package '%s' for classes.", resolvedPath), e); |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - /** |
67 |
| - * Converts the scanPackage to something that the resource loader can handleRequest. |
68 |
| - */ |
69 |
| - private String resolvePackageToScan() { |
70 |
| - return CLASSPATH_URL_PREFIX + convertClassNameToResourcePath(scanPackage) + "/**/*.class"; |
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Registers a new proxy bean with the bean factory. |
75 |
| - */ |
76 |
| - private void registerJsonProxyBean(DefaultListableBeanFactory defaultListableBeanFactory, String className, String path) { |
77 |
| - BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder |
78 |
| - .rootBeanDefinition(JsonProxyFactoryBean.class) |
79 |
| - .addPropertyValue("serviceUrl", appendBasePath(path)) |
80 |
| - .addPropertyValue("serviceInterface", className); |
81 |
| - |
82 |
| - if (objectMapper != null) { |
83 |
| - beanDefinitionBuilder.addPropertyValue("objectMapper", objectMapper); |
84 |
| - } |
85 |
| - |
86 |
| - if (contentType != null) { |
87 |
| - beanDefinitionBuilder.addPropertyValue("contentType", contentType); |
88 |
| - } |
89 |
| - |
90 |
| - defaultListableBeanFactory.registerBeanDefinition(className + "-clientProxy", beanDefinitionBuilder.getBeanDefinition()); |
91 |
| - } |
92 |
| - |
93 |
| - /** |
94 |
| - * Appends the base path to the path found in the interface. |
95 |
| - */ |
96 |
| - private String appendBasePath(String path) { |
97 |
| - try { |
98 |
| - return new URL(baseUrl, path).toString(); |
99 |
| - } catch (MalformedURLException e) { |
100 |
| - throw new IllegalArgumentException(format("Cannot combine URLs '%s' and '%s' to valid URL.", baseUrl, path), e); |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - @Override |
105 |
| - public void setApplicationContext(ApplicationContext applicationContext) { |
106 |
| - this.applicationContext = applicationContext; |
107 |
| - } |
108 |
| - |
109 |
| - public void setBaseUrl(URL baseUrl) { |
110 |
| - this.baseUrl = baseUrl; |
111 |
| - } |
112 |
| - |
113 |
| - public void setScanPackage(String scanPackage) { |
114 |
| - this.scanPackage = scanPackage; |
115 |
| - } |
116 |
| - |
117 |
| - public void setObjectMapper(ObjectMapper objectMapper) { |
118 |
| - this.objectMapper = objectMapper; |
119 |
| - } |
120 |
| - |
121 |
| - public void setContentType(String contextType) { |
122 |
| - this.contentType = contextType; |
123 |
| - } |
124 |
| -} |
| 1 | +package com.googlecode.jsonrpc4j.spring; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.googlecode.jsonrpc4j.JsonRpcService; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 8 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 9 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 10 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 11 | +import org.springframework.context.ApplicationContext; |
| 12 | +import org.springframework.context.ApplicationContextAware; |
| 13 | +import org.springframework.context.EnvironmentAware; |
| 14 | +import org.springframework.core.env.Environment; |
| 15 | +import org.springframework.core.io.Resource; |
| 16 | +import org.springframework.core.type.AnnotationMetadata; |
| 17 | +import org.springframework.core.type.ClassMetadata; |
| 18 | +import org.springframework.core.type.classreading.MetadataReader; |
| 19 | +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | + |
| 25 | +import static java.lang.String.format; |
| 26 | +import static org.springframework.util.ClassUtils.convertClassNameToResourcePath; |
| 27 | +import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX; |
| 28 | + |
| 29 | +/** |
| 30 | + * Auto-creates proxies for service interfaces annotated with {@link JsonRpcService}. |
| 31 | + */ |
| 32 | +@SuppressWarnings("unused") |
| 33 | +public class AutoJsonRpcClientProxyCreator implements BeanFactoryPostProcessor, ApplicationContextAware, EnvironmentAware { |
| 34 | + |
| 35 | + private static final Logger logger = LoggerFactory.getLogger(AutoJsonRpcClientProxyCreator.class); |
| 36 | + private ApplicationContext applicationContext; |
| 37 | + private Environment environment; |
| 38 | + private String scanPackage; |
| 39 | + private URL baseUrl; |
| 40 | + private ObjectMapper objectMapper; |
| 41 | + private String contentType; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { |
| 45 | + SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext); |
| 46 | + DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; |
| 47 | + String resolvedPath = resolvePackageToScan(); |
| 48 | + logger.debug("Scanning '{}' for JSON-RPC service interfaces.", resolvedPath); |
| 49 | + try { |
| 50 | + for (Resource resource : applicationContext.getResources(resolvedPath)) { |
| 51 | + if (resource.isReadable()) { |
| 52 | + MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); |
| 53 | + ClassMetadata classMetadata = metadataReader.getClassMetadata(); |
| 54 | + AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); |
| 55 | + String jsonRpcPathAnnotation = JsonRpcService.class.getName(); |
| 56 | + if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) { |
| 57 | + String className = classMetadata.getClassName(); |
| 58 | + String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("value"); |
| 59 | + path = this.environment.resolvePlaceholders(path); |
| 60 | + logger.debug("Found JSON-RPC service to proxy [{}] on path '{}'.", className, path); |
| 61 | + registerJsonProxyBean(defaultListableBeanFactory, className, path); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (IOException e) { |
| 66 | + throw new IllegalStateException(format("Cannot scan package '%s' for classes.", resolvedPath), e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Converts the scanPackage to something that the resource loader can handleRequest. |
| 72 | + */ |
| 73 | + private String resolvePackageToScan() { |
| 74 | + return CLASSPATH_URL_PREFIX + convertClassNameToResourcePath(scanPackage) + "/**/*.class"; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Registers a new proxy bean with the bean factory. |
| 79 | + */ |
| 80 | + private void registerJsonProxyBean(DefaultListableBeanFactory defaultListableBeanFactory, String className, String path) { |
| 81 | + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder |
| 82 | + .rootBeanDefinition(JsonProxyFactoryBean.class) |
| 83 | + .addPropertyValue("serviceUrl", appendBasePath(path)) |
| 84 | + .addPropertyValue("serviceInterface", className); |
| 85 | + |
| 86 | + if (objectMapper != null) { |
| 87 | + beanDefinitionBuilder.addPropertyValue("objectMapper", objectMapper); |
| 88 | + } |
| 89 | + |
| 90 | + if (contentType != null) { |
| 91 | + beanDefinitionBuilder.addPropertyValue("contentType", contentType); |
| 92 | + } |
| 93 | + |
| 94 | + defaultListableBeanFactory.registerBeanDefinition(className + "-clientProxy", beanDefinitionBuilder.getBeanDefinition()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Appends the base path to the path found in the interface. |
| 99 | + */ |
| 100 | + private String appendBasePath(String path) { |
| 101 | + try { |
| 102 | + return new URL(baseUrl, path).toString(); |
| 103 | + } catch (MalformedURLException e) { |
| 104 | + throw new IllegalArgumentException(format("Cannot combine URLs '%s' and '%s' to valid URL.", baseUrl, path), e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 110 | + this.applicationContext = applicationContext; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setEnvironment(Environment environment) { |
| 116 | + this.environment = environment; |
| 117 | + } |
| 118 | + |
| 119 | + public void setBaseUrl(URL baseUrl) { |
| 120 | + this.baseUrl = baseUrl; |
| 121 | + } |
| 122 | + |
| 123 | + public void setScanPackage(String scanPackage) { |
| 124 | + this.scanPackage = scanPackage; |
| 125 | + } |
| 126 | + |
| 127 | + public void setObjectMapper(ObjectMapper objectMapper) { |
| 128 | + this.objectMapper = objectMapper; |
| 129 | + } |
| 130 | + |
| 131 | + public void setContentType(String contextType) { |
| 132 | + this.contentType = contextType; |
| 133 | + } |
| 134 | +} |
0 commit comments