KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > SingleGBeanBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.deployment.service;
19
20 import java.beans.PropertyEditor JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 /**
44  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
45  */

46 public class SingleGBeanBuilder {
47     private final GBeanData gbean;
48     private final ClassLoader JavaDoc classLoader;
49     private final DeploymentContext context;
50     private final AbstractName moduleName;
51     private final Map JavaDoc xmlAttributeBuilderMap;
52     private final Map JavaDoc xmlReferenceBuilderMap;
53
54     SingleGBeanBuilder(AbstractName abstractName, GBeanInfo gBeanInfo, ClassLoader JavaDoc classLoader, DeploymentContext context, AbstractName moduleName, Map JavaDoc xmlAttributeBuilderMap, Map JavaDoc 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 JavaDoc name, String JavaDoc type, String JavaDoc text) throws DeploymentException {
65         if (text != null) {
66             text = text.trim(); // avoid formatting errors due to extra whitespace in XML configuration file
67
}
68         try {
69             // @todo we should not need all of common just for this
70
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 JavaDoc 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 JavaDoc value = editor.getValue();
84             gbean.setAttribute(name, value);
85         } catch (DeploymentException e) {
86             throw e;
87         } catch (ClassNotFoundException JavaDoc e) {
88             throw new DeploymentException("Unable to find PropertyEditor for " + type, e);
89         } catch (Exception JavaDoc e) {
90             throw new DeploymentException("Unable to set attribute " + name + " to " + text, e);
91         }
92     }
93
94     public void setXmlAttribute(String JavaDoc name, XmlObject xmlObject) throws DeploymentException {
95         String JavaDoc 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 JavaDoc type = attribute.getType();
105         Object JavaDoc value = builder.getValue(xmlObject, type, classLoader);
106         gbean.setAttribute(name, value);
107     }
108
109     public void setXmlReference(String JavaDoc name, XmlObject xmlObject) throws DeploymentException {
110         String JavaDoc 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 JavaDoc name, ReferenceType pattern, AbstractName parentName) throws DeploymentException {
122         setReference(name, new PatternType[]{pattern}, parentName);
123     }
124
125     public void setReference(String JavaDoc name, PatternType[] patterns, AbstractName parentName) throws DeploymentException {
126         Set JavaDoc patternNames = new HashSet JavaDoc(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 JavaDoc refName, PatternType pattern) throws DeploymentException {
139 // if (refName == null) {
140
// throw new DeploymentException("No type specified in dependency pattern " + pattern + " for gbean " + gbean.getName());
141
// }
142
assert refName != null;
143         GReferenceInfo referenceInfo = null;
144         Set JavaDoc referenceInfos = gbean.getGBeanInfo().getReferences();
145         for (Iterator JavaDoc iterator = referenceInfos.iterator(); iterator.hasNext();) {
146             GReferenceInfo testReferenceInfo = (GReferenceInfo) iterator.next();
147             String JavaDoc 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 JavaDoc groupId = pattern.isSetGroupId() ? pattern.getGroupId().trim() : null;
161         String JavaDoc artifactid = pattern.isSetArtifactId() ? pattern.getArtifactId().trim() : null;
162         String JavaDoc version = pattern.isSetVersion() ? pattern.getVersion().trim() : null;
163         String JavaDoc module = pattern.isSetModule() ? pattern.getModule().trim() : null;
164         String JavaDoc type = pattern.isSetType() ? pattern.getType().trim() : null;
165         String JavaDoc name = pattern.isSetName() ? pattern.getName().trim() : null;
166
167         Artifact artifact = artifactid != null? new Artifact(groupId, artifactid, version, "car"): null;
168         //get the type from the gbean info if not supplied explicitly
169
if (type == null && referenceInfo != null) {
170             type = referenceInfo.getNameTypeName();
171         }
172         Map JavaDoc nameMap = new HashMap JavaDoc();
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 JavaDoc 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