alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.registry.mergedctx;

import org.netbeans.api.registry.ObjectRef;
import org.netbeans.spi.registry.BasicContext;
import org.netbeans.spi.registry.SpiUtils;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.*;

/**
 * Provides caching mechanism for BasicContextImpl and RootContextImpl, which should be constructed
 * exclusively by calling these factory methods: createRootContext, createContext, getOrCreateParentCtx
 * and getOrCreateSubCtx.
 *
 * @author Radek Matous
 */
final class Cache {
    /**  all live MergedContext provided by this impl.*/
    private final Map contextCache = Collections.synchronizedMap(new WeakHashMap());
    private final Map objectCache = Collections.synchronizedMap(new WeakHashMap());

    Cache() {
    }

    Object getContextSync() {
        return contextCache;
    }

    BasicContextImpl cacheContext(final BasicContextImpl ctxImpl) {
        final boolean isRoot = ctxImpl.getAbsolutePath().isRoot();

        Reference retVal = (isRoot) ? new HardReference(ctxImpl) : new SoftReference(ctxImpl);
        retVal = (Reference) contextCache.put(ctxImpl.getAbsolutePath(), retVal);
        return (retVal == null) ? null : (BasicContextImpl) retVal.get();
    }

    BasicContextImpl getContext(final Resource resource) {
        final Reference ref = (Reference) contextCache.get(resource);
        return (ref == null) ? null : (BasicContextImpl) ref.get();
    }

    void removeContext(final Resource resource) {
        contextCache.remove(resource);
    }

    void cacheObjectRef(final BasicContext root, final BasicContext ctx, final String bindingName, final Object object) {
        if (object != null) {
            objectCache.put(object, SpiUtils.createObjectRef(ctx, null, bindingName));
        }
    }

    void removeObjectRef(final Object object) {
        if (object != null)
            objectCache.remove(object);
    }

    ObjectRef getObjectRef(final Object object) {
        return (ObjectRef) objectCache.get(object);
    }

    Collection existingSubcontexts(final BasicContextImpl ctxImpl) {
        final List eSubctxs = new ArrayList();
        synchronized (contextCache) {
            for (Iterator iterator = contextCache.values().iterator(); iterator.hasNext();) {
                final Reference ref = (Reference) iterator.next();
                final BasicContextImpl iCtx = (BasicContextImpl) ((ref == null) ? null : ref.get());
                if (iCtx != null && ctxImpl.getAbsolutePath().isSuperior(iCtx.getAbsolutePath())) {
                    eSubctxs.add(iCtx);
                }
            }
        }
        Collections.sort(eSubctxs, new Comparator() {
            public int compare(final Object o1, final Object o2) {
                final BasicContextImpl ctx1 = (BasicContextImpl) o1;
                final BasicContextImpl ctx2 = (BasicContextImpl) o2;
                final int len1 = ctx1.getAbsolutePath().getPath().length();
                final int len2 = ctx2.getAbsolutePath().getPath().length();
                return (len2 - len1);
            }
        });

        return Collections.unmodifiableCollection(eSubctxs);
    }


    private static final class HardReference extends SoftReference {
        Object referent;

        public HardReference(final Object referent) {
            super(referent);
            this.referent = referent;
        }

        public Object get() {
            return referent;
        }
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.