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.deployment.service;
017:
018: import java.beans.PropertyEditorManager;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.apache.geronimo.common.DeploymentException;
026: import org.apache.geronimo.deployment.DeploymentContext;
027: import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
028: import org.apache.geronimo.deployment.xbeans.AttributeType;
029: import org.apache.geronimo.deployment.xbeans.GbeanDocument;
030: import org.apache.geronimo.deployment.xbeans.GbeanType;
031: import org.apache.geronimo.deployment.xbeans.PatternType;
032: import org.apache.geronimo.deployment.xbeans.ReferenceType;
033: import org.apache.geronimo.deployment.xbeans.ReferencesType;
034: import org.apache.geronimo.deployment.xbeans.ServiceDocument;
035: import org.apache.geronimo.deployment.xbeans.XmlAttributeType;
036: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
037: import org.apache.geronimo.gbean.AbstractName;
038: import org.apache.geronimo.gbean.GBeanData;
039: import org.apache.geronimo.gbean.GBeanInfo;
040: import org.apache.geronimo.gbean.GBeanInfoBuilder;
041: import org.apache.geronimo.gbean.ReferenceMap;
042: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
043: import org.apache.geronimo.kernel.repository.Environment;
044: import org.apache.xmlbeans.QNameSet;
045: import org.apache.xmlbeans.XmlException;
046: import org.apache.xmlbeans.XmlObject;
047:
048: /**
049: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
050: */
051: public class GBeanBuilder implements NamespaceDrivenBuilder {
052: protected Map attrRefMap;
053: protected Map refRefMap;
054: public static final QName SERVICE_QNAME = ServiceDocument.type
055: .getDocumentElementName();
056: private static final QName GBEAN_QNAME = GbeanDocument.type
057: .getDocumentElementName();
058: private static final QNameSet GBEAN_QNAME_SET = QNameSet
059: .singleton(GBEAN_QNAME);
060:
061: public GBeanBuilder(Collection xmlAttributeBuilders,
062: Collection xmlReferenceBuilders) {
063: if (xmlAttributeBuilders != null) {
064: ReferenceMap.Key key = new ReferenceMap.Key() {
065:
066: public Object getKey(Object object) {
067: return ((XmlAttributeBuilder) object)
068: .getNamespace();
069: }
070: };
071: attrRefMap = new ReferenceMap(xmlAttributeBuilders,
072: new HashMap(), key);
073: } else {
074: attrRefMap = new HashMap();
075: }
076:
077: if (xmlReferenceBuilders != null) {
078: ReferenceMap.Key key = new ReferenceMap.Key() {
079:
080: public Object getKey(Object object) {
081: return ((XmlReferenceBuilder) object)
082: .getNamespace();
083: }
084: };
085: refRefMap = new ReferenceMap(xmlReferenceBuilders,
086: new HashMap(), key);
087: }
088: EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
089: attrRefMap.put(environmentBuilder.getNamespace(),
090: environmentBuilder);
091: }
092:
093: public void buildEnvironment(XmlObject container,
094: Environment environment) throws DeploymentException {
095: }
096:
097: public void build(XmlObject container,
098: DeploymentContext applicationContext,
099: DeploymentContext moduleContext) throws DeploymentException {
100: XmlObject[] items = container.selectChildren(GBEAN_QNAME_SET);
101: for (int i = 0; i < items.length; i++) {
102: GbeanType gbean;
103: try {
104: gbean = (GbeanType) XmlBeansUtil.typedCopy(items[i],
105: GbeanType.type);
106: } catch (XmlException e) {
107: throw new DeploymentException(
108: "Could not validate gbean xml", e);
109: }
110: addGBeanData(gbean, moduleContext.getModuleName(),
111: moduleContext.getClassLoader(), moduleContext);
112: }
113: }
114:
115: private AbstractName addGBeanData(GbeanType gbean,
116: AbstractName moduleName, ClassLoader cl,
117: DeploymentContext context) throws DeploymentException {
118: GBeanInfo gBeanInfo = GBeanInfo.getGBeanInfo(gbean.getClass1(),
119: cl);
120: String namePart = gbean.getName();
121: String j2eeType = gBeanInfo.getJ2eeType();
122: AbstractName abstractName = context.getNaming()
123: .createChildName(moduleName, namePart, j2eeType);
124: SingleGBeanBuilder builder = new SingleGBeanBuilder(
125: abstractName, gBeanInfo, cl, context, moduleName,
126: attrRefMap, refRefMap);
127:
128: // set up attributes
129: AttributeType[] attributeArray = gbean.getAttributeArray();
130: if (attributeArray != null) {
131: for (int j = 0; j < attributeArray.length; j++) {
132: builder.setAttribute(
133: attributeArray[j].getName().trim(),
134: attributeArray[j].getType(), attributeArray[j]
135: .getStringValue());
136: }
137: }
138:
139: XmlAttributeType[] xmlAttributeArray = gbean
140: .getXmlAttributeArray();
141: if (xmlAttributeArray != null) {
142: for (int i = 0; i < xmlAttributeArray.length; i++) {
143: XmlAttributeType xmlAttributeType = xmlAttributeArray[i];
144: String name = xmlAttributeType.getName().trim();
145: XmlObject[] anys = xmlAttributeType
146: .selectChildren(XmlAttributeType.type
147: .qnameSetForWildcardElements());
148: if (anys.length != 1) {
149: throw new DeploymentException(
150: "Unexpected count of xs:any elements in xml-attribute "
151: + anys.length
152: + " qnameset: "
153: + XmlAttributeType.type
154: .qnameSetForWildcardElements());
155: }
156: builder.setXmlAttribute(name, anys[0]);
157: }
158: }
159:
160: // set up all single pattern references
161: ReferenceType[] referenceArray = gbean.getReferenceArray();
162: if (referenceArray != null) {
163: for (int j = 0; j < referenceArray.length; j++) {
164: builder.setReference(referenceArray[j].getName2(),
165: referenceArray[j], moduleName);
166: }
167: }
168:
169: // set up app multi-patterned references
170: ReferencesType[] referencesArray = gbean.getReferencesArray();
171: if (referencesArray != null) {
172: for (int j = 0; j < referencesArray.length; j++) {
173: builder.setReference(referencesArray[j].getName(),
174: referencesArray[j].getPatternArray(),
175: moduleName);
176: }
177: }
178:
179: XmlAttributeType[] xmlReferenceArray = gbean
180: .getXmlReferenceArray();
181: if (xmlReferenceArray != null) {
182: for (int i = 0; i < xmlReferenceArray.length; i++) {
183: XmlAttributeType xmlAttributeType = xmlReferenceArray[i];
184: String name = xmlAttributeType.getName().trim();
185: XmlObject[] anys = xmlAttributeType
186: .selectChildren(XmlAttributeType.type
187: .qnameSetForWildcardElements());
188: if (anys.length != 1) {
189: throw new DeploymentException(
190: "Unexpected count of xs:any elements in xml-attribute "
191: + anys.length
192: + " qnameset: "
193: + XmlAttributeType.type
194: .qnameSetForWildcardElements());
195: }
196: builder.setXmlReference(name, anys[0]);
197: }
198: }
199:
200: PatternType[] dependencyArray = gbean.getDependencyArray();
201: if (dependencyArray != null) {
202: for (int i = 0; i < dependencyArray.length; i++) {
203: PatternType patternType = dependencyArray[i];
204: builder.addDependency(patternType);
205: }
206: }
207:
208: GBeanData gbeanData = builder.getGBeanData();
209: try {
210: context.addGBean(gbeanData);
211: } catch (GBeanAlreadyExistsException e) {
212: throw new DeploymentException(e);
213: }
214: return abstractName;
215: }
216:
217: public QNameSet getSpecQNameSet() {
218: return QNameSet.EMPTY;
219: }
220:
221: public QNameSet getPlanQNameSet() {
222: return GBEAN_QNAME_SET;
223: }
224:
225: public static final GBeanInfo GBEAN_INFO;
226:
227: static {
228: PropertyEditorManager.registerEditor(Environment.class,
229: EnvironmentBuilder.class);
230:
231: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
232: GBeanBuilder.class, "ModuleBuilder");
233:
234: infoBuilder.addInterface(NamespaceDrivenBuilder.class);
235:
236: infoBuilder.addReference("XmlAttributeBuilders",
237: XmlAttributeBuilder.class, "XmlAttributeBuilder");
238: infoBuilder.addReference("XmlReferenceBuilders",
239: XmlReferenceBuilder.class, "XmlReferenceBuilder");
240:
241: infoBuilder.setConstructor(new String[] {
242: "XmlAttributeBuilders", "XmlReferenceBuilders" });
243:
244: GBEAN_INFO = infoBuilder.getBeanInfo();
245: }
246:
247: public static GBeanInfo getGBeanInfo() {
248: return GBEAN_INFO;
249: }
250:
251: }
|