|
1 |
| package com.agical.rmock.core.configuration; |
|
2 |
| |
|
3 |
| import java.util.HashMap; |
|
4 |
| import java.util.HashSet; |
|
5 |
| import java.util.Iterator; |
|
6 |
| import java.util.Map; |
|
7 |
| import java.util.Set; |
|
8 |
| |
|
9 |
| import com.agical.rmock.core.find.iterator.Service; |
|
10 |
| import com.agical.rmock.core.hub.Hub; |
|
11 |
| import com.agical.rmock.core.util.NamingUtils; |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| public final class ConfiguratorImpl implements Configurator { |
|
19 |
| public static class ConfiguratorNameSpaceProvider implements NameSpaceProvider { |
|
20 |
| private final ConfiguratorImpl configurator; |
|
21 |
| |
|
22 |
6
| public ConfiguratorNameSpaceProvider(ConfiguratorImpl configurator) {
|
|
23 |
6
| this.configurator = configurator;
|
|
24 |
| } |
|
25 |
| |
|
26 |
1
| public Map getNameSpaces() {
|
|
27 |
1
| return configurator.nameSpaces;
|
|
28 |
| } |
|
29 |
| } |
|
30 |
| |
|
31 |
| private Map nameSpaces = new HashMap(); |
|
32 |
| private static ConfiguratorImpl configurator; |
|
33 |
| private NamingUtils namingUtils = new NamingUtils(); |
|
34 |
| |
|
35 |
| private Hub hub = new Hub(); |
|
36 |
| |
|
37 |
| private boolean initialized = false; |
|
38 |
| |
|
39 |
| private Set singletons = new HashSet(); |
|
40 |
| |
|
41 |
6
| ConfiguratorImpl() {
|
|
42 |
6
| super();
|
|
43 |
| |
|
44 |
6
| Service service = new Service();
|
|
45 |
| |
|
46 |
6
| registerServices(service);
|
|
47 |
6
| registerSimpleServices(service);
|
|
48 |
6
| hub.connect(new ConfiguratorNameSpaceProvider(this));
|
|
49 |
| } |
|
50 |
| |
|
51 |
6
| private void registerSimpleServices(Service service) {
|
|
52 |
6
| for( Iterator simpleServices = service.providers( RMockSimpleService.class ); simpleServices.hasNext();) {
|
|
53 |
6
| RMockSimpleService rMockSimpleService = (RMockSimpleService) simpleServices.next();
|
|
54 |
6
| Map nameSpace = getNameSpace(rMockSimpleService.getNameSpace());
|
|
55 |
6
| nameSpace.put( namingUtils.createNameFromType(rMockSimpleService.getClass()), rMockSimpleService);
|
|
56 |
6
| hub.connect(rMockSimpleService);
|
|
57 |
| } |
|
58 |
| } |
|
59 |
| |
|
60 |
6
| private Map getNameSpace(String nameSpace) {
|
|
61 |
6
| Map nameSpaceMap = (Map) nameSpaces.get(nameSpace);
|
|
62 |
6
| if ( nameSpaceMap == null) {
|
|
63 |
6
| nameSpaceMap = new HashMap();
|
|
64 |
6
| nameSpaces.put(nameSpace, nameSpaceMap);
|
|
65 |
| } |
|
66 |
6
| return nameSpaceMap;
|
|
67 |
| } |
|
68 |
| |
|
69 |
6
| private void registerServices(Service service) {
|
|
70 |
6
| Iterator serviceProviders = service.providers( RMockServiceFactory.class );
|
|
71 |
6
| while( serviceProviders.hasNext() ) {
|
|
72 |
30
| RMockServiceFactory serviceFactory = (RMockServiceFactory) serviceProviders.next();
|
|
73 |
30
| Class type = serviceFactory.getServiceType();
|
|
74 |
30
| if( serviceFactory.canCreateService() && !singletons.contains( type ) ) {
|
|
75 |
24
| if( RMockSingletonService.class.isAssignableFrom(type) ) {
|
|
76 |
12
| singletons.add( type );
|
|
77 |
| } |
|
78 |
24
| hub.connect( serviceFactory.createService() );
|
|
79 |
| } |
|
80 |
| } |
|
81 |
| } |
|
82 |
| |
|
83 |
1136
| public static synchronized Configurator getInstance() {
|
|
84 |
1136
| if( configurator == null ) {
|
|
85 |
1
| configurator = new ConfiguratorImpl();
|
|
86 |
| } |
|
87 |
1136
| return configurator;
|
|
88 |
| } |
|
89 |
| |
|
90 |
1140
| public Hub getHub() {
|
|
91 |
1140
| return hub;
|
|
92 |
| } |
|
93 |
| |
|
94 |
1
| public Map getServiceNameSpaces() {
|
|
95 |
1
| return nameSpaces;
|
|
96 |
| } |
|
97 |
| } |