1 17 18 package org.apache.geronimo.deployment.service; 19 20 import java.beans.PropertyEditor ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.apache.geronimo.common.DeploymentException; 29 import org.apache.geronimo.common.propertyeditor.PropertyEditors; 30 import org.apache.geronimo.deployment.DeploymentContext; 31 import org.apache.geronimo.deployment.xbeans.PatternType; 32 import org.apache.geronimo.deployment.xbeans.ReferenceType; 33 import org.apache.geronimo.gbean.AbstractName; 34 import org.apache.geronimo.gbean.AbstractNameQuery; 35 import org.apache.geronimo.gbean.GAttributeInfo; 36 import org.apache.geronimo.gbean.GBeanData; 37 import org.apache.geronimo.gbean.GBeanInfo; 38 import org.apache.geronimo.gbean.GReferenceInfo; 39 import org.apache.geronimo.gbean.ReferencePatterns; 40 import org.apache.geronimo.kernel.repository.Artifact; 41 import org.apache.xmlbeans.XmlObject; 42 43 46 public class SingleGBeanBuilder { 47 private final GBeanData gbean; 48 private final ClassLoader classLoader; 49 private final DeploymentContext context; 50 private final AbstractName moduleName; 51 private final Map xmlAttributeBuilderMap; 52 private final Map xmlReferenceBuilderMap; 53 54 SingleGBeanBuilder(AbstractName abstractName, GBeanInfo gBeanInfo, ClassLoader classLoader, DeploymentContext context, AbstractName moduleName, Map xmlAttributeBuilderMap, Map xmlReferenceBuilderMap) { 55 56 this.classLoader = classLoader; 57 this.context = context; 58 this.moduleName = moduleName; 59 this.gbean = new GBeanData(abstractName, gBeanInfo); 60 this.xmlAttributeBuilderMap = xmlAttributeBuilderMap; 61 this.xmlReferenceBuilderMap = xmlReferenceBuilderMap; 62 } 63 64 public void setAttribute(String name, String type, String text) throws DeploymentException { 65 if (text != null) { 66 text = text.trim(); } 68 try { 69 if (type == null) { 71 GAttributeInfo attribute = gbean.getGBeanInfo().getAttribute(name); 72 if (attribute == null) { 73 throw new DeploymentException("Unknown attribute " + name + " on " + gbean.getAbstractName()); 74 } 75 type = attribute.getType(); 76 } 77 78 PropertyEditor editor = PropertyEditors.findEditor(type, classLoader); 79 if (editor == null) { 80 throw new DeploymentException("Unable to find PropertyEditor for " + type); 81 } 82 editor.setAsText(text); 83 Object value = editor.getValue(); 84 gbean.setAttribute(name, value); 85 } catch (DeploymentException e) { 86 throw e; 87 } catch (ClassNotFoundException e) { 88 throw new DeploymentException("Unable to find PropertyEditor for " + type, e); 89 } catch (Exception e) { 90 throw new DeploymentException("Unable to set attribute " + name + " to " + text, e); 91 } 92 } 93 94 public void setXmlAttribute(String name, XmlObject xmlObject) throws DeploymentException { 95 String namespace = xmlObject.getDomNode().getNamespaceURI(); 96 XmlAttributeBuilder builder = (XmlAttributeBuilder) xmlAttributeBuilderMap.get(namespace); 97 if (builder == null) { 98 throw new DeploymentException("No attribute builder deployed for namespace: " + namespace); 99 } 100 GAttributeInfo attribute = gbean.getGBeanInfo().getAttribute(name); 101 if (attribute == null) { 102 throw new DeploymentException("Unknown attribute " + name + " on " + gbean.getAbstractName()); 103 } 104 String type = attribute.getType(); 105 Object value = builder.getValue(xmlObject, type, classLoader); 106 gbean.setAttribute(name, value); 107 } 108 109 public void setXmlReference(String name, XmlObject xmlObject) throws DeploymentException { 110 String namespace = xmlObject.getDomNode().getNamespaceURI(); 111 XmlReferenceBuilder builder = (XmlReferenceBuilder) xmlReferenceBuilderMap.get(namespace); 112 if (builder == null) { 113 throw new DeploymentException("No reference builder deployed for namespace: " + namespace); 114 } 115 ReferencePatterns references = builder.getReferences(xmlObject, context, moduleName, classLoader); 116 if (references != null) { 117 gbean.setReferencePatterns(name, references); 118 } 119 } 120 121 public void setReference(String name, ReferenceType pattern, AbstractName parentName) throws DeploymentException { 122 setReference(name, new PatternType[]{pattern}, parentName); 123 } 124 125 public void setReference(String name, PatternType[] patterns, AbstractName parentName) throws DeploymentException { 126 Set patternNames = new HashSet (patterns.length); 127 for (int i = 0; i < patterns.length; i++) { 128 patternNames.add(buildAbstractNameQuery(name, patterns[i])); 129 } 130 gbean.setReferencePatterns(name, patternNames); 131 } 132 133 public void addDependency(PatternType patternType) throws DeploymentException { 134 AbstractNameQuery refInfo = buildAbstractNameQuery(patternType, null); 135 gbean.addDependency(refInfo); 136 } 137 138 private AbstractNameQuery buildAbstractNameQuery(String refName, PatternType pattern) throws DeploymentException { 139 assert refName != null; 143 GReferenceInfo referenceInfo = null; 144 Set referenceInfos = gbean.getGBeanInfo().getReferences(); 145 for (Iterator iterator = referenceInfos.iterator(); iterator.hasNext();) { 146 GReferenceInfo testReferenceInfo = (GReferenceInfo) iterator.next(); 147 String testRefName = testReferenceInfo.getName(); 148 if (testRefName.equals(refName)) { 149 referenceInfo = testReferenceInfo; 150 } 151 } 152 if (referenceInfo == null) { 153 throw new DeploymentException("No reference named " + refName + " in gbean " + gbean.getAbstractName()); 154 } 155 156 return buildAbstractNameQuery(pattern, referenceInfo); 157 } 158 159 public static AbstractNameQuery buildAbstractNameQuery(PatternType pattern, GReferenceInfo referenceInfo) { 160 String groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null; 161 String artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null; 162 String version = pattern.isSetVersion() ? pattern.getVersion().trim() : null; 163 String module = pattern.isSetModule() ? pattern.getModule().trim() : null; 164 String type = pattern.isSetType() ? pattern.getType().trim() : null; 165 String name = pattern.isSetName() ? pattern.getName().trim() : null; 166 167 Artifact artifact = artifactid != null? new Artifact(groupId, artifactid, version, "car"): null; 168 if (type == null && referenceInfo != null) { 170 type = referenceInfo.getNameTypeName(); 171 } 172 Map nameMap = new HashMap (); 173 if (name != null) { 174 nameMap.put("name", name); 175 } 176 if (type != null) { 177 nameMap.put("j2eeType", type); 178 } 179 if (module != null) { 180 nameMap.put("J2EEModule", module); 181 } 182 Set interfaceTypes = referenceInfo == null? null: Collections.singleton(referenceInfo.getReferenceType()); 183 return new AbstractNameQuery(artifact, nameMap, interfaceTypes); 184 } 185 186 public GBeanData getGBeanData() { 187 return gbean; 188 } 189 190 } 191 | Popular Tags |