Skip to content

Commit

Permalink
Merge pull request #3416 from eclipse/jetty-10.0.x-1676-removing-reso…
Browse files Browse the repository at this point in the history
…urce-geturl

Issue #1676 - Removing Deprecated Resource.getURL()
  • Loading branch information
joakime authored Mar 1, 2019
2 parents ce3f663 + 5658bf9 commit a1567e0
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void copyTo(File directory) throws IOException
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -380,11 +374,17 @@ protected void applyMetaInfContextXml(Resource rootResource, String overrideBund

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)
Expand All @@ -401,18 +401,20 @@ protected void applyMetaInfContextXml(Resource rootResource, String overrideBund
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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void preconfigure(Resource war, Resource dir, Resource xml) throws
{
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());
Expand Down
46 changes: 32 additions & 14 deletions jetty-runner/src/main/java/org/eclipse/jetty/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

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;
Expand Down Expand Up @@ -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
{
Expand All @@ -123,8 +126,7 @@ public void addJars (Resource lib) throws IOException
if (lowerCasePath.endsWith(".jar") ||
lowerCasePath.endsWith(".zip"))
{
URL url = item.getURL();
_classpath.add(url);
_classpath.add(item.getURI());
}
}
}
Expand All @@ -136,13 +138,13 @@ public void addPath (Resource path)
{
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]);
}
}

Expand Down Expand Up @@ -325,7 +327,7 @@ else if (args[i].startsWith("--"))
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);
}
}
Expand Down Expand Up @@ -430,7 +432,7 @@ else if (args[i].startsWith("--"))
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)
Expand Down Expand Up @@ -522,21 +524,37 @@ public void run() throws Exception
_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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -290,19 +288,6 @@ public void testNoextension() throws Exception
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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;

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ public long length()
}

@Override
public URL getURL()
public URI getURI()
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -377,24 +371,6 @@ public long length()
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void copyTo(File directory)
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,6 @@ public URI getURI()
return this.uri;
}

@Override
public URL getURL()
{
try
{
return path.toUri().toURL();
}
catch (MalformedURLException e)
{
return null;
}
}

@Override
public int hashCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,35 +349,13 @@ public static boolean isContainedIn (Resource r, Resource containingResource) th
*/
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();

/* ------------------------------------------------------------ */
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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;
Expand Down Expand Up @@ -380,16 +380,16 @@ public String getName()
}

@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;
Expand Down
Loading

0 comments on commit a1567e0

Please sign in to comment.