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