001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.naming.reference;
019    
020    import org.apache.geronimo.gbean.AbstractNameQuery;
021    import org.apache.geronimo.gbean.AbstractName;
022    import org.apache.geronimo.kernel.config.Configuration;
023    import org.apache.geronimo.kernel.config.ConfigurationManager;
024    import org.apache.geronimo.kernel.config.ConfigurationUtil;
025    import org.apache.geronimo.kernel.repository.Artifact;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.kernel.GBeanNotFoundException;
028    
029    import java.util.Set;
030    import java.util.Collections;
031    
032    /**
033     * TODO: document me
034     *
035     * NOTE: this class is serialized when modules are installed.  Any changes should
036     * be carefully reviewed to ensure they don't cause deserialization errors!
037     *
038     * @version $Rev: 412622 $ $Date: 2006-06-07 17:20:33 -0700 (Wed, 07 Jun 2006) $
039     */
040    public abstract class ConfigurationAwareReference extends SimpleAwareReference {
041        private static final long serialVersionUID = 283358809226901462L;
042        private final Artifact configId;
043        protected final Set abstractNameQueries;
044    
045        protected ConfigurationAwareReference(Artifact configId, AbstractNameQuery abstractNameQuery) {
046            this.configId = configId;
047            this.abstractNameQueries = Collections.singleton(abstractNameQuery);
048        }
049    
050        protected ConfigurationAwareReference(Artifact configId, Set abstractNameQueries) {
051            this.configId = configId;
052            this.abstractNameQueries = abstractNameQueries;
053        }
054    
055        public Configuration getConfiguration() {
056            Kernel kernel = getKernel();
057            ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
058            return configurationManager.getConfiguration(configId);
059        }
060    
061        public AbstractName resolveTargetName() throws GBeanNotFoundException {
062            Configuration configuration = getConfiguration();
063            try {
064                return configuration.findGBean(abstractNameQueries);
065            } catch (GBeanNotFoundException e) {
066                Set results = getKernel().listGBeans(abstractNameQueries);
067                if (results.size() == 1) {
068                    return (AbstractName) results.iterator().next();
069                }
070                throw new GBeanNotFoundException("Name query " + abstractNameQueries + " not satisfied in kernel, matches: " + results, e);
071            }
072        }
073    
074    }