001    // Copyright 2007 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.definition.impl;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    import java.util.Collections;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.hivemind.ApplicationRuntimeException;
024    import org.apache.hivemind.ClassResolver;
025    import org.apache.hivemind.Location;
026    import org.apache.hivemind.definition.ConfigurationParserDefinition;
027    import org.apache.hivemind.definition.ConfigurationPointDefinition;
028    import org.apache.hivemind.definition.ContributionDefinition;
029    import org.apache.hivemind.definition.DefinitionMessages;
030    import org.apache.hivemind.definition.ExtensionDefinition;
031    import org.apache.hivemind.definition.ModuleDefinition;
032    import org.apache.hivemind.definition.ImplementationDefinition;
033    import org.apache.hivemind.definition.InterceptorDefinition;
034    import org.apache.hivemind.definition.ServicePointDefinition;
035    import org.apache.hivemind.definition.UnresolvedExtension;
036    
037    /**
038     * Default implementation of {@link ExtensionDefinition}.
039     * 
040     * @author Achim Huegen
041     */
042    public class ModuleDefinitionImpl implements ModuleDefinition
043    {
044        private String _id;
045    
046        private Location _location;
047    
048        private String _packageName;
049    
050        private ClassResolver _classResolver;
051        
052        private Map _servicePoints = new HashMap();
053        
054        private Map _configurationPoints = new HashMap();
055        
056        private Collection _dependencies = new ArrayList();
057    
058        private Collection _unresolvedImplementations = new ArrayList();
059    
060        private Collection _unresolvedContributions = new ArrayList();
061    
062        private Collection _unresolvedInterceptors = new ArrayList();
063        
064        private Collection _unresolvedConfigurationParsers = new ArrayList();
065        
066        public ModuleDefinitionImpl()
067        {
068        }
069    
070        /**
071         * @param id  the id of the module 
072         * @param location  the location of the module 
073         * @param resolver the {@link ClassResolver} used to resolve all classes referenced from 
074         *          elements inside this module.
075         * @param packageName  name of the package to search for class names within. If null, it defaults to the id 
076         */
077        public ModuleDefinitionImpl(String id, Location location, ClassResolver resolver, String packageName)
078        {
079            _id = id;
080            _location = location;
081            _classResolver = resolver;
082            if (packageName != null)
083                _packageName = packageName;
084            else _packageName = id;
085        }
086    
087        /**
088         * @see org.apache.hivemind.definition.ModuleDefinition#getClassResolver()
089         */
090        public ClassResolver getClassResolver()
091        {
092            return _classResolver;
093        }
094    
095        /**
096         * Sets the {@link ClassResolver} used to resolve all classes referenced from elements 
097         * inside the module.
098         */
099        public void setClassResolver(ClassResolver classResolver)
100        {
101            _classResolver = classResolver;
102        }
103    
104        /**
105         * @see org.apache.hivemind.definition.ModuleDefinition#getLocation()
106         */
107        public Location getLocation()
108        {
109            return _location;
110        }
111    
112        /**
113         * Sets the location of the module.
114         */
115        public void setLocation(Location location)
116        {
117            _location = location;
118        }
119    
120        /**
121         * @see org.apache.hivemind.definition.ModuleDefinition#getId()
122         */
123        public String getId()
124        {
125            return _id;
126        }
127    
128        /**
129         * Sets the id of the module.
130         */
131        public void setId(String moduleId)
132        {
133            this._id = moduleId;
134        }
135    
136        /**
137         * @see org.apache.hivemind.definition.ModuleDefinition#getPackageName()
138         */
139        public String getPackageName()
140        {
141            return _packageName;
142        }
143    
144        /**
145         * Sets the package name of the module.
146         */
147        public void setPackageName(String packageName)
148        {
149            _packageName = packageName;
150        }
151        
152        /**
153         * Adds a service point definition to the module.
154         * @param servicePoint  the service point
155         * @throws ApplicationRuntimeException  if another service point with the same id has already been defined
156         */
157        public void addServicePoint(ServicePointDefinition servicePoint)
158        {
159            if (_servicePoints.containsKey(servicePoint.getId())) {
160                throw new ApplicationRuntimeException(DefinitionMessages.duplicateServicePointId(servicePoint.getQualifiedId(), 
161                        this));
162            }
163            _servicePoints.put(servicePoint.getId(), servicePoint);
164        }
165    
166        /**
167         * @see org.apache.hivemind.definition.ModuleDefinition#getServicePoint(java.lang.String)
168         */
169        public ServicePointDefinition getServicePoint(String id)
170        {
171            return (ServicePointDefinition) _servicePoints.get(id);
172        }
173        
174        /**
175         * @see org.apache.hivemind.definition.ModuleDefinition#getServicePoints()
176         */
177        public Collection getServicePoints()
178        {
179            return Collections.unmodifiableCollection(_servicePoints.values());
180        }
181     
182        /**
183         * Adds a configuration point definition to the module.
184         * @param configurationPoint  the configuration point
185         * @throws ApplicationRuntimeException  if another configuration point with the same id has already been defined
186         */
187        public void addConfigurationPoint(ConfigurationPointDefinition configurationPoint)
188        {
189            if (_configurationPoints.containsKey(configurationPoint.getId())) {
190                throw new ApplicationRuntimeException(DefinitionMessages.duplicateConfigurationPointId(configurationPoint.getId(), 
191                        this));
192            }
193            _configurationPoints.put(configurationPoint.getId(), configurationPoint);
194        }
195    
196        /**
197         * @see org.apache.hivemind.definition.ModuleDefinition#getConfigurationPoint(java.lang.String)
198         */
199        public ConfigurationPointDefinition getConfigurationPoint(String id)
200        {
201            return (ConfigurationPointDefinition) _configurationPoints.get(id);
202        }
203        
204        /**
205         * @see org.apache.hivemind.definition.ModuleDefinition#getConfigurationPoints()
206         */
207        public Collection getConfigurationPoints()
208        {
209            return Collections.unmodifiableCollection(_configurationPoints.values());
210        }
211    
212        /**
213         * @see org.apache.hivemind.definition.ModuleDefinition#getDependencies()
214         */
215        public Collection getDependencies()
216        {
217            return Collections.unmodifiableCollection(_dependencies);
218        }
219        
220        /**
221         * Defines a dependency on another module. The presence of that module
222         * is checked during registry construction.
223         * 
224         * @param dependsOnModuleId  the id of the module this module depends on
225         */
226        public void addDependency(String dependsOnModuleId) 
227        {
228            _dependencies.add(dependsOnModuleId);
229        }
230       
231        /**
232         * Adds a implementation for a service point which can be defined in this
233         * module or another module.
234         * 
235         * @param qualifiedServicePointId  the fully qualified service point id
236         * @param implementation  the implementation definition
237         */
238        public void addImplementation(String qualifiedServicePointId,
239                ImplementationDefinition implementation)
240        {
241            UnresolvedExtension unresolvedExtension = new UnresolvedExtension(implementation,
242                    qualifiedServicePointId);
243            _unresolvedImplementations.add(unresolvedExtension);
244        }
245    
246        /**
247         * Adds a interceptor for a service point which can be defined in this
248         * module or another module.
249         * 
250         * @param qualifiedServicePointId  the fully qualified service point id
251         * @param interceptor  the interceptor definition
252         */
253        public void addInterceptor(String qualifiedServicePointId,
254                InterceptorDefinition interceptor)
255        {
256            UnresolvedExtension unresolvedExtension = new UnresolvedExtension(interceptor,
257                    qualifiedServicePointId);
258            _unresolvedInterceptors.add(unresolvedExtension);
259        }
260    
261        /**
262         * Adds a contribution for a configuration point which can be defined in this
263         * module or another module.
264         * 
265         * @param qualifiedConfigurationPointId  the fully qualified configuration point id
266         * @param contribution  the contribution definition
267         */
268        public void addContribution(String qualifiedConfigurationPointId,
269                ContributionDefinition contribution)
270        {
271            UnresolvedExtension unresolvedExtension = new UnresolvedExtension(contribution,
272                    qualifiedConfigurationPointId);
273            _unresolvedContributions.add(unresolvedExtension);
274        }
275        
276        /**
277         * Adds a configuration parser for a configuration point which can be defined in this
278         * module or another module.
279         * 
280         * @param qualifiedConfigurationPointId  the fully qualified configuration point id
281         * @param parser  the parser definition
282         */
283        public void addConfigurationParser(String qualifiedConfigurationPointId, ConfigurationParserDefinition parser)
284        {
285            UnresolvedExtension unresolvedExtension = new UnresolvedExtension(parser,
286                    qualifiedConfigurationPointId);
287            _unresolvedConfigurationParsers.add(unresolvedExtension);
288        }  
289        
290        /**
291         * @see org.apache.hivemind.definition.ModuleDefinition#getContributions()
292         */
293        public Collection getContributions()
294        {
295            return _unresolvedContributions;
296        }
297    
298        /**
299         * @see org.apache.hivemind.definition.ModuleDefinition#getImplementations()
300         */
301        public Collection getImplementations()
302        {
303            return _unresolvedImplementations;
304        }
305    
306        /**
307         * @see org.apache.hivemind.definition.ModuleDefinition#getInterceptors()
308         */
309        public Collection getInterceptors()
310        {
311            return _unresolvedInterceptors;
312        }
313    
314        /**
315         * @see org.apache.hivemind.definition.ModuleDefinition#getConfigurationParsers()
316         */
317        public Collection getConfigurationParsers()
318        {
319            return _unresolvedConfigurationParsers;
320        }
321    
322      
323    }