Improved handling of overlays and resourceCollections

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@652 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-08-06 12:30:24 +00:00
parent d6e61ed545
commit 8913557e88
7 changed files with 119 additions and 32 deletions

View File

@ -105,6 +105,14 @@ class BadResource extends URLResource
return null; return null;
} }
/* ------------------------------------------------------------ */
@Override
public void copyTo(File destination)
throws IOException
{
throw new SecurityException(_message);
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public String toString() public String toString()
{ {

View File

@ -25,6 +25,7 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.security.Permission; import java.security.Permission;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
@ -347,8 +348,26 @@ public class FileResource extends URLResource
/** /**
* @return the hashcode. * @return the hashcode.
*/ */
@Override
public int hashCode() public int hashCode()
{ {
return null == _file ? super.hashCode() : _file.hashCode(); return null == _file ? super.hashCode() : _file.hashCode();
} }
/* ------------------------------------------------------------ */
@Override
public void copyTo(File destination)
throws IOException
{
if (isDirectory())
{
IO.copyDir(getFile(),destination);
}
else
{
if (destination.exists())
throw new IllegalArgumentException(destination+" exists");
IO.copy(getFile(),destination);
}
}
} }

View File

@ -115,13 +115,23 @@ public class JarResource extends URLResource
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public static void extract(Resource resource, File directory, boolean deleteOnExit) @Deprecated
public void extract(File dest, boolean deleteOnExit)
throws IOException throws IOException
{ {
if(Log.isDebugEnabled())Log.debug("Extract "+resource+" to "+directory); if (deleteOnExit)
dest.deleteOnExit();
copyTo(dest);
}
/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory)
throws IOException
{
if(Log.isDebugEnabled())Log.debug("Extract "+this+" to "+directory);
String urlString = resource.getURL().toExternalForm().trim(); String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/"); int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0?4:0); int startOfJarUrl = (endOfJarUrl >= 0?4:0);
@ -213,8 +223,6 @@ public class JarResource extends URLResource
if (entry.getTime()>=0) if (entry.getTime()>=0)
file.setLastModified(entry.getTime()); file.setLastModified(entry.getTime());
} }
if (deleteOnExit)
file.deleteOnExit();
} }
if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF")))
@ -233,10 +241,10 @@ public class JarResource extends URLResource
IO.close(jin); IO.close(jin);
} }
/* ------------------------------------------------------------ */ public static Resource newJarResource(Resource resource) throws IOException
public void extract(File directory, boolean deleteOnExit)
throws IOException
{ {
extract(this,directory,deleteOnExit); if (resource instanceof JarResource)
return resource;
return Resource.newResource("jar:" + resource + "!/");
} }
} }

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.util.resource; package org.eclipse.jetty.util.resource;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -609,6 +610,13 @@ public abstract class Resource implements Serializable
} }
} }
/* ------------------------------------------------------------ */
public void copyTo(File destination)
throws IOException
{
if (destination.exists())
throw new IllegalArgumentException(destination+" exists");
writeTo(new FileOutputStream(destination),0,-1);
}
} }

View File

@ -422,15 +422,26 @@ public class ResourceCollection extends Resource
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override
public boolean renameTo(Resource dest) throws SecurityException public boolean renameTo(Resource dest) throws SecurityException
{ {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/* ------------------------------------------------------------ */
@Override
public void copyTo(File destination)
throws IOException
{
for (int r=_resources.length;r-->0;)
_resources[r].copyTo(destination);
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* @return the list of resources separated by a path separator * @return the list of resources separated by a path separator
*/ */
@Override
public String toString() public String toString()
{ {
if(_resources==null) if(_resources==null)
@ -442,7 +453,8 @@ public class ResourceCollection extends Resource
return buffer.toString(); return buffer.toString();
} }
/* ------------------------------------------------------------ */
@Override
public boolean isContainedIn(Resource r) throws MalformedURLException public boolean isContainedIn(Resource r) throws MalformedURLException
{ {
// TODO could look at implementing the semantic of is this collection a subset of the Resource r? // TODO could look at implementing the semantic of is this collection a subset of the Resource r?

View File

@ -14,6 +14,7 @@
package org.eclipse.jetty.util.resource; package org.eclipse.jetty.util.resource;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -62,11 +63,37 @@ public class ResourceCollectionTest extends TestCase
assertEquals("3 - three", getContent(rc, "3.txt")); assertEquals("3 - three", getContent(rc, "3.txt"));
} }
static String getContent(ResourceCollection rc, String path) throws Exception public void testCopyTo() throws Exception
{
ResourceCollection rc = new ResourceCollection(new String[]{
"src/test/resources/org/eclipse/jetty/util/resource/one/",
"src/test/resources/org/eclipse/jetty/util/resource/two/",
"src/test/resources/org/eclipse/jetty/util/resource/three/"
});
File dest = File.createTempFile("copyto",null);
if (dest.exists())
dest.delete();
dest.mkdir();
dest.deleteOnExit();
rc.copyTo(dest);
Resource r = Resource.newResource(dest.toURI());
assertEquals("1 - one", getContent(r, "1.txt"));
assertEquals("2 - two", getContent(r, "2.txt"));
assertEquals("3 - three", getContent(r, "3.txt"));
r = r.addPath("dir");
assertEquals("1 - one", getContent(r, "1.txt"));
assertEquals("2 - two", getContent(r, "2.txt"));
assertEquals("3 - three", getContent(r, "3.txt"));
}
static String getContent(Resource r, String path) throws Exception
{ {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
String line = null; String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(rc.addPath(path).getURL().openStream())); BufferedReader br = new BufferedReader(new InputStreamReader(r.addPath(path).getURL().openStream()));
while((line=br.readLine())!=null) while((line=br.readLine())!=null)
buffer.append(line); buffer.append(line);
br.close(); br.close();

View File

@ -357,14 +357,14 @@ public class WebInfConfiguration implements Configuration
public void unpack (WebAppContext context) throws IOException public void unpack (WebAppContext context) throws IOException
{ {
Resource web_app = context.getBaseResource(); Resource web_app = context.getBaseResource();
if (web_app == null) if (web_app == null)
{ {
String war = context.getWar(); String war = context.getWar();
if (war==null || war.length()==0) if (war!=null && war.length()>0)
war=context.getResourceBase();
// Set dir or WAR
web_app = context.newResource(war); web_app = context.newResource(war);
else
web_app=context.getBaseResource();
// Accept aliases for WAR files // Accept aliases for WAR files
if (web_app.getAlias() != null) if (web_app.getAlias() != null)
@ -380,7 +380,7 @@ public class WebInfConfiguration implements Configuration
if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:")) if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:"))
{ {
// No - then lets see if it can be turned into a jar URL. // No - then lets see if it can be turned into a jar URL.
Resource jarWebApp = context.newResource("jar:" + web_app + "!/"); Resource jarWebApp = JarResource.newJarResource(web_app);
if (jarWebApp.exists() && jarWebApp.isDirectory()) if (jarWebApp.exists() && jarWebApp.isDirectory())
web_app= jarWebApp; web_app= jarWebApp;
} }
@ -399,8 +399,8 @@ public class WebInfConfiguration implements Configuration
if (web_app.getFile()!=null && web_app.getFile().isDirectory()) if (web_app.getFile()!=null && web_app.getFile().isDirectory())
{ {
// Copy directory // Copy directory
Log.info("Copy " + web_app.getFile() + " to " + extractedWebAppDir); Log.info("Copy " + web_app + " to " + extractedWebAppDir);
IO.copyDir(web_app.getFile(),extractedWebAppDir); web_app.copyTo(extractedWebAppDir);
} }
else else
{ {
@ -408,8 +408,9 @@ public class WebInfConfiguration implements Configuration
{ {
//it hasn't been extracted before so extract it //it hasn't been extracted before so extract it
extractedWebAppDir.mkdir(); extractedWebAppDir.mkdir();
Log.info("Extract " + war + " to " + extractedWebAppDir); Log.info("Extract " + web_app + " to " + extractedWebAppDir);
JarResource.extract(web_app, extractedWebAppDir, false); Resource jar_web_app = JarResource.newJarResource(web_app);
jar_web_app.copyTo(extractedWebAppDir);
} }
else else
{ {
@ -418,8 +419,9 @@ public class WebInfConfiguration implements Configuration
{ {
extractedWebAppDir.delete(); extractedWebAppDir.delete();
extractedWebAppDir.mkdir(); extractedWebAppDir.mkdir();
Log.info("Extract " + war + " to " + extractedWebAppDir); Log.info("Extract " + web_app + " to " + extractedWebAppDir);
JarResource.extract(web_app, extractedWebAppDir, false); Resource jar_web_app = JarResource.newJarResource(web_app);
jar_web_app.copyTo(extractedWebAppDir);
} }
} }
} }
@ -442,7 +444,10 @@ public class WebInfConfiguration implements Configuration
// Do we need to extract WEB-INF/lib? // Do we need to extract WEB-INF/lib?
Resource web_inf= web_app.addPath("WEB-INF/"); Resource web_inf= web_app.addPath("WEB-INF/");
if (web_inf.exists() && web_inf.isDirectory() && (web_inf.getFile()==null || !web_inf.getFile().isDirectory())) if (web_inf instanceof ResourceCollection ||
web_inf.exists() &&
web_inf.isDirectory() &&
(web_inf.getFile()==null || !web_inf.getFile().isDirectory()))
{ {
File extractedWebInfDir= new File(context.getTempDirectory(), "webinf"); File extractedWebInfDir= new File(context.getTempDirectory(), "webinf");
if (extractedWebInfDir.exists()) if (extractedWebInfDir.exists())
@ -451,7 +456,7 @@ public class WebInfConfiguration implements Configuration
File webInfDir=new File(extractedWebInfDir,"WEB-INF"); File webInfDir=new File(extractedWebInfDir,"WEB-INF");
webInfDir.mkdir(); webInfDir.mkdir();
Log.info("Extract " + web_inf + " to " + webInfDir); Log.info("Extract " + web_inf + " to " + webInfDir);
JarResource.extract(web_inf, webInfDir, false); web_inf.copyTo(webInfDir);
web_inf=Resource.newResource(extractedWebInfDir.toURL()); web_inf=Resource.newResource(extractedWebInfDir.toURL());
ResourceCollection rc = new ResourceCollection(new Resource[]{web_inf,web_app}); ResourceCollection rc = new ResourceCollection(new Resource[]{web_inf,web_app});
context.setBaseResource(rc); context.setBaseResource(rc);