KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import com.thoughtworks.xstream.XStream;
30 import com.thoughtworks.xstream.io.xml.DomReader;
31 import com.thoughtworks.xstream.io.xml.DomWriter;
32 import org.apache.geronimo.gbean.AbstractName;
33 import org.apache.geronimo.gbean.GBeanData;
34 import org.apache.geronimo.gbean.GBeanInfo;
35 import org.apache.geronimo.kernel.Naming;
36 import org.apache.geronimo.kernel.util.XmlUtil;
37 import org.apache.geronimo.kernel.config.InvalidConfigException;
38 import org.apache.geronimo.kernel.config.GBeanState;
39 import org.apache.geronimo.kernel.repository.Environment;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
45  */

46 public class XStreamGBeanState implements GBeanState {
47     /**
48      * GBeans contained in this configuration.
49      */

50     private final List JavaDoc gbeans = new ArrayList JavaDoc();
51
52     /**
53      * The serialized form of the gbeans. Once this is set on more gbeans can be added.
54      */

55     private Element JavaDoc gbeanState;
56
57     public XStreamGBeanState(Element JavaDoc gbeanState) {
58         this.gbeanState = gbeanState;
59     }
60
61     public XStreamGBeanState(Collection JavaDoc gbeans) {
62         if (gbeans != null){
63             this.gbeans.addAll(gbeans);
64         }
65     }
66
67     public Element JavaDoc getGBeanState() throws IOException JavaDoc {
68         if (gbeanState == null) {
69             gbeanState = XStreamGBeanState.storeGBeans(gbeans);
70             gbeans.clear();
71         }
72         return gbeanState;
73     }
74
75     public List JavaDoc getGBeans(ClassLoader JavaDoc classLoader) throws InvalidConfigException {
76         if (gbeanState == null) {
77             return Collections.unmodifiableList(gbeans);
78         }
79         gbeans.addAll(XStreamGBeanState.loadGBeans(gbeanState, classLoader));
80         return Collections.unmodifiableList(gbeans);
81     }
82
83     public void addGBean(GBeanData gbeanData) {
84         if (gbeanState != null) {
85             throw new IllegalStateException JavaDoc("GBeans have been serialized, so no more GBeans can be added");
86         }
87
88         gbeans.add(gbeanData);
89     }
90
91     public GBeanData addGBean(String JavaDoc name, GBeanInfo gbeanInfo, Naming naming, Environment environment) {
92         if (gbeanState != null) {
93             throw new IllegalStateException JavaDoc("GBeans have been serialized, so no more GBeans can be added");
94         }
95
96         String JavaDoc j2eeType = gbeanInfo.getJ2eeType();
97         if (j2eeType == null) j2eeType = "GBean";
98         AbstractName abstractName = naming.createRootName(environment.getConfigId(), name, j2eeType);
99         GBeanData gBeanData = new GBeanData(abstractName, gbeanInfo);
100         addGBean(gBeanData);
101         return gBeanData;
102     }
103
104     private static List JavaDoc loadGBeans(Element JavaDoc element, ClassLoader JavaDoc classLoader) throws InvalidConfigException {
105         if (element != null) {
106             // Set the thread context classloader so deserializing classes can grab the cl from the thread
107
ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
108             try {
109                 Thread.currentThread().setContextClassLoader(classLoader);
110
111                 DomReader reader = new DomReader(element);
112                 XStream xstream = XStreamUtil.createXStream();
113                 xstream.setClassLoader(classLoader);
114                 Object JavaDoc o = xstream.unmarshal(reader);
115                 GBeanData[] gbeanDatas = (GBeanData[]) o;
116                 return Arrays.asList(gbeanDatas);
117             } catch (Exception JavaDoc e) {
118                 throw new InvalidConfigException("Unable to load gbeans", e);
119             } finally {
120                 Thread.currentThread().setContextClassLoader(oldCl);
121             }
122         }
123         return Collections.EMPTY_LIST;
124     }
125
126     private static Element JavaDoc storeGBeans(List JavaDoc gbeans) throws IOException JavaDoc {
127         GBeanData[] gbeanDatas = (GBeanData[]) gbeans.toArray(new GBeanData[gbeans.size()]);
128
129         DocumentBuilderFactory JavaDoc documentBuilderFactory = XmlUtil.newDocumentBuilderFactory();
130         DocumentBuilder JavaDoc documentBuilder = null;
131         try {
132             documentBuilder = documentBuilderFactory.newDocumentBuilder();
133         } catch (ParserConfigurationException JavaDoc e) {
134             throw (IOException JavaDoc)new IOException JavaDoc("Cannot instantiate " + Document JavaDoc.class.getName()).initCause(e);
135         }
136         Document JavaDoc document = documentBuilder.newDocument();
137         DomWriter writer = new DomWriter(document);
138
139         XStream xstream = XStreamUtil.createXStream();
140         xstream.marshal(gbeanDatas, writer);
141
142 // FileWriter w = new FileWriter("target/foo.xml");
143
// xstream.toXML(gbeanDatas, w);
144
// w.close();
145

146         return document.getDocumentElement();
147     }
148 }
149
Popular Tags