KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ServiceMapImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
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  * Implementation of {@link org.apache.tapestry.services.ServiceMap}
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class ServiceMapImpl implements ServiceMap, EngineServiceSource
34 {
35     /**
36      * List of {@link EngineServiceContribution}.
37      */

38     private List JavaDoc _applicationServices;
39
40     /**
41      * List of {@link EngineServiceContribution}.
42      */

43     private List JavaDoc _factoryServices;
44
45     private ErrorLog _errorLog;
46
47     /**
48      * Map of {@link EngineServiceContribution} keyed on String name.
49      */

50     private Map JavaDoc _services;
51
52     /**
53      * Map of {@link org.apache.tapestry.services.impl.EngineServiceOuterProxy}, keyed on String
54      * name.
55      */

56
57     private Map JavaDoc _proxies = new HashMap JavaDoc();
58
59     public void initializeService()
60     {
61         Map JavaDoc factoryMap = buildServiceMap(_factoryServices);
62         Map JavaDoc applicationMap = buildServiceMap(_applicationServices);
63
64         // Add services from the applicationMap to factoryMap, overwriting
65
// factoryMap entries with the same name.
66

67         factoryMap.putAll(applicationMap);
68
69         _services = factoryMap;
70     }
71
72     private Map JavaDoc buildServiceMap(List JavaDoc services)
73     {
74         Map JavaDoc result = new HashMap JavaDoc();
75
76         Iterator JavaDoc i = services.iterator();
77         while (i.hasNext())
78         {
79             EngineServiceContribution contribution = (EngineServiceContribution) i.next();
80             String JavaDoc name = contribution.getName();
81
82             EngineServiceContribution existing = (EngineServiceContribution) result.get(name);
83
84             if (existing != null)
85             {
86                 _errorLog.error(
87                         ImplMessages.dupeService(name, existing),
88                         existing.getLocation(),
89                         null);
90                 continue;
91             }
92
93             result.put(name, contribution);
94         }
95
96         return result;
97     }
98
99     public synchronized IEngineService getService(String JavaDoc name)
100     {
101         IEngineService result = (IEngineService) _proxies.get(name);
102
103         if (result == null)
104         {
105             result = buildProxy(name);
106             _proxies.put(name, result);
107         }
108
109         return result;
110     }
111
112     /**
113      * This returns the actual service, not the outer proxy.
114      */

115
116     public IEngineService resolveEngineService(String JavaDoc name)
117     {
118         EngineServiceContribution contribution = (EngineServiceContribution) _services.get(name);
119
120         if (contribution == null)
121             throw new ApplicationRuntimeException(ImplMessages.noSuchService(name));
122
123         IEngineService service = contribution.getService();
124         String JavaDoc serviceName = service.getName();
125
126         if (!serviceName.equals(name))
127             throw new ApplicationRuntimeException(ImplMessages.serviceNameMismatch(
128                     service,
129                     name,
130                     serviceName), contribution.getLocation(), null);
131
132         return service;
133     }
134
135     private IEngineService buildProxy(String JavaDoc name)
136     {
137         if (!_services.containsKey(name))
138             throw new ApplicationRuntimeException(ImplMessages.noSuchService(name));
139
140         EngineServiceOuterProxy outer = new EngineServiceOuterProxy(name);
141
142         EngineServiceInnerProxy inner = new EngineServiceInnerProxy(name, outer, this);
143
144         outer.installDelegate(inner);
145
146         return outer;
147     }
148
149     public void setApplicationServices(List JavaDoc applicationServices)
150     {
151         _applicationServices = applicationServices;
152     }
153
154     public void setFactoryServices(List JavaDoc factoryServices)
155     {
156         _factoryServices = factoryServices;
157     }
158
159     public void setErrorLog(ErrorLog errorLog)
160     {
161         _errorLog = errorLog;
162     }
163 }
Popular Tags