KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > ModuleImpl


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.hivemind.impl;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.ClassResolver;
23 import org.apache.hivemind.ErrorHandler;
24 import org.apache.hivemind.HiveMind;
25 import org.apache.hivemind.Messages;
26 import org.apache.hivemind.internal.MessageFinder;
27 import org.apache.hivemind.internal.Module;
28 import org.apache.hivemind.internal.RegistryInfrastructure;
29 import org.apache.hivemind.internal.ServiceModelFactory;
30 import org.apache.hivemind.internal.ServicePoint;
31 import org.apache.hivemind.service.ThreadLocale;
32 import org.apache.hivemind.util.IdUtils;
33 import org.apache.hivemind.util.ToStringBuilder;
34
35 /**
36  * Implementation of {@link org.apache.hivemind.internal.Module}.
37  *
38  * @author Howard Lewis Ship
39  */

40 public final class ModuleImpl extends BaseLocatable implements Module
41 {
42     private String JavaDoc _moduleId;
43
44     /** @since 1.1 */
45     private String JavaDoc _packageName;
46
47     private RegistryInfrastructure _registry;
48
49     private ClassResolver _resolver;
50
51     private Messages _messages;
52
53     /**
54      * Map from (partial) class name to Class. Related to performance bug HIVEMIND-162.
55      *
56      * @since 1.1.1
57      */

58     private final Map JavaDoc _typeCache = new HashMap JavaDoc();
59
60     public Object JavaDoc getConfiguration(String JavaDoc extensionPointId)
61     {
62         String JavaDoc qualifiedId = IdUtils.qualify(_moduleId, extensionPointId);
63
64         return _registry.getConfiguration(qualifiedId, this);
65     }
66
67     public String JavaDoc getModuleId()
68     {
69         return _moduleId;
70     }
71
72     /** @since 1.1 */
73
74     public void setPackageName(String JavaDoc packageName)
75     {
76         _packageName = packageName;
77     }
78
79     public boolean containsService(Class JavaDoc serviceInterface)
80     {
81         return _registry.containsService(serviceInterface, this);
82     }
83
84     public Object JavaDoc getService(String JavaDoc serviceId, Class JavaDoc serviceInterface)
85     {
86         String JavaDoc qualifiedId = IdUtils.qualify(_moduleId, serviceId);
87
88         return _registry.getService(qualifiedId, serviceInterface, this);
89     }
90
91     public Object JavaDoc getService(Class JavaDoc serviceInterface)
92     {
93         return _registry.getService(serviceInterface, this);
94     }
95
96     public void setModuleId(String JavaDoc string)
97     {
98         _moduleId = string;
99     }
100
101     public void setRegistry(RegistryInfrastructure registry)
102     {
103         _registry = registry;
104     }
105
106     public void setClassResolver(ClassResolver resolver)
107     {
108         _resolver = resolver;
109     }
110
111     public ClassResolver getClassResolver()
112     {
113         return _resolver;
114     }
115
116     public synchronized Messages getMessages()
117     {
118         if (_messages == null)
119         {
120             ThreadLocale threadLocale = (ThreadLocale) _registry.getService(
121                     HiveMind.THREAD_LOCALE_SERVICE,
122                     ThreadLocale.class,
123                     this);
124
125             MessageFinder finder = new MessageFinderImpl(getLocation().getResource());
126
127             _messages = new ModuleMessages(finder, threadLocale);
128         }
129
130         return _messages;
131     }
132
133     public String JavaDoc toString()
134     {
135         ToStringBuilder builder = new ToStringBuilder(this);
136
137         builder.append("moduleId", _moduleId);
138         builder.append("classResolver", _resolver);
139
140         return builder.toString();
141     }
142
143     public ServicePoint getServicePoint(String JavaDoc serviceId)
144     {
145         String JavaDoc qualifiedId = IdUtils.qualify(_moduleId, serviceId);
146
147         return _registry.getServicePoint(qualifiedId, this);
148     }
149
150     public ServiceModelFactory getServiceModelFactory(String JavaDoc name)
151     {
152         return _registry.getServiceModelFactory(name);
153     }
154
155     public Locale JavaDoc getLocale()
156     {
157         return _registry.getLocale();
158     }
159
160     public ErrorHandler getErrorHandler()
161     {
162         return _registry.getErrorHander();
163     }
164
165     public synchronized Class JavaDoc resolveType(String JavaDoc type)
166     {
167         Class JavaDoc result = (Class JavaDoc) _typeCache.get(type);
168
169         if (result == null)
170         {
171             result = findTypeInClassResolver(type);
172
173             _typeCache.put(type, result);
174         }
175
176         return result;
177     }
178
179     private Class JavaDoc findTypeInClassResolver(String JavaDoc type)
180     {
181         Class JavaDoc result = _resolver.checkForClass(type);
182
183         if (result == null)
184             result = _resolver.checkForClass(_packageName + "." + type);
185
186         if (result == null)
187             throw new ApplicationRuntimeException(ImplMessages.unableToConvertType(
188                     type,
189                     _packageName));
190
191         return result;
192     }
193
194     /**
195      * @see org.apache.hivemind.internal.Module#getRegistry()
196      */

197     public RegistryInfrastructure getRegistry()
198     {
199         return _registry;
200     }
201     
202 }
Popular Tags