001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.naming.deployment; 020 021 import java.util.Set; 022 import java.util.HashSet; 023 import java.util.Arrays; 024 import java.util.Map; 025 026 import javax.xml.namespace.QName; 027 028 import org.apache.xmlbeans.XmlObject; 029 import org.apache.xmlbeans.QNameSet; 030 import org.apache.geronimo.kernel.repository.Environment; 031 import org.apache.geronimo.kernel.config.Configuration; 032 import org.apache.geronimo.kernel.GBeanNotFoundException; 033 import org.apache.geronimo.kernel.ClassLoading; 034 import org.apache.geronimo.naming.reference.GBeanReference; 035 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefDocument; 036 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefType; 037 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 038 import org.apache.geronimo.common.DeploymentException; 039 import org.apache.geronimo.gbean.AbstractNameQuery; 040 import org.apache.geronimo.gbean.GBeanData; 041 import org.apache.geronimo.gbean.GBeanInfo; 042 import org.apache.geronimo.gbean.GBeanInfoBuilder; 043 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 044 import org.apache.geronimo.j2ee.deployment.Module; 045 import org.apache.geronimo.j2ee.deployment.NamingBuilder; 046 047 /** 048 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $ 049 */ 050 public class GBeanRefBuilder implements NamingBuilder { 051 private static final QName GBEAN_REF_QNAME = GerGbeanRefDocument.type.getDocumentElementName(); 052 private static final QNameSet GBEAN_REF_QNAME_SET = QNameSet.singleton(GBEAN_REF_QNAME); 053 054 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) { 055 } 056 057 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 058 } 059 060 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException { 061 if (plan == null) { 062 return; 063 } 064 XmlObject[] gbeanRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GBEAN_REF_QNAME_SET); 065 for (int i = 0; i < gbeanRefsUntyped.length; i++) { 066 XmlObject gbeanRefUntyped = gbeanRefsUntyped[i]; 067 GerGbeanRefType gbeanRef = (GerGbeanRefType) gbeanRefUntyped.copy().changeType(GerGbeanRefType.type); 068 if (gbeanRef == null) { 069 throw new DeploymentException("Could not read gbeanRef " + gbeanRefUntyped + " as the correct xml type"); 070 } 071 GerPatternType[] gbeanLocatorArray = gbeanRef.getPatternArray(); 072 073 String[] interfaceTypesArray = gbeanRef.getRefTypeArray(); 074 Set interfaceTypes = new HashSet(Arrays.asList(interfaceTypesArray)); 075 Set queries = new HashSet(); 076 for (int j = 0; j < gbeanLocatorArray.length; j++) { 077 GerPatternType patternType = gbeanLocatorArray[j]; 078 AbstractNameQuery abstractNameQuery = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, interfaceTypes); 079 queries.add(abstractNameQuery); 080 } 081 082 GBeanData gBeanData; 083 try { 084 gBeanData = localConfiguration.findGBeanData(queries); 085 } catch (GBeanNotFoundException e) { 086 throw new DeploymentException("Could not resolve reference at deploy time for queries " + queries, e); 087 } 088 089 if (interfaceTypes.isEmpty()) { 090 interfaceTypes.add(gBeanData.getGBeanInfo().getClassName()); 091 } 092 ClassLoader cl = module.getEarContext().getClassLoader(); 093 Class gBeanType; 094 try { 095 gBeanType = ClassLoading.loadClass(gBeanData.getGBeanInfo().getClassName(), cl); 096 } catch (ClassNotFoundException e) { 097 throw new DeploymentException("Cannot load GBean class", e); 098 } 099 100 String refName = gbeanRef.getRefName(); 101 102 ((Map)componentContext.get(JNDI_KEY)).put(ENV + refName, new GBeanReference(localConfiguration.getId(), queries, gBeanType)); 103 104 } 105 } 106 107 public QNameSet getSpecQNameSet() { 108 return QNameSet.EMPTY; 109 } 110 111 public QNameSet getPlanQNameSet() { 112 return GBEAN_REF_QNAME_SET; 113 } 114 115 public static final GBeanInfo GBEAN_INFO; 116 117 static { 118 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanRefBuilder.class, NameFactory.MODULE_BUILDER); 119 120 GBEAN_INFO = infoBuilder.getBeanInfo(); 121 } 122 123 public static GBeanInfo getGBeanInfo() { 124 return GBEAN_INFO; 125 } 126 127 }