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.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.ApplicationRuntimeException;
027    import org.apache.hivemind.definition.ConfigurationPointDefinition;
028    import org.apache.hivemind.definition.DefinitionMessages;
029    import org.apache.hivemind.definition.ModuleDefinition;
030    import org.apache.hivemind.definition.RegistryDefinition;
031    import org.apache.hivemind.definition.RegistryDefinitionPostProcessor;
032    import org.apache.hivemind.definition.ServicePointDefinition;
033    import org.apache.hivemind.events.RegistryInitializationListener;
034    import org.apache.hivemind.util.IdUtils;
035    
036    /**
037     * Default implementation of {@link RegistryDefinition}.
038     * 
039     * @author Achim Huegen
040     */
041    public class RegistryDefinitionImpl implements RegistryDefinition
042    {
043        private static final Log LOG = LogFactory.getLog(RegistryDefinitionImpl.class);
044    
045        private Map _modules = new HashMap();
046    
047        private List _postProcessors = new ArrayList();
048        
049        private List _initializationListeners = new ArrayList();
050    
051        public RegistryDefinitionImpl()
052        {
053        }
054        
055        /**
056         * @see org.apache.hivemind.definition.RegistryDefinition#addModule(org.apache.hivemind.definition.ModuleDefinition)
057         */
058        public void addModule(ModuleDefinition module) throws ApplicationRuntimeException
059        {
060            if (_modules.containsKey(module.getId()))
061            {
062                throw new ApplicationRuntimeException(DefinitionMessages.duplicateModuleId(module.getId()));
063            }
064            else
065            {
066                if (LOG.isDebugEnabled())
067                    LOG.debug("Adding module " + module.getId() + " to registry definition");
068    
069                _modules.put(module.getId(), module);
070            }
071        }
072        
073        /**
074         * @see org.apache.hivemind.definition.RegistryDefinition#addPostProcessor(org.apache.hivemind.definition.RegistryDefinitionPostProcessor)
075         */
076        public void addPostProcessor(RegistryDefinitionPostProcessor postProcessor)
077        {
078            _postProcessors.add(postProcessor);
079        }
080    
081        /**
082         * @see org.apache.hivemind.definition.RegistryDefinition#getPostProcessors()
083         */
084        public List getPostProcessors()
085        {
086            return Collections.unmodifiableList(_postProcessors);
087        }
088    
089        /**
090         * @see org.apache.hivemind.definition.RegistryDefinition#addRegistryInitializationListener(org.apache.hivemind.events.RegistryInitializationListener)
091         */
092        public void addRegistryInitializationListener(RegistryInitializationListener listener)
093        {
094            _initializationListeners.add(listener);
095        }
096    
097        /**
098         * @see org.apache.hivemind.definition.RegistryDefinition#getRegistryInitializationListeners()
099         */
100        public List getRegistryInitializationListeners()
101        {
102            return Collections.unmodifiableList(_initializationListeners);
103        }
104        
105        /**
106         * @see org.apache.hivemind.definition.RegistryDefinition#getModules()
107         */
108        public Collection getModules()
109        {
110            return Collections.unmodifiableCollection(_modules.values());
111        }
112    
113        /**
114         * @see org.apache.hivemind.definition.RegistryDefinition#getModule(java.lang.String)
115         */
116        public ModuleDefinition getModule(String id)
117        {
118            return (ModuleDefinition) _modules.get(id);
119        }
120    
121        /**
122         * @see org.apache.hivemind.definition.RegistryDefinition#getServicePoint(java.lang.String)
123         */
124        public ServicePointDefinition getServicePoint(String qualifiedServicePointId)
125        {
126            String moduleId = IdUtils.extractModule(qualifiedServicePointId);
127            String servicePointId = IdUtils.stripModule(qualifiedServicePointId);
128    
129            ServicePointDefinition servicePoint = null;
130            ModuleDefinition module = getModule(moduleId);
131            if (module != null)
132            {
133                servicePoint = module.getServicePoint(servicePointId);
134            }
135            return servicePoint;
136        }
137    
138        /**
139         * @see org.apache.hivemind.definition.RegistryDefinition#getConfigurationPoint(java.lang.String)
140         */
141        public ConfigurationPointDefinition getConfigurationPoint(String qualifiedConfigurationPointId)
142        {
143            String moduleId = IdUtils.extractModule(qualifiedConfigurationPointId);
144            String configurationPointId = IdUtils.stripModule(qualifiedConfigurationPointId);
145    
146            ConfigurationPointDefinition configurationPoint = null;
147            ModuleDefinition module = getModule(moduleId);
148            if (module != null)
149            {
150                configurationPoint = module.getConfigurationPoint(configurationPointId);
151            }
152            return configurationPoint;
153        }
154    
155    }