resource loader should use url instead of inputstream

This commit is contained in:
Sebastian Sdorra
2014-10-10 20:30:56 +02:00
parent 7f38d40cfb
commit 63ab9e0701
3 changed files with 37 additions and 34 deletions

View File

@@ -39,7 +39,7 @@ import sonia.scm.plugin.ExtensionPoint;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.InputStream; import java.net.URL;
/** /**
* *
@@ -63,7 +63,7 @@ public interface ResourceHandler
* *
* @return * @return
*/ */
public InputStream getResource(); public URL getResource();
/** /**
* Method description * Method description

View File

@@ -41,7 +41,7 @@ import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.InputStream; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
@@ -116,7 +116,7 @@ public class ResourceHandlerComparatorTest
* @return * @return
*/ */
@Override @Override
public InputStream getResource() public URL getResource()
{ {
throw new UnsupportedOperationException("Not supported yet."); throw new UnsupportedOperationException("Not supported yet.");
} }

View File

@@ -35,19 +35,21 @@ package sonia.scm.resources;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.Resources;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.plugin.PluginLoader; import sonia.scm.plugin.PluginLoader;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util; import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -107,10 +109,7 @@ public abstract class AbstractResource implements Resource
for (ResourceHandler resourceHandler : resourceHandlers) for (ResourceHandler resourceHandler : resourceHandlers)
{ {
if (resourceHandler.getType() == ResourceType.SCRIPT) processResourceHandler(stream, resourceHandler);
{
appendResource(resourceHandler.getResource(), stream);
}
} }
} }
} }
@@ -120,22 +119,22 @@ public abstract class AbstractResource implements Resource
* *
* *
* @param stream * @param stream
* @param resource * @param path
* *
* @throws IOException * @throws IOException
*/ */
private void appendResource(OutputStream stream, String resource) private void appendResource(OutputStream stream, String path)
throws IOException throws IOException
{ {
InputStream input = getResourceAsStream(resource); URL resource = getResourceAsURL(path);
if (input != null) if (resource != null)
{ {
appendResource(input, stream); Resources.copy(resource, stream);
} }
else if (logger.isWarnEnabled()) else if (logger.isWarnEnabled())
{ {
logger.warn("could not find resource {}", resource); logger.warn("could not find resource {}", path);
} }
} }
@@ -143,23 +142,32 @@ public abstract class AbstractResource implements Resource
* Method description * Method description
* *
* *
* @param input
* @param stream * @param stream
* @param resourceHandler
* *
* @throws IOException * @throws IOException
*/ */
private void appendResource(InputStream input, OutputStream stream) private void processResourceHandler(OutputStream stream,
ResourceHandler resourceHandler)
throws IOException throws IOException
{ {
if (input != null) if (resourceHandler.getType() == getType())
{ {
try if (logger.isTraceEnabled())
{ {
IOUtil.copy(input, stream); logger.trace("process resource handler {}", resourceHandler.getClass());
} }
finally
URL resource = resourceHandler.getResource();
if (resource != null)
{ {
IOUtil.close(input); Resources.copy(resource, stream);
}
else if (logger.isDebugEnabled())
{
logger.debug("resource handler {} does not return a resource",
resourceHandler.getClass());
} }
} }
} }
@@ -170,33 +178,28 @@ public abstract class AbstractResource implements Resource
* Method description * Method description
* *
* *
* @param resource * @param path
* *
* @return * @return
*/ */
private InputStream getResourceAsStream(String resource) private URL getResourceAsURL(String path)
{ {
InputStream input = null; URL resource = null;
ClassLoader classLoader = pluginLoader.getUberClassLoader(); ClassLoader classLoader = pluginLoader.getUberClassLoader();
if (classLoader != null) if (classLoader != null)
{ {
String classLoaderResource = resource; String classLoaderResource = path;
if (classLoaderResource.startsWith("/")) if (classLoaderResource.startsWith("/"))
{ {
classLoaderResource = classLoaderResource.substring(1); classLoaderResource = classLoaderResource.substring(1);
} }
input = classLoader.getResourceAsStream(classLoaderResource); resource = classLoader.getResource(classLoaderResource);
} }
if (input == null) return resource;
{
input = ScriptResourceServlet.class.getResourceAsStream(resource);
}
return input;
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------