JETTY-1439 space in directory installation path causes classloader problem

This commit is contained in:
Jan Bartel 2011-09-28 11:26:24 +10:00
parent 12b04e4e7f
commit 76c0479ed3
1 changed files with 61 additions and 93 deletions

View File

@ -23,36 +23,39 @@ import java.net.URLClassLoader;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
/** /**
* Class to handle CLASSPATH construction * Class to handle CLASSPATH construction
*/ */
public class Classpath { public class Classpath
{
private final Vector<File> _elements = new Vector<File>(); private final Vector<File> _elements = new Vector<File>();
public Classpath() public Classpath()
{} {
}
public Classpath(String initial) public Classpath(String initial)
{ {
addClasspath(initial); addClasspath(initial);
} }
public File[] getElements() public File[] getElements()
{ {
return _elements.toArray(new File[_elements.size()]); return _elements.toArray(new File[_elements.size()]);
} }
public int count() public int count()
{ {
return _elements.size(); return _elements.size();
} }
public boolean addComponent(String component) public boolean addComponent(String component)
{ {
if ((component != null)&&(component.length()>0)) { if ((component != null) && (component.length() > 0))
try { {
try
{
File f = new File(component); File f = new File(component);
if (f.exists()) if (f.exists())
{ {
@ -63,36 +66,46 @@ public class Classpath {
return true; return true;
} }
} }
} catch (IOException e) {} }
catch (IOException e)
{
}
} }
return false; return false;
} }
public boolean addComponent(File component) public boolean addComponent(File component)
{ {
if (component != null) { if (component != null)
try { {
if (component.exists()) { try
{
if (component.exists())
{
File key = component.getCanonicalFile(); File key = component.getCanonicalFile();
if (!_elements.contains(key)) { if (!_elements.contains(key))
{
_elements.add(key); _elements.add(key);
return true; return true;
} }
} }
} catch (IOException e) {} }
catch (IOException e)
{
}
} }
return false; return false;
} }
public boolean addClasspath(String s) public boolean addClasspath(String s)
{ {
boolean added=false; boolean added = false;
if (s != null) if (s != null)
{ {
StringTokenizer t = new StringTokenizer(s, File.pathSeparator); StringTokenizer t = new StringTokenizer(s, File.pathSeparator);
while (t.hasMoreTokens()) while (t.hasMoreTokens())
{ {
added|=addComponent(t.nextToken()); added |= addComponent(t.nextToken());
} }
} }
return added; return added;
@ -103,40 +116,49 @@ public class Classpath {
int i = 0; int i = 0;
for (File element : _elements) for (File element : _elements)
{ {
out.printf("%2d: %s\n",i++,element.getAbsolutePath()); out.printf("%2d: %s\n", i++, element.getAbsolutePath());
} }
} }
@Override @Override
public String toString() public String toString()
{ {
StringBuffer cp = new StringBuffer(1024); StringBuffer cp = new StringBuffer(1024);
int cnt = _elements.size(); int cnt = _elements.size();
if (cnt >= 1) { if (cnt >= 1)
cp.append( ((_elements.elementAt(0))).getPath() ); {
cp.append(((_elements.elementAt(0))).getPath());
} }
for (int i=1; i < cnt; i++) { for (int i = 1; i < cnt; i++)
{
cp.append(File.pathSeparatorChar); cp.append(File.pathSeparatorChar);
cp.append( ((_elements.elementAt(i))).getPath() ); cp.append(((_elements.elementAt(i))).getPath());
} }
return cp.toString(); return cp.toString();
} }
public ClassLoader getClassLoader() { public ClassLoader getClassLoader()
{
int cnt = _elements.size(); int cnt = _elements.size();
URL[] urls = new URL[cnt]; URL[] urls = new URL[cnt];
for (int i=0; i < cnt; i++) { for (int i = 0; i < cnt; i++)
try { {
String u=_elements.elementAt(i).toURI().toURL().toString(); try
urls[i] = new URL(encodeFileURL(u)); {
} catch (MalformedURLException e) {} urls[i] = _elements.elementAt(i).toURI().toURL();
}
catch (MalformedURLException e)
{
}
} }
ClassLoader parent = Thread.currentThread().getContextClassLoader(); ClassLoader parent = Thread.currentThread().getContextClassLoader();
if (parent == null) { if (parent == null)
{
parent = Classpath.class.getClassLoader(); parent = Classpath.class.getClassLoader();
} }
if (parent == null) { if (parent == null)
{
parent = ClassLoader.getSystemClassLoader(); parent = ClassLoader.getSystemClassLoader();
} }
return new Loader(urls, parent); return new Loader(urls, parent);
@ -152,71 +174,17 @@ public class Classpath {
@Override @Override
public String toString() public String toString()
{ {
return "startJarLoader@"+Long.toHexString(hashCode()); return "startJarLoader@" + Long.toHexString(hashCode());
} }
} }
public static String encodeFileURL(String path)
{
byte[] bytes;
try
{
bytes=path.getBytes("utf-8");
}
catch (UnsupportedEncodingException e)
{
bytes=path.getBytes();
}
StringBuffer buf = new StringBuffer(bytes.length*2);
buf.append("file:");
synchronized(buf)
{
for (int i=5;i<bytes.length;i++)
{
byte b=bytes[i];
switch(b)
{
case '%':
buf.append("%25");
continue;
case ' ':
buf.append("%20");
continue;
case '/':
case '.':
case '-':
case '_':
buf.append((char)b);
continue;
default:
// let's be over conservative here!
if (Character.isJavaIdentifierPart((char)b))
{
if(b>='a' && b<='z' || b>='A' && b<='Z' || b>='0' && b<='9')
{
buf.append((char)b);
continue;
}
}
buf.append('%');
buf.append(Integer.toHexString((0xf0&b)>>4));
buf.append(Integer.toHexString((0x0f&b)));
continue;
}
}
}
return buf.toString();
}
/** /**
* Overlay another classpath, copying its elements into place on this Classpath, while eliminating duplicate entries * Overlay another classpath, copying its elements into place on this
* on the classpath. * Classpath, while eliminating duplicate entries on the classpath.
* *
* @param cpOther * @param cpOther the other classpath to overlay
* the other classpath to overlay
*/ */
public void overlay(Classpath cpOther) public void overlay(Classpath cpOther)
{ {