01: /*****************************************************************************
02: * Copyright (C) NanoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: *****************************************************************************/package org.nanocontainer.script;
09:
10: import org.nanocontainer.ClassPathElement;
11: import org.nanocontainer.NanoContainer;
12:
13: import java.net.URL;
14: import java.net.MalformedURLException;
15: import java.security.AccessController;
16: import java.security.PrivilegedAction;
17: import java.io.File;
18:
19: public class ClassPathElementHelper {
20: public static final String HTTP = "http://";
21:
22: public static ClassPathElement addClassPathElement(
23: final String path, NanoContainer nanoContainer) {
24: URL pathURL = null;
25: try {
26: if (path.toLowerCase().startsWith(HTTP)) {
27: pathURL = new URL(path);
28: } else {
29: Object rVal = AccessController
30: .doPrivileged(new PrivilegedAction() {
31: public Object run() {
32: try {
33: File file = new File(path);
34: if (!file.exists()) {
35: return new NanoContainerMarkupException(
36: "classpath '"
37: + path
38: + "' does not exist ");
39: }
40: return file.toURL();
41: } catch (MalformedURLException e) {
42: return e;
43: }
44:
45: }
46: });
47: if (rVal instanceof MalformedURLException) {
48: throw (MalformedURLException) rVal;
49: }
50: if (rVal instanceof NanoContainerMarkupException) {
51: throw (NanoContainerMarkupException) rVal;
52: }
53: pathURL = (URL) rVal;
54: }
55: } catch (MalformedURLException e) {
56: throw new NanoContainerMarkupException("classpath '" + path
57: + "' malformed ", e);
58: }
59: return nanoContainer.addClassLoaderURL(pathURL);
60: }
61: }
|