Merge pull request #3416 from eclipse/jetty-10.0.x-1676-removing-resource-geturl

Issue #1676 - Removing Deprecated Resource.getURL()
This commit is contained in:
Joakim Erdfelt 2019-03-01 10:28:22 -05:00 committed by GitHub
commit a1567e0149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 113 additions and 155 deletions

View File

@ -127,7 +127,7 @@ public class SelectiveJarResource extends JarResource
if (!exists())
return;
String urlString = this.getURL().toExternalForm().trim();
String urlString = this.getURI().toASCIIString().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0?4:0);

View File

@ -19,30 +19,24 @@
package org.eclipse.jetty.osgi.boot;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
import org.eclipse.jetty.osgi.boot.internal.webapp.OSGiWebappClassLoader;
import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.JarResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
@ -380,11 +374,17 @@ public abstract class AbstractWebAppProvider extends AbstractLifeCycle implement
Thread.currentThread().setContextClassLoader(_webApp.getClassLoader());
URI contextXmlUri = null;
//TODO replace this with getting the InputStream so we don't cache in URL
//Try looking for a context xml file in META-INF with a specific name
URL contextXmlUrl = _bundle.getEntry("/META-INF/jetty-webapp-context.xml");
if (contextXmlUrl == null)
URL url = _bundle.getEntry("/META-INF/jetty-webapp-context.xml");
if(url != null)
{
contextXmlUri = url.toURI();
}
if (contextXmlUri == null)
{
//Didn't find specially named file, try looking for a property that names a context xml file to use
if (_properties != null)
@ -401,18 +401,20 @@ public abstract class AbstractWebAppProvider extends AbstractLifeCycle implement
jettyHome = System.getProperty(OSGiServerConstants.JETTY_HOME);
Resource res = findFile(filename, jettyHome, overrideBundleInstallLocation, _bundle);
if (res != null)
contextXmlUrl = res.getURL();
{
contextXmlUri = res.getURI();
}
}
}
}
}
if (contextXmlUrl == null)
if (contextXmlUri == null)
return;
// Apply it just as the standard jetty ContextProvider would do
LOG.info("Applying " + contextXmlUrl + " to " + _webApp);
LOG.info("Applying " + contextXmlUri + " to " + _webApp);
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXmlUrl);
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXmlUri);
WebAppClassLoader.runWithServerClassAccess(()->
{
HashMap<String,String> properties = new HashMap<>();

View File

@ -105,7 +105,7 @@ public class PreconfigureQuickStartWar
{
if (xml.isDirectory() || !xml.toString().toLowerCase(Locale.ENGLISH).endsWith(".xml"))
error("Bad context.xml: "+xml);
XmlConfiguration xmlConfiguration = new XmlConfiguration(xml.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(xml.getURI());
xmlConfiguration.configure(webapp);
}
webapp.setResourceBase(dir.getFile().getAbsolutePath());

View File

@ -20,9 +20,12 @@ package org.eclipse.jetty.runner;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -97,7 +100,7 @@ public class Runner
*/
public class Classpath
{
private List<URL> _classpath = new ArrayList<>();
private List<URI> _classpath = new ArrayList<>();
public void addJars (Resource lib) throws IOException
{
@ -123,8 +126,7 @@ public class Runner
if (lowerCasePath.endsWith(".jar") ||
lowerCasePath.endsWith(".zip"))
{
URL url = item.getURL();
_classpath.add(url);
_classpath.add(item.getURI());
}
}
}
@ -136,13 +138,13 @@ public class Runner
{
if (path == null || !path.exists())
throw new IllegalStateException ("No such path: "+path);
_classpath.add(path.getURL());
_classpath.add(path.getURI());
}
public URL[] asArray ()
public URI[] asArray ()
{
return _classpath.toArray(new URL[_classpath.size()]);
return _classpath.toArray(new URI[0]);
}
}
@ -325,7 +327,7 @@ public class Runner
for (String cfg : _configFiles)
{
try (Resource resource = Resource.newResource(cfg)) {
XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.getURI());
xmlConfiguration.configure(_server);
}
}
@ -430,7 +432,7 @@ public class Runner
if (!ctx.isDirectory() && ctx.toString().toLowerCase(Locale.ENGLISH).endsWith(".xml"))
{
// It is a context config file
XmlConfiguration xmlConfiguration = new XmlConfiguration(ctx.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(ctx.getURI());
xmlConfiguration.getIdMap().put("Server", _server);
ContextHandler handler = (ContextHandler) xmlConfiguration.configure();
if (contextPathSet)
@ -522,21 +524,37 @@ public class Runner
_server.join();
}
private URL toURL(URI uri)
{
try
{
return uri.toURL();
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
/**
* Establish a classloader with custom paths (if any)
*/
protected void initClassLoader()
{
URL[] paths = _classpath.asArray();
URL[] paths = Arrays.stream(_classpath.asArray()).map(this::toURL).toArray(URL[]::new);
if (_classLoader==null && paths !=null && paths.length > 0)
if (_classLoader == null && paths.length > 0)
{
ClassLoader context=Thread.currentThread().getContextClassLoader();
ClassLoader context = Thread.currentThread().getContextClassLoader();
if (context==null)
_classLoader=new URLClassLoader(paths);
if (context == null)
{
_classLoader = new URLClassLoader(paths);
}
else
_classLoader=new URLClassLoader(paths, context);
{
_classLoader = new URLClassLoader(paths, context);
}
Thread.currentThread().setContextClassLoader(_classLoader);
}

View File

@ -18,10 +18,8 @@
package org.eclipse.jetty.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.eclipse.jetty.http.CompressedContentFormat;
@ -290,19 +288,6 @@ public class ResourceCacheTest
assertEquals(getContent(cache, "four"), "4 - four (no extension)");
}
static String getContent(Resource r, String path) throws Exception
{
StringBuilder buffer = new StringBuilder();
String line = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(r.addPath(path).getURL().openStream())))
{
while((line=br.readLine())!=null)
buffer.append(line);
}
return buffer.toString();
}
static String getContent(CachedContentFactory rc, String path) throws Exception
{
HttpContent content = rc.getContent(path, rc.getMaxCachedFileSize());

View File

@ -22,7 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.nio.channels.ReadableByteChannel;
/**
@ -74,7 +74,7 @@ public class EmptyResource extends Resource
}
@Override
public URL getURL()
public URI getURI()
{
return null;
}

View File

@ -46,17 +46,11 @@ public class JarFileResource extends JarResource
private String _path;
private boolean _exists;
/* -------------------------------------------------------- */
protected JarFileResource(URL url)
{
super(url);
}
/* ------------------------------------------------------------ */
protected JarFileResource(URL url, boolean useCaches)
{
super(url, useCaches);
}
}
/* ------------------------------------------------------------ */
@Override
@ -377,24 +371,6 @@ public class JarFileResource extends JarResource
return -1;
}
/**
* Take a Resource that possibly might use URLConnection caching
* and turn it into one that doesn't.
* @param resource the JarFileResource to obtain without URLConnection caching.
* @return the non-caching resource
*/
public static Resource getNonCachingResource (Resource resource)
{
if (!(resource instanceof JarFileResource))
return resource;
JarFileResource oldResource = (JarFileResource)resource;
JarFileResource newResource = new JarFileResource(oldResource.getURL(), false);
return newResource;
}
/**
* Check if this jar:file: resource is contained in the
* named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>

View File

@ -144,7 +144,7 @@ public class JarResource extends URLResource
if(LOG.isDebugEnabled())
LOG.debug("Extract "+this+" to "+directory);
String urlString = this.getURL().toExternalForm().trim();
String urlString = this.getURI().toASCIIString().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0?4:0);

View File

@ -408,19 +408,6 @@ public class PathResource extends Resource
return this.uri;
}
@Override
public URL getURL()
{
try
{
return path.toUri().toURL();
}
catch (MalformedURLException e)
{
return null;
}
}
@Override
public int hashCode()
{

View File

@ -349,35 +349,13 @@ public abstract class Resource implements ResourceFactory, Closeable
*/
public abstract long length();
/* ------------------------------------------------------------ */
/**
* URL representing the resource.
*
* @return an URL representing the given resource
* @deprecated use {{@link #getURI()}.toURL() instead.
*/
@Deprecated
public abstract URL getURL();
/* ------------------------------------------------------------ */
/**
* URI representing the resource.
*
* @return an URI representing the given resource
*/
public URI getURI()
{
try
{
return getURL().toURI();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public abstract URI getURI();
/* ------------------------------------------------------------ */
/**

View File

@ -22,7 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
@ -380,16 +380,16 @@ public class ResourceCollection extends Resource
}
@Override
public URL getURL()
public URI getURI()
{
assertResourcesSet();
for (Resource r : _resources)
{
URL url = r.getURL();
if (url != null)
URI uri = r.getURI();
if (uri != null)
{
return url;
return uri;
}
}
return null;

View File

@ -22,10 +22,11 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.ReadableByteChannel;
import java.security.Permission;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
@ -154,12 +155,19 @@ public class URLResource extends Resource
/* ------------------------------------------------------------ */
/**
* Returns an URL representing the given resource
* Returns a URI representing the given resource
*/
@Override
public URL getURL()
public URI getURI()
{
return _url;
try
{
return _url.toURI();
}
catch (URISyntaxException e)
{
throw new RuntimeException(e);
}
}
/* ------------------------------------------------------------ */

View File

@ -578,26 +578,6 @@ public class FileSystemResourceTest
}
}
@SuppressWarnings("deprecation")
@ParameterizedTest
@MethodSource("fsResourceProvider")
public void testGetURL(Class<PathResource> resourceClass) throws Exception
{
Path dir = workDir.getEmptyPathDir();
Files.createDirectories(dir);
Path file = dir.resolve("foo");
Files.createFile(file);
URL expected = file.toUri().toURL();
try (Resource base = newResource(resourceClass, dir.toFile()))
{
Resource foo = base.addPath("foo");
assertThat("getURL",foo.getURL(),is(expected));
}
}
@ParameterizedTest
@MethodSource("fsResourceProvider")
public void testList(Class<PathResource> resourceClass) throws Exception

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.util.resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
@ -31,8 +32,8 @@ import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -158,7 +159,7 @@ public class ResourceCollectionTest
assertThrows(IllegalStateException.class, coll::getFile);
assertThrows(IllegalStateException.class, coll::getInputStream);
assertThrows(IllegalStateException.class, coll::getReadableByteChannel);
assertThrows(IllegalStateException.class, coll::getURL);
assertThrows(IllegalStateException.class, coll::getURI);
assertThrows(IllegalStateException.class, coll::getName);
assertThrows(IllegalStateException.class, coll::isDirectory);
assertThrows(IllegalStateException.class, coll::lastModified);
@ -239,12 +240,17 @@ public class ResourceCollectionTest
static String getContent(Resource r, String path) throws Exception
{
Resource resource = r.addPath(path);
StringBuilder buffer = new StringBuilder();
String line;
try (BufferedReader br = new BufferedReader(new InputStreamReader(r.addPath(path).getURL().openStream())))
try (InputStream in = resource.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader))
{
while((line=br.readLine())!=null)
String line;
while ((line = br.readLine()) != null)
{
buffer.append(line);
}
}
return buffer.toString();
}

View File

@ -18,18 +18,11 @@
package org.eclipse.jetty.webapp;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.List;
@ -37,6 +30,13 @@ import java.util.List;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
/**
* OrderingTest
*/
@ -112,7 +112,7 @@ public class OrderingTest
}
@Override
public URL getURL()
public URI getURI()
{
return null;
}

View File

@ -29,6 +29,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Path;
@ -191,6 +192,23 @@ public class XmlConfiguration
}
}
/**
* Reads and parses the XML configuration file.
*
* @param configuration the URI of the XML configuration
* @throws IOException if the configuration could not be read
* @throws SAXException if the configuration could not be parsed
*/
public XmlConfiguration(URI configuration) throws SAXException, IOException
{
synchronized (__parser)
{
_url=configuration.toURL();
setConfig(__parser.parse(configuration.toString()));
_dtd=__parser.getDTD();
}
}
/**
* Reads and parses the XML configuration string.
*

View File

@ -74,7 +74,7 @@ public class QuickStartTest
if (contextXml != null)
{
// System.err.println("Applying "+contextXml);
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURI());
xmlConfiguration.configure(webapp);
}
@ -123,7 +123,7 @@ public class QuickStartTest
if (contextXml != null)
{
// System.err.println("Applying "+contextXml);
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURI());
xmlConfiguration.configure(webapp);
}

View File

@ -49,7 +49,7 @@ public class Quickstart
if (contextXml != null)
{
// System.err.println("Applying "+contextXml);
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURL());
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextXml.getURI());
xmlConfiguration.configure(webapp);
}