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.annotations.internal;
016    
017    import org.apache.hivemind.annotations.TypedRegistry;
018    import org.apache.hivemind.internal.Module;
019    import org.apache.hivemind.internal.RegistryInfrastructure;
020    import org.apache.hivemind.service.Autowiring;
021    import org.apache.hivemind.util.IdUtils;
022    
023    /**
024     * Implementation of {@link TypedRegistry}.
025     * Wraps an instance of {@link RegistryInfrastructure} to provide registry access.
026     * 
027     * @author Huegen
028     */
029    public class TypedRegistryImpl implements TypedRegistry
030    {
031        private Module _callingModule;
032    
033        private RegistryInfrastructure _delegate;
034    
035        /**
036         * @param callingModule  the module that gets access registry access by this instance.
037         *                       Used for visibility checks when services and configurations are retrieved.
038         *                       Can be null, in this case only public extension points are visible.
039         * @param delegate
040         */
041        public TypedRegistryImpl(Module callingModule, RegistryInfrastructure delegate)
042        {
043            _callingModule = callingModule;
044            _delegate = delegate;
045        }
046    
047        /**
048         * @see org.apache.hivemind.annotations.TypedRegistry#getConfiguration(java.lang.String, java.lang.Class)
049         */
050        public <T> T getConfiguration(String configurationId, Class<T> configurationType)
051        {
052            String qualifiedConfigurationId = qualifyExtensionPointId(configurationId);
053            Object configuration = _delegate.getConfiguration(
054                    qualifiedConfigurationId,
055                    _callingModule);
056            return (T) configuration;
057        }
058    
059        private String qualifyExtensionPointId(String extensionPointId)
060        {
061            if (_callingModule == null) {
062                return extensionPointId;
063            } else {
064                return IdUtils.qualify(
065                        _callingModule.getModuleId(),
066                        extensionPointId);
067            }
068        }
069    
070        /**
071         * @see org.apache.hivemind.annotations.TypedRegistry#getConfiguration(java.lang.Class)
072         */
073        public <T> T getConfiguration(Class<T> configurationType)
074        {
075            Object configuration = _delegate.getConfiguration(configurationType, _callingModule);
076            return (T) configuration;
077        }
078    
079        /**
080         * @see org.apache.hivemind.annotations.TypedRegistry#getService(java.lang.String, java.lang.Class)
081         */
082        public <T> T getService(String serviceId, Class<T> serviceInterface)
083        {
084            String qualifiedServiceId = qualifyExtensionPointId(serviceId);
085            Object service = _delegate.getService(qualifiedServiceId, serviceInterface, _callingModule);
086            return (T) service;
087        }
088    
089        /**
090         * @see org.apache.hivemind.annotations.TypedRegistry#getService(java.lang.Class)
091         */
092        public <T> T getService(Class<T> serviceInterface)
093        {
094            Object service = _delegate.getService(serviceInterface, _callingModule);
095            return (T) service;
096        }
097    
098        /**
099         * @see org.apache.hivemind.annotations.TypedRegistry#getAutowiring()
100         */
101        public Autowiring getAutowiring()
102        {
103            return getService(Autowiring.class);
104        }
105    
106        /**
107         * @see org.apache.hivemind.annotations.TypedRegistry#shutdown()
108         */
109        public void shutdown()
110        {
111            _delegate.shutdown();
112            
113        }
114    
115    }