KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > PolicyHandler


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.internal.core;
12
13 import java.net.URL JavaDoc;
14 import java.util.*;
15 import org.osgi.framework.*;
16
17 public class PolicyHandler {
18     //Key for the framework buddies
19
private final static String JavaDoc DEPENDENT_POLICY = "dependent"; //$NON-NLS-1$
20
private final static String JavaDoc GLOBAL_POLICY = "global"; //$NON-NLS-1$
21
private final static String JavaDoc REGISTERED_POLICY = "registered"; //$NON-NLS-1$
22
private final static String JavaDoc APP_POLICY = "app"; //$NON-NLS-1$
23
private final static String JavaDoc EXT_POLICY = "ext"; //$NON-NLS-1$
24
private final static String JavaDoc BOOT_POLICY = "boot"; //$NON-NLS-1$
25
private final static String JavaDoc PARENT_POLICY = "parent"; //$NON-NLS-1$
26

27     //The loader to which this policy is attached.
28
BundleLoader policedLoader;
29     //List of the policies as well as cache for the one that have been created. The size of this array never changes over time. This is why the synchronization is not done when iterating over it.
30
Object JavaDoc[] policies = null;
31
32     //Support to cut class / resource loading cycles in the context of one thread. The contained object is a set of classname
33
private ThreadLocal JavaDoc 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 JavaDoc list = (String JavaDoc) policedLoader.getBundle().getBundleData().getManifest().get(Constants.BUDDY_LOADER);
41                 synchronized (this) {
42                     policies = getArrayFromList(list);
43                 }
44             } catch (BundleException e) {
45                 //Ignore
46
}
47         }
48     };
49
50     public PolicyHandler(BundleLoader loader, String JavaDoc buddyList) {
51         policedLoader = loader;
52         policies = getArrayFromList(buddyList);
53         beingLoaded = new ThreadLocal JavaDoc();
54         policedLoader.bundle.framework.systemBundle.context.addBundleListener(listener);
55     }
56
57     static Object JavaDoc[] getArrayFromList(String JavaDoc stringList) {
58         if (stringList == null || stringList.trim().equals("")) //$NON-NLS-1$
59
return null;
60         Vector list = new Vector();
61         StringTokenizer tokens = new StringTokenizer(stringList, ","); //$NON-NLS-1$
62
while (tokens.hasMoreTokens()) {
63             String JavaDoc token = tokens.nextToken().trim();
64             if (!token.equals("")) //$NON-NLS-1$
65
list.addElement(token);
66         }
67         return list.isEmpty() ? new Object JavaDoc[0] : (Object JavaDoc[]) list.toArray(new Object JavaDoc[list.size()]);
68     }
69
70     private synchronized IBuddyPolicy getPolicyImplementation(int policyOrder) {
71         if (policies[policyOrder] instanceof String JavaDoc) {
72             String JavaDoc buddyName = (String JavaDoc) 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             // //Buddy policy can be provided by service implementations
104
// BundleContext fwkCtx = policedLoader.bundle.framework.systemBundle.context;
105
// ServiceReference[] matchingBuddies = null;
106
// try {
107
// matchingBuddies = fwkCtx.getAllServiceReferences(IBuddyPolicy.class.getName(), "buddyName=" + buddyName);
108
// } catch (InvalidSyntaxException e) {
109
// //The filter is valid
110
// }
111
// if (matchingBuddies == null)
112
// return new IBuddyPolicy() {
113
// public Class loadClass(String name) {
114
// return null;
115
// }
116
//
117
// public URL loadResource(String name) {
118
// return null;
119
// }
120
//
121
// public Enumeration loadResources(String name) {
122
// return null;
123
// }
124
// };
125
//
126
// //The policies loaded through service are not cached
127
// return ((IBuddyPolicy) fwkCtx.getService(matchingBuddies[0]));
128
}
129         return (IBuddyPolicy) policies[policyOrder];
130     }
131
132     public Class JavaDoc doBuddyClassLoading(String JavaDoc name) {
133         if (startLoading(name) == false)
134             return null;
135
136         Class JavaDoc 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 JavaDoc doBuddyResourceLoading(String JavaDoc name) {
145         if (startLoading(name) == false)
146             return null;
147
148         if (policies == null)
149             return null;
150         URL JavaDoc 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 JavaDoc 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 JavaDoc url = result.nextElement();
172                     if (!results.contains(url)) //only add if not already added
173
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 JavaDoc 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 JavaDoc 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