KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > GBeanBuilder


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.deployment.service;
19
20 import java.beans.PropertyEditorManager JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.apache.geronimo.common.DeploymentException;
28 import org.apache.geronimo.deployment.DeploymentContext;
29 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
30 import org.apache.geronimo.deployment.xbeans.AttributeType;
31 import org.apache.geronimo.deployment.xbeans.GbeanDocument;
32 import org.apache.geronimo.deployment.xbeans.GbeanType;
33 import org.apache.geronimo.deployment.xbeans.PatternType;
34 import org.apache.geronimo.deployment.xbeans.ReferenceType;
35 import org.apache.geronimo.deployment.xbeans.ReferencesType;
36 import org.apache.geronimo.deployment.xbeans.ServiceDocument;
37 import org.apache.geronimo.deployment.xbeans.XmlAttributeType;
38 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
39 import org.apache.geronimo.gbean.AbstractName;
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.gbean.ReferenceMap;
44 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
45 import org.apache.geronimo.kernel.repository.Environment;
46 import org.apache.xmlbeans.QNameSet;
47 import org.apache.xmlbeans.XmlException;
48 import org.apache.xmlbeans.XmlObject;
49
50 /**
51  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
52  */

53 public class GBeanBuilder implements NamespaceDrivenBuilder {
54     protected Map JavaDoc attrRefMap;
55     protected Map JavaDoc refRefMap;
56     public static final QName JavaDoc SERVICE_QNAME = ServiceDocument.type.getDocumentElementName();
57     private static final QName JavaDoc GBEAN_QNAME = GbeanDocument.type.getDocumentElementName();
58     private static final QNameSet GBEAN_QNAME_SET = QNameSet.singleton(GBEAN_QNAME);
59
60     public GBeanBuilder(Collection JavaDoc xmlAttributeBuilders, Collection JavaDoc xmlReferenceBuilders) {
61         if (xmlAttributeBuilders != null) {
62             ReferenceMap.Key key = new ReferenceMap.Key() {
63
64                 public Object JavaDoc getKey(Object JavaDoc object) {
65                     return ((XmlAttributeBuilder) object).getNamespace();
66                 }
67             };
68             attrRefMap = new ReferenceMap(xmlAttributeBuilders, new HashMap JavaDoc(), key);
69         } else {
70             attrRefMap = new HashMap JavaDoc();
71         }
72
73         if (xmlReferenceBuilders != null) {
74             ReferenceMap.Key key = new ReferenceMap.Key() {
75
76                 public Object JavaDoc getKey(Object JavaDoc object) {
77                     return ((XmlReferenceBuilder) object).getNamespace();
78                 }
79             };
80             refRefMap = new ReferenceMap(xmlReferenceBuilders, new HashMap JavaDoc(), key);
81         }
82         EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
83         attrRefMap.put(environmentBuilder.getNamespace(), environmentBuilder);
84     }
85
86     public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
87     }
88
89     public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
90         XmlObject[] items = container.selectChildren(GBEAN_QNAME_SET);
91         for (int i = 0; i < items.length; i++) {
92             GbeanType gbean;
93             try {
94                 gbean = (GbeanType) XmlBeansUtil.typedCopy(items[i], GbeanType.type);
95             } catch (XmlException e) {
96                 throw new DeploymentException("Could not validate gbean xml", e);
97             }
98             addGBeanData(gbean, moduleContext.getModuleName(), moduleContext.getClassLoader(), moduleContext);
99         }
100     }
101
102     private AbstractName addGBeanData(GbeanType gbean, AbstractName moduleName, ClassLoader JavaDoc cl, DeploymentContext context) throws DeploymentException {
103         GBeanInfo gBeanInfo = GBeanInfo.getGBeanInfo(gbean.getClass1(), cl);
104         String JavaDoc namePart = gbean.getName();
105         String JavaDoc j2eeType = gBeanInfo.getJ2eeType();
106         AbstractName abstractName = context.getNaming().createChildName(moduleName, namePart, j2eeType);
107         SingleGBeanBuilder builder = new SingleGBeanBuilder(abstractName, gBeanInfo, cl, context, moduleName, attrRefMap , refRefMap);
108
109         // set up attributes
110
AttributeType[] attributeArray = gbean.getAttributeArray();
111         if (attributeArray != null) {
112             for (int j = 0; j < attributeArray.length; j++) {
113                 builder.setAttribute(attributeArray[j].getName().trim(), attributeArray[j].getType(), attributeArray[j].getStringValue());
114             }
115         }
116
117         XmlAttributeType[] xmlAttributeArray = gbean.getXmlAttributeArray();
118         if (xmlAttributeArray != null) {
119             for (int i = 0; i < xmlAttributeArray.length; i++) {
120                 XmlAttributeType xmlAttributeType = xmlAttributeArray[i];
121                 String JavaDoc name = xmlAttributeType.getName().trim();
122                 XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
123                 if (anys.length != 1) {
124                     throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
125                 }
126                 builder.setXmlAttribute(name, anys[0]);
127             }
128         }
129
130         // set up all single pattern references
131
ReferenceType[] referenceArray = gbean.getReferenceArray();
132         if (referenceArray != null) {
133             for (int j = 0; j < referenceArray.length; j++) {
134                 builder.setReference(referenceArray[j].getName2(), referenceArray[j], moduleName);
135             }
136         }
137
138         // set up app multi-patterned references
139
ReferencesType[] referencesArray = gbean.getReferencesArray();
140         if (referencesArray != null) {
141             for (int j = 0; j < referencesArray.length; j++) {
142                 builder.setReference(referencesArray[j].getName(), referencesArray[j].getPatternArray(), moduleName);
143             }
144         }
145
146         XmlAttributeType[] xmlReferenceArray = gbean.getXmlReferenceArray();
147         if (xmlReferenceArray != null) {
148             for (int i = 0; i < xmlReferenceArray.length; i++) {
149                 XmlAttributeType xmlAttributeType = xmlReferenceArray[i];
150                 String JavaDoc name = xmlAttributeType.getName().trim();
151                 XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
152                 if (anys.length != 1) {
153                     throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
154                 }
155                 builder.setXmlReference(name, anys[0]);
156             }
157         }
158
159         PatternType[] dependencyArray = gbean.getDependencyArray();
160         if (dependencyArray != null) {
161             for (int i = 0; i < dependencyArray.length; i++) {
162                 PatternType patternType = dependencyArray[i];
163                 builder.addDependency(patternType);
164             }
165         }
166
167         GBeanData gbeanData = builder.getGBeanData();
168         try {
169             context.addGBean(gbeanData);
170         } catch (GBeanAlreadyExistsException e) {
171             throw new DeploymentException(e);
172         }
173         return abstractName;
174     }
175
176     public QNameSet getSpecQNameSet() {
177         return QNameSet.EMPTY;
178     }
179
180     public QNameSet getPlanQNameSet() {
181         return GBEAN_QNAME_SET;
182     }
183
184     public static final GBeanInfo GBEAN_INFO;
185
186     static {
187         PropertyEditorManager.registerEditor(Environment.class, EnvironmentBuilder.class);
188
189         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanBuilder.class, "ModuleBuilder");
190
191         infoBuilder.addInterface(NamespaceDrivenBuilder.class);
192
193         infoBuilder.addReference("XmlAttributeBuilders", XmlAttributeBuilder.class, "XmlAttributeBuilder");
194         infoBuilder.addReference("XmlReferenceBuilders", XmlReferenceBuilder.class, "XmlReferenceBuilder");
195
196         infoBuilder.setConstructor(new String JavaDoc[]{"XmlAttributeBuilders", "XmlReferenceBuilders"});
197
198         GBEAN_INFO = infoBuilder.getBeanInfo();
199     }
200
201     public static GBeanInfo getGBeanInfo() {
202         return GBEAN_INFO;
203     }
204
205 }
206
Popular Tags