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.net.URLClassLoader;
19: import java.net.URL;
20: import java.net.URLDecoder;
21: import java.io.File;
22:
23: /*-------------------------------------------------------*/
24: /* System ClassLoader Support */
25: /*-------------------------------------------------------*/
26:
27: public class SystemClassPath extends BasicURLClassPath {
28:
29: private URLClassLoader sysLoader;
30:
31: public void addJarsToPath(File dir) throws Exception {
32: this .addJarsToPath(dir, getSystemLoader());
33: this .rebuildJavaClassPathVariable();
34: }
35:
36: public void addJarToPath(URL jar) throws Exception {
37:
38: this .addJarToPath(jar, getSystemLoader());
39: this .rebuildJavaClassPathVariable();
40: }
41:
42: public ClassLoader getClassLoader() {
43: try {
44: return getSystemLoader();
45: } catch (Exception e) {
46: throw new RuntimeException(e);
47: }
48: }
49:
50: private URLClassLoader getSystemLoader() throws Exception {
51: if (sysLoader == null) {
52: sysLoader = (URLClassLoader) ClassLoader
53: .getSystemClassLoader();
54: }
55: return sysLoader;
56: }
57:
58: private void rebuildJavaClassPathVariable() throws Exception {
59: sun.misc.URLClassPath cp = getURLClassPath(getSystemLoader());
60: URL[] urls = cp.getURLs();
61:
62: if (urls.length < 1)
63: return;
64:
65: StringBuffer path = new StringBuffer(urls.length * 32);
66:
67: File s = new File(URLDecoder.decode(urls[0].getFile()));
68: path.append(s.getPath());
69:
70: for (int i = 1; i < urls.length; i++) {
71: path.append(File.pathSeparator);
72:
73: s = new File(URLDecoder.decode(urls[i].getFile()));
74:
75: path.append(s.getPath());
76: }
77: try {
78: System.setProperty("java.class.path", path.toString());
79: } catch (Exception e) {
80: }
81: }
82: }
|