1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.naming.deployment;
20
21 import java.util.Set;
22 import java.util.HashSet;
23 import java.util.Arrays;
24 import java.util.Map;
25
26 import javax.xml.namespace.QName;
27
28 import org.apache.xmlbeans.XmlObject;
29 import org.apache.xmlbeans.QNameSet;
30 import org.apache.geronimo.kernel.repository.Environment;
31 import org.apache.geronimo.kernel.config.Configuration;
32 import org.apache.geronimo.kernel.GBeanNotFoundException;
33 import org.apache.geronimo.kernel.ClassLoading;
34 import org.apache.geronimo.naming.reference.GBeanReference;
35 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefDocument;
36 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefType;
37 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
38 import org.apache.geronimo.common.DeploymentException;
39 import org.apache.geronimo.gbean.AbstractNameQuery;
40 import org.apache.geronimo.gbean.GBeanData;
41 import org.apache.geronimo.gbean.GBeanInfo;
42 import org.apache.geronimo.gbean.GBeanInfoBuilder;
43 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
44 import org.apache.geronimo.j2ee.deployment.Module;
45 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
46
47 /**
48 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
49 */
50 public class GBeanRefBuilder implements NamingBuilder {
51 private static final QName GBEAN_REF_QNAME = GerGbeanRefDocument.type.getDocumentElementName();
52 private static final QNameSet GBEAN_REF_QNAME_SET = QNameSet.singleton(GBEAN_REF_QNAME);
53
54 public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
55 }
56
57 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
58 }
59
60 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException {
61 if (plan == null) {
62 return;
63 }
64 XmlObject[] gbeanRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GBEAN_REF_QNAME_SET);
65 for (int i = 0; i < gbeanRefsUntyped.length; i++) {
66 XmlObject gbeanRefUntyped = gbeanRefsUntyped[i];
67 GerGbeanRefType gbeanRef = (GerGbeanRefType) gbeanRefUntyped.copy().changeType(GerGbeanRefType.type);
68 if (gbeanRef == null) {
69 throw new DeploymentException("Could not read gbeanRef " + gbeanRefUntyped + " as the correct xml type");
70 }
71 GerPatternType[] gbeanLocatorArray = gbeanRef.getPatternArray();
72
73 String[] interfaceTypesArray = gbeanRef.getRefTypeArray();
74 Set interfaceTypes = new HashSet(Arrays.asList(interfaceTypesArray));
75 Set queries = new HashSet();
76 for (int j = 0; j < gbeanLocatorArray.length; j++) {
77 GerPatternType patternType = gbeanLocatorArray[j];
78 AbstractNameQuery abstractNameQuery = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, interfaceTypes);
79 queries.add(abstractNameQuery);
80 }
81
82 GBeanData gBeanData;
83 try {
84 gBeanData = localConfiguration.findGBeanData(queries);
85 } catch (GBeanNotFoundException e) {
86 throw new DeploymentException("Could not resolve reference at deploy time for queries " + queries, e);
87 }
88
89 if (interfaceTypes.isEmpty()) {
90 interfaceTypes.add(gBeanData.getGBeanInfo().getClassName());
91 }
92 ClassLoader cl = module.getEarContext().getClassLoader();
93 Class gBeanType;
94 try {
95 gBeanType = ClassLoading.loadClass(gBeanData.getGBeanInfo().getClassName(), cl);
96 } catch (ClassNotFoundException e) {
97 throw new DeploymentException("Cannot load GBean class", e);
98 }
99
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 }