1 19 package org.netbeans.modules.registry.mergedctx; 20 21 import org.netbeans.api.registry.ObjectRef; 22 import org.netbeans.spi.registry.BasicContext; 23 import org.netbeans.spi.registry.SpiUtils; 24 25 import java.lang.ref.Reference ; 26 import java.lang.ref.SoftReference ; 27 import java.util.*; 28 29 36 final class Cache { 37 38 private final Map contextCache = Collections.synchronizedMap(new WeakHashMap()); 39 private final Map objectCache = Collections.synchronizedMap(new WeakHashMap()); 40 41 Cache() { 42 } 43 44 Object getContextSync() { 45 return contextCache; 46 } 47 48 BasicContextImpl cacheContext(final BasicContextImpl ctxImpl) { 49 final boolean isRoot = ctxImpl.getAbsolutePath().isRoot(); 50 51 Reference retVal = (isRoot) ? new HardReference(ctxImpl) : new SoftReference (ctxImpl); 52 retVal = (Reference ) contextCache.put(ctxImpl.getAbsolutePath(), retVal); 53 return (retVal == null) ? null : (BasicContextImpl) retVal.get(); 54 } 55 56 BasicContextImpl getContext(final Resource resource) { 57 final Reference ref = (Reference ) contextCache.get(resource); 58 return (ref == null) ? null : (BasicContextImpl) ref.get(); 59 } 60 61 void removeContext(final Resource resource) { 62 contextCache.remove(resource); 63 } 64 65 void cacheObjectRef(final BasicContext root, final BasicContext ctx, final String bindingName, final Object object) { 66 if (object != null) { 67 objectCache.put(object, SpiUtils.createObjectRef(ctx, null, bindingName)); 68 } 69 } 70 71 void removeObjectRef(final Object object) { 72 if (object != null) 73 objectCache.remove(object); 74 } 75 76 ObjectRef getObjectRef(final Object object) { 77 return (ObjectRef) objectCache.get(object); 78 } 79 80 Collection existingSubcontexts(final BasicContextImpl ctxImpl) { 81 final List eSubctxs = new ArrayList(); 82 synchronized (contextCache) { 83 for (Iterator iterator = contextCache.values().iterator(); iterator.hasNext();) { 84 final Reference ref = (Reference ) iterator.next(); 85 final BasicContextImpl iCtx = (BasicContextImpl) ((ref == null) ? null : ref.get()); 86 if (iCtx != null && ctxImpl.getAbsolutePath().isSuperior(iCtx.getAbsolutePath())) { 87 eSubctxs.add(iCtx); 88 } 89 } 90 } 91 Collections.sort(eSubctxs, new Comparator() { 92 public int compare(final Object o1, final Object o2) { 93 final BasicContextImpl ctx1 = (BasicContextImpl) o1; 94 final BasicContextImpl ctx2 = (BasicContextImpl) o2; 95 final int len1 = ctx1.getAbsolutePath().getPath().length(); 96 final int len2 = ctx2.getAbsolutePath().getPath().length(); 97 return (len2 - len1); 98 } 99 }); 100 101 return Collections.unmodifiableCollection(eSubctxs); 102 } 103 104 105 private static final class HardReference extends SoftReference { 106 Object referent; 107 108 public HardReference(final Object referent) { 109 super(referent); 110 this.referent = referent; 111 } 112 113 public Object get() { 114 return referent; 115 } 116 } 117 118 } 119 | Popular Tags |