KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > xstream > GBeanDataConverter


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 package org.apache.geronimo.kernel.config.xstream;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.LinkedHashSet JavaDoc;
23 import java.util.LinkedHashMap JavaDoc;
24 import java.net.URI JavaDoc;
25
26 import com.thoughtworks.xstream.converters.Converter;
27 import com.thoughtworks.xstream.converters.MarshallingContext;
28 import com.thoughtworks.xstream.converters.UnmarshallingContext;
29 import com.thoughtworks.xstream.converters.ConversionException;
30 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
31 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
32 import com.thoughtworks.xstream.mapper.Mapper;
33 import org.apache.geronimo.gbean.AbstractName;
34 import org.apache.geronimo.gbean.GBeanData;
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.gbean.ReferencePatterns;
37
38 /**
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 public class GBeanDataConverter implements Converter {
42     private final Mapper mapper;
43
44     public GBeanDataConverter(Mapper mapper) {
45         this.mapper = mapper;
46     }
47
48     public boolean canConvert(Class JavaDoc clazz) {
49         return GBeanData.class.isAssignableFrom(clazz);
50     }
51
52     public void marshal(Object JavaDoc object, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
53         GBeanData gbeanData = (GBeanData) object;
54
55         // name
56
AbstractName abstractName = gbeanData.getAbstractName();
57         if (abstractName != null) {
58             writer.addAttribute("name", abstractName.toString());
59         }
60
61         // gbeanInfo
62
GBeanInfo gbeanInfo = gbeanData.getGBeanInfo();
63         String JavaDoc sourceClass = gbeanInfo.getSourceClass();
64         if (sourceClass != null) {
65             writer.addAttribute("sourceClass", sourceClass);
66         } else {
67             writer.startNode("gbean-info");
68             marshallingContext.convertAnother(gbeanInfo);
69             writer.endNode();
70         }
71
72         // dependencies Set<ReferencePatterns>
73
Set JavaDoc dependencies = gbeanData.getDependencies();
74         for (Iterator JavaDoc iterator = dependencies.iterator(); iterator.hasNext();) {
75             ReferencePatterns referencePatterns = (ReferencePatterns) iterator.next();
76             writer.startNode("dependency");
77             marshallingContext.convertAnother(referencePatterns);
78             writer.endNode();
79         }
80
81         // attributes Map<String, Object>
82
Map JavaDoc attributes = gbeanData.getAttributes();
83         for (Iterator JavaDoc iterator = attributes.entrySet().iterator(); iterator.hasNext();) {
84             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
85             String JavaDoc attributeName = (String JavaDoc) entry.getKey();
86             Object JavaDoc attributeValue = entry.getValue();
87             if (attributeValue != null) {
88                 writer.startNode("attribute");
89                 writer.addAttribute("name", attributeName);
90
91                 writer.startNode(mapper.serializedClass(attributeValue.getClass()));
92                 marshallingContext.convertAnother(attributeValue);
93                 writer.endNode();
94
95                 writer.endNode();
96             }
97         }
98         // references Map<String, ReferencePatterns>
99
Map JavaDoc references = gbeanData.getReferences();
100         for (Iterator JavaDoc iterator = references.entrySet().iterator(); iterator.hasNext();) {
101             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
102             String JavaDoc referenceName = (String JavaDoc) entry.getKey();
103             ReferencePatterns referencePatterns = (ReferencePatterns) entry.getValue();
104             writer.startNode("reference");
105             writer.addAttribute("name", referenceName);
106             marshallingContext.convertAnother(referencePatterns);
107             writer.endNode();
108         }
109     }
110
111     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
112         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
113         if (classLoader == null) {
114             classLoader = this.getClass().getClassLoader();
115         }
116
117         // name
118
String JavaDoc gbeanName = reader.getAttribute("name");
119         AbstractName abstractName = null;
120         if (gbeanName != null) {
121             abstractName = new AbstractName(URI.create(gbeanName));
122         }
123
124         // gbeanInfo
125
GBeanInfo gbeanInfo = null;
126         String JavaDoc sourceClass = reader.getAttribute("sourceClass");
127         if (sourceClass != null) {
128             gbeanInfo = GBeanInfo.getGBeanInfo(sourceClass, classLoader);
129         }
130
131         Set JavaDoc dependencies = new LinkedHashSet JavaDoc();
132         Map JavaDoc attributes = new LinkedHashMap JavaDoc();
133         Map JavaDoc references = new LinkedHashMap JavaDoc();
134         while (reader.hasMoreChildren()) {
135             reader.moveDown();
136
137             String JavaDoc nodeName = reader.getNodeName();
138             if (nodeName.equals("gbean-info")) {
139                 if (gbeanInfo != null) {
140                     throw new ConversionException("GBean info declared more than once in gbean " + abstractName);
141                 }
142                 gbeanInfo = (GBeanInfo) unmarshallingContext.convertAnother(reader, GBeanInfo.class);
143             } else if (nodeName.equals("dependency")) {
144                 ReferencePatterns referencePatterns = (ReferencePatterns) unmarshallingContext.convertAnother(reader, ReferencePatterns.class);
145                 dependencies.add(referencePatterns);
146             } else if (nodeName.equals("attribute")) {
147                 String JavaDoc attributeName = reader.getAttribute("name");
148
149                 reader.moveDown();
150                 String JavaDoc classAttribute = reader.getAttribute(mapper.attributeForImplementationClass());
151                 Class JavaDoc type;
152                 if (classAttribute == null) {
153                     type = mapper.realClass(reader.getNodeName());
154                 } else {
155                     type = mapper.realClass(classAttribute);
156                 }
157                 Object JavaDoc attributeValue = unmarshallingContext.convertAnother(reader, type);
158                 reader.moveUp();
159
160                 attributes.put(attributeName, attributeValue);
161             } else if (nodeName.equals("reference")) {
162                 String JavaDoc referenceName = reader.getAttribute("name");
163                 ReferencePatterns referencePatterns = (ReferencePatterns) unmarshallingContext.convertAnother(reader, ReferencePatterns.class);
164                 references.put(referenceName, referencePatterns);
165             } else {
166                 throw new ConversionException("Unknown nested node in GBean: " + nodeName);
167             }
168
169             reader.moveUp();
170         }
171
172         if (gbeanInfo == null) {
173             throw new ConversionException("GBean info not declared in gbean " + abstractName);
174         }
175
176         GBeanData gbeanData = new GBeanData(abstractName, gbeanInfo);
177         gbeanData.setDependencies(dependencies);
178         for (Iterator JavaDoc iterator = attributes.entrySet().iterator(); iterator.hasNext();) {
179             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
180             String JavaDoc attributeName = (String JavaDoc) entry.getKey();
181             Object JavaDoc attributeValue = entry.getValue();
182             gbeanData.setAttribute(attributeName, attributeValue);
183         }
184         for (Iterator JavaDoc iterator = references.entrySet().iterator(); iterator.hasNext();) {
185             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
186             String JavaDoc referenceName = (String JavaDoc) entry.getKey();
187             ReferencePatterns referencePatterns = (ReferencePatterns) entry.getValue();
188             gbeanData.setReferencePatterns(referenceName, referencePatterns);
189         }
190
191         return gbeanData;
192     }
193 }
194
Popular Tags