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;
016    
017    import org.apache.hivemind.service.Autowiring;
018    
019    /**
020     * Ancestor for annotated module classes. Provides convenience methods
021     * for the access to {@link TypedRegistry} and {@link Autowiring}.
022     * 
023     * @author Achim Huegen
024     */
025    public class AbstractAnnotatedModule
026    {
027        private TypedRegistry _typedRegistry;
028    
029        /**
030         * @return  the registry the module is loaded in
031         */
032        public TypedRegistry getRegistry()
033        {
034            return _typedRegistry;
035        }
036    
037        /**
038         * This setter is used to inject the registry reference.
039         * @param typedRegistry  the registry
040         */
041        public void setRegistry(TypedRegistry typedRegistry)
042        {
043            _typedRegistry = typedRegistry;
044        }
045        
046        /**
047         * @return  a reference to the {@link Autowiring} service.
048         */
049        protected Autowiring getAutowiring()
050        {
051            return _typedRegistry.getAutowiring();
052        }
053        
054        /**
055         * Autowires any object by use of the {@link Autowiring} service.
056         * @param target  the object to wire
057         * @return the wired object 
058         */
059        protected <T> T autowireProperties(T target) 
060        {
061            return (T) getAutowiring().autowireProperties(target);
062        }
063        
064        /**
065         * Returns a service from the registry.
066         * 
067         * @see org.apache.hivemind.Registry#getService(String, Class)
068         */
069        protected <T> T service(String serviceId, Class<T> serviceInterface)
070        {
071            return _typedRegistry.getService(serviceId, serviceInterface);
072        }
073    
074        /**
075         * Finds a service that implements the provided interface. 
076         * Exactly one such service may exist or an exception is thrown.
077         * 
078         * @see org.apache.hivemind.Registry#getService(Class)
079         */
080        protected <T> T service(Class<T> serviceInterface)
081        {
082            return _typedRegistry.getService(serviceInterface);
083        }
084        
085        /**
086         * Returns the specified configuration from the registry.
087         * 
088         * @see org.apache.hivemind.Registry#getConfiguration(String)
089         */
090        protected <T> T configuration(String configurationId, Class<T> configurationType)
091        {
092            return _typedRegistry.getConfiguration(configurationId, configurationType);
093        }
094        
095        /**
096         * Finds a configuration by its type. 
097         * Exactly one such configuration may exist or an exception is thrown.
098         * 
099         * @see org.apache.hivemind.Registry#getConfiguration(String)
100         */
101        protected <T> T configuration(Class<T> configurationType)
102        {
103            return _typedRegistry.getConfiguration(configurationType);
104        }
105    
106    }