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.deployment;
019
020 import java.util.Arrays;
021 import java.util.HashSet;
022 import java.util.Map;
023 import java.util.Set;
024
025 import javax.xml.namespace.QName;
026
027 import org.apache.geronimo.common.DeploymentException;
028 import org.apache.geronimo.gbean.AbstractNameQuery;
029 import org.apache.geronimo.gbean.GBeanData;
030 import org.apache.geronimo.gbean.GBeanInfo;
031 import org.apache.geronimo.gbean.GBeanInfoBuilder;
032 import org.apache.geronimo.j2ee.deployment.Module;
033 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
034 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035 import org.apache.geronimo.kernel.ClassLoading;
036 import org.apache.geronimo.kernel.GBeanNotFoundException;
037 import org.apache.geronimo.naming.reference.GBeanReference;
038 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefDocument;
039 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefType;
040 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
041 import org.apache.xmlbeans.QNameSet;
042 import org.apache.xmlbeans.XmlObject;
043
044 /**
045 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
046 */
047 public class GBeanRefBuilder extends AbstractNamingBuilder {
048 private static final QName GBEAN_REF_QNAME = GerGbeanRefDocument.type.getDocumentElementName();
049 private static final QNameSet GBEAN_REF_QNAME_SET = QNameSet.singleton(GBEAN_REF_QNAME);
050
051 public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
052 if (plan == null) {
053 return;
054 }
055 XmlObject[] gbeanRefsUntyped = plan.selectChildren(GBEAN_REF_QNAME_SET);
056 for (XmlObject gbeanRefUntyped : gbeanRefsUntyped) {
057 GerGbeanRefType gbeanRef = (GerGbeanRefType) gbeanRefUntyped.copy().changeType(GerGbeanRefType.type);
058 if (gbeanRef == null) {
059 throw new DeploymentException("Could not read gbeanRef " + gbeanRefUntyped + " as the correct xml type");
060 }
061 GerPatternType[] gbeanLocatorArray = gbeanRef.getPatternArray();
062
063 String[] interfaceTypesArray = gbeanRef.getRefTypeArray();
064 Set<String> interfaceTypes = new HashSet<String>(Arrays.asList(interfaceTypesArray));
065 Set<AbstractNameQuery> queries = new HashSet<AbstractNameQuery>();
066 for (GerPatternType patternType : gbeanLocatorArray) {
067 AbstractNameQuery abstractNameQuery = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, interfaceTypes);
068 queries.add(abstractNameQuery);
069 }
070
071 GBeanData gBeanData;
072 try {
073 gBeanData = module.getEarContext().getConfiguration().findGBeanData(queries);
074 } catch (GBeanNotFoundException e) {
075 throw new DeploymentException("Could not resolve reference at deploy time for queries " + queries, e);
076 }
077
078 if (interfaceTypes.isEmpty()) {
079 interfaceTypes.add(gBeanData.getGBeanInfo().getClassName());
080 }
081 ClassLoader cl = module.getEarContext().getClassLoader();
082 Class gBeanType;
083 try {
084 gBeanType = ClassLoading.loadClass(gBeanData.getGBeanInfo().getClassName(), cl);
085 } catch (ClassNotFoundException e) {
086 throw new DeploymentException("Cannot load GBean class", e);
087 }
088
089 String refName = gbeanRef.getRefName();
090
091 NamingBuilder.JNDI_KEY.get(componentContext).put(ENV + refName, new GBeanReference(module.getConfigId(), queries, gBeanType));
092
093 }
094 }
095
096 public QNameSet getSpecQNameSet() {
097 return QNameSet.EMPTY;
098 }
099
100 public QNameSet getPlanQNameSet() {
101 return GBEAN_REF_QNAME_SET;
102 }
103
104 public static final GBeanInfo GBEAN_INFO;
105
106 static {
107 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanRefBuilder.class, NameFactory.MODULE_BUILDER);
108
109 GBEAN_INFO = infoBuilder.getBeanInfo();
110 }
111
112 public static GBeanInfo getGBeanInfo() {
113 return GBEAN_INFO;
114 }
115
116 }