01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.loader;
17:
18: import java.io.File;
19: import java.net.URL;
20: import java.net.URLClassLoader;
21: import java.security.AccessController;
22: import java.security.PrivilegedAction;
23:
24: public abstract class BasicURLClassPath implements ClassPath {
25: public static ClassLoader getContextClassLoader() {
26: return (ClassLoader) java.security.AccessController
27: .doPrivileged(new java.security.PrivilegedAction() {
28: public Object run() {
29: return Thread.currentThread()
30: .getContextClassLoader();
31: }
32: });
33: }
34:
35: private java.lang.reflect.Field ucpField;
36:
37: protected void addJarToPath(final URL jar,
38: final URLClassLoader loader) throws Exception {
39: this .getURLClassPath(loader).addURL(jar);
40: }
41:
42: protected void addJarsToPath(final File dir,
43: final URLClassLoader loader) throws Exception {
44: if (dir == null || !dir.exists())
45: return;
46:
47: String[] jarNames = dir.list(new java.io.FilenameFilter() {
48: public boolean accept(File dir, String name) {
49:
50: return (name.endsWith(".jar") || name.endsWith(".zip"));
51: }
52: });
53:
54: final URL[] jars = new URL[jarNames.length];
55: for (int j = 0; j < jarNames.length; j++) {
56: jars[j] = new File(dir, jarNames[j]).toURL();
57: }
58:
59: sun.misc.URLClassPath path = getURLClassPath(loader);
60: for (int i = 0; i < jars.length; i++) {
61:
62: path.addURL(jars[i]);
63: }
64: }
65:
66: protected sun.misc.URLClassPath getURLClassPath(
67: URLClassLoader loader) throws Exception {
68: return (sun.misc.URLClassPath) getUcpField().get(loader);
69: }
70:
71: private java.lang.reflect.Field getUcpField() throws Exception {
72: if (ucpField == null) {
73:
74: ucpField = (java.lang.reflect.Field) AccessController
75: .doPrivileged(new PrivilegedAction() {
76: public Object run() {
77: java.lang.reflect.Field ucp = null;
78: try {
79: ucp = URLClassLoader.class
80: .getDeclaredField("ucp");
81: ucp.setAccessible(true);
82: } catch (Exception e2) {
83: e2.printStackTrace();
84: }
85: return ucp;
86: }
87: });
88: }
89:
90: return ucpField;
91: }
92:
93: }
|