1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.net.URL ; 14 import java.util.*; 15 import org.osgi.framework.*; 16 17 public class PolicyHandler { 18 private final static String DEPENDENT_POLICY = "dependent"; private final static String GLOBAL_POLICY = "global"; private final static String REGISTERED_POLICY = "registered"; private final static String APP_POLICY = "app"; private final static String EXT_POLICY = "ext"; private final static String BOOT_POLICY = "boot"; private final static String PARENT_POLICY = "parent"; 27 BundleLoader policedLoader; 29 Object [] policies = null; 31 32 private ThreadLocal beingLoaded; 34 35 private BundleListener listener = new BundleListener() { 36 public void bundleChanged(BundleEvent event) { 37 if (event.getType() == BundleEvent.STARTED || event.getType() == BundleEvent.STOPPED) 38 return; 39 try { 40 String list = (String ) policedLoader.getBundle().getBundleData().getManifest().get(Constants.BUDDY_LOADER); 41 synchronized (this) { 42 policies = getArrayFromList(list); 43 } 44 } catch (BundleException e) { 45 } 47 } 48 }; 49 50 public PolicyHandler(BundleLoader loader, String buddyList) { 51 policedLoader = loader; 52 policies = getArrayFromList(buddyList); 53 beingLoaded = new ThreadLocal (); 54 policedLoader.bundle.framework.systemBundle.context.addBundleListener(listener); 55 } 56 57 static Object [] getArrayFromList(String stringList) { 58 if (stringList == null || stringList.trim().equals("")) return null; 60 Vector list = new Vector(); 61 StringTokenizer tokens = new StringTokenizer(stringList, ","); while (tokens.hasMoreTokens()) { 63 String token = tokens.nextToken().trim(); 64 if (!token.equals("")) list.addElement(token); 66 } 67 return list.isEmpty() ? new Object [0] : (Object []) list.toArray(new Object [list.size()]); 68 } 69 70 private synchronized IBuddyPolicy getPolicyImplementation(int policyOrder) { 71 if (policies[policyOrder] instanceof String ) { 72 String buddyName = (String ) policies[policyOrder]; 73 74 if (REGISTERED_POLICY.equals(buddyName)) { 75 policies[policyOrder] = new RegisteredPolicy(policedLoader); 76 return (IBuddyPolicy) policies[policyOrder]; 77 } 78 if (BOOT_POLICY.equals(buddyName)) { 79 policies[policyOrder] = SystemPolicy.getInstance(SystemPolicy.BOOT); 80 return (IBuddyPolicy) policies[policyOrder]; 81 } 82 if (APP_POLICY.equals(buddyName)) { 83 policies[policyOrder] = SystemPolicy.getInstance(SystemPolicy.APP); 84 return (IBuddyPolicy) policies[policyOrder]; 85 } 86 if (EXT_POLICY.equals(buddyName)) { 87 policies[policyOrder] = SystemPolicy.getInstance(SystemPolicy.EXT); 88 return (IBuddyPolicy) policies[policyOrder]; 89 } 90 if (DEPENDENT_POLICY.equals(buddyName)) { 91 policies[policyOrder] = new DependentPolicy(policedLoader); 92 return (IBuddyPolicy) policies[policyOrder]; 93 } 94 if (GLOBAL_POLICY.equals(buddyName)) { 95 policies[policyOrder] = new GlobalPolicy(policedLoader.bundle.framework.packageAdmin); 96 return (IBuddyPolicy) policies[policyOrder]; 97 } 98 if (PARENT_POLICY.equals(buddyName)) { 99 policies[policyOrder] = new SystemPolicy(policedLoader.getParentClassLoader()); 100 return (IBuddyPolicy) policies[policyOrder]; 101 } 102 103 } 129 return (IBuddyPolicy) policies[policyOrder]; 130 } 131 132 public Class doBuddyClassLoading(String name) { 133 if (startLoading(name) == false) 134 return null; 135 136 Class result = null; 137 for (int i = 0; i < policies.length && result == null; i++) { 138 result = getPolicyImplementation(i).loadClass(name); 139 } 140 stopLoading(name); 141 return result; 142 } 143 144 public URL doBuddyResourceLoading(String name) { 145 if (startLoading(name) == false) 146 return null; 147 148 if (policies == null) 149 return null; 150 URL result = null; 151 for (int i = 0; i < policies.length && result == null; i++) { 152 result = getPolicyImplementation(i).loadResource(name); 153 } 154 stopLoading(name); 155 return result; 156 } 157 158 public Enumeration doBuddyResourcesLoading(String name) { 159 if (startLoading(name) == false) 160 return null; 161 162 if (policies == null) 163 return null; 164 Vector results = null; 165 for (int i = 0; i < policies.length; i++) { 166 Enumeration result = getPolicyImplementation(i).loadResources(name); 167 if (result != null) { 168 if (results == null) 169 results = new Vector(policies.length); 170 while (result.hasMoreElements()) { 171 Object url = result.nextElement(); 172 if (!results.contains(url)) results.add(url); 174 } 175 } 176 } 177 stopLoading(name); 178 return results == null || results.isEmpty() ? null : results.elements(); 179 } 180 181 private boolean startLoading(String name) { 182 Set classesAndResources = (Set) beingLoaded.get(); 183 if (classesAndResources != null && classesAndResources.contains(name)) 184 return false; 185 186 if (classesAndResources == null) { 187 classesAndResources = new HashSet(3); 188 beingLoaded.set(classesAndResources); 189 } 190 classesAndResources.add(name); 191 return true; 192 } 193 194 private void stopLoading(String name) { 195 ((Set) beingLoaded.get()).remove(name); 196 } 197 198 public void close() { 199 policedLoader.bundle.framework.systemBundle.context.removeBundleListener(listener); 200 } 201 } 202 | Popular Tags |