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