001 /******************************************************************************* 002 * Copyright (C) PicoContainer Organization. All rights reserved. 003 * --------------------------------------------------------------------------- 004 * The software in this package is published under the terms of the BSD style 005 * license a copy of which has been included with this distribution in the 006 * LICENSE.txt file. 007 ******************************************************************************/ 008 package org.picocontainer.script.util; 009 010 011 import java.net.URL; 012 import java.net.MalformedURLException; 013 import java.security.AccessController; 014 import java.security.PrivilegedAction; 015 import java.io.File; 016 017 import org.picocontainer.classname.ClassPathElement; 018 import org.picocontainer.classname.ClassLoadingPicoContainer; 019 import org.picocontainer.script.ScriptedPicoContainerMarkupException; 020 021 public class ClassPathElementHelper { 022 public static final String HTTP = "http://"; 023 024 public static ClassPathElement addClassPathElement(final String path, ClassLoadingPicoContainer container) { 025 URL pathURL; 026 try { 027 if (path.toLowerCase().startsWith(HTTP)) { 028 pathURL = new URL(path); 029 } else { 030 Object rVal = AccessController.doPrivileged(new PrivilegedAction<Object>() { 031 public Object run() { 032 try { 033 File file = new File(path); 034 if (!file.exists()) { 035 return new ScriptedPicoContainerMarkupException("classpath '" + path + "' does not exist "); 036 } 037 return file.toURI().toURL(); 038 } catch (MalformedURLException e) { 039 return e; 040 } 041 042 } 043 }); 044 if (rVal instanceof MalformedURLException) { 045 throw (MalformedURLException) rVal; 046 } 047 if (rVal instanceof ScriptedPicoContainerMarkupException) { 048 throw (ScriptedPicoContainerMarkupException) rVal; 049 } 050 pathURL = (URL) rVal; 051 } 052 } catch (MalformedURLException e) { 053 throw new ScriptedPicoContainerMarkupException("classpath '" + path + "' malformed ", e); 054 } 055 return container.addClassLoaderURL(pathURL); 056 } 057 }