1 19 package org.netbeans.mdr.storagemodel; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 import java.util.ListIterator ; 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 42 public class AttrCollection implements Collection { 43 protected Collection inner = new ArrayList (); 44 protected String attrName; 45 46 protected transient StorableFeatured mdrObject; 47 protected transient int maxSize; 48 protected transient Class 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 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 it = values.iterator(); it.hasNext();) { 71 Object 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 values, int maxSize, Class type, String 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 it = ((List ) inner).listIterator(); it.hasNext();) { 94 Object 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 it.remove(); 102 Logger.getDefault().log(Logger.WARNING, "Invalid element found in attr. collection - removing."); 103 } 104 } else if (temp == null) { 105 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 stream, StorableFeatured storable) throws IOException { 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 stream) throws IOException { 133 IOUtils.writeInt(stream, inner.size()); 134 IOUtils.writeString(stream, attrName); 135 for (Iterator 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 e) { 152 return null; 153 } 154 } 155 156 public boolean add(Object 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 collection) { 180 throw new DebugException(); 182 } 183 184 public void clear() { 185 throw new DebugException(); 187 } 188 189 public Iterator iterator() { 190 checkUnwrap(); 191 return new AttrIterator(inner.iterator()); 192 } 193 194 public boolean removeAll(Collection collection) { 195 throw new DebugException(); 197 } 198 199 public boolean retainAll(Collection collection) { 200 throw new DebugException(); 202 } 203 204 public int size() { 205 checkUnwrap(); 206 return inner.size(); 207 } 208 209 public boolean contains(Object obj) { 210 checkUnwrap(); 211 return inner.contains(obj); 212 } 213 214 public boolean containsAll(Collection 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 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 [] toArray() { 246 checkUnwrap(); 247 return inner.toArray(); 248 } 249 250 public Object [] toArray(Object [] obj) { 251 checkUnwrap(); 252 return inner.toArray(obj); 253 } 254 255 public boolean equals(Object 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 obj) { 266 if (obj == null) { 267 throw new NullPointerException (); 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 { 292 private final Iterator inner; 293 protected Object lastRead = null; 294 295 public AttrIterator(Iterator inner) { 296 this.inner = inner; 297 } 298 299 public boolean hasNext() { 300 return inner.hasNext(); 301 } 302 303 public Object 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 o) { 325 return inner.equals(o); 326 } 327 328 public int hashCode() { 329 return inner.hashCode(); 330 } 331 } 332 } 333 | Popular Tags |