1 19 package org.netbeans.mdr.util; 20 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import java.security.AllPermission ; 24 import java.security.PermissionCollection ; 25 import java.security.Permissions ; 26 import java.security.ProtectionDomain ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.WeakHashMap ; 30 31 37 public abstract class ImplClass { 38 39 40 protected static final String classNameSuffix = "$Impl"; 41 private static final ProtectionDomain pd; 42 43 private static final Map loaderToCache = new WeakHashMap (3); 44 private static final Object pendingGenerationMarker = new Object (); 45 46 static { 47 PermissionCollection pc = new Permissions (); 48 pc.add(new AllPermission ()); 49 pd = new ProtectionDomain (null, pc); 50 } 51 52 public static String getClassNameSuffix() { 53 return classNameSuffix; 54 } 55 56 62 protected static void check(ClassLoader loader, Class ifc) throws IllegalArgumentException { 63 Class interfaceClass = null; 64 65 try { 66 interfaceClass = Class.forName(ifc.getName(), false, loader); 67 } catch (ClassNotFoundException e) { 68 } 69 if (interfaceClass != ifc) { 70 throw new IllegalArgumentException (ifc + " is not visible from class loader"); 71 } 72 73 if (!interfaceClass.isInterface()) { 74 throw new IllegalArgumentException (interfaceClass.getName() + " is not an interface"); 75 } 76 } 77 78 81 protected static String getName(Class ifc) { 82 return ifc.getName() + classNameSuffix; 83 } 84 85 88 protected static Map getLoaderCache(ClassLoader loader) { 89 Map cache; 90 synchronized (loaderToCache) { 91 cache = (Map ) loaderToCache.get(loader); 92 if (cache == null) { 93 cache = new HashMap (3); 94 loaderToCache.put(loader, cache); 95 } 96 } 97 98 return cache; 99 } 100 101 119 protected static Class getFromCache(Map cache, Class ifc, String className) { 120 Class implClass = null; 121 122 synchronized (cache) { 123 do { 124 Object value = cache.get(className); 125 if (value instanceof Reference ) { 126 implClass = (Class ) ((Reference ) value).get(); 127 } 128 if (implClass != null) { 129 return implClass; 130 } else if (value == pendingGenerationMarker) { 131 try { 132 cache.wait(); 133 } catch (InterruptedException e) { 134 } 135 continue; 136 } else { 137 cache.put(className, pendingGenerationMarker); 138 break; 139 } 140 } while (true); 141 } 142 143 return null; 144 } 145 146 151 protected static void releaseCache(Map cache, Class implClass, String className) { 152 synchronized (cache) { 153 if (implClass != null) { 154 cache.put(className, new WeakReference (implClass)); 155 } else { 156 cache.remove(className); 157 } 158 cache.notifyAll(); 159 } 160 } 161 162 } 174 | Popular Tags |