KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > ClassPathElementHelper


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9
10 package org.nanocontainer.script;
11
12 import org.nanocontainer.ClassPathElement;
13 import org.nanocontainer.NanoContainer;
14
15 import java.net.URL JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.security.AccessController JavaDoc;
18 import java.security.PrivilegedAction JavaDoc;
19 import java.io.File JavaDoc;
20
21 public class ClassPathElementHelper {
22     public static final String JavaDoc HTTP = "http://";
23
24     public static ClassPathElement addClassPathElement(final String JavaDoc path, NanoContainer nanoContainer) {
25         URL JavaDoc pathURL = null;
26         try {
27             if (path.toLowerCase().startsWith(HTTP)) {
28                 pathURL = new URL JavaDoc(path);
29             } else {
30                 Object JavaDoc rVal = AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
31                     public Object JavaDoc run() {
32                         try {
33                             File JavaDoc file = new File JavaDoc(path);
34                             if (!file.exists()) {
35                                 return new NanoContainerMarkupException("classpath '" + path + "' does not exist ");
36                             }
37                             return file.toURL();
38                         } catch (MalformedURLException JavaDoc e) {
39                             return e;
40                         }
41
42                     }
43                 });
44                 if (rVal instanceof MalformedURLException JavaDoc) {
45                     throw (MalformedURLException JavaDoc) rVal;
46                 }
47                 if (rVal instanceof NanoContainerMarkupException) {
48                     throw (NanoContainerMarkupException) rVal;
49                 }
50                 pathURL = (URL JavaDoc) rVal;
51             }
52         } catch (MalformedURLException JavaDoc e) {
53             throw new NanoContainerMarkupException("classpath '" + path + "' malformed ", e);
54         }
55         return nanoContainer.addClassLoaderURL(pathURL);
56     }
57 }
58
Popular Tags