KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > deployment > GBeanRefBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.naming.deployment;
19
20 import java.util.Set JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.apache.xmlbeans.XmlObject;
28 import org.apache.xmlbeans.QNameSet;
29 import org.apache.geronimo.kernel.repository.Environment;
30 import org.apache.geronimo.kernel.config.Configuration;
31 import org.apache.geronimo.kernel.GBeanNotFoundException;
32 import org.apache.geronimo.kernel.ClassLoading;
33 import org.apache.geronimo.naming.reference.GBeanReference;
34 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefDocument;
35 import org.apache.geronimo.xbeans.geronimo.naming.GerGbeanRefType;
36 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
37 import org.apache.geronimo.common.DeploymentException;
38 import org.apache.geronimo.gbean.AbstractNameQuery;
39 import org.apache.geronimo.gbean.GBeanData;
40 import org.apache.geronimo.gbean.GBeanInfo;
41 import org.apache.geronimo.gbean.GBeanInfoBuilder;
42 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
43 import org.apache.geronimo.j2ee.deployment.Module;
44 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
45
46 /**
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class GBeanRefBuilder implements NamingBuilder {
50     private static final QName JavaDoc GBEAN_REF_QNAME = GerGbeanRefDocument.type.getDocumentElementName();
51     private static final QNameSet GBEAN_REF_QNAME_SET = QNameSet.singleton(GBEAN_REF_QNAME);
52
53     public void buildEnvironment(XmlObject specDD, XmlObject plan, Environment environment) {
54     }
55
56     public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException {
57     }
58
59     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
60         if (plan == null) {
61             return;
62         }
63         XmlObject[] gbeanRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GBEAN_REF_QNAME_SET);
64         for (int i = 0; i < gbeanRefsUntyped.length; i++) {
65             XmlObject gbeanRefUntyped = gbeanRefsUntyped[i];
66             GerGbeanRefType gbeanRef = (GerGbeanRefType) gbeanRefUntyped.copy().changeType(GerGbeanRefType.type);
67             if (gbeanRef == null) {
68                 throw new DeploymentException("Could not read gbeanRef " + gbeanRefUntyped + " as the correct xml type");
69             }
70             GerPatternType[] gbeanLocatorArray = gbeanRef.getPatternArray();
71
72             String JavaDoc[] interfaceTypesArray = gbeanRef.getRefTypeArray();
73             Set JavaDoc interfaceTypes = new HashSet JavaDoc(Arrays.asList(interfaceTypesArray));
74             Set JavaDoc queries = new HashSet JavaDoc();
75             for (int j = 0; j < gbeanLocatorArray.length; j++) {
76                 GerPatternType patternType = gbeanLocatorArray[j];
77                 AbstractNameQuery abstractNameQuery = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, interfaceTypes);
78                 queries.add(abstractNameQuery);
79             }
80
81             GBeanData gBeanData;
82             try {
83                 gBeanData = localConfiguration.findGBeanData(queries);
84             } catch (GBeanNotFoundException e) {
85                 throw new DeploymentException("Could not resolve reference at deploy time for queries " + queries, e);
86             }
87
88             if (interfaceTypes.isEmpty()) {
89                 interfaceTypes.add(gBeanData.getGBeanInfo().getClassName());
90             }
91             ClassLoader JavaDoc cl = module.getEarContext().getClassLoader();
92             Class JavaDoc gBeanType;
93             try {
94                 gBeanType = ClassLoading.loadClass(gBeanData.getGBeanInfo().getClassName(), cl);
95             } catch (ClassNotFoundException JavaDoc e) {
96                 throw new DeploymentException("Cannot load GBean class", e);
97             }
98
99             String JavaDoc refName = gbeanRef.getRefName();
100
101             ((Map JavaDoc)componentContext.get(JNDI_KEY)).put(ENV + refName, new GBeanReference(localConfiguration.getId(), queries, gBeanType));
102
103         }
104     }
105
106     public QNameSet getSpecQNameSet() {
107         return QNameSet.EMPTY;
108     }
109
110     public QNameSet getPlanQNameSet() {
111         return GBEAN_REF_QNAME_SET;
112     }
113
114     public static final GBeanInfo GBEAN_INFO;
115
116     static {
117         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanRefBuilder.class, NameFactory.MODULE_BUILDER);
118
119         GBEAN_INFO = infoBuilder.getBeanInfo();
120     }
121
122     public static GBeanInfo getGBeanInfo() {
123         return GBEAN_INFO;
124     }
125
126 }
127
Popular Tags