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