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.Location;
025    import org.apache.hivemind.definition.ConfigurationParserDefinition;
026    import org.apache.hivemind.definition.ConfigurationPointDefinition;
027    import org.apache.hivemind.definition.ContributionDefinition;
028    import org.apache.hivemind.definition.DefinitionMessages;
029    import org.apache.hivemind.definition.ExtensionDefinition;
030    import org.apache.hivemind.definition.ModuleDefinition;
031    import org.apache.hivemind.definition.Occurances;
032    import org.apache.hivemind.definition.Visibility;
033    
034    /**
035     * Default implementation of {@link ConfigurationPointDefinition}.
036     * 
037     * @author Achim Huegen
038     */
039    public class ConfigurationPointDefinitionImpl extends ExtensionPointDefinitionImpl implements ConfigurationPointDefinition
040    {
041        private String _containerClassName;
042    
043        private Occurances _expectedContributions;
044    
045        private Collection _contributions = new ArrayList();
046        
047        private Map _parsers = new HashMap();
048    
049        public ConfigurationPointDefinitionImpl(ModuleDefinition module)
050        {
051            super(module);
052        }
053    
054        public ConfigurationPointDefinitionImpl(ModuleDefinition module, String id, Location location, Visibility visibility,
055                String containerClassName, Occurances expectedContributions)
056        {
057            super(module, id, location, visibility);
058            _containerClassName = containerClassName;
059            _expectedContributions = expectedContributions;
060        }
061    
062        /**
063         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#getConfigurationTypeName()
064         */
065        public String getConfigurationTypeName()
066        {
067            return _containerClassName;
068        }
069    
070        /**
071         * Set the class name of the configuration.
072         */
073        public void setConfigurationTypeName(String containerClassName)
074        {
075            _containerClassName = containerClassName;
076        }
077    
078        /**
079         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#getExpectedContributions()
080         */
081        public Occurances getExpectedContributions()
082        {
083            return _expectedContributions;
084        }
085    
086        /**
087         * Sets the expected number of contributions to the configuration.
088         */
089        public void setExpectedContributions(Occurances expectedContributions)
090        {
091            _expectedContributions = expectedContributions;
092        }
093    
094        /**
095         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#getContributions()
096         */
097        public Collection getContributions()
098        {
099            return Collections.unmodifiableCollection(_contributions);
100        }
101    
102        /**
103         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#addContribution(org.apache.hivemind.definition.ContributionDefinition)
104         */
105        public void addContribution(ContributionDefinition contribution)
106        {
107            checkVisibility(contribution);
108            _contributions.add(contribution);
109        }
110    
111        /**
112         * Checks if Extension can see this configuration point. 
113         * @throws ApplicationRuntimeException  if not visible
114         */
115        private void checkVisibility(ExtensionDefinition extension)
116        {
117            if (Visibility.PRIVATE.equals(getVisibility())
118                    && !extension.getModuleId().equals(getModuleId()))
119            {
120                throw new ApplicationRuntimeException(DefinitionMessages.configurationPointNotVisible(
121                        this,
122                        extension.getModule()));
123            }
124        }
125        
126        /**
127         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#addParser(org.apache.hivemind.definition.ConfigurationParserDefinition)
128         */
129        public void addParser(ConfigurationParserDefinition parser)
130        {
131            checkVisibility(parser);
132    
133            if (_parsers.containsKey(parser.getInputFormat())) {
134                throw new ApplicationRuntimeException(DefinitionMessages.duplicateParserInputFormat(parser.getInputFormat(), 
135                        this));
136            }
137            _parsers.put(parser.getInputFormat(), parser);
138        }
139    
140        /**
141         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#getParsers()
142         */
143        public Collection getParsers()
144        {
145            return Collections.unmodifiableCollection(_parsers.values());
146        }
147    
148        /**
149         * @see org.apache.hivemind.definition.ConfigurationPointDefinition#getParser(java.lang.String)
150         */
151        public ConfigurationParserDefinition getParser(String inputFormat)
152        {
153            return (ConfigurationParserDefinition) _parsers.get(inputFormat);
154        }
155    
156    
157    }