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.deployment.service;
20
21 import java.util.Map;
22 import java.util.HashMap;
23 import java.util.Collection;
24 import java.beans.PropertyEditorManager;
25
26 import javax.xml.namespace.QName;
27
28 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
29 import org.apache.geronimo.deployment.DeploymentContext;
30 import org.apache.geronimo.deployment.ConfigurationBuilder;
31 import org.apache.geronimo.deployment.xbeans.GbeanType;
32 import org.apache.geronimo.deployment.xbeans.AttributeType;
33 import org.apache.geronimo.deployment.xbeans.XmlAttributeType;
34 import org.apache.geronimo.deployment.xbeans.ReferenceType;
35 import org.apache.geronimo.deployment.xbeans.ReferencesType;
36 import org.apache.geronimo.deployment.xbeans.PatternType;
37 import org.apache.geronimo.deployment.xbeans.ServiceDocument;
38 import org.apache.geronimo.deployment.xbeans.GbeanDocument;
39 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
40 import org.apache.geronimo.common.DeploymentException;
41 import org.apache.geronimo.gbean.AbstractName;
42 import org.apache.geronimo.gbean.GBeanInfo;
43 import org.apache.geronimo.gbean.GBeanData;
44 import org.apache.geronimo.gbean.ReferenceMap;
45 import org.apache.geronimo.gbean.GBeanInfoBuilder;
46 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
47 import org.apache.geronimo.kernel.Kernel;
48 import org.apache.geronimo.kernel.repository.Environment;
49 import org.apache.geronimo.kernel.repository.Repository;
50 import org.apache.xmlbeans.XmlObject;
51 import org.apache.xmlbeans.QNameSet;
52
53 /**
54 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
55 */
56 public class GBeanBuilder implements NamespaceDrivenBuilder {
57 protected Map attrRefMap;
58 protected Map refRefMap;
59 private static final QName SERVICE_QNAME = ServiceDocument.type.getDocumentElementName();
60 private static final QName GBEAN_QNAME = GbeanDocument.type.getDocumentElementName();
61 private static final QNameSet GBEAN_QNAME_SET = QNameSet.singleton(GBEAN_QNAME);
62
63 public GBeanBuilder(Collection xmlAttributeBuilders, Collection xmlReferenceBuilders) {
64 if (xmlAttributeBuilders != null) {
65 ReferenceMap.Key key = new ReferenceMap.Key() {
66
67 public Object getKey(Object object) {
68 return ((XmlAttributeBuilder) object).getNamespace();
69 }
70 };
71 attrRefMap = new ReferenceMap(xmlAttributeBuilders, new HashMap(), key);
72 } else {
73 attrRefMap = new HashMap();
74 }
75
76 if (xmlReferenceBuilders != null) {
77 ReferenceMap.Key key = new ReferenceMap.Key() {
78
79 public Object getKey(Object object) {
80 return ((XmlReferenceBuilder) object).getNamespace();
81 }
82 };
83 refRefMap = new ReferenceMap(xmlReferenceBuilders, new HashMap(), key);
84 }
85 EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
86 attrRefMap.put(environmentBuilder.getNamespace(), environmentBuilder);
87 }
88
89 public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
90 }
91
92 public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
93 XmlObject[] items = container.selectChildren(GBEAN_QNAME_SET);
94 GbeanType[] gbeans = new GbeanType[items.length];
95 for (int i = 0; i < items.length; i++) {
96 XmlObject any = items[i];
97 gbeans[i] = (GbeanType) any.copy().changeType(GbeanType.type);
98 }
99 for (int i1 = 0; i1 < gbeans.length; i1++) {
100 addGBeanData(gbeans[i1], moduleContext.getModuleName(), moduleContext.getClassLoader(), moduleContext);
101 }
102 }
103
104 public String getNamespace() {
105 XmlBeansUtil.registerSubstitutionGroupElements(SERVICE_QNAME, QNameSet.singleton(GBEAN_QNAME));
106 return GBEAN_QNAME.getLocalPart();
107 }
108
109 private AbstractName addGBeanData(GbeanType gbean, AbstractName moduleName, ClassLoader cl, DeploymentContext context) throws DeploymentException {
110 GBeanInfo gBeanInfo = GBeanInfo.getGBeanInfo(gbean.getClass1(), cl);
111 String namePart = gbean.getName();
112 String j2eeType = gBeanInfo.getJ2eeType();
113 AbstractName abstractName = context.getNaming().createChildName(moduleName, namePart, j2eeType);
114 SingleGBeanBuilder builder = new SingleGBeanBuilder(abstractName, gBeanInfo, cl, context, moduleName, attrRefMap , refRefMap);
115
116
117 AttributeType[] attributeArray = gbean.getAttributeArray();
118 if (attributeArray != null) {
119 for (int j = 0; j < attributeArray.length; j++) {
120 builder.setAttribute(attributeArray[j].getName().trim(), attributeArray[j].getType(), attributeArray[j].getStringValue());
121 }
122 }
123
124 XmlAttributeType[] xmlAttributeArray = gbean.getXmlAttributeArray();
125 if (xmlAttributeArray != null) {
126 for (int i = 0; i < xmlAttributeArray.length; i++) {
127 XmlAttributeType xmlAttributeType = xmlAttributeArray[i];
128 String name = xmlAttributeType.getName().trim();
129 XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
130 if (anys.length != 1) {
131 throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
132 }
133 builder.setXmlAttribute(name, anys[0]);
134 }
135 }
136
137
138 ReferenceType[] referenceArray = gbean.getReferenceArray();
139 if (referenceArray != null) {
140 for (int j = 0; j < referenceArray.length; j++) {
141 builder.setReference(referenceArray[j].getName2(), referenceArray[j], moduleName);
142 }
143 }
144
145
146 ReferencesType[] referencesArray = gbean.getReferencesArray();
147 if (referencesArray != null) {
148 for (int j = 0; j < referencesArray.length; j++) {
149 builder.setReference(referencesArray[j].getName(), referencesArray[j].getPatternArray(), moduleName);
150 }
151 }
152
153 XmlAttributeType[] xmlReferenceArray = gbean.getXmlReferenceArray();
154 if (xmlReferenceArray != null) {
155 for (int i = 0; i < xmlReferenceArray.length; i++) {
156 XmlAttributeType xmlAttributeType = xmlReferenceArray[i];
157 String name = xmlAttributeType.getName().trim();
158 XmlObject[] anys = xmlAttributeType.selectChildren(XmlAttributeType.type.qnameSetForWildcardElements());
159 if (anys.length != 1) {
160 throw new DeploymentException("Unexpected count of xs:any elements in xml-attribute " + anys.length + " qnameset: " + XmlAttributeType.type.qnameSetForWildcardElements());
161 }
162 builder.setXmlReference(name, anys[0]);
163 }
164 }
165
166 PatternType[] dependencyArray = gbean.getDependencyArray();
167 if (dependencyArray != null) {
168 for (int i = 0; i < dependencyArray.length; i++) {
169 PatternType patternType = dependencyArray[i];
170 builder.addDependency(patternType);
171 }
172 }
173
174 GBeanData gbeanData = builder.getGBeanData();
175 try {
176 context.addGBean(gbeanData);
177 } catch (GBeanAlreadyExistsException e) {
178 throw new DeploymentException(e);
179 }
180 return abstractName;
181 }
182
183 public static final GBeanInfo GBEAN_INFO;
184
185 static {
186 PropertyEditorManager.registerEditor(Environment.class, EnvironmentBuilder.class);
187
188 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(GBeanBuilder.class, "ModuleBuilder");
189
190 infoBuilder.addInterface(NamespaceDrivenBuilder.class);
191
192 infoBuilder.addReference("XmlAttributeBuilders", XmlAttributeBuilder.class, "XmlAttributeBuilder");
193 infoBuilder.addReference("XmlReferenceBuilders", XmlReferenceBuilder.class, "XmlReferenceBuilder");
194
195 infoBuilder.setConstructor(new String[]{"XmlAttributeBuilders", "XmlReferenceBuilders"});
196
197 GBEAN_INFO = infoBuilder.getBeanInfo();
198 }
199
200 public static GBeanInfo getGBeanInfo() {
201 return GBEAN_INFO;
202 }
203
204 }