diff --git a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java index d7dcc33367..8ca980f89c 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java @@ -69,6 +69,9 @@ import sonia.scm.repository.RepositoryBrowserUtil; import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryProvider; import sonia.scm.repository.xml.XmlRepositoryManager; +import sonia.scm.resources.DefaultResourceManager; +import sonia.scm.resources.DevelopmentResourceManager; +import sonia.scm.resources.ResourceManager; import sonia.scm.security.CipherHandler; import sonia.scm.security.CipherUtil; import sonia.scm.security.EncryptionHandler; @@ -259,6 +262,16 @@ public class ScmServletModule extends ServletModule // bind httpclient bind(HttpClient.class).to(URLHttpClient.class); + // bind resourcemanager + if (context.getStage() == Stage.DEVELOPMENT) + { + bind(ResourceManager.class).to(DevelopmentResourceManager.class); + } + else + { + bind(ResourceManager.class).to(DefaultResourceManager.class); + } + // bind url provider staff bind(UrlProvider.class).annotatedWith( Names.named(UrlProviderFactory.TYPE_RESTAPI_JSON)).toProvider( diff --git a/scm-webapp/src/main/java/sonia/scm/resources/AbstractResource.java b/scm-webapp/src/main/java/sonia/scm/resources/AbstractResource.java new file mode 100644 index 0000000000..b4c3136128 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/AbstractResource.java @@ -0,0 +1,215 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.boot.BootstrapUtil; +import sonia.scm.plugin.ScriptResourceServlet; +import sonia.scm.util.IOUtil; +import sonia.scm.util.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import java.util.Collections; +import java.util.List; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public abstract class AbstractResource implements Resource +{ + + /** + * the logger for AbstractResource + */ + private static final Logger logger = + LoggerFactory.getLogger(AbstractResource.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param servletContext + * @param resources + * @param resourceHandlers + */ + public AbstractResource(ServletContext servletContext, + List resources, + List resourceHandlers) + { + this.servletContext = servletContext; + this.resources = resources; + this.resourceHandlers = resourceHandlers; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param stream + * + * @throws IOException + */ + protected void appendResources(OutputStream stream) throws IOException + { + if (Util.isNotEmpty(resources)) + { + for (String resource : resources) + { + appendResource(stream, resource); + } + } + + if (Util.isNotEmpty(resourceHandlers)) + { + Collections.sort(resourceHandlers, new ResourceHandlerComparator()); + + for (ResourceHandler resourceHandler : resourceHandlers) + { + if (resourceHandler.getType() == ResourceType.SCRIPT) + { + appendResource(resourceHandler.getResource(), stream); + } + } + } + } + + /** + * Method description + * + * + * @param stream + * @param resource + * + * @throws IOException + */ + private void appendResource(OutputStream stream, String resource) + throws IOException + { + InputStream input = getResourceAsStream(resource); + + if (input != null) + { + appendResource(input, stream); + } + else if (logger.isWarnEnabled()) + { + logger.warn("could not find resource {}", resource); + } + } + + /** + * Method description + * + * + * @param input + * @param stream + * + * @throws IOException + */ + private void appendResource(InputStream input, OutputStream stream) + throws IOException + { + if (input != null) + { + try + { + IOUtil.copy(input, stream); + } + finally + { + IOUtil.close(input); + } + } + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param resource + * + * @return + */ + private InputStream getResourceAsStream(String resource) + { + InputStream input = null; + ClassLoader classLoader = BootstrapUtil.getClassLoader(servletContext); + + if (classLoader != null) + { + String classLoaderResource = resource; + + if (classLoaderResource.startsWith("/")) + { + classLoaderResource = classLoaderResource.substring(1); + } + + input = classLoader.getResourceAsStream(classLoaderResource); + } + + if (input == null) + { + input = ScriptResourceServlet.class.getResourceAsStream(resource); + } + + return input; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + protected List resourceHandlers; + + /** Field description */ + protected List resources; + + /** Field description */ + private ServletContext servletContext; +} diff --git a/scm-webapp/src/main/java/sonia/scm/resources/AbstractResourceManager.java b/scm-webapp/src/main/java/sonia/scm/resources/AbstractResourceManager.java new file mode 100644 index 0000000000..2cc39ee5fc --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/AbstractResourceManager.java @@ -0,0 +1,309 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.plugin.Plugin; +import sonia.scm.plugin.PluginLoader; +import sonia.scm.plugin.PluginResources; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public abstract class AbstractResourceManager implements ResourceManager +{ + + /** + * Constructs ... + * + * + * + * @param servletContext + * @param pluginLoader + * @param resourceHandlers + */ + public AbstractResourceManager(ServletContext servletContext, + PluginLoader pluginLoader, + Set resourceHandlers) + { + this.servletContext = servletContext; + this.pluginLoader = pluginLoader; + this.resourceHandlers = resourceHandlers; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param resourceMap + */ + protected abstract void collectResources(Map resourceMap); + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param type + * @param name + * + * @return + */ + @Override + public Resource getResource(ResourceType type, String name) + { + return resourceMap.get(new ResourceKey(name, type)); + } + + /** + * Method description + * + * + * @param type + * + * @return + */ + @Override + public List getResources(ResourceType type) + { + List resources = new ArrayList(); + + for (Entry e : resourceMap.entrySet()) + { + if (e.getKey().getType() == type) + { + resources.add(e.getValue()); + } + } + + return resources; + } + + /** + * Method description + * + * + * @return + */ + protected List getScriptResources() + { + List resources = new ArrayList(); + Collection plugins = pluginLoader.getInstalledPlugins(); + + if (plugins != null) + { + for (Plugin plugin : plugins) + { + processPlugin(resources, plugin); + } + } + + return resources; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param resources + * @param plugin + */ + private void processPlugin(List resources, Plugin plugin) + { + PluginResources pluginResources = plugin.getResources(); + + if (pluginResources != null) + { + Set scriptResources = pluginResources.getScriptResources(); + + if (scriptResources != null) + { + resources.addAll(scriptResources); + } + } + } + + //~--- inner classes -------------------------------------------------------- + + /** + * Class description + * + * + * @version Enter version here..., 12/02/03 + * @author Enter your name here... + */ + protected static class ResourceKey + { + + /** + * Constructs ... + * + * + * @param name + * @param type + */ + public ResourceKey(String name, ResourceType type) + { + this.name = name; + this.type = type; + } + + //~--- methods ------------------------------------------------------------ + + /** + * Method description + * + * + * @param obj + * + * @return + */ + @Override + public boolean equals(Object obj) + { + if (obj == null) + { + return false; + } + + if (getClass() != obj.getClass()) + { + return false; + } + + final ResourceKey other = (ResourceKey) obj; + + if ((this.name == null) + ? (other.name != null) + : !this.name.equals(other.name)) + { + return false; + } + + if (this.type != other.type) + { + return false; + } + + return true; + } + + /** + * Method description + * + * + * @return + */ + @Override + public int hashCode() + { + int hash = 7; + + hash = 53 * hash + ((this.name != null) + ? this.name.hashCode() + : 0); + hash = 53 * hash + ((this.type != null) + ? this.type.hashCode() + : 0); + + return hash; + } + + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public String getName() + { + return name; + } + + /** + * Method description + * + * + * @return + */ + public ResourceType getType() + { + return type; + } + + //~--- fields ------------------------------------------------------------- + + /** Field description */ + private String name; + + /** Field description */ + private ResourceType type; + } + + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + protected PluginLoader pluginLoader; + + /** Field description */ + protected Set resourceHandlers; + + /** Field description */ + protected Map resourceMap = new HashMap(); + + /** Field description */ + protected ServletContext servletContext; +} diff --git a/scm-webapp/src/main/java/sonia/scm/resources/DefaultResource.java b/scm-webapp/src/main/java/sonia/scm/resources/DefaultResource.java new file mode 100644 index 0000000000..ba85a984a5 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/DefaultResource.java @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.util.ChecksumUtil; +import sonia.scm.util.IOUtil; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.util.List; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public class DefaultResource extends AbstractResource +{ + + /** + * Constructs ... + * + * + * @param servletContext + * @param resources + * @param resourceHandlers + * @param type + * + * @throws IOException + */ + public DefaultResource(ServletContext servletContext, List resources, + List resourceHandlers, + ResourceType type) + throws IOException + { + super(servletContext, resources, resourceHandlers); + this.type = type; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + try + { + appendResources(baos); + this.content = baos.toByteArray(); + this.name = ChecksumUtil.createChecksum(this.content); + } + finally + { + IOUtil.close(baos); + } + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param output + * + * @throws IOException + */ + @Override + public void copyTo(OutputStream output) throws IOException + { + output.write(content); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getName() + { + return name; + } + + /** + * Method description + * + * + * @return + */ + @Override + public ResourceType getType() + { + return type; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private byte[] content; + + /** Field description */ + private String name; + + /** Field description */ + private ResourceType type; +} diff --git a/scm-webapp/src/main/java/sonia/scm/resources/DefaultResourceManager.java b/scm-webapp/src/main/java/sonia/scm/resources/DefaultResourceManager.java new file mode 100644 index 0000000000..be35c8c663 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/DefaultResourceManager.java @@ -0,0 +1,112 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.plugin.PluginLoader; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public class DefaultResourceManager extends AbstractResourceManager +{ + + /** + * the logger for DefaultResourceManager + */ + private static final Logger logger = + LoggerFactory.getLogger(DefaultResourceManager.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param servletContext + * @param pluginLoader + * @param resourceHandlers + */ + @Inject + public DefaultResourceManager(ServletContext servletContext, + PluginLoader pluginLoader, + Set resourceHandlers) + { + super(servletContext, pluginLoader, resourceHandlers); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param resourceMap + */ + @Override + protected void collectResources(Map resourceMap) + { + List resources = getScriptResources(); + + try + { + Resource resource = new DefaultResource(servletContext, resources, + new ArrayList(resourceHandlers), + ResourceType.SCRIPT); + + resourceMap.put(new ResourceKey(resource.getName(), ResourceType.SCRIPT), + resource); + } + catch (IOException ex) + { + logger.error("could not collect resources", ex); + } + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResource.java b/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResource.java new file mode 100644 index 0000000000..3dda675d9e --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResource.java @@ -0,0 +1,120 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; +import java.io.OutputStream; + +import java.util.List; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public class DevelopmentResource extends AbstractResource +{ + + /** + * Constructs ... + * + * + * + * @param servletContext + * @param resources + * @param resourceHandlers + * @param name + * @param type + */ + public DevelopmentResource(ServletContext servletContext, + List resources, + List resourceHandlers, + String name, ResourceType type) + { + super(servletContext, resources, resourceHandlers); + this.name = name; + this.type = type; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param output + * + * @throws IOException + */ + @Override + public void copyTo(OutputStream output) throws IOException + { + appendResources(output); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getName() + { + return name; + } + + /** + * Method description + * + * + * @return + */ + @Override + public ResourceType getType() + { + return type; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String name; + + /** Field description */ + private ResourceType type; +} diff --git a/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResourceManager.java b/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResourceManager.java new file mode 100644 index 0000000000..f1e62da46e --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/resources/DevelopmentResourceManager.java @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.resources; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; + +import sonia.scm.plugin.PluginLoader; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.ServletContext; + +/** + * + * @author Sebastian Sdorra + */ +public class DevelopmentResourceManager extends AbstractResourceManager +{ + + /** Field description */ + public static final String PREFIX_HANDLER = "handler/"; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param servletContext + * @param pluginLoader + * @param resourceHandlers + */ + @Inject + public DevelopmentResourceManager(ServletContext servletContext, + PluginLoader pluginLoader, + Set resourceHandlers) + { + super(servletContext, pluginLoader, resourceHandlers); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param resourceMap + */ + @Override + protected void collectResources(Map resourceMap) + { + List scripts = getScriptResources(); + + for (String script : scripts) + { + resourceMap.put(new ResourceKey(script, ResourceType.SCRIPT), + new DevelopmentResource(servletContext, + Arrays.asList(script), Collections.EMPTY_LIST, script, + ResourceType.SCRIPT)); + } + + for (ResourceHandler handler : resourceHandlers) + { + String name = handler.getName(); + + if (name.startsWith("/")) + { + name = name.substring(1); + } + + name = PREFIX_HANDLER.concat(name); + resourceMap.put(new ResourceKey(name, ResourceType.SCRIPT), + new DevelopmentResource(servletContext, + Collections.EMPTY_LIST, Arrays.asList(handler), name, + ResourceType.SCRIPT)); + } + } +}