001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.impl;
016    
017    import java.util.List;
018    import java.util.Locale;
019    import java.util.Map;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.ClassResolver;
023    import org.apache.hivemind.ErrorHandler;
024    import org.apache.hivemind.HiveMind;
025    import org.apache.hivemind.Location;
026    import org.apache.hivemind.Messages;
027    import org.apache.hivemind.internal.MessageFinder;
028    import org.apache.hivemind.internal.Module;
029    import org.apache.hivemind.internal.RegistryInfrastructure;
030    import org.apache.hivemind.internal.ServiceModelFactory;
031    import org.apache.hivemind.internal.ServicePoint;
032    import org.apache.hivemind.schema.Translator;
033    import org.apache.hivemind.service.ThreadLocale;
034    import org.apache.hivemind.util.IdUtils;
035    import org.apache.hivemind.util.ToStringBuilder;
036    
037    /**
038     * Implementation of {@link org.apache.hivemind.internal.Module}.
039     * 
040     * @author Howard Lewis Ship
041     */
042    public final class ModuleImpl extends BaseLocatable implements Module
043    {
044        private String _moduleId;
045    
046        /** @since 1.1 */
047        private String _packageName;
048    
049        private RegistryInfrastructure _registry;
050    
051        private ClassResolver _resolver;
052    
053        private Messages _messages;
054    
055        public List getConfiguration(String extensionPointId)
056        {
057            String qualifiedId = IdUtils.qualify(_moduleId, extensionPointId);
058    
059            return _registry.getConfiguration(qualifiedId, this);
060        }
061    
062        public boolean isConfigurationMappable(String configurationId)
063        {
064            String qualifiedId = IdUtils.qualify(_moduleId, configurationId);
065    
066            return _registry.getConfigurationPoint(qualifiedId, this).areElementsMappable();
067        }
068    
069        public Map getConfigurationAsMap(String configurationId)
070        {
071            String qualifiedId = IdUtils.qualify(_moduleId, configurationId);
072    
073            return _registry.getConfigurationPoint(qualifiedId, this).getElementsAsMap();
074        }
075    
076        public String getModuleId()
077        {
078            return _moduleId;
079        }
080    
081        /** @since 1.1 */
082    
083        public void setPackageName(String packageName)
084        {
085            _packageName = packageName;
086        }
087    
088        public boolean containsService(Class serviceInterface)
089        {
090            return _registry.containsService(serviceInterface, this);
091        }
092    
093        public Object getService(String serviceId, Class serviceInterface)
094        {
095            String qualifiedId = IdUtils.qualify(_moduleId, serviceId);
096    
097            return _registry.getService(qualifiedId, serviceInterface, this);
098        }
099    
100        public Object getService(Class serviceInterface)
101        {
102            return _registry.getService(serviceInterface, this);
103        }
104    
105        public void setModuleId(String string)
106        {
107            _moduleId = string;
108        }
109    
110        public void setRegistry(RegistryInfrastructure registry)
111        {
112            _registry = registry;
113        }
114    
115        public void setClassResolver(ClassResolver resolver)
116        {
117            _resolver = resolver;
118        }
119    
120        public ClassResolver getClassResolver()
121        {
122            return _resolver;
123        }
124    
125        public synchronized Messages getMessages()
126        {
127            if (_messages == null)
128            {
129                ThreadLocale threadLocale = (ThreadLocale) _registry.getService(
130                        HiveMind.THREAD_LOCALE_SERVICE,
131                        ThreadLocale.class,
132                        this);
133    
134                MessageFinder finder = new MessageFinderImpl(getLocation().getResource());
135    
136                _messages = new ModuleMessages(finder, threadLocale);
137            }
138    
139            return _messages;
140        }
141    
142        public String expandSymbols(String input, Location location)
143        {
144            return _registry.expandSymbols(input, location);
145        }
146    
147        public String toString()
148        {
149            ToStringBuilder builder = new ToStringBuilder(this);
150    
151            builder.append("moduleId", _moduleId);
152            builder.append("classResolver", _resolver);
153    
154            return builder.toString();
155        }
156    
157        public ServicePoint getServicePoint(String serviceId)
158        {
159            String qualifiedId = IdUtils.qualify(_moduleId, serviceId);
160    
161            return _registry.getServicePoint(qualifiedId, this);
162        }
163    
164        public ServiceModelFactory getServiceModelFactory(String name)
165        {
166            return _registry.getServiceModelFactory(name);
167        }
168    
169        public Translator getTranslator(String translator)
170        {
171            return _registry.getTranslator(translator);
172        }
173    
174        public Locale getLocale()
175        {
176            return _registry.getLocale();
177        }
178    
179        public ErrorHandler getErrorHandler()
180        {
181            return _registry.getErrorHander();
182        }
183    
184        public String valueForSymbol(String symbol)
185        {
186            return _registry.valueForSymbol(symbol);
187        }
188    
189        public Class resolveType(String type)
190        {
191            Class result = _resolver.checkForClass(type);
192    
193            if (result == null)
194                result = _resolver.checkForClass(_packageName + "." + type);
195    
196            if (result == null)
197                throw new ApplicationRuntimeException(ImplMessages.unableToConvertType(
198                        type,
199                        _packageName));
200    
201            return result;
202        }
203    }