KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > SerializedGBeanState


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;
18
19 import java.io.Serializable JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.EOFException JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.apache.geronimo.gbean.GBeanData;
33 import org.apache.geronimo.gbean.GBeanInfo;
34 import org.apache.geronimo.gbean.AbstractName;
35 import org.apache.geronimo.kernel.Naming;
36 import org.apache.geronimo.kernel.ObjectInputStreamExt;
37 import org.apache.geronimo.kernel.repository.Environment;
38
39 /**
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public class SerializedGBeanState implements GBeanState, Serializable JavaDoc {
43     private static final long serialVersionUID = 6015138334529564307L;
44
45     /**
46      * GBeans contained in this configuration.
47      */

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

53     private byte[] gbeanState;
54
55     public SerializedGBeanState(Collection JavaDoc gbeans) {
56         if (gbeans != null){
57             this.gbeans.addAll(gbeans);
58         }
59     }
60
61     public List JavaDoc getGBeans(ClassLoader JavaDoc classLoader) throws InvalidConfigException {
62         if (gbeanState == null) {
63             return Collections.unmodifiableList(gbeans);
64         }
65         gbeans.addAll(loadGBeans(gbeanState, classLoader));
66         return Collections.unmodifiableList(gbeans);
67     }
68
69     public void addGBean(GBeanData gbeanData) {
70         if (gbeanState != null) {
71             throw new IllegalStateException JavaDoc("GBeans have been serialized, so no more GBeans can be added");
72         }
73
74         gbeans.add(gbeanData);
75     }
76
77     public GBeanData addGBean(String JavaDoc name, GBeanInfo gbeanInfo, Naming naming, Environment environment) {
78         if (gbeanState != null) {
79             throw new IllegalStateException JavaDoc("GBeans have been serialized, so no more GBeans can be added");
80         }
81
82         String JavaDoc j2eeType = gbeanInfo.getJ2eeType();
83         if (j2eeType == null) j2eeType = "GBean";
84         AbstractName abstractName = naming.createRootName(environment.getConfigId(), name, j2eeType);
85         GBeanData gBeanData = new GBeanData(abstractName, gbeanInfo);
86         addGBean(gBeanData);
87         return gBeanData;
88     }
89
90     private void writeObject(java.io.ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
91         if (gbeanState == null) {
92             gbeanState = storeGBeans(gbeans);
93             gbeans.clear();
94         }
95
96         stream.defaultWriteObject();
97     }
98
99     private static List JavaDoc loadGBeans(byte[] gbeanState, ClassLoader JavaDoc classLoader) throws InvalidConfigException {
100         List JavaDoc gbeans = new ArrayList JavaDoc();
101         if (gbeanState != null && gbeanState.length > 0) {
102             // Set the thread context classloader so deserializing classes can grab the cl from the thread
103
ClassLoader JavaDoc oldCl = Thread.currentThread().getContextClassLoader();
104             try {
105                 Thread.currentThread().setContextClassLoader(classLoader);
106
107                 ObjectInputStream JavaDoc ois = new ObjectInputStreamExt(new ByteArrayInputStream JavaDoc(gbeanState), classLoader);
108                 try {
109                     while (true) {
110                         GBeanData gbeanData = new GBeanData();
111                         gbeanData.readExternal(ois);
112                         gbeans.add(gbeanData);
113                     }
114                 } catch (EOFException JavaDoc e) {
115                     // ok
116
} finally {
117                     ois.close();
118                 }
119             } catch (Exception JavaDoc e) {
120                 throw new InvalidConfigException("Unable to deserialize GBeanState", e);
121             } finally {
122                 Thread.currentThread().setContextClassLoader(oldCl);
123             }
124         }
125         return gbeans;
126     }
127
128     private static byte[] storeGBeans(List JavaDoc gbeans) throws IOException JavaDoc {
129         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
130         ObjectOutputStream JavaDoc oos;
131         try {
132             oos = new ObjectOutputStream JavaDoc(baos);
133         } catch (IOException JavaDoc e) {
134             throw (AssertionError JavaDoc) new AssertionError JavaDoc("Unable to initialize ObjectOutputStream").initCause(e);
135         }
136         for (Iterator JavaDoc iterator = gbeans.iterator(); iterator.hasNext();) {
137             GBeanData gbeanData = (GBeanData) iterator.next();
138             try {
139                 gbeanData.writeExternal(oos);
140             } catch (Exception JavaDoc e) {
141                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to serialize GBeanData for " + gbeanData.getAbstractName()).initCause(e);
142             }
143         }
144         try {
145             oos.flush();
146         } catch (IOException JavaDoc e) {
147             throw (AssertionError JavaDoc) new AssertionError JavaDoc("Unable to flush ObjectOutputStream").initCause(e);
148         }
149         return baos.toByteArray();
150     }
151 }
152
Popular Tags