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.Iterator;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.hivemind.Location;
024    import org.apache.hivemind.definition.DefinitionMessages;
025    import org.apache.hivemind.definition.ExtensionDefinition;
026    import org.apache.hivemind.definition.ImplementationDefinition;
027    import org.apache.hivemind.definition.InterceptorDefinition;
028    import org.apache.hivemind.definition.ModuleDefinition;
029    import org.apache.hivemind.definition.ServicePointDefinition;
030    import org.apache.hivemind.definition.Visibility;
031    
032    /**
033     * Default implementation of {@link ServicePointDefinition}.
034     * 
035     * @author Achim Huegen
036     */
037    public class ServicePointDefinitionImpl extends ExtensionPointDefinitionImpl implements ServicePointDefinition
038    {
039        private String _interfaceClassName;
040        
041        private Collection _implementations = new ArrayList();
042    
043        private Collection _interceptors = new ArrayList(); 
044    
045        public ServicePointDefinitionImpl(ModuleDefinition module)
046        {
047            super(module);
048        }
049    
050        public ServicePointDefinitionImpl(ModuleDefinition module, String id, Location location, Visibility visibility, String interfaceClassName)
051        {
052            super(module, id, location, visibility);
053            _interfaceClassName = interfaceClassName;
054        }
055    
056        /**
057         * @see org.apache.hivemind.definition.ServicePointDefinition#getInterfaceClassName()
058         */
059        public String getInterfaceClassName()
060        {
061            return _interfaceClassName;
062        }
063    
064        /**
065         * Sets the class name of the service interface.
066         * 
067         * @param interfaceClassName the fully qualified class name of the service interface. 
068         *          This may be the name of a ordinary class or an interface.
069         */
070        public void setInterfaceClassName(String interfaceClassName)
071        {
072            _interfaceClassName = interfaceClassName;
073        }
074    
075        /**
076         * @see org.apache.hivemind.definition.ServicePointDefinition#getImplementations()
077         */
078        public Collection getImplementations()
079        {
080            return Collections.unmodifiableCollection(_implementations);
081        }
082        
083        /**
084         * @see org.apache.hivemind.definition.ServicePointDefinition#getDefaultImplementation()
085         */
086        public ImplementationDefinition getDefaultImplementation()
087        {
088            ImplementationDefinition defaulImplementation = null;
089            for (Iterator iter = _implementations.iterator(); iter.hasNext();)
090            {
091                ImplementationDefinition impl = (ImplementationDefinition) iter.next();
092                if (defaulImplementation == null)
093                    defaulImplementation = impl;
094                if (impl.isDefault()) {
095                    defaulImplementation = impl;
096                    break;
097                }
098            }
099            
100            return defaulImplementation;
101        }
102    
103        /**
104         * Checks if Extension can see this service point. 
105         * @throws ApplicationRuntimeException  if not visible
106         */
107        private void checkVisibility(ExtensionDefinition extension)
108        {
109            if (Visibility.PRIVATE.equals(getVisibility())
110                    && !extension.getModuleId().equals(getModuleId()))
111            {
112                throw new ApplicationRuntimeException(DefinitionMessages.servicePointNotVisible(
113                        this,
114                        extension.getModule()));
115            }
116        }
117        
118        /**
119         * @see org.apache.hivemind.definition.ServicePointDefinition#addImplementation(org.apache.hivemind.definition.ImplementationDefinition)
120         */
121        public void addImplementation(ImplementationDefinition implementation)
122        {
123            checkVisibility(implementation);
124            _implementations.add(implementation);
125        }
126        
127        /**
128         * @see org.apache.hivemind.definition.ServicePointDefinition#getInterceptors()
129         */
130        public Collection getInterceptors()
131        {
132            return Collections.unmodifiableCollection(_interceptors);
133        }
134    
135        /**
136         * @see org.apache.hivemind.definition.ServicePointDefinition#addInterceptor(org.apache.hivemind.definition.InterceptorDefinition)
137         */
138        public void addInterceptor(InterceptorDefinition interceptor)
139        {
140            checkVisibility(interceptor);
141            _interceptors.add(interceptor);
142        }
143    }