1 19 package org.netbeans.mdr.handlers; 20 21 import java.lang.ref.WeakReference ; 22 import java.lang.ref.ReferenceQueue ; 23 import java.util.HashMap ; 24 25 import org.netbeans.mdr.storagemodel.AssociationLink; 26 27 import javax.jmi.reflect.RefAssociationLink; 28 import javax.jmi.reflect.RefObject; 29 import org.netbeans.mdr.storagemodel.StorableObject; 30 31 35 class AssociationLinkWrapper implements RefAssociationLink { 36 private static final IdentityCache identityCache = new IdentityCache(); 37 38 private final AssociationLink innerLink; 39 40 41 private AssociationLinkWrapper(AssociationLink innerLink) { 42 this.innerLink = innerLink; 43 } 44 45 AssociationLink getInnerLink() { 46 return innerLink; 47 } 48 49 public RefObject refFirstEnd() { 50 StorableObject fe = innerLink.getFirstEnd(); 51 return (RefObject) fe.getMdrStorage().getRepository().getHandler(fe); 52 } 53 54 public RefObject refSecondEnd() { 55 StorableObject se = innerLink.getSecondEnd(); 56 return (RefObject) se.getMdrStorage().getRepository().getHandler(se); 57 } 58 59 public boolean equals(Object other) { 60 if (other instanceof AssociationLinkWrapper) { 61 return this == other; 62 } else return (other instanceof RefAssociationLink) && 63 ((RefAssociationLink) other).refFirstEnd().equals(refFirstEnd()) && 64 ((RefAssociationLink) other).refSecondEnd().equals(refSecondEnd()); 65 } 66 67 public int hashCode() { 68 return (innerLink.getFirstEnd().getMofId().toString() + innerLink.getSecondEnd().getMofId().toString()).hashCode(); 69 } 70 71 static RefAssociationLink wrapLink(AssociationLink innerLink) { 72 if (innerLink == null) { 73 return null; 74 } 75 76 String linkID = innerLink.getFirstEnd().getMofId().toString() + ":" + innerLink.getSecondEnd().getMofId().toString(); 77 78 synchronized (identityCache) { 79 RefAssociationLink result = (RefAssociationLink) identityCache.get(linkID); 80 if (result == null) { 81 result = new AssociationLinkWrapper(innerLink); 82 identityCache.put(linkID, result); 83 } 84 return result; 85 } 86 } 87 88 private static class IdentityCache extends HashMap { 89 private final ReferenceQueue queue = new ReferenceQueue (); 90 91 private class LinkReference extends WeakReference { 92 private String mofId; 93 94 public LinkReference(String key, RefAssociationLink link) { 95 super(link, queue); 96 97 mofId = key; 98 } 99 100 public String getProxyMofId() { 101 return mofId; 102 } 103 } 104 105 private void cleanUp() { 106 LinkReference reference; 107 108 while ((reference = (LinkReference) queue.poll()) != null) { 109 this.remove(reference.getProxyMofId()); 111 } 112 } 113 114 public Object put(Object key, Object value) { 115 cleanUp(); 116 Object result = super.put(key, new LinkReference((String ) key, (RefAssociationLink) value)); 117 if (result != null) { 118 return ((LinkReference) result).get(); 119 } else { 120 return result; 121 } 122 } 123 124 public Object get(Object key) { 125 cleanUp(); 126 Object result = super.get(key); 127 if (result != null) { 128 return ((LinkReference) result).get(); 129 } else { 130 return result; 131 } 132 } 133 } 134 } 135 | Popular Tags |