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.impl;
016    
017    import org.apache.commons.logging.Log;
018    import org.apache.commons.logging.LogFactory;
019    import org.apache.hivemind.ErrorLog;
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.definition.ExtensionPointDefinition;
022    import org.apache.hivemind.definition.Visibility;
023    import org.apache.hivemind.internal.ExtensionPoint;
024    import org.apache.hivemind.internal.Module;
025    import org.apache.hivemind.util.ToStringBuilder;
026    
027    /**
028     * Base class for extension points; provides module, visibility and extensionPointId properties.
029     * 
030     * @author Howard Lewis Ship
031     */
032    /**
033     * @author Huegen
034     */
035    public abstract class AbstractExtensionPoint extends BaseLocatable implements ExtensionPoint
036    {
037        private ExtensionPointDefinition _definition;
038    
039        private Module _module;
040    
041        /** @since 1.1 */
042    
043        private ErrorLog _errorLog;
044        
045        /**
046         * @param definition  the definition of this extension point
047         */
048        public AbstractExtensionPoint(Module module, ExtensionPointDefinition definition)
049        {
050            _module = module;
051            _definition = definition;
052        }
053        
054        public synchronized String toString()
055        {
056            ToStringBuilder builder = new ToStringBuilder(this);
057            builder.append("extensionPointId", getExtensionPointId());
058            builder.append("visibility", getVisibility());
059    
060            extendDescription(builder);
061    
062            return builder.toString();
063        }
064    
065        /**
066         * Implemented in subclasses to provide details about subclass properties.
067         */
068        protected abstract void extendDescription(ToStringBuilder builder);
069    
070        public Visibility getVisibility()
071        {
072            return _definition.getVisibility();
073        }
074        
075        public Location getLocation()
076        {
077            return _definition.getLocation();
078        }
079        
080        public String getExtensionPointId()
081        {
082            return _module.getModuleId() + "." + _definition.getId();
083        }
084    
085        public Module getModule()
086        {
087            return _module;
088        }
089    
090        /**
091         * Returns true if the extension point is public, or the extgension point is visible to the
092         * module.
093         * 
094         * @param module
095         *            The module to validate visibility against, or null for no module ... such as when
096         *            the application accesses an extension via {@link org.apache.hivemind.Registry}.
097         * @since 1.1
098         */
099        public boolean visibleToModule(Module module)
100        {
101            if (getVisibility() == Visibility.PUBLIC)
102                return true;
103    
104            return _module.equals(module);
105        }
106    
107        /** @since 1.1 */
108        public Log getLog()
109        {
110            return LogFactory.getLog(getExtensionPointId());
111        }
112    
113        /** @since 1.1 */
114        public synchronized ErrorLog getErrorLog()
115        {
116            if (_errorLog == null)
117                _errorLog = new ErrorLogImpl(_module.getErrorHandler(), getLog());
118    
119            return _errorLog;
120        }
121    
122        protected ExtensionPointDefinition getDefinition()
123        {
124            return _definition;
125        }   
126        
127    
128    }