KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanData


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.gbean;
18
19 import java.io.Externalizable JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInput JavaDoc;
22 import java.io.ObjectOutput JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public class GBeanData implements Externalizable JavaDoc {
35     private static final long serialVersionUID = -1012491431781444074L;
36
37     private Externalizable JavaDoc backwardExternalizables[] = new Externalizable JavaDoc[] {
38             new V0Externalizable(),
39             new V1Externalizable()
40     };
41
42     private GBeanInfo gbeanInfo;
43     private final Map JavaDoc attributes;
44     private final Map JavaDoc references;
45     private final Set JavaDoc dependencies;
46     private AbstractName abstractName;
47     private int priority;
48
49     public GBeanData() {
50         attributes = new HashMap JavaDoc();
51         references = new HashMap JavaDoc();
52         dependencies = new HashSet JavaDoc();
53     }
54
55     public GBeanData(GBeanInfo gbeanInfo) {
56         this();
57         setGBeanInfo(gbeanInfo);
58     }
59
60     public GBeanData(AbstractName abstractName, GBeanInfo gbeanInfo) {
61         this();
62         this.abstractName = abstractName;
63         setGBeanInfo(gbeanInfo);
64     }
65
66     public GBeanData(GBeanData gbeanData) {
67         setGBeanInfo(gbeanData.gbeanInfo);
68         attributes = new HashMap JavaDoc(gbeanData.attributes);
69         references = new HashMap JavaDoc(gbeanData.references);
70         dependencies = new HashSet JavaDoc(gbeanData.dependencies);
71         abstractName = gbeanData.abstractName;
72     }
73
74     public AbstractName getAbstractName() {
75         return abstractName;
76     }
77
78     public void setAbstractName(AbstractName abstractName) {
79         this.abstractName = abstractName;
80     }
81
82     public GBeanInfo getGBeanInfo() {
83         return gbeanInfo;
84     }
85
86     public void clearAttribute(String JavaDoc name) {
87         attributes.remove(name);
88     }
89
90     public void clearReference(String JavaDoc name) {
91         references.remove(name);
92     }
93
94     public void setGBeanInfo(GBeanInfo gbeanInfo) {
95         this.gbeanInfo = gbeanInfo;
96         if (gbeanInfo == null) {
97             priority = GBeanInfo.PRIORITY_NORMAL;
98         } else {
99             priority = gbeanInfo.getPriority();
100         }
101     }
102
103     public Map JavaDoc getAttributes() {
104         return new HashMap JavaDoc(attributes);
105     }
106
107     public Set JavaDoc getAttributeNames() {
108         return new HashSet JavaDoc(attributes.keySet());
109     }
110
111     public Object JavaDoc getAttribute(String JavaDoc name) {
112         return attributes.get(name);
113     }
114
115     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
116         attributes.put(name, value);
117     }
118
119     public Map JavaDoc getReferences() {
120         return new HashMap JavaDoc(references);
121     }
122
123     public Set JavaDoc getReferencesNames() {
124         return new HashSet JavaDoc(references.keySet());
125     }
126
127     public ReferencePatterns getReferencePatterns(String JavaDoc name) {
128         return (ReferencePatterns) references.get(name);
129     }
130
131     public void setReferencePattern(String JavaDoc name, AbstractNameQuery pattern) {
132         setReferencePatterns(name, Collections.singleton(pattern));
133     }
134
135     public void setReferencePattern(String JavaDoc name, AbstractName abstractName) {
136         setReferencePatterns(name, new ReferencePatterns(abstractName));
137     }
138
139     public void setReferencePatterns(String JavaDoc name, Set JavaDoc patterns) {
140         setReferencePatterns(name, new ReferencePatterns(patterns));
141     }
142
143     public void setReferencePatterns(String JavaDoc name, ReferencePatterns patterns) {
144         references.put(name, patterns);
145     }
146
147     public Set JavaDoc getDependencies() {
148         return new HashSet JavaDoc(dependencies);
149     }
150
151     public void setDependencies(Set JavaDoc dependencies) {
152         this.dependencies.clear();
153         addDependencies(dependencies);
154     }
155
156     public void addDependencies(Set JavaDoc dependencies) {
157         for (Iterator JavaDoc iterator = dependencies.iterator(); iterator.hasNext();) {
158             Object JavaDoc dependency = iterator.next();
159             if (dependency instanceof AbstractName) {
160                 AbstractName name = (AbstractName) dependency;
161                 addDependency(name);
162             } else if (dependency instanceof AbstractNameQuery) {
163                 AbstractNameQuery nameQuery = (AbstractNameQuery) dependency;
164                 addDependency(nameQuery);
165             } else if (dependency instanceof ReferencePatterns) {
166                 ReferencePatterns referencePatterns = (ReferencePatterns) dependency;
167                 addDependency(referencePatterns);
168             } else {
169                 throw new IllegalArgumentException JavaDoc("Unknown dependency type: " + dependency);
170             }
171         }
172     }
173
174     public void addDependency(ReferencePatterns dependency) {
175         this.dependencies.add(dependency);
176     }
177
178     public void addDependency(AbstractNameQuery refInfo) {
179         this.dependencies.add(new ReferencePatterns(refInfo));
180     }
181
182     public void addDependency(AbstractName dependency) {
183         this.dependencies.add(new ReferencePatterns(dependency));
184     }
185
186     public int getPriority() {
187         return priority;
188     }
189
190     public void setPriority(int priority) {
191         this.priority = priority;
192     }
193
194     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
195         // write version index
196
out.writeObject(new Integer JavaDoc(backwardExternalizables.length -1));
197
198         // write the gbean info
199
out.writeObject(gbeanInfo);
200
201         // write the abstract name
202
out.writeObject(abstractName);
203
204         // write the priority
205
out.writeInt(priority);
206
207         // write the attributes
208
out.writeInt(attributes.size());
209         for (Iterator JavaDoc iterator = attributes.entrySet().iterator(); iterator.hasNext();) {
210             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
211             String JavaDoc name = (String JavaDoc) entry.getKey();
212             Object JavaDoc value = entry.getValue();
213             try {
214                 out.writeObject(name);
215                 out.writeObject(value);
216             } catch (IOException JavaDoc e) {
217                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to write attribute: " + name + " in gbean: " + abstractName).initCause(e);
218             } catch (NoClassDefFoundError JavaDoc e) {
219                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to write attribute: " + name + " in gbean: " + abstractName).initCause(e);
220             }
221         }
222
223         // write the references
224
out.writeInt(references.size());
225         for (Iterator JavaDoc iterator = references.entrySet().iterator(); iterator.hasNext();) {
226             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
227             String JavaDoc name = (String JavaDoc) entry.getKey();
228             ReferencePatterns value = (ReferencePatterns) entry.getValue();
229             try {
230                 out.writeObject(name);
231                 out.writeObject(value);
232             } catch (IOException JavaDoc e) {
233                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to write reference pattern: " + name + " in gbean: " + abstractName).initCause(e);
234             }
235         }
236         //write the dependencies
237
out.writeInt(dependencies.size());
238         for (Iterator JavaDoc iterator = dependencies.iterator(); iterator.hasNext();) {
239             ReferencePatterns referencePatterns = (ReferencePatterns) iterator.next();
240             try {
241                 out.writeObject(referencePatterns);
242             } catch (IOException JavaDoc e) {
243                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to write dependency pattern in gbean: " + abstractName).initCause(e);
244             }
245         }
246     }
247
248
249     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
250         Object JavaDoc opaque = in.readObject();
251         if (opaque instanceof Integer JavaDoc) {
252             backwardExternalizables[((Integer JavaDoc) opaque).intValue()].readExternal(in);
253         } else {
254             gbeanInfo = (GBeanInfo) opaque;
255             backwardExternalizables[0].readExternal(in);
256         }
257     }
258
259     /**
260      * Note: this comparator
261      * imposes orderings that are inconsistent with equals.
262      */

263     public static class PriorityComparator implements Comparator JavaDoc {
264
265         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
266             if (o1 instanceof GBeanData && o2 instanceof GBeanData) {
267                 return ((GBeanData)o1).priority - ((GBeanData)o2).priority;
268             }
269             return 0;
270         }
271     }
272
273     private class V0Externalizable implements Externalizable JavaDoc {
274
275         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
276             throw new UnsupportedOperationException JavaDoc();
277         }
278
279         public final void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
280             // read the gbean info
281
readGBeanInfo(in);
282
283             // read the abstract name
284
try {
285                 abstractName = (AbstractName) in.readObject();
286             } catch (IOException JavaDoc e) {
287                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to deserialize AbstractName for GBeanData of type " + gbeanInfo.getClassName()).initCause(e);
288             }
289
290             readPriority(in);
291
292             try {
293                 // read the attributes
294
int attributeCount = in.readInt();
295                 for (int i = 0; i < attributeCount; i++) {
296                     String JavaDoc attributeName = (String JavaDoc) in.readObject();
297                     Object JavaDoc attributeValue;
298                     try {
299                         attributeValue = in.readObject();
300                     } catch (ClassNotFoundException JavaDoc e) {
301                         throw new ClassNotFoundException JavaDoc("Unable to find class used in GBeanData " + abstractName + ", attribute: " + attributeName, e);
302                     } catch (IOException JavaDoc e) {
303                         throw (IOException JavaDoc) new IOException JavaDoc("Unable to deserialize GBeanData " + abstractName + ", attribute: " + attributeName).initCause(e);
304                     }
305                     setAttribute(attributeName, attributeValue);
306                 }
307
308                 // read the references
309
int endpointCount = in.readInt();
310                 for (int i = 0; i < endpointCount; i++) {
311                     String JavaDoc referenceName = (String JavaDoc) in.readObject();
312                     ReferencePatterns referencePattern;
313                     try {
314                         referencePattern = (ReferencePatterns) in.readObject();
315                     } catch (ClassNotFoundException JavaDoc e) {
316                         throw new ClassNotFoundException JavaDoc("Unable to find class used in GBeanData " + abstractName + ", reference: " + referenceName, e);
317                     } catch (IOException JavaDoc e) {
318                         throw (IOException JavaDoc) new IOException JavaDoc("Unable to deserialize GBeanData " + abstractName + ", reference: " + referenceName).initCause(e);
319                     }
320                     setReferencePatterns(referenceName, referencePattern);
321                 }
322
323                 //read the dependencies
324
int dependencyCount = in.readInt();
325                 for (int i = 0; i < dependencyCount; i++) {
326                     ReferencePatterns depdendencyPattern = (ReferencePatterns) in.readObject();
327                     dependencies.add(depdendencyPattern);
328                 }
329             } catch (IOException JavaDoc e) {
330                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to deserialize GBeanData " + abstractName).initCause(e);
331             } catch (ClassNotFoundException JavaDoc e) {
332                 throw new ClassNotFoundException JavaDoc("Unable to find class used in GBeanData " + abstractName, e);
333             }
334         }
335
336         protected void readGBeanInfo(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
337         }
338
339         protected void readPriority(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
340             priority = GBeanInfo.PRIORITY_NORMAL;
341         }
342
343     }
344
345     private class V1Externalizable extends V0Externalizable {
346
347         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
348             throw new UnsupportedOperationException JavaDoc();
349         }
350
351         protected void readGBeanInfo(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
352             gbeanInfo = (GBeanInfo) in.readObject();
353         }
354
355         protected void readPriority(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
356             priority = in.readInt();
357         }
358
359     }
360
361 }
362
363
Popular Tags