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.PropertyEditor;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.geronimo.common.DeploymentException;
027: import org.apache.geronimo.common.propertyeditor.PropertyEditors;
028: import org.apache.geronimo.deployment.DeploymentContext;
029: import org.apache.geronimo.deployment.xbeans.PatternType;
030: import org.apache.geronimo.deployment.xbeans.ReferenceType;
031: import org.apache.geronimo.gbean.AbstractName;
032: import org.apache.geronimo.gbean.AbstractNameQuery;
033: import org.apache.geronimo.gbean.GAttributeInfo;
034: import org.apache.geronimo.gbean.GBeanData;
035: import org.apache.geronimo.gbean.GBeanInfo;
036: import org.apache.geronimo.gbean.GReferenceInfo;
037: import org.apache.geronimo.gbean.ReferencePatterns;
038: import org.apache.geronimo.kernel.repository.Artifact;
039: import org.apache.xmlbeans.XmlObject;
040:
041: /**
042: * @version $Rev: 549098 $ $Date: 2007-06-20 05:58:51 -0700 (Wed, 20 Jun 2007) $
043: */
044: public class SingleGBeanBuilder {
045: private final GBeanData gbean;
046: private final ClassLoader classLoader;
047: private final DeploymentContext context;
048: private final AbstractName moduleName;
049: private final Map xmlAttributeBuilderMap;
050: private final Map xmlReferenceBuilderMap;
051:
052: SingleGBeanBuilder(AbstractName abstractName, GBeanInfo gBeanInfo,
053: ClassLoader classLoader, DeploymentContext context,
054: AbstractName moduleName, Map xmlAttributeBuilderMap,
055: Map xmlReferenceBuilderMap) {
056:
057: this .classLoader = classLoader;
058: this .context = context;
059: this .moduleName = moduleName;
060: this .gbean = new GBeanData(abstractName, gBeanInfo);
061: this .xmlAttributeBuilderMap = xmlAttributeBuilderMap;
062: this .xmlReferenceBuilderMap = xmlReferenceBuilderMap;
063: }
064:
065: public void setAttribute(String name, String type, String text)
066: throws DeploymentException {
067: if (text != null) {
068: text = text.trim(); // avoid formatting errors due to extra whitespace in XML configuration file
069: }
070: try {
071: // @todo we should not need all of common just for this
072: if (type == null) {
073: GAttributeInfo attribute = gbean.getGBeanInfo()
074: .getAttribute(name);
075: if (attribute == null) {
076: throw new DeploymentException("Unknown attribute "
077: + name + " on " + gbean.getAbstractName());
078: }
079: type = attribute.getType();
080: }
081:
082: PropertyEditor editor = PropertyEditors.findEditor(type,
083: classLoader);
084: if (editor == null) {
085: throw new DeploymentException(
086: "Unable to find PropertyEditor for " + type);
087: }
088: editor.setAsText(text);
089: Object value = editor.getValue();
090: gbean.setAttribute(name, value);
091: } catch (DeploymentException e) {
092: throw e;
093: } catch (ClassNotFoundException e) {
094: throw new DeploymentException(
095: "Unable to find PropertyEditor for " + type, e);
096: } catch (Exception e) {
097: throw new DeploymentException("Unable to set attribute "
098: + name + " to " + text, e);
099: }
100: }
101:
102: public void setXmlAttribute(String name, XmlObject xmlObject)
103: throws DeploymentException {
104: String namespace = xmlObject.getDomNode().getNamespaceURI();
105: XmlAttributeBuilder builder = (XmlAttributeBuilder) xmlAttributeBuilderMap
106: .get(namespace);
107: if (builder == null) {
108: throw new DeploymentException(
109: "No attribute builder deployed for namespace: "
110: + namespace);
111: }
112: GAttributeInfo attribute = gbean.getGBeanInfo().getAttribute(
113: name);
114: if (attribute == null) {
115: throw new DeploymentException("Unknown attribute " + name
116: + " on " + gbean.getAbstractName());
117: }
118: String type = attribute.getType();
119: Object value = builder.getValue(xmlObject, type, classLoader);
120: gbean.setAttribute(name, value);
121: }
122:
123: public void setXmlReference(String name, XmlObject xmlObject)
124: throws DeploymentException {
125: String namespace = xmlObject.getDomNode().getNamespaceURI();
126: XmlReferenceBuilder builder = (XmlReferenceBuilder) xmlReferenceBuilderMap
127: .get(namespace);
128: if (builder == null) {
129: throw new DeploymentException(
130: "No reference builder deployed for namespace: "
131: + namespace);
132: }
133: ReferencePatterns references = builder.getReferences(xmlObject,
134: context, moduleName, classLoader);
135: if (references != null) {
136: gbean.setReferencePatterns(name, references);
137: }
138: }
139:
140: public void setReference(String name, ReferenceType pattern,
141: AbstractName parentName) throws DeploymentException {
142: setReference(name, new PatternType[] { pattern }, parentName);
143: }
144:
145: public void setReference(String name, PatternType[] patterns,
146: AbstractName parentName) throws DeploymentException {
147: Set patternNames = new HashSet(patterns.length);
148: for (int i = 0; i < patterns.length; i++) {
149: patternNames.add(buildAbstractNameQuery(name, patterns[i]));
150: }
151: gbean.setReferencePatterns(name, patternNames);
152: }
153:
154: public void addDependency(PatternType patternType)
155: throws DeploymentException {
156: AbstractNameQuery refInfo = buildAbstractNameQuery(patternType,
157: null);
158: gbean.addDependency(refInfo);
159: }
160:
161: private AbstractNameQuery buildAbstractNameQuery(String refName,
162: PatternType pattern) throws DeploymentException {
163: // if (refName == null) {
164: // throw new DeploymentException("No type specified in dependency pattern " + pattern + " for gbean " + gbean.getName());
165: // }
166: assert refName != null;
167: GReferenceInfo referenceInfo = null;
168: Set referenceInfos = gbean.getGBeanInfo().getReferences();
169: for (Iterator iterator = referenceInfos.iterator(); iterator
170: .hasNext();) {
171: GReferenceInfo testReferenceInfo = (GReferenceInfo) iterator
172: .next();
173: String testRefName = testReferenceInfo.getName();
174: if (testRefName.equals(refName)) {
175: referenceInfo = testReferenceInfo;
176: }
177: }
178: if (referenceInfo == null) {
179: throw new DeploymentException("No reference named "
180: + refName + " in gbean " + gbean.getAbstractName());
181: }
182:
183: return buildAbstractNameQuery(pattern, referenceInfo);
184: }
185:
186: public static AbstractNameQuery buildAbstractNameQuery(
187: PatternType pattern, GReferenceInfo referenceInfo) {
188: String nameTypeName = referenceInfo == null ? null
189: : referenceInfo.getNameTypeName();
190: Set interfaceTypes = referenceInfo == null ? null : Collections
191: .singleton(referenceInfo.getReferenceType());
192: return buildAbstractNameQuery(pattern, nameTypeName,
193: interfaceTypes);
194: }
195:
196: public static AbstractNameQuery buildAbstractNameQuery(
197: PatternType pattern, String nameTypeName, Set interfaceTypes) {
198: String groupId = pattern.isSetGroupId() ? pattern.getGroupId()
199: .trim() : null;
200: String artifactid = pattern.isSetArtifactId() ? pattern
201: .getArtifactId().trim() : null;
202: String version = pattern.isSetVersion() ? pattern.getVersion()
203: .trim() : null;
204: String module = pattern.isSetModule() ? pattern.getModule()
205: .trim() : null;
206: String type = pattern.isSetType() ? pattern.getType().trim()
207: : null;
208: String name = pattern.isSetName() ? pattern.getName().trim()
209: : null;
210:
211: Artifact artifact = artifactid != null ? new Artifact(groupId,
212: artifactid, version, "car") : null;
213: //get the type from the gbean info if not supplied explicitly
214: if (type == null) {
215: type = nameTypeName;
216: }
217: Map nameMap = new HashMap();
218: if (name != null) {
219: nameMap.put("name", name);
220: }
221: if (type != null) {
222: nameMap.put("j2eeType", type);
223: }
224: if (module != null) {
225: nameMap.put("J2EEModule", module);
226: }
227: return new AbstractNameQuery(artifact, nameMap, interfaceTypes);
228: }
229:
230: public GBeanData getGBeanData() {
231: return gbean;
232: }
233:
234: }
|