KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > AttrCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.storagemodel;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ListIterator JavaDoc;
30
31 import javax.jmi.reflect.*;
32
33 import org.netbeans.mdr.persistence.StorageException;
34 import org.netbeans.mdr.persistence.MOFID;
35 import org.netbeans.mdr.handlers.BaseObjectHandler;
36 import org.netbeans.mdr.util.*;
37
38 /**
39  *
40  * @author Martin Matula
41  */

42 public class AttrCollection implements Collection JavaDoc {
43     protected Collection JavaDoc inner = new ArrayList JavaDoc();
44     protected String JavaDoc attrName;
45
46     protected transient StorableFeatured mdrObject;
47     protected transient int maxSize;
48     protected transient Class JavaDoc type;
49     protected transient boolean isRefObject;
50     protected transient MOFID metaMofId;
51     protected transient boolean isIndexed = false;
52     
53     protected boolean needsUnwrap = false;
54     
55     public AttrCollection() {
56     }
57
58     AttrCollection(StorableFeatured mdrObject, StorableClass.AttributeDescriptor desc) throws StorageException {
59         this(mdrObject, desc, null);
60     }
61
62     protected AttrCollection(StorableFeatured mdrObject, StorableClass.AttributeDescriptor desc, Collection JavaDoc values) throws StorageException {
63         this.mdrObject = mdrObject;
64         this.attrName = desc.getName();
65         
66         cacheValues(desc);
67
68         if (values != null && !values.isEmpty()) {
69             checkMaxSize(values.size());
70             for (Iterator JavaDoc it = values.iterator(); it.hasNext();) {
71                 Object JavaDoc value = it.next();
72                 checkType(value);
73                 inner.add(value);
74                 if (isRefObject) {
75                     setAttribComposite((RefObject) value);
76                 }
77             }
78         }
79     }
80
81     protected AttrCollection(StorableFeatured mdrObject, List JavaDoc values, int maxSize, Class JavaDoc type, String JavaDoc attrName, boolean isRefObject, MOFID metaMofId) {
82         this.mdrObject = mdrObject;
83         this.inner = values;
84         this.maxSize = maxSize;
85         this.type = type;
86         this.attrName = attrName;
87         this.isRefObject = isRefObject;
88         this.metaMofId = metaMofId;
89     }
90     
91     protected synchronized void checkUnwrap() {
92         if (needsUnwrap) {
93             for (ListIterator JavaDoc it = ((List JavaDoc) inner).listIterator(); it.hasNext();) {
94                 Object JavaDoc temp = it.next();
95                 if (temp instanceof MOFID) {
96                     temp = mdrObject.getMdrStorage().getRepository().getByMofId((MOFID) temp);
97                     if (temp != null) {
98                         it.set(temp);
99                     } else {
100                         // recovery: remove the object if it cannot be resolved
101
it.remove();
102                         Logger.getDefault().log(Logger.WARNING, "Invalid element found in attr. collection - removing.");
103                     }
104                 } else if (temp == null) {
105                     // recovery: remove the element if it is null
106
it.remove();
107                     Logger.getDefault().log(Logger.WARNING, "Null found in attr. collection - removing.");
108                 }
109             }
110             needsUnwrap = false;
111         }
112     }
113
114     public void read(InputStream JavaDoc stream, StorableFeatured storable) throws IOException JavaDoc {
115         int size = IOUtils.readInt(stream);
116
117         mdrObject = storable;
118         
119         try {
120             attrName = IOUtils.readString(stream);
121             cacheValues(storable.getClassProxy().getAttrDesc(attrName));
122         } catch (StorageException e) {
123             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
124         }
125
126         for (int i = 0; i < size; i++) {
127             inner.add(IOUtils.read(stream, storable, type.getName()));
128         }
129         needsUnwrap = this.isRefObject;
130     }
131     
132     public void write(OutputStream JavaDoc stream) throws IOException JavaDoc {
133         IOUtils.writeInt(stream, inner.size());
134         IOUtils.writeString(stream, attrName);
135         for (Iterator JavaDoc it = inner.iterator(); it.hasNext();) {
136             IOUtils.write(stream, it.next(), mdrObject);
137         }
138     }
139     
140     private void cacheValues(StorableClass.AttributeDescriptor attrDesc) throws StorageException {
141         this.type = attrDesc.getType();
142         this.maxSize = attrDesc.getMaxSize();
143         this.isRefObject = RefObject.class.isAssignableFrom(this.type);
144         this.metaMofId = attrDesc.getMofId();
145         this.isIndexed = (mdrObject instanceof StorableObject) && attrDesc.isIndexed ();
146     }
147     
148     protected RefObject getMetaElement() {
149         try {
150             return (RefObject) mdrObject.getMdrStorage().getRepository().getByMofId(metaMofId);
151         } catch (Exception JavaDoc e) {
152             return null;
153         }
154     }
155
156     public boolean add(Object JavaDoc obj) {
157         checkType(obj);
158         checkMaxSize(1);
159         checkUnwrap();
160         mdrObject.objectWillChange();
161         if (isIndexed)
162             ((StorableObject) mdrObject).removeFromIndex (metaMofId);
163         boolean result = inner.add(obj);
164         if (result) {
165             if (isRefObject) {
166                 try {
167                     setAttribComposite((RefObject) obj);
168                 } catch (StorageException e) {
169                     throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
170                 }
171             }
172             mdrObject.objectChanged();
173         }
174         if (isIndexed)
175             ((StorableObject) mdrObject).addToIndex (metaMofId);
176         return result;
177     }
178     
179     public boolean addAll(Collection JavaDoc collection) {
180         // should never be called
181
throw new DebugException();
182     }
183     
184     public void clear() {
185         // should never be called
186
throw new DebugException();
187     }
188     
189     public Iterator JavaDoc iterator() {
190         checkUnwrap();
191         return new AttrIterator(inner.iterator());
192     }
193     
194     public boolean removeAll(Collection JavaDoc collection) {
195         // should never be called
196
throw new DebugException();
197     }
198     
199     public boolean retainAll(Collection JavaDoc collection) {
200         // should never be called
201
throw new DebugException();
202     }
203     
204     public int size() {
205         checkUnwrap();
206         return inner.size();
207     }
208     
209     public boolean contains(Object JavaDoc obj) {
210         checkUnwrap();
211         return inner.contains(obj);
212     }
213     
214     public boolean containsAll(Collection JavaDoc collection) {
215         checkUnwrap();
216         return inner.containsAll(collection);
217     }
218     
219     public boolean isEmpty() {
220         checkUnwrap();
221         return inner.isEmpty();
222     }
223     
224     public boolean remove(Object JavaDoc obj) {
225         checkUnwrap();
226         mdrObject.objectWillChange();
227         if (isIndexed)
228             ((StorableObject) mdrObject).removeFromIndex (metaMofId);
229         boolean result = inner.remove(obj);
230         if (result) {
231             if (isRefObject) {
232                 try {
233                     clearAttribComposite((RefObject) obj);
234                 } catch (StorageException e) {
235                     throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
236                 }
237             }
238             mdrObject.objectChanged();
239         }
240         if (isIndexed)
241             ((StorableObject) mdrObject).addToIndex (metaMofId);
242         return result;
243     }
244     
245     public Object JavaDoc[] toArray() {
246         checkUnwrap();
247         return inner.toArray();
248     }
249     
250     public Object JavaDoc[] toArray(Object JavaDoc[] obj) {
251         checkUnwrap();
252         return inner.toArray(obj);
253     }
254     
255     public boolean equals(Object JavaDoc o) {
256         checkUnwrap();
257         return inner.equals(o);
258     }
259     
260     public int hashCode() {
261         checkUnwrap();
262         return inner.hashCode();
263     }
264     
265     protected void checkType(Object JavaDoc obj) {
266         if (obj == null) {
267             throw new NullPointerException JavaDoc();
268         }
269         
270         if (!type.isInstance(obj)) {
271             throw new TypeMismatchException(type, obj, getMetaElement(), "Expected type: " + type + ", supplied type: " + obj.getClass().getName());
272         }
273     }
274     
275     protected void checkMaxSize(int size) {
276         if (maxSize != -1) {
277             if (maxSize < (size + size())) throw new WrongSizeException(getMetaElement());
278         }
279     }
280
281     protected void setAttribComposite(RefObject object) throws StorageException {
282         StorableObject storable = (StorableObject) ((BaseObjectHandler) object)._getDelegate();
283         storable.setComposite(mdrObject, storable.getMofId(), metaMofId);
284     }
285     
286     protected void clearAttribComposite(RefObject object) throws StorageException {
287         StorableObject storable = (StorableObject) ((BaseObjectHandler) object)._getDelegate();
288         storable.clearComposite();
289     }
290     
291     protected class AttrIterator implements Iterator JavaDoc {
292         private final Iterator JavaDoc inner;
293         protected Object JavaDoc lastRead = null;
294         
295         public AttrIterator(Iterator JavaDoc inner) {
296             this.inner = inner;
297         }
298         
299         public boolean hasNext() {
300             return inner.hasNext();
301         }
302         
303         public Object JavaDoc next() {
304             return (lastRead = inner.next());
305         }
306         
307         public void remove() {
308             mdrObject.objectWillChange();
309             if (isIndexed)
310                 ((StorableObject) mdrObject).removeFromIndex (metaMofId);
311             inner.remove();
312             if (isRefObject) {
313                 try {
314                     clearAttribComposite((RefObject) lastRead);
315                 } catch (StorageException e) {
316                     throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
317                 }
318             }
319             if (isIndexed)
320                 ((StorableObject) mdrObject).addToIndex (metaMofId);
321             mdrObject.objectChanged();
322         }
323         
324         public boolean equals(Object JavaDoc o) {
325             return inner.equals(o);
326         }
327         
328         public int hashCode() {
329             return inner.hashCode();
330         }
331     }
332 }
333
Popular Tags