1 17 18 package org.apache.geronimo.deployment.service; 19 20 import java.beans.PropertyEditorManager ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.xml.namespace.QName ; 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 53 public class GBeanBuilder implements NamespaceDrivenBuilder { 54 protected Map attrRefMap; 55 protected Map refRefMap; 56 public static final QName SERVICE_QNAME = ServiceDocument.type.getDocumentElementName(); 57 private static final QName GBEAN_QNAME = GbeanDocument.type.getDocumentElementName(); 58 private static final QNameSet GBEAN_QNAME_SET = QNameSet.singleton(GBEAN_QNAME); 59 60 public GBeanBuilder(Collection xmlAttributeBuilders, Collection xmlReferenceBuilders) { 61 if (xmlAttributeBuilders != null) { 62 ReferenceMap.Key key = new ReferenceMap.Key() { 63 64 public Object getKey(Object object) { 65 return ((XmlAttributeBuilder) object).getNamespace(); 66 } 67 }; 68 attrRefMap = new ReferenceMap(xmlAttributeBuilders, new HashMap (), key); 69 } else { 70 attrRefMap = new HashMap (); 71 } 72 73 if (xmlReferenceBuilders != null) { 74 ReferenceMap.Key key = new ReferenceMap.Key() { 75 76 public Object getKey(Object object) { 77 return ((XmlReferenceBuilder) object).getNamespace(); 78 } 79 }; 80 refRefMap = new ReferenceMap(xmlReferenceBuilders, new HashMap (), 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 cl, DeploymentContext context) throws DeploymentException { 103 GBeanInfo gBeanInfo = GBeanInfo.getGBeanInfo(gbean.getClass1(), cl); 104 String namePart = gbean.getName(); 105 String 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 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 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 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 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 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 []{"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 |