001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 import java.util.List;
032
033 /**
034 * TODO: document me
035 *
036 * NOTE: this class is serialized when modules are installed. Any changes should
037 * be carefully reviewed to ensure they don't cause deserialization errors!
038 *
039 * @version $Rev: 534984 $ $Date: 2007-05-03 16:29:47 -0400 (Thu, 03 May 2007) $
040 */
041 public abstract class ConfigurationAwareReference extends SimpleAwareReference {
042 private static final long serialVersionUID = 283358809226901462L;
043 private final Artifact[] configId;
044 protected final Set<AbstractNameQuery> abstractNameQueries;
045
046 protected ConfigurationAwareReference(Artifact[] configId, AbstractNameQuery abstractNameQuery) {
047 this(configId, Collections.singleton(abstractNameQuery));
048 }
049
050 protected ConfigurationAwareReference(Artifact[] configId, Set<AbstractNameQuery> abstractNameQueries) {
051 if (configId == null || configId.length == 0) {
052 throw new NullPointerException("No configId");
053 }
054 this.configId = configId;
055 this.abstractNameQueries = abstractNameQueries;
056 }
057
058 public Configuration getConfiguration() {
059 Kernel kernel = getKernel();
060 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
061 Configuration configuration = configurationManager.getConfiguration(configId[0]);
062 if (configuration == null) {
063 throw new IllegalStateException("No configuration found for id: " + configId[0]);
064 }
065 next: for (int i = 1; i < configId.length; i++) {
066 List<Configuration> children = configuration.getChildren();
067 for (Configuration child: children) {
068 if (child.getId().equals(configId[i])) {
069 configuration = child;
070 break next;
071 }
072 }
073 throw new IllegalStateException("No configuration found for id: " + configId[i]);
074 }
075 return configuration;
076 }
077
078 public AbstractName resolveTargetName() throws GBeanNotFoundException {
079 Configuration configuration = getConfiguration();
080 try {
081 return configuration.findGBean(abstractNameQueries);
082 } catch (GBeanNotFoundException e) {
083 Set results = getKernel().listGBeans(abstractNameQueries);
084 if (results.size() == 1) {
085 return (AbstractName) results.iterator().next();
086 }
087 throw new GBeanNotFoundException("Name query " + abstractNameQueries + " not satisfied in kernel, matches: " + results, e);
088 }
089 }
090
091 }