001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.io.Externalizable;
019: import java.io.IOException;
020: import java.io.ObjectInput;
021: import java.io.ObjectOutput;
022: import java.util.Collections;
023: import java.util.Comparator;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: /**
030: * @version $Rev: 556119 $ $Date: 2007-07-13 12:34:02 -0700 (Fri, 13 Jul 2007) $
031: */
032: public class GBeanData implements Externalizable {
033: private static final long serialVersionUID = -1012491431781444074L;
034:
035: private Externalizable backwardExternalizables[] = new Externalizable[] {
036: new V0Externalizable(), new V1Externalizable() };
037:
038: private GBeanInfo gbeanInfo;
039: private final Map<String, Object> attributes;
040: private final Map<String, ReferencePatterns> references;
041: private final Set<ReferencePatterns> dependencies;
042: private AbstractName abstractName;
043: private int priority;
044:
045: public GBeanData() {
046: attributes = new HashMap<String, Object>();
047: references = new HashMap<String, ReferencePatterns>();
048: dependencies = new HashSet<ReferencePatterns>();
049: }
050:
051: public GBeanData(GBeanInfo gbeanInfo) {
052: this ();
053: setGBeanInfo(gbeanInfo);
054: }
055:
056: public GBeanData(AbstractName abstractName, GBeanInfo gbeanInfo) {
057: this ();
058: this .abstractName = abstractName;
059: setGBeanInfo(gbeanInfo);
060: }
061:
062: public GBeanData(GBeanData gbeanData) {
063: setGBeanInfo(gbeanData.gbeanInfo);
064: attributes = new HashMap<String, Object>(gbeanData.attributes);
065: references = new HashMap<String, ReferencePatterns>(
066: gbeanData.references);
067: dependencies = new HashSet<ReferencePatterns>(
068: gbeanData.dependencies);
069: abstractName = gbeanData.abstractName;
070: }
071:
072: public AbstractName getAbstractName() {
073: return abstractName;
074: }
075:
076: public void setAbstractName(AbstractName abstractName) {
077: this .abstractName = abstractName;
078: }
079:
080: public GBeanInfo getGBeanInfo() {
081: return gbeanInfo;
082: }
083:
084: public void clearAttribute(String name) {
085: attributes.remove(name);
086: }
087:
088: public void clearReference(String name) {
089: references.remove(name);
090: }
091:
092: public void setGBeanInfo(GBeanInfo gbeanInfo) {
093: this .gbeanInfo = gbeanInfo;
094: if (gbeanInfo == null) {
095: priority = GBeanInfo.PRIORITY_NORMAL;
096: } else {
097: priority = gbeanInfo.getPriority();
098: }
099: }
100:
101: public Map<String, Object> getAttributes() {
102: return new HashMap<String, Object>(attributes);
103: }
104:
105: public Set<String> getAttributeNames() {
106: return new HashSet<String>(attributes.keySet());
107: }
108:
109: public Object getAttribute(String name) {
110: return attributes.get(name);
111: }
112:
113: public void setAttribute(String name, Object value) {
114: attributes.put(name, value);
115: }
116:
117: public Map<String, ReferencePatterns> getReferences() {
118: return new HashMap<String, ReferencePatterns>(references);
119: }
120:
121: public Set<String> getReferencesNames() {
122: return new HashSet<String>(references.keySet());
123: }
124:
125: public ReferencePatterns getReferencePatterns(String name) {
126: return references.get(name);
127: }
128:
129: public void setReferencePattern(String name,
130: AbstractNameQuery pattern) {
131: setReferencePatterns(name, Collections.singleton(pattern));
132: }
133:
134: public void setReferencePattern(String name,
135: AbstractName abstractName) {
136: setReferencePatterns(name, new ReferencePatterns(abstractName));
137: }
138:
139: public void setReferencePatterns(String name, Set patterns) {
140: setReferencePatterns(name, new ReferencePatterns(patterns));
141: }
142:
143: public void setReferencePatterns(String name,
144: ReferencePatterns patterns) {
145: references.put(name, patterns);
146: }
147:
148: public Set<ReferencePatterns> getDependencies() {
149: return new HashSet<ReferencePatterns>(dependencies);
150: }
151:
152: public void setDependencies(Set<ReferencePatterns> dependencies) {
153: this .dependencies.clear();
154: addDependencies(dependencies);
155: }
156:
157: public void addDependencies(Set<? extends Object> dependencies) {
158: for (Object dependency : dependencies) {
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(
170: "Unknown dependency type: " + dependency);
171: }
172: }
173: }
174:
175: public void addDependency(ReferencePatterns dependency) {
176: this .dependencies.add(dependency);
177: }
178:
179: public void addDependency(AbstractNameQuery refInfo) {
180: this .dependencies.add(new ReferencePatterns(refInfo));
181: }
182:
183: public void addDependency(AbstractName dependency) {
184: this .dependencies.add(new ReferencePatterns(dependency));
185: }
186:
187: public int getPriority() {
188: return priority;
189: }
190:
191: public void setPriority(int priority) {
192: this .priority = priority;
193: }
194:
195: public void writeExternal(ObjectOutput out) throws IOException {
196: // write version index
197: out.writeObject(backwardExternalizables.length - 1);
198:
199: // write the gbean info
200: out.writeObject(gbeanInfo);
201:
202: // write the abstract name
203: out.writeObject(abstractName);
204:
205: // write the priority
206: out.writeInt(priority);
207:
208: // write the attributes
209: out.writeInt(attributes.size());
210: for (Map.Entry<String, Object> entry : attributes.entrySet()) {
211: String name = entry.getKey();
212: Object value = entry.getValue();
213: try {
214: out.writeObject(name);
215: out.writeObject(value);
216: } catch (IOException e) {
217: throw (IOException) new IOException(
218: "Unable to write attribute: " + name
219: + " in gbean: " + abstractName)
220: .initCause(e);
221: } catch (NoClassDefFoundError e) {
222: throw (IOException) new IOException(
223: "Unable to write attribute: " + name
224: + " in gbean: " + abstractName)
225: .initCause(e);
226: }
227: }
228:
229: // write the references
230: out.writeInt(references.size());
231: for (Map.Entry<String, ReferencePatterns> entry : references
232: .entrySet()) {
233: String name = entry.getKey();
234: ReferencePatterns value = entry.getValue();
235: try {
236: out.writeObject(name);
237: out.writeObject(value);
238: } catch (IOException e) {
239: throw (IOException) new IOException(
240: "Unable to write reference pattern: " + name
241: + " in gbean: " + abstractName)
242: .initCause(e);
243: }
244: }
245: //write the dependencies
246: out.writeInt(dependencies.size());
247: for (ReferencePatterns referencePatterns : dependencies) {
248: try {
249: out.writeObject(referencePatterns);
250: } catch (IOException e) {
251: throw (IOException) new IOException(
252: "Unable to write dependency pattern in gbean: "
253: + abstractName).initCause(e);
254: }
255: }
256: }
257:
258: public void readExternal(ObjectInput in) throws IOException,
259: ClassNotFoundException {
260: Object opaque = in.readObject();
261: if (opaque instanceof Integer) {
262: backwardExternalizables[((Integer) opaque)]
263: .readExternal(in);
264: } else {
265: gbeanInfo = (GBeanInfo) opaque;
266: backwardExternalizables[0].readExternal(in);
267: }
268: }
269:
270: /**
271: * Note: this comparator
272: * imposes orderings that are inconsistent with equals.
273: */
274: public static class PriorityComparator implements
275: Comparator<GBeanData> {
276:
277: public int compare(GBeanData o1, GBeanData o2) {
278: return o1.priority - o2.priority;
279: }
280: }
281:
282: private class V0Externalizable implements Externalizable {
283:
284: public void writeExternal(ObjectOutput out) throws IOException {
285: throw new UnsupportedOperationException();
286: }
287:
288: public final void readExternal(ObjectInput in)
289: throws IOException, ClassNotFoundException {
290: // read the gbean info
291: readGBeanInfo(in);
292:
293: // read the abstract name
294: try {
295: abstractName = (AbstractName) in.readObject();
296: } catch (IOException e) {
297: throw (IOException) new IOException(
298: "Unable to deserialize AbstractName for GBeanData of type "
299: + gbeanInfo.getClassName())
300: .initCause(e);
301: }
302:
303: readPriority(in);
304:
305: try {
306: // read the attributes
307: int attributeCount = in.readInt();
308: for (int i = 0; i < attributeCount; i++) {
309: String attributeName = (String) in.readObject();
310: Object attributeValue;
311: try {
312: attributeValue = in.readObject();
313: } catch (ClassNotFoundException e) {
314: throw new ClassNotFoundException(
315: "Unable to find class used in GBeanData "
316: + abstractName
317: + ", attribute: "
318: + attributeName, e);
319: } catch (IOException e) {
320: throw (IOException) new IOException(
321: "Unable to deserialize GBeanData "
322: + abstractName
323: + ", attribute: "
324: + attributeName).initCause(e);
325: }
326: setAttribute(attributeName, attributeValue);
327: }
328:
329: // read the references
330: int endpointCount = in.readInt();
331: for (int i = 0; i < endpointCount; i++) {
332: String referenceName = (String) in.readObject();
333: ReferencePatterns referencePattern;
334: try {
335: referencePattern = (ReferencePatterns) in
336: .readObject();
337: } catch (ClassNotFoundException e) {
338: throw new ClassNotFoundException(
339: "Unable to find class used in GBeanData "
340: + abstractName
341: + ", reference: "
342: + referenceName, e);
343: } catch (IOException e) {
344: throw (IOException) new IOException(
345: "Unable to deserialize GBeanData "
346: + abstractName
347: + ", reference: "
348: + referenceName).initCause(e);
349: }
350: setReferencePatterns(referenceName,
351: referencePattern);
352: }
353:
354: //read the dependencies
355: int dependencyCount = in.readInt();
356: for (int i = 0; i < dependencyCount; i++) {
357: ReferencePatterns depdendencyPattern = (ReferencePatterns) in
358: .readObject();
359: dependencies.add(depdendencyPattern);
360: }
361: } catch (IOException e) {
362: throw (IOException) new IOException(
363: "Unable to deserialize GBeanData "
364: + abstractName).initCause(e);
365: } catch (ClassNotFoundException e) {
366: throw new ClassNotFoundException(
367: "Unable to find class used in GBeanData "
368: + abstractName, e);
369: }
370: }
371:
372: protected void readGBeanInfo(ObjectInput in)
373: throws IOException, ClassNotFoundException {
374: }
375:
376: protected void readPriority(ObjectInput in) throws IOException,
377: ClassNotFoundException {
378: priority = GBeanInfo.PRIORITY_NORMAL;
379: }
380:
381: }
382:
383: private class V1Externalizable extends V0Externalizable {
384:
385: public void writeExternal(ObjectOutput out) throws IOException {
386: throw new UnsupportedOperationException();
387: }
388:
389: protected void readGBeanInfo(ObjectInput in)
390: throws IOException, ClassNotFoundException {
391: gbeanInfo = (GBeanInfo) in.readObject();
392: }
393:
394: protected void readPriority(ObjectInput in) throws IOException,
395: ClassNotFoundException {
396: priority = in.readInt();
397: }
398:
399: }
400:
401: }
|