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.parse;
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.List;
022    import java.util.Map;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.hivemind.ClassResolver;
027    import org.apache.hivemind.ErrorHandler;
028    import org.apache.hivemind.schema.Schema;
029    import org.apache.hivemind.schema.impl.SchemaImpl;
030    import org.apache.hivemind.util.ToStringBuilder;
031    
032    /**
033     * Representation of a HiveMind module descriptor, as parsed by
034     * {@link org.apache.hivemind.parse.DescriptorParser}. Corresponds to the root <module>
035     * element.
036     * 
037     * @author Howard Lewis Ship
038     */
039    public final class ModuleDescriptor extends BaseAnnotationHolder
040    {
041        /** @since 1.1 */
042        private static final Log LOG = LogFactory.getLog(ModuleDescriptor.class);
043    
044        private String _moduleId;
045    
046        private String _version;
047    
048        /** @since 1.1 */
049    
050        private String _packageName;
051    
052        private List _servicePoints;
053    
054        private List _implementations;
055    
056        private List _configurationPoints;
057    
058        private List _contributions;
059    
060        private List _subModules;
061    
062        private List _dependencies;
063    
064        /** @since 1.1 */
065        private Map _schemas;
066    
067        private ClassResolver _resolver;
068    
069        /** @since 1.1 */
070        private ErrorHandler _errorHandler;
071    
072        public ModuleDescriptor(ClassResolver resolver, ErrorHandler errorHandler)
073        {
074            _resolver = resolver;
075            _errorHandler = errorHandler;
076        }
077    
078        public String toString()
079        {
080            ToStringBuilder builder = new ToStringBuilder(this);
081    
082            builder.append("moduleId", _moduleId);
083            builder.append("version", _version);
084    
085            return builder.toString();
086        }
087    
088        public void addServicePoint(ServicePointDescriptor service)
089        {
090            if (_servicePoints == null)
091                _servicePoints = new ArrayList();
092    
093            _servicePoints.add(service);
094        }
095    
096        public List getServicePoints()
097        {
098            return _servicePoints;
099        }
100    
101        public void addImplementation(ImplementationDescriptor descriptor)
102        {
103            if (_implementations == null)
104                _implementations = new ArrayList();
105    
106            _implementations.add(descriptor);
107        }
108    
109        public List getImplementations()
110        {
111            return _implementations;
112        }
113    
114        public void addConfigurationPoint(ConfigurationPointDescriptor descriptor)
115        {
116            if (_configurationPoints == null)
117                _configurationPoints = new ArrayList();
118    
119            _configurationPoints.add(descriptor);
120        }
121    
122        public List getConfigurationPoints()
123        {
124            return _configurationPoints;
125        }
126    
127        public void addContribution(ContributionDescriptor descriptor)
128        {
129            if (_contributions == null)
130                _contributions = new ArrayList();
131    
132            _contributions.add(descriptor);
133        }
134    
135        public List getContributions()
136        {
137            return _contributions;
138        }
139    
140        public void addSubModule(SubModuleDescriptor subModule)
141        {
142            if (_subModules == null)
143                _subModules = new ArrayList();
144    
145            _subModules.add(subModule);
146        }
147    
148        public List getSubModules()
149        {
150            return _subModules;
151        }
152    
153        public void addDependency(DependencyDescriptor dependency)
154        {
155            if (_dependencies == null)
156                _dependencies = new ArrayList();
157    
158            _dependencies.add(dependency);
159        }
160    
161        public List getDependencies()
162        {
163            return _dependencies;
164        }
165    
166        /**
167         * Adds a schema to this module descriptor. If a schema with the same id already has been added,
168         * an error is reported and the given schema is ignored.
169         * 
170         * @since 1.1
171         */
172        public void addSchema(SchemaImpl schema)
173        {
174            if (_schemas == null)
175                _schemas = new HashMap();
176    
177            String schemaId = schema.getId();
178    
179            Schema existing = getSchema(schemaId);
180    
181            if (existing != null)
182            {
183                _errorHandler.error(LOG, ParseMessages.duplicateSchema(
184                        _moduleId + '.' + schemaId,
185                        existing), schema.getLocation(), null);
186                return;
187            }
188    
189            _schemas.put(schemaId, schema);
190        }
191    
192        /** @since 1.1 */
193        public Schema getSchema(String id)
194        {
195            return _schemas == null ? null : (Schema) _schemas.get(id);
196        }
197    
198        /**
199         * Returns a Collection of {@link org.apache.hivemind.schema.impl.SchemaImpl}.
200         * 
201         * @since 1.1
202         */
203        public Collection getSchemas()
204        {
205            return _schemas != null ? _schemas.values() : Collections.EMPTY_LIST;
206        }
207    
208        public String getModuleId()
209        {
210            return _moduleId;
211        }
212    
213        public String getVersion()
214        {
215            return _version;
216        }
217    
218        public void setModuleId(String string)
219        {
220            _moduleId = string;
221        }
222    
223        public void setVersion(String string)
224        {
225            _version = string;
226        }
227    
228        public ClassResolver getClassResolver()
229        {
230            return _resolver;
231        }
232    
233        /**
234         * Returns the name of the package to search for class names within. By default, the package
235         * name will match the module id, but this can be overridden in the module descriptor.
236         * 
237         * @since 1.1
238         */
239    
240        public String getPackageName()
241        {
242            return _packageName;
243        }
244    
245        /** @since 1.1 */
246    
247        public void setPackageName(String packageName)
248        {
249            _packageName = packageName;
250        }
251    }