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 org.apache.hivemind.definition.ConfigurationPointDefinition;
018    import org.apache.hivemind.definition.Contribution;
019    import org.apache.hivemind.definition.ContributionDefinition;
020    import org.apache.hivemind.definition.ImplementationConstructor;
021    import org.apache.hivemind.definition.ImplementationDefinition;
022    import org.apache.hivemind.definition.ModuleDefinition;
023    import org.apache.hivemind.definition.Occurances;
024    import org.apache.hivemind.definition.ServicePointDefinition;
025    import org.apache.hivemind.definition.Visibility;
026    import org.apache.hivemind.impl.CreateClassServiceConstructor;
027    import org.apache.hivemind.internal.ServiceModel;
028    
029    /**
030     * Helper class that offers convenience functions for the definition of modules
031     * and its extension points. All instances created are 
032     * {@link org.apache.hivemind.definition.impl standard implementations} 
033     * of the definition interfaces.
034     * 
035     * @author Achim Huegen
036     */
037    public class ModuleDefinitionHelper
038    {
039        private ModuleDefinitionImpl _module;
040    
041        public ModuleDefinitionHelper(ModuleDefinitionImpl module)
042        {
043            _module = module;
044        }
045    
046        public ServicePointDefinition addServicePoint(String servicePointId, String serviceInterface)
047        {
048            ServicePointDefinitionImpl result = new ServicePointDefinitionImpl(_module, servicePointId, _module
049                    .getLocation(), Visibility.PUBLIC, serviceInterface);
050    
051            _module.addServicePoint(result);
052            return result;
053        }
054    
055        public ServicePointDefinition addServicePointWithDefaultImplementation(String servicePointId, String serviceInterface)
056        {
057            ServicePointDefinition result = addServicePoint(servicePointId, serviceInterface);
058            String defaultImplementationName = serviceInterface + "Impl";
059            addSimpleServiceImplementation(result, defaultImplementationName, ServiceModel.SINGLETON);
060            return result;
061        }
062     
063        public ImplementationDefinition addServiceImplementation(
064                ServicePointDefinition servicePoint,
065                ImplementationConstructor constructor, String serviceModel
066                )
067        {
068            // These implementations override the inline implementations, so default is true here
069            ImplementationDefinition result = new ImplementationDefinitionImpl(_module, _module
070                    .getLocation(), constructor, serviceModel, true);
071            servicePoint.addImplementation(result);
072            return result;
073        }
074        
075        public ImplementationDefinition addSimpleServiceImplementation(
076                ServicePointDefinition servicePoint,
077                String serviceImplementationClass, String serviceModel)
078        {
079            return addServiceImplementation(servicePoint, 
080                    new CreateClassServiceConstructor(_module.getLocation(), serviceImplementationClass), 
081                    serviceModel);
082        }
083        
084        public ConfigurationPointDefinition addConfigurationPoint(String configurationPointId, String containerType)
085        {
086            ConfigurationPointDefinitionImpl result = new ConfigurationPointDefinitionImpl(_module, configurationPointId, _module
087                    .getLocation(), Visibility.PUBLIC, containerType, Occurances.UNBOUNDED);
088    
089            _module.addConfigurationPoint(result);
090            return result;
091        }
092        
093        public ContributionDefinition addContributionDefinition(ConfigurationPointDefinition configurationPoint,
094                Contribution contributionConstructor)
095        {
096            ContributionDefinition result = new ContributionDefinitionImpl(_module, _module.getLocation(), contributionConstructor, false);
097            configurationPoint.addContribution(result);
098            return result;
099        }
100        
101        public ContributionDefinition addContributionDefinition(String qualifiedConfigurationPointId, 
102                Contribution contributionConstructor)
103        {
104            ContributionDefinitionImpl result = new ContributionDefinitionImpl(_module, _module.getLocation(), contributionConstructor, false);
105            _module.addContribution(qualifiedConfigurationPointId, result);
106            return result;
107        }
108    
109        public ModuleDefinition getModule()
110        {
111            return _module;
112        }
113    
114    }