support for external objects added BLD200403021900
authormmatula@netbeans.org
Tue, 02 Mar 2004 17:02:16 +0000
changeset 1462c8441156bc77
parent 1461 4db144b8ff61
child 1463 c5701d69a320
support for external objects added
mdr/src/org/netbeans/mdr/storagemodel/MdrStorage.java
     1.1 --- a/mdr/src/org/netbeans/mdr/storagemodel/MdrStorage.java	Tue Mar 02 17:02:01 2004 +0000
     1.2 +++ b/mdr/src/org/netbeans/mdr/storagemodel/MdrStorage.java	Tue Mar 02 17:02:16 2004 +0000
     1.3 @@ -12,22 +12,20 @@
     1.4   */
     1.5  package org.netbeans.mdr.storagemodel;
     1.6  
     1.7 +import org.netbeans.mdr.NBMDRepositoryImpl;
     1.8 +import org.netbeans.mdr.handlers.BaseObjectHandler;
     1.9 +import org.netbeans.mdr.persistence.*;
    1.10 +import org.netbeans.mdr.util.*;
    1.11 +import javax.jmi.model.ModelElement;
    1.12 +import javax.jmi.model.ModelPackage;
    1.13 +import javax.jmi.model.NameNotFoundException;
    1.14 +import javax.jmi.model.Namespace;
    1.15 +import javax.jmi.reflect.*;
    1.16 +import java.io.IOException;
    1.17 +import java.lang.ref.ReferenceQueue;
    1.18 +import java.lang.ref.WeakReference;
    1.19 +import java.lang.reflect.Constructor;
    1.20  import java.util.*;
    1.21 -import java.io.IOException;
    1.22 -import java.lang.reflect.Constructor;
    1.23 -
    1.24 -import javax.jmi.reflect.*;
    1.25 -import javax.jmi.model.Namespace;
    1.26 -import javax.jmi.model.ModelPackage;
    1.27 -import javax.jmi.model.ModelElement;
    1.28 -import javax.jmi.model.NameNotFoundException;
    1.29 -
    1.30 -import org.netbeans.api.mdr.*;
    1.31 -
    1.32 -import org.netbeans.mdr.persistence.*;
    1.33 -import org.netbeans.mdr.NBMDRepositoryImpl;
    1.34 -import org.netbeans.mdr.util.*;
    1.35 -import org.netbeans.mdr.handlers.*;
    1.36  
    1.37  /**
    1.38   * Instances of <code>MdrStorage</code> create and wrap {@link
    1.39 @@ -50,7 +48,7 @@
    1.40      /* -- Private static constants ---------------------------------------- */
    1.41      /* -------------------------------------------------------------------- */
    1.42  
    1.43 -    private static final int STORAGE_VERSION = 20;
    1.44 +    private static final int STORAGE_VERSION = 21;
    1.45      
    1.46      // names of global storage indexes
    1.47      static final String IDX_OBJECTS_BY_CLASSES = "ObjectsByClasses";
    1.48 @@ -73,7 +71,9 @@
    1.49      
    1.50      /** instances of MdrStorage by storage ID */
    1.51      private static final Hashtable instances = new Hashtable();
    1.52 -    
    1.53 +
    1.54 +    private InstanceMap externalObjects = null;
    1.55 +
    1.56  //    private static MdrStorage currentMdrStorage = null;
    1.57  //    private static Storage currentStorage = null;
    1.58      
    1.59 @@ -90,7 +90,28 @@
    1.60  //        if (result == null) result = currentMdrStorage;
    1.61          return result;
    1.62      }
    1.63 -    
    1.64 +
    1.65 +    public void registerExternal(StorableBaseObject storable) {
    1.66 +        if (externalObjects == null) {
    1.67 +            externalObjects = new InstanceMap();
    1.68 +        }
    1.69 +        externalObjects.put(storable.getMofId(), storable);
    1.70 +    }
    1.71 +
    1.72 +    public void removeExternal(StorableBaseObject storable) {
    1.73 +        if (externalObjects != null) {
    1.74 +            externalObjects.remove(storable.getMofId());
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +    private StorableBaseObject getExternal(MOFID mofId) {
    1.79 +        StorableBaseObject result = null;
    1.80 +        if (externalObjects != null) {
    1.81 +            result = (StorableBaseObject) externalObjects.get(mofId);
    1.82 +        }
    1.83 +        return result;
    1.84 +    }
    1.85 +
    1.86      /* -------------------------------------------------------------------- */
    1.87      /* -- Private attributes ---------------------------------------------- */
    1.88      /* -------------------------------------------------------------------- */
    1.89 @@ -661,12 +682,16 @@
    1.90      public StorableBaseObject getObject(org.netbeans.mdr.persistence.MOFID mofid) throws StorageException {
    1.91          if (mofid == null)
    1.92              return null;
    1.93 -        SinglevaluedIndex objectsIndex = getObjectsIndexByMofId(mofid);
    1.94 -        if (objectsIndex == null)
    1.95 -            return null;    // Partition was not found
    1.96 -        synchronized (getStorageById(mofid.getStorageID())) {
    1.97 -            return (StorableBaseObject) objectsIndex.getIfExists(mofid);
    1.98 +        StorableBaseObject result = getExternal(mofid);
    1.99 +        if (result == null) {
   1.100 +            SinglevaluedIndex objectsIndex = getObjectsIndexByMofId(mofid);
   1.101 +            if (objectsIndex == null)
   1.102 +                return null;    // Partition was not found
   1.103 +            synchronized (getStorageById(mofid.getStorageID())) {
   1.104 +                result = (StorableBaseObject) objectsIndex.getIfExists(mofid);
   1.105 +            }
   1.106          }
   1.107 +        return result;
   1.108      }
   1.109      
   1.110      /** Gets object from a singlevalued index using primary index.
   1.111 @@ -1777,4 +1802,59 @@
   1.112              }
   1.113          }
   1.114      }
   1.115 +
   1.116 +    private class InstanceMap extends HashMap {
   1.117 +        private final ReferenceQueue queue = new ReferenceQueue();
   1.118 +
   1.119 +        private class InstanceReference extends WeakReference {
   1.120 +            private Object key;
   1.121 +
   1.122 +            public InstanceReference(Object key, Object instance) {
   1.123 +                super(instance, queue);
   1.124 +                this.key = key;
   1.125 +            }
   1.126 +
   1.127 +            public Object getKey() {
   1.128 +                return key;
   1.129 +            }
   1.130 +        }
   1.131 +
   1.132 +        private void cleanUp() {
   1.133 +            InstanceMap.InstanceReference reference;
   1.134 +
   1.135 +            while ((reference = (InstanceMap.InstanceReference) queue.poll()) != null) {
   1.136 +//                Logger.getDefault().log("Removing: " + reference.getProxyMofId());
   1.137 +                this.remove(reference.getKey());
   1.138 +            }
   1.139 +        }
   1.140 +
   1.141 +        public Object put(Object key, Object value) {
   1.142 +            cleanUp();
   1.143 +            Object result = super.put(key, new InstanceMap.InstanceReference(key, value));
   1.144 +            if (result != null) {
   1.145 +                return ((InstanceMap.InstanceReference) result).get();
   1.146 +            } else {
   1.147 +                return result;
   1.148 +            }
   1.149 +        }
   1.150 +
   1.151 +        public Object get(Object key) {
   1.152 +            cleanUp();
   1.153 +            Object result = super.get(key);
   1.154 +            if (result != null) {
   1.155 +                return ((InstanceMap.InstanceReference) result).get();
   1.156 +            } else {
   1.157 +                return result;
   1.158 +            }
   1.159 +        }
   1.160 +
   1.161 +        public Collection values() {
   1.162 +            ArrayList result = new ArrayList();
   1.163 +            cleanUp();
   1.164 +            for (Iterator it = super.values().iterator(); it.hasNext();) {
   1.165 +                result.add(((InstanceMap.InstanceReference) it.next()).get());
   1.166 +            }
   1.167 +            return result;
   1.168 +        }
   1.169 +    }
   1.170  }