1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.services.impl; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.List; |
20 |
| import java.util.Map; |
21 |
| |
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
23 |
| import org.apache.hivemind.ErrorLog; |
24 |
| import org.apache.tapestry.engine.IEngineService; |
25 |
| import org.apache.tapestry.services.ServiceMap; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class ServiceMapImpl implements ServiceMap, EngineServiceSource |
34 |
| { |
35 |
| |
36 |
| |
37 |
| |
38 |
| private List _applicationServices; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| private List _factoryServices; |
44 |
| |
45 |
| private ErrorLog _errorLog; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| private Map _services; |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| private Map _proxies = new HashMap(); |
58 |
| |
59 |
45
| public void initializeService()
|
60 |
| { |
61 |
45
| Map factoryMap = buildServiceMap(_factoryServices);
|
62 |
45
| Map applicationMap = buildServiceMap(_applicationServices);
|
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
45
| factoryMap.putAll(applicationMap);
|
68 |
| |
69 |
45
| _services = factoryMap;
|
70 |
| } |
71 |
| |
72 |
90
| private Map buildServiceMap(List services)
|
73 |
| { |
74 |
90
| Map result = new HashMap();
|
75 |
| |
76 |
90
| Iterator i = services.iterator();
|
77 |
90
| while (i.hasNext())
|
78 |
| { |
79 |
320
| EngineServiceContribution contribution = (EngineServiceContribution) i.next();
|
80 |
320
| String name = contribution.getName();
|
81 |
| |
82 |
320
| EngineServiceContribution existing = (EngineServiceContribution) result.get(name);
|
83 |
| |
84 |
320
| if (existing != null)
|
85 |
| { |
86 |
1
| _errorLog.error(
|
87 |
| ImplMessages.dupeService(name, existing), |
88 |
| existing.getLocation(), |
89 |
| null); |
90 |
1
| continue;
|
91 |
| } |
92 |
| |
93 |
319
| result.put(name, contribution);
|
94 |
| } |
95 |
| |
96 |
90
| return result;
|
97 |
| } |
98 |
| |
99 |
283
| public synchronized IEngineService getService(String name)
|
100 |
| { |
101 |
283
| IEngineService result = (IEngineService) _proxies.get(name);
|
102 |
| |
103 |
283
| if (result == null)
|
104 |
| { |
105 |
149
| result = buildProxy(name);
|
106 |
148
| _proxies.put(name, result);
|
107 |
| } |
108 |
| |
109 |
282
| return result;
|
110 |
| } |
111 |
| |
112 |
3
| public boolean isValid(String name)
|
113 |
| { |
114 |
3
| return _services.containsKey(name);
|
115 |
| } |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
120
| public IEngineService resolveEngineService(String name)
|
122 |
| { |
123 |
120
| EngineServiceContribution contribution = (EngineServiceContribution) _services.get(name);
|
124 |
| |
125 |
120
| if (contribution == null)
|
126 |
1
| throw new ApplicationRuntimeException(ImplMessages.noSuchService(name));
|
127 |
| |
128 |
119
| IEngineService service = contribution.getService();
|
129 |
119
| String serviceName = service.getName();
|
130 |
| |
131 |
119
| if (!name.equals(serviceName))
|
132 |
1
| throw new ApplicationRuntimeException(ImplMessages.serviceNameMismatch(
|
133 |
| service, |
134 |
| name, |
135 |
| serviceName), contribution.getLocation(), null); |
136 |
| |
137 |
118
| return service;
|
138 |
| } |
139 |
| |
140 |
149
| private IEngineService buildProxy(String name)
|
141 |
| { |
142 |
149
| if (!_services.containsKey(name))
|
143 |
1
| throw new ApplicationRuntimeException(ImplMessages.noSuchService(name));
|
144 |
| |
145 |
148
| EngineServiceOuterProxy outer = new EngineServiceOuterProxy(name);
|
146 |
| |
147 |
148
| EngineServiceInnerProxy inner = new EngineServiceInnerProxy(name, outer, this);
|
148 |
| |
149 |
148
| outer.installDelegate(inner);
|
150 |
| |
151 |
148
| return outer;
|
152 |
| } |
153 |
| |
154 |
45
| public void setApplicationServices(List applicationServices)
|
155 |
| { |
156 |
45
| _applicationServices = applicationServices;
|
157 |
| } |
158 |
| |
159 |
45
| public void setFactoryServices(List factoryServices)
|
160 |
| { |
161 |
45
| _factoryServices = factoryServices;
|
162 |
| } |
163 |
| |
164 |
40
| public void setErrorLog(ErrorLog errorLog)
|
165 |
| { |
166 |
40
| _errorLog = errorLog;
|
167 |
| } |
168 |
| } |