409556 Resource files not closed
Made Resource a closeable so that it is easier to close in a try(){} block. Deprecated release() and made it close() instead. FileResource no longer extends URLResource as it can more efficiently implement all the methods with pure File operations and have no connections or streams that need to be release/closed
This commit is contained in:
parent
952f8442e5
commit
7bb3a7be28
|
@ -540,9 +540,8 @@ public class AntWebAppContext extends WebAppContext
|
|||
|
||||
if (getDescriptor() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(getDescriptor());)
|
||||
{
|
||||
Resource r = Resource.newResource(getDescriptor());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -553,9 +552,8 @@ public class AntWebAppContext extends WebAppContext
|
|||
|
||||
if (getJettyEnvXml() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(getJettyEnvXml());)
|
||||
{
|
||||
Resource r = Resource.newResource(getJettyEnvXml());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -566,11 +564,10 @@ public class AntWebAppContext extends WebAppContext
|
|||
|
||||
if (getDefaultsDescriptor() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(getDefaultsDescriptor());)
|
||||
{
|
||||
if (!AntWebAppContext.WEB_DEFAULTS_XML.equals(getDefaultsDescriptor()))
|
||||
{
|
||||
Resource r = Resource.newResource(getDefaultsDescriptor());
|
||||
if (!WebAppContext.WEB_DEFAULTS_XML.equals(getDefaultsDescriptor()))
|
||||
{
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -171,7 +171,7 @@ public interface HttpContent
|
|||
@Override
|
||||
public void release()
|
||||
{
|
||||
_resource.release();
|
||||
_resource.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -405,9 +405,8 @@ public class JettyRunMojo extends AbstractJettyMojo
|
|||
scanList = new ArrayList<File>();
|
||||
if (webApp.getDescriptor() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(webApp.getDescriptor());)
|
||||
{
|
||||
Resource r = Resource.newResource(webApp.getDescriptor());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -418,9 +417,8 @@ public class JettyRunMojo extends AbstractJettyMojo
|
|||
|
||||
if (webApp.getJettyEnvXml() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(webApp.getJettyEnvXml());)
|
||||
{
|
||||
Resource r = Resource.newResource(webApp.getJettyEnvXml());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -431,13 +429,10 @@ public class JettyRunMojo extends AbstractJettyMojo
|
|||
|
||||
if (webApp.getDefaultsDescriptor() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(webApp.getDefaultsDescriptor());)
|
||||
{
|
||||
if (!WebAppContext.WEB_DEFAULTS_XML.equals(webApp.getDefaultsDescriptor()))
|
||||
{
|
||||
Resource r = Resource.newResource(webApp.getDefaultsDescriptor());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
@ -447,9 +442,8 @@ public class JettyRunMojo extends AbstractJettyMojo
|
|||
|
||||
if (webApp.getOverrideDescriptor() != null)
|
||||
{
|
||||
try
|
||||
try (Resource r = Resource.newResource(webApp.getOverrideDescriptor());)
|
||||
{
|
||||
Resource r = Resource.newResource(webApp.getOverrideDescriptor());
|
||||
scanList.add(r.getFile());
|
||||
}
|
||||
catch (IOException e)
|
||||
|
|
|
@ -169,7 +169,7 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen
|
|||
jettyHome = jettyHome.substring(0,jettyHome.length()-1);
|
||||
|
||||
res = getFileAsResource(jettyHome, _contextFile);
|
||||
if (LOG.isDebugEnabled()) LOG.debug("jetty home context file:"+res);
|
||||
LOG.debug("jetty home context file: {}",res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -179,8 +179,11 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen
|
|||
{
|
||||
if (bundleOverrideLocation != null)
|
||||
{
|
||||
res = getFileAsResource(Resource.newResource(bundleOverrideLocation).getFile(), _contextFile);
|
||||
if (LOG.isDebugEnabled()) LOG.debug("Bundle override location context file:"+res);
|
||||
try(Resource location=Resource.newResource(bundleOverrideLocation))
|
||||
{
|
||||
res=location.addPath(_contextFile);
|
||||
}
|
||||
LOG.debug("Bundle override location context file: {}",res);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -295,22 +298,6 @@ public abstract class AbstractContextProvider extends AbstractLifeCycle implemen
|
|||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private Resource getFileAsResource (File dir, String file)
|
||||
{
|
||||
Resource r = null;
|
||||
try
|
||||
{
|
||||
File asFile = new File (dir, file);
|
||||
if (asFile.exists())
|
||||
r = Resource.newResource(asFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
r = null;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -118,17 +118,18 @@ public class Runner
|
|||
if (".".equals(path) || "..".equals(path))
|
||||
continue;
|
||||
|
||||
Resource item = lib.addPath(path);
|
||||
|
||||
if (item.isDirectory())
|
||||
addJars(item);
|
||||
else
|
||||
try(Resource item = lib.addPath(path);)
|
||||
{
|
||||
if (path.toLowerCase().endsWith(".jar") ||
|
||||
path.toLowerCase().endsWith(".zip"))
|
||||
if (item.isDirectory())
|
||||
addJars(item);
|
||||
else
|
||||
{
|
||||
URL url = item.getURL();
|
||||
_classpath.add(url);
|
||||
if (path.toLowerCase().endsWith(".jar") ||
|
||||
path.toLowerCase().endsWith(".zip"))
|
||||
{
|
||||
URL url = item.getURL();
|
||||
_classpath.add(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -214,24 +215,30 @@ public class Runner
|
|||
{
|
||||
if ("--lib".equals(args[i]))
|
||||
{
|
||||
Resource lib = Resource.newResource(args[++i]);
|
||||
if (!lib.exists() || !lib.isDirectory())
|
||||
usage("No such lib directory "+lib);
|
||||
_classpath.addJars(lib);
|
||||
try(Resource lib = Resource.newResource(args[++i]);)
|
||||
{
|
||||
if (!lib.exists() || !lib.isDirectory())
|
||||
usage("No such lib directory "+lib);
|
||||
_classpath.addJars(lib);
|
||||
}
|
||||
}
|
||||
else if ("--jar".equals(args[i]))
|
||||
{
|
||||
Resource jar = Resource.newResource(args[++i]);
|
||||
if (!jar.exists() || jar.isDirectory())
|
||||
usage("No such jar "+jar);
|
||||
_classpath.addPath(jar);
|
||||
try(Resource jar = Resource.newResource(args[++i]);)
|
||||
{
|
||||
if (!jar.exists() || jar.isDirectory())
|
||||
usage("No such jar "+jar);
|
||||
_classpath.addPath(jar);
|
||||
}
|
||||
}
|
||||
else if ("--classes".equals(args[i]))
|
||||
{
|
||||
Resource classes = Resource.newResource(args[++i]);
|
||||
if (!classes.exists() || !classes.isDirectory())
|
||||
usage("No such classes directory "+classes);
|
||||
_classpath.addPath(classes);
|
||||
try(Resource classes = Resource.newResource(args[++i]);)
|
||||
{
|
||||
if (!classes.exists() || !classes.isDirectory())
|
||||
usage("No such classes directory "+classes);
|
||||
_classpath.addPath(classes);
|
||||
}
|
||||
}
|
||||
else if (args[i].startsWith("--"))
|
||||
i++;
|
||||
|
@ -315,8 +322,11 @@ public class Runner
|
|||
{
|
||||
for (String cfg:_configFiles)
|
||||
{
|
||||
XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.newResource(cfg).getURL());
|
||||
xmlConfiguration.configure(_server);
|
||||
try (Resource resource=Resource.newResource(cfg))
|
||||
{
|
||||
XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.getURL());
|
||||
xmlConfiguration.configure(_server);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -416,34 +426,35 @@ public class Runner
|
|||
}
|
||||
|
||||
// Create a context
|
||||
Resource ctx = Resource.newResource(args[i]);
|
||||
if (!ctx.exists())
|
||||
usage("Context '"+ctx+"' does not exist");
|
||||
|
||||
if (contextPathSet && !(contextPath.startsWith("/")))
|
||||
contextPath = "/"+contextPath;
|
||||
try(Resource ctx = Resource.newResource(args[i]);)
|
||||
{
|
||||
if (!ctx.exists())
|
||||
usage("Context '"+ctx+"' does not exist");
|
||||
|
||||
// Configure the context
|
||||
if (!ctx.isDirectory() && ctx.toString().toLowerCase().endsWith(".xml"))
|
||||
{
|
||||
// It is a context config file
|
||||
XmlConfiguration xmlConfiguration=new XmlConfiguration(ctx.getURL());
|
||||
xmlConfiguration.getIdMap().put("Server",_server);
|
||||
ContextHandler handler=(ContextHandler)xmlConfiguration.configure();
|
||||
if (contextPathSet)
|
||||
handler.setContextPath(contextPath);
|
||||
_contexts.addHandler(handler);
|
||||
handler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", __containerIncludeJarPattern);
|
||||
if (contextPathSet && !(contextPath.startsWith("/")))
|
||||
contextPath = "/"+contextPath;
|
||||
|
||||
// Configure the context
|
||||
if (!ctx.isDirectory() && ctx.toString().toLowerCase().endsWith(".xml"))
|
||||
{
|
||||
// It is a context config file
|
||||
XmlConfiguration xmlConfiguration=new XmlConfiguration(ctx.getURL());
|
||||
xmlConfiguration.getIdMap().put("Server",_server);
|
||||
ContextHandler handler=(ContextHandler)xmlConfiguration.configure();
|
||||
if (contextPathSet)
|
||||
handler.setContextPath(contextPath);
|
||||
_contexts.addHandler(handler);
|
||||
handler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", __containerIncludeJarPattern);
|
||||
}
|
||||
else
|
||||
{
|
||||
// assume it is a WAR file
|
||||
WebAppContext webapp = new WebAppContext(_contexts,ctx.toString(),contextPath);
|
||||
webapp.setConfigurationClasses(__plusConfigurationClasses);
|
||||
webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
|
||||
__containerIncludeJarPattern);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// assume it is a WAR file
|
||||
WebAppContext webapp = new WebAppContext(_contexts,ctx.toString(),contextPath);
|
||||
webapp.setConfigurationClasses(__plusConfigurationClasses);
|
||||
webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
|
||||
__containerIncludeJarPattern);
|
||||
}
|
||||
|
||||
//reset
|
||||
contextPathSet = false;
|
||||
contextPath = __defaultContextPath;
|
||||
|
|
|
@ -444,7 +444,7 @@ public class ResourceCache
|
|||
// Invalidate it
|
||||
_cachedSize.addAndGet(-_length);
|
||||
_cachedFiles.decrementAndGet();
|
||||
_resource.release();
|
||||
_resource.close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -567,7 +567,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
|
|||
if (content!=null)
|
||||
content.release();
|
||||
else if (resource!=null)
|
||||
resource.release();
|
||||
resource.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ import java.io.RandomAccessFile;
|
|||
import java.nio.Buffer;
|
||||
import java.nio.BufferOverflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.charset.Charset;
|
||||
|
@ -372,12 +371,14 @@ public class BufferUtil
|
|||
/* ------------------------------------------------------------ */
|
||||
public static void readFrom(File file, ByteBuffer buffer) throws IOException
|
||||
{
|
||||
RandomAccessFile raf = new RandomAccessFile(file,"r");
|
||||
FileChannel channel = raf.getChannel();
|
||||
long needed=raf.length();
|
||||
try(RandomAccessFile raf = new RandomAccessFile(file,"r");)
|
||||
{
|
||||
FileChannel channel = raf.getChannel();
|
||||
long needed=raf.length();
|
||||
|
||||
while (needed>0 && buffer.hasRemaining())
|
||||
needed=needed-channel.read(buffer);
|
||||
while (needed>0 && buffer.hasRemaining())
|
||||
needed=needed-channel.read(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -50,7 +50,10 @@ public class FileDestroyable implements Destroyable
|
|||
|
||||
public void addFile(String file) throws IOException
|
||||
{
|
||||
_files.add(Resource.newResource(file).getFile());
|
||||
try(Resource r = Resource.newResource(file);)
|
||||
{
|
||||
_files.add(r.getFile());
|
||||
}
|
||||
}
|
||||
|
||||
public void addFile(File file)
|
||||
|
@ -65,7 +68,10 @@ public class FileDestroyable implements Destroyable
|
|||
|
||||
public void removeFile(String file) throws IOException
|
||||
{
|
||||
_files.remove(Resource.newResource(file).getFile());
|
||||
try(Resource r = Resource.newResource(file);)
|
||||
{
|
||||
_files.remove(r.getFile());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFile(File file)
|
||||
|
@ -73,6 +79,7 @@ public class FileDestroyable implements Destroyable
|
|||
_files.remove(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
for (File file : _files)
|
||||
|
|
|
@ -20,10 +20,8 @@ package org.eclipse.jetty.util.resource;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
@ -50,25 +48,24 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
*
|
||||
*
|
||||
*/
|
||||
public class FileResource extends URLResource
|
||||
public class FileResource extends Resource
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(FileResource.class);
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private File _file;
|
||||
private transient URL _alias=null;
|
||||
private transient boolean _aliasChecked=false;
|
||||
private final File _file;
|
||||
private final String _uri;
|
||||
private final URL _alias;
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
public FileResource(URL url)
|
||||
throws IOException, URISyntaxException
|
||||
{
|
||||
super(url,null);
|
||||
|
||||
File file;
|
||||
try
|
||||
{
|
||||
// Try standard API to convert URL to file.
|
||||
_file =new File(new URI(url.toString()));
|
||||
file =new File(url.toURI());
|
||||
}
|
||||
catch (URISyntaxException e)
|
||||
{
|
||||
|
@ -76,48 +73,86 @@ public class FileResource extends URLResource
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!url.toString().startsWith("file:"))
|
||||
throw new IllegalArgumentException("!file:");
|
||||
|
||||
LOG.ignore(e);
|
||||
try
|
||||
{
|
||||
// Assume that File.toURL produced unencoded chars. So try
|
||||
// encoding them.
|
||||
// Assume that File.toURL produced unencoded chars. So try encoding them.
|
||||
String file_url="file:"+URIUtil.encodePath(url.toString().substring(5));
|
||||
URI uri = new URI(file_url);
|
||||
if (uri.getAuthority()==null)
|
||||
_file = new File(uri);
|
||||
file = new File(uri);
|
||||
else
|
||||
_file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile()));
|
||||
file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile()));
|
||||
}
|
||||
catch (Exception e2)
|
||||
{
|
||||
LOG.ignore(e2);
|
||||
|
||||
// Still can't get the file. Doh! try good old hack!
|
||||
checkConnection();
|
||||
Permission perm = _connection.getPermission();
|
||||
_file = new File(perm==null?url.getFile():perm.getName());
|
||||
URLConnection connection=url.openConnection();
|
||||
Permission perm = connection.getPermission();
|
||||
file = new File(perm==null?url.getFile():perm.getName());
|
||||
}
|
||||
}
|
||||
if (_file.isDirectory())
|
||||
{
|
||||
if (!_urlString.endsWith("/"))
|
||||
_urlString=_urlString+"/";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_urlString.endsWith("/"))
|
||||
_urlString=_urlString.substring(0,_urlString.length()-1);
|
||||
}
|
||||
|
||||
|
||||
_file=file;
|
||||
_uri=normalizeURI(_file,url.toURI());
|
||||
_alias=checkAlias(_file);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
FileResource(URL url, URLConnection connection, File file)
|
||||
public FileResource(URI uri)
|
||||
{
|
||||
super(url,connection);
|
||||
File file=new File(uri);
|
||||
_file=file;
|
||||
if (_file.isDirectory() && !_urlString.endsWith("/"))
|
||||
_urlString=_urlString+"/";
|
||||
_uri=normalizeURI(_file,uri);
|
||||
_alias=checkAlias(_file);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
FileResource(File file)
|
||||
{
|
||||
_file=file;
|
||||
_uri=normalizeURI(_file,_file.toURI());
|
||||
_alias=checkAlias(_file);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
private static String normalizeURI(File file, URI uri)
|
||||
{
|
||||
String u =uri.toASCIIString();
|
||||
if (file.isDirectory())
|
||||
{
|
||||
if(!u.endsWith("/"))
|
||||
u+="/";
|
||||
}
|
||||
else if (file.exists() && u.endsWith("/"))
|
||||
u=u.substring(0,u.length()-1);
|
||||
return u;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
private static URL checkAlias(File file)
|
||||
{
|
||||
try
|
||||
{
|
||||
String abs=file.getAbsolutePath();
|
||||
String can=file.getCanonicalPath();
|
||||
|
||||
if (!abs.equals(can))
|
||||
{
|
||||
LOG.debug("ALIAS abs={} can={}",abs,can);
|
||||
return new File(can).toURI().toURL();
|
||||
}
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
LOG.warn(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------- */
|
||||
|
@ -125,45 +160,35 @@ public class FileResource extends URLResource
|
|||
public Resource addPath(String path)
|
||||
throws IOException,MalformedURLException
|
||||
{
|
||||
URLResource r=null;
|
||||
String url=null;
|
||||
|
||||
path = org.eclipse.jetty.util.URIUtil.canonicalPath(path);
|
||||
|
||||
|
||||
if (path==null)
|
||||
throw new MalformedURLException();
|
||||
|
||||
if ("/".equals(path))
|
||||
return this;
|
||||
else if (!isDirectory())
|
||||
{
|
||||
r=(FileResource)super.addPath(path);
|
||||
url=r._urlString;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (path==null)
|
||||
throw new MalformedURLException();
|
||||
|
||||
// treat all paths being added as relative
|
||||
String rel=path;
|
||||
if (path.startsWith("/"))
|
||||
rel = path.substring(1);
|
||||
|
||||
url=URIUtil.addPaths(_urlString,URIUtil.encodePath(rel));
|
||||
r=(URLResource)Resource.newResource(url);
|
||||
}
|
||||
|
||||
String encoded=URIUtil.encodePath(path);
|
||||
int expected=r.toString().length()-encoded.length();
|
||||
int index = r._urlString.lastIndexOf(encoded, expected);
|
||||
path=URIUtil.encodePath(path);
|
||||
|
||||
if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory()))
|
||||
URI uri;
|
||||
try
|
||||
{
|
||||
if (!(r instanceof BadResource))
|
||||
if (_file.isDirectory())
|
||||
{
|
||||
((FileResource)r)._alias=new URL(url);
|
||||
((FileResource)r)._aliasChecked=true;
|
||||
// treat all paths being added as relative
|
||||
uri=new URI(URIUtil.addPaths(_uri,path));
|
||||
}
|
||||
}
|
||||
return r;
|
||||
else
|
||||
{
|
||||
uri=new URI(_uri+path);
|
||||
}
|
||||
}
|
||||
catch(final URISyntaxException e)
|
||||
{
|
||||
throw new MalformedURLException(){{initCause(e);}};
|
||||
}
|
||||
|
||||
return new FileResource(uri);
|
||||
}
|
||||
|
||||
|
||||
|
@ -171,30 +196,6 @@ public class FileResource extends URLResource
|
|||
@Override
|
||||
public URL getAlias()
|
||||
{
|
||||
if (!_aliasChecked)
|
||||
{
|
||||
try
|
||||
{
|
||||
String abs=_file.getAbsolutePath();
|
||||
String can=_file.getCanonicalPath();
|
||||
|
||||
if (abs.length()!=can.length() || !abs.equals(can))
|
||||
_alias=Resource.toURL(new File(can));
|
||||
|
||||
_aliasChecked=true;
|
||||
|
||||
if (_alias!=null && LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("ALIAS abs="+abs);
|
||||
LOG.debug("ALIAS can="+can);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.warn(Log.EXCEPTION,e);
|
||||
return getURL();
|
||||
}
|
||||
}
|
||||
return _alias;
|
||||
}
|
||||
|
||||
|
@ -220,12 +221,12 @@ public class FileResource extends URLResource
|
|||
|
||||
/* -------------------------------------------------------- */
|
||||
/**
|
||||
* Returns true if the respresenetd resource is a container/directory.
|
||||
* Returns true if the resource is a container/directory.
|
||||
*/
|
||||
@Override
|
||||
public boolean isDirectory()
|
||||
{
|
||||
return _file.isDirectory();
|
||||
return _file.exists() && _file.isDirectory() || _uri.endsWith("/");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------- */
|
||||
|
@ -320,18 +321,6 @@ public class FileResource extends URLResource
|
|||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Encode according to this resource type.
|
||||
* File URIs are encoded.
|
||||
* @param uri URI to encode.
|
||||
* @return The uri unchanged.
|
||||
*/
|
||||
@Override
|
||||
public String encode(String uri)
|
||||
{
|
||||
return uri;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
|
@ -377,4 +366,35 @@ public class FileResource extends URLResource
|
|||
IO.copy(getFile(),destination);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContainedIn(Resource r) throws MalformedURLException
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL()
|
||||
{
|
||||
try
|
||||
{
|
||||
return _file.toURI().toURL();
|
||||
}
|
||||
catch (MalformedURLException e)
|
||||
{
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return _uri;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ class JarFileResource extends JarResource
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public synchronized void release()
|
||||
public synchronized void close()
|
||||
{
|
||||
_list=null;
|
||||
_entry=null;
|
||||
|
@ -83,7 +83,7 @@ class JarFileResource extends JarResource
|
|||
}
|
||||
}
|
||||
_jarFile=null;
|
||||
super.release();
|
||||
super.close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -374,18 +374,6 @@ class JarFileResource extends JarResource
|
|||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Encode according to this resource type.
|
||||
* File URIs are not encoded.
|
||||
* @param uri URI to encode.
|
||||
* @return The uri unchanged.
|
||||
*/
|
||||
@Override
|
||||
public String encode(String uri)
|
||||
{
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -55,10 +55,10 @@ public class JarResource extends URLResource
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public synchronized void release()
|
||||
public synchronized void close()
|
||||
{
|
||||
_jarConnection=null;
|
||||
super.release();
|
||||
super.close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.eclipse.jetty.util.resource;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -26,7 +27,6 @@ import java.io.OutputStream;
|
|||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Arrays;
|
||||
|
@ -45,7 +45,7 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
/**
|
||||
* Abstract resource class.
|
||||
*/
|
||||
public abstract class Resource implements ResourceFactory
|
||||
public abstract class Resource implements ResourceFactory, Closeable
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(Resource.class);
|
||||
public static boolean __defaultUseCaches = true;
|
||||
|
@ -149,7 +149,7 @@ public abstract class Resource implements ResourceFactory
|
|||
* @param useCaches controls URLConnection caching
|
||||
* @return A Resource object.
|
||||
*/
|
||||
public static Resource newResource (String resource, boolean useCaches)
|
||||
public static Resource newResource(String resource, boolean useCaches)
|
||||
throws MalformedURLException, IOException
|
||||
{
|
||||
URL url=null;
|
||||
|
@ -171,11 +171,7 @@ public abstract class Resource implements ResourceFactory
|
|||
resource=resource.substring(2);
|
||||
|
||||
File file=new File(resource).getCanonicalFile();
|
||||
url=Resource.toURL(file);
|
||||
|
||||
URLConnection connection=url.openConnection();
|
||||
connection.setUseCaches(useCaches);
|
||||
return new FileResource(url,connection,file);
|
||||
return new FileResource(file);
|
||||
}
|
||||
catch(Exception e2)
|
||||
{
|
||||
|
@ -194,15 +190,9 @@ public abstract class Resource implements ResourceFactory
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public static Resource newResource (File file)
|
||||
throws MalformedURLException, IOException
|
||||
public static Resource newResource(File file)
|
||||
{
|
||||
file = file.getCanonicalFile();
|
||||
URL url = Resource.toURL(file);
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
FileResource fileResource = new FileResource(url, connection, file);
|
||||
return fileResource;
|
||||
return new FileResource(file);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -300,7 +290,7 @@ public abstract class Resource implements ResourceFactory
|
|||
@Override
|
||||
protected void finalize()
|
||||
{
|
||||
release();
|
||||
close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -309,9 +299,18 @@ public abstract class Resource implements ResourceFactory
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Release any temporary resources held by the resource.
|
||||
* @deprecated use {@link #close()}
|
||||
*/
|
||||
public abstract void release();
|
||||
|
||||
public final void release()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Release any temporary resources held by the resource.
|
||||
*/
|
||||
@Override
|
||||
public abstract void close();
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
|
@ -420,19 +419,19 @@ public abstract class Resource implements ResourceFactory
|
|||
/**
|
||||
* Returns the resource contained inside the current resource with the
|
||||
* given name.
|
||||
* @param path The path segment to add, which should be encoded by the
|
||||
* encode method.
|
||||
* @param path The path segment to add, which is not encoded
|
||||
*/
|
||||
public abstract Resource addPath(String path)
|
||||
throws IOException,MalformedURLException;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get a resource from withing this resource.
|
||||
/** Get a resource from within this resource.
|
||||
* <p>
|
||||
* This method is essentially an alias for {@link #addPath(String)}, but without checked exceptions.
|
||||
* This method satisfied the {@link ResourceFactory} interface.
|
||||
* @see org.eclipse.jetty.util.resource.ResourceFactory#getResource(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Resource getResource(String path)
|
||||
{
|
||||
try
|
||||
|
@ -447,14 +446,12 @@ public abstract class Resource implements ResourceFactory
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Encode according to this resource type.
|
||||
* The default implementation calls URI.encodePath(uri)
|
||||
* @param uri
|
||||
* @return String encoded for this resource type.
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public String encode(String uri)
|
||||
{
|
||||
return URIUtil.encodePath(uri);
|
||||
return null;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -21,7 +21,6 @@ package org.eclipse.jetty.util.resource;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
|
@ -32,7 +31,6 @@ import java.util.List;
|
|||
import java.util.StringTokenizer;
|
||||
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
|
@ -228,12 +226,15 @@ public class ResourceCollection extends Resource
|
|||
Resource r = _resources[i].addPath(path);
|
||||
if (r.exists() && r.isDirectory())
|
||||
{
|
||||
if (resources==null)
|
||||
resources = new ArrayList<Resource>();
|
||||
|
||||
if (resource!=null)
|
||||
{
|
||||
resources = new ArrayList<Resource>();
|
||||
resources.add(resource);
|
||||
resource=null;
|
||||
}
|
||||
|
||||
resources.add(r);
|
||||
}
|
||||
}
|
||||
|
@ -443,13 +444,13 @@ public class ResourceCollection extends Resource
|
|||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void release()
|
||||
public void close()
|
||||
{
|
||||
if(_resources==null)
|
||||
throw new IllegalStateException("*resources* not set.");
|
||||
|
||||
for(Resource r : _resources)
|
||||
r.release();
|
||||
r.close();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
|
@ -81,7 +81,7 @@ public class URLResource extends Resource
|
|||
/** Release any resources held by the resource.
|
||||
*/
|
||||
@Override
|
||||
public synchronized void release()
|
||||
public synchronized void close()
|
||||
{
|
||||
if (_in!=null)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util.resource;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.eclipse.jetty.toolchain.test.FS;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.eclipse.jetty.toolchain.test.TestingDir;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ResourceAliasTest
|
||||
{
|
||||
static File __dir;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass()
|
||||
{
|
||||
__dir=MavenTestingUtils.getTargetTestingDir("RAT");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before()
|
||||
{
|
||||
FS.ensureDirExists(__dir);
|
||||
FS.ensureEmpty(__dir);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Test
|
||||
public void testNullCharEndingFilename() throws Exception
|
||||
{
|
||||
File file=new File(__dir,"test.txt");
|
||||
assertFalse(file.exists());
|
||||
file.createNewFile();
|
||||
assertTrue(file.exists());
|
||||
|
||||
File file0=new File(__dir,"test.txt\0");
|
||||
if (!file0.exists())
|
||||
return; // this file system does not suffer this problem
|
||||
|
||||
assertTrue(file0.exists()); // This is an alias!
|
||||
|
||||
Resource dir = Resource.newResource(__dir);
|
||||
|
||||
// Test not alias paths
|
||||
Resource resource = Resource.newResource(file);
|
||||
assertTrue(resource.exists());
|
||||
assertNull(resource.getAlias());
|
||||
resource = Resource.newResource(file.getAbsoluteFile());
|
||||
assertTrue(resource.exists());
|
||||
assertNull(resource.getAlias());
|
||||
resource = Resource.newResource(file.toURI());
|
||||
assertTrue(resource.exists());
|
||||
assertNull(resource.getAlias());
|
||||
resource = Resource.newResource(file.toURI().toString());
|
||||
assertTrue(resource.exists());
|
||||
assertNull(resource.getAlias());
|
||||
resource = dir.addPath("test.txt");
|
||||
assertTrue(resource.exists());
|
||||
assertNull(resource.getAlias());
|
||||
|
||||
|
||||
// Test alias paths
|
||||
resource = Resource.newResource(file0);
|
||||
assertTrue(resource.exists());
|
||||
assertNotNull(resource.getAlias());
|
||||
resource = Resource.newResource(file0.getAbsoluteFile());
|
||||
assertTrue(resource.exists());
|
||||
assertNotNull(resource.getAlias());
|
||||
resource = Resource.newResource(file0.toURI());
|
||||
assertTrue(resource.exists());
|
||||
assertNotNull(resource.getAlias());
|
||||
resource = Resource.newResource(file0.toURI().toString());
|
||||
assertTrue(resource.exists());
|
||||
assertNotNull(resource.getAlias());
|
||||
|
||||
try
|
||||
{
|
||||
resource = dir.addPath("test.txt\0");
|
||||
assertTrue(resource.exists());
|
||||
assertNotNull(resource.getAlias());
|
||||
}
|
||||
catch(MalformedURLException e)
|
||||
{
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,12 +33,8 @@ import java.util.Arrays;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import java.util.TimeZone;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.eclipse.jetty.toolchain.test.OS;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
|
@ -329,13 +325,13 @@ public class ResourceTest
|
|||
{
|
||||
String s = "jar:"+__userURL+"TestData/test.zip!/subdir/numbers";
|
||||
|
||||
|
||||
ZipFile zf = new ZipFile(MavenTestingUtils.getTestResourceFile("TestData/test.zip"));
|
||||
|
||||
long last = zf.getEntry("subdir/numbers").getTime();
|
||||
try(ZipFile zf = new ZipFile(MavenTestingUtils.getTestResourceFile("TestData/test.zip")))
|
||||
{
|
||||
long last = zf.getEntry("subdir/numbers").getTime();
|
||||
|
||||
Resource r = Resource.newResource(s);
|
||||
assertEquals(last,r.lastModified());
|
||||
Resource r = Resource.newResource(s);
|
||||
assertEquals(last,r.lastModified());
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -367,6 +363,7 @@ public class ResourceTest
|
|||
assertEquals(1, dest.getParentFile().listFiles().length);
|
||||
|
||||
FilenameFilter dotdotFilenameFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File directory, String name)
|
||||
{
|
||||
return name.equals("dotdot.txt");
|
||||
|
@ -376,6 +373,7 @@ public class ResourceTest
|
|||
assertEquals(0, dest.getParentFile().listFiles(dotdotFilenameFilter).length);
|
||||
|
||||
FilenameFilter extractfileFilenameFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File directory, String name)
|
||||
{
|
||||
return name.equals("extract-filenotdir");
|
||||
|
@ -385,6 +383,7 @@ public class ResourceTest
|
|||
assertEquals(0, dest.getParentFile().listFiles(extractfileFilenameFilter).length);
|
||||
|
||||
FilenameFilter currentDirectoryFilenameFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File directory, String name)
|
||||
{
|
||||
return name.equals("current.txt");
|
||||
|
@ -405,15 +404,14 @@ public class ResourceTest
|
|||
{
|
||||
final String classPathName="Resource.class";
|
||||
|
||||
Resource resource=Resource.newClassPathResource(classPathName);
|
||||
try(Resource resource=Resource.newClassPathResource(classPathName);)
|
||||
{
|
||||
// A class path cannot be a directory
|
||||
assertFalse("Class path cannot be a directory.",resource.isDirectory());
|
||||
|
||||
assertTrue(resource!=null);
|
||||
|
||||
// A class path cannot be a directory
|
||||
assertFalse("Class path cannot be a directory.",resource.isDirectory());
|
||||
|
||||
// A class path must exist
|
||||
assertTrue("Class path resource does not exist.",resource.exists());
|
||||
// A class path must exist
|
||||
assertTrue("Class path resource does not exist.",resource.exists());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -426,8 +424,6 @@ public class ResourceTest
|
|||
|
||||
Resource resource=Resource.newClassPathResource(classPathName);
|
||||
|
||||
assertTrue(resource!=null);
|
||||
|
||||
// A class path cannot be a directory
|
||||
assertFalse("Class path cannot be a directory.",resource.isDirectory());
|
||||
|
||||
|
@ -445,9 +441,6 @@ public class ResourceTest
|
|||
|
||||
Resource resource=Resource.newClassPathResource(classPathName);
|
||||
|
||||
|
||||
assertTrue(resource!=null);
|
||||
|
||||
// A class path must be a directory
|
||||
assertTrue("Class path must be a directory.",resource.isDirectory());
|
||||
|
||||
|
@ -469,8 +462,6 @@ public class ResourceTest
|
|||
// Will locate a resource in the class path
|
||||
Resource resource=Resource.newClassPathResource(classPathName);
|
||||
|
||||
assertTrue(resource!=null);
|
||||
|
||||
// A class path cannot be a directory
|
||||
assertFalse("Class path must be a directory.",resource.isDirectory());
|
||||
|
||||
|
@ -478,7 +469,6 @@ public class ResourceTest
|
|||
|
||||
File file=resource.getFile();
|
||||
|
||||
assertTrue("File returned from class path should not be null.",file!=null);
|
||||
assertEquals("File name from class path is not equal.",fileName,file.getName());
|
||||
assertTrue("File returned from class path should be a file.",file.isFile());
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ public abstract class Descriptor
|
|||
}
|
||||
finally
|
||||
{
|
||||
_xml.release();
|
||||
_xml.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,10 +167,10 @@ public class OrderingTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.eclipse.jetty.util.resource.Resource#release()
|
||||
* @see org.eclipse.jetty.util.resource.Resource#close()
|
||||
*/
|
||||
@Override
|
||||
public void release()
|
||||
public void close()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ public class WebAppClassLoaderTest
|
|||
|
||||
assertTrue(cantLoadClass("org.eclipse.jetty.webapp.Configuration"));
|
||||
|
||||
Class clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA");
|
||||
Class<?> clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA");
|
||||
assertTrue(clazzA.getField("FROM_PARENT")!=null);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue