KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17 import java.util.Enumeration JavaDoc;
18
19 public class SystemPolicy implements IBuddyPolicy {
20
21     private static class ParentClassLoader extends ClassLoader JavaDoc {
22         protected ParentClassLoader() {
23             super(null);
24         }
25     }
26
27     public static final byte BOOT = 0;
28     public static final byte EXT = 1;
29     public static final byte APP = 2;
30
31     private static SystemPolicy[] instances = new SystemPolicy[3];
32
33     private ClassLoader JavaDoc classLoader;
34
35     public static SystemPolicy getInstance(final byte type) {
36         if (instances[type] == null) {
37             instances[type] = new SystemPolicy();
38             instances[type].classLoader = (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
39                 public Object JavaDoc run() {
40                     return createClassLoader(type);
41                 }
42             });
43         }
44         return instances[type];
45     }
46
47     public SystemPolicy() {
48         //Nothing to do
49
}
50     
51     public SystemPolicy(ClassLoader JavaDoc parent) {
52         classLoader = parent;
53     }
54     
55     static ClassLoader JavaDoc createClassLoader(byte type) {
56         switch (type) {
57             case APP :
58                 if (ClassLoader.getSystemClassLoader() != null)
59                     return ClassLoader.getSystemClassLoader();
60                 return new ParentClassLoader();
61
62             case BOOT :
63                 return new ParentClassLoader();
64
65             case EXT :
66                 if (ClassLoader.getSystemClassLoader() != null)
67                     return ClassLoader.getSystemClassLoader().getParent();
68                 return new ParentClassLoader();
69         }
70         return null;
71     }
72
73     public Class JavaDoc loadClass(String JavaDoc name) {
74         try {
75             return classLoader.loadClass(name);
76         } catch (ClassNotFoundException JavaDoc e) {
77             return null;
78         }
79     }
80
81     public URL JavaDoc loadResource(String JavaDoc name) {
82         return classLoader.getResource(name);
83     }
84
85     public Enumeration JavaDoc loadResources(String JavaDoc name) {
86         try {
87             return classLoader.getResources(name);
88         } catch (IOException JavaDoc e) {
89             return null;
90         }
91     }
92
93 }
94
Popular Tags