|
1 |
| package com.agical.rmock.core.hub; |
|
2 |
| |
|
3 |
| import java.beans.BeanInfo; |
|
4 |
| import java.beans.IntrospectionException; |
|
5 |
| import java.beans.Introspector; |
|
6 |
| import java.beans.PropertyDescriptor; |
|
7 |
| import java.util.HashMap; |
|
8 |
| import java.util.Map; |
|
9 |
| |
|
10 |
| public class InterfaceToInstanceConnectionStrategy implements ConnectionStrategy { |
|
11 |
| private final Map beanInfoCache = new HashMap(); |
|
12 |
| private final Class itf; |
|
13 |
| private final Map instanceToConnectionStrategy = new HashMap(); |
|
14 |
| |
|
15 |
271
| public InterfaceToInstanceConnectionStrategy(Class itf) {
|
|
16 |
271
| this.itf = itf;
|
|
17 |
| } |
|
18 |
| |
|
19 |
14783
| public void connectToConsumer(Object consumer, Object consumable) {
|
|
20 |
14783
| ConnectionStrategy strategy = getStrategyForInstance(consumer);
|
|
21 |
14783
| strategy.connectToConsumer(consumer, consumable);
|
|
22 |
| } |
|
23 |
| |
|
24 |
34135
| private ConnectionStrategy getStrategyForInstance(Object instance) {
|
|
25 |
34135
| ConnectionStrategy strategy = (ConnectionStrategy) instanceToConnectionStrategy.get(instance);
|
|
26 |
34135
| if(strategy == null) {
|
|
27 |
9331
| strategy = createStrategyForInstance(instance);
|
|
28 |
9331
| instanceToConnectionStrategy.put(instance, strategy);
|
|
29 |
| } |
|
30 |
34135
| return strategy;
|
|
31 |
| } |
|
32 |
| |
|
33 |
19352
| public void disconnectFromConsumer(Object consumer, Object consumable) {
|
|
34 |
19352
| getStrategyForInstance(consumer).disconnectFromConsumer(consumer, consumable);
|
|
35 |
| } |
|
36 |
| |
|
37 |
9331
| private ConnectionStrategy createStrategyForInstance(Object instance) {
|
|
38 |
9331
| try {
|
|
39 |
9331
| BeanInfo beanInfo = (BeanInfo) beanInfoCache.get(instance.getClass());
|
|
40 |
9331
| if (beanInfo == null) {
|
|
41 |
1228
| beanInfo = Introspector.getBeanInfo(instance.getClass(), Introspector.IGNORE_ALL_BEANINFO);
|
|
42 |
1228
| beanInfoCache.put(instance.getClass(), beanInfo);
|
|
43 |
| } |
|
44 |
9331
| PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
|
45 |
46141
| for (int i = 0; i < propertyDescriptors.length; i++) {
|
|
46 |
46141
| PropertyDescriptor descriptor = propertyDescriptors[i];
|
|
47 |
46141
| if(descriptor.getPropertyType() != null && itf.isAssignableFrom(descriptor.getPropertyType()) && descriptor.getWriteMethod() != null) {
|
|
48 |
9331
| return new ProxySetMethodConnectionStrategy(descriptor.getWriteMethod());
|
|
49 |
| } |
|
50 |
| } |
|
51 |
| |
|
52 |
| } |
|
53 |
| catch (IntrospectionException e) { |
|
54 |
0
| throw new IntrospectionSystemException(e);
|
|
55 |
| } |
|
56 |
0
| return null;
|
|
57 |
| } |
|
58 |
| } |