diff --git a/scm-core/src/main/java/sonia/scm/resources/ResourceHandler.java b/scm-core/src/main/java/sonia/scm/resources/ResourceHandler.java new file mode 100644 index 0000000000..fcc4bf149f --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/resources/ResourceHandler.java @@ -0,0 +1,75 @@ +/** + * 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.ExtensionPoint; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.InputStream; + +/** + * + * @author Sebastian Sdorra + */ +@ExtensionPoint +public interface ResourceHandler +{ + + /** + * Method description + * + * + * @return + */ + public String getName(); + + /** + * Method description + * + * + * @return + */ + public InputStream getResource(); + + /** + * Method description + * + * + * @return + */ + public ResourceType getType(); +} diff --git a/scm-core/src/main/java/sonia/scm/resources/ResourceHandlerComparator.java b/scm-core/src/main/java/sonia/scm/resources/ResourceHandlerComparator.java new file mode 100644 index 0000000000..d1e30f0780 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/resources/ResourceHandlerComparator.java @@ -0,0 +1,65 @@ +/** + * 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.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Comparator; + +/** + * + * @author Sebastian Sdorra + */ +public class ResourceHandlerComparator implements Comparator +{ + + /** + * Method description + * + * + * @param handler + * @param otherHandler + * + * @return + */ + @Override + public int compare(ResourceHandler handler, ResourceHandler otherHandler) + { + return Util.compare(handler.getName(), otherHandler.getName()); + } +} diff --git a/scm-core/src/main/java/sonia/scm/resources/ResourceType.java b/scm-core/src/main/java/sonia/scm/resources/ResourceType.java new file mode 100644 index 0000000000..85864cf355 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/resources/ResourceType.java @@ -0,0 +1,40 @@ +/** + * 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; + +/** + * + * @author Sebastian Sdorra + */ +public enum ResourceType { SCRIPT, STYLESHEET } diff --git a/scm-core/src/main/java/sonia/scm/util/Util.java b/scm-core/src/main/java/sonia/scm/util/Util.java index 60a5a58612..e9e7852da5 100644 --- a/scm-core/src/main/java/sonia/scm/util/Util.java +++ b/scm-core/src/main/java/sonia/scm/util/Util.java @@ -54,6 +54,36 @@ public class Util //~--- methods -------------------------------------------------------------- + /** + * Method description + * + * + * @param object + * @param otherObject + * @param + * + * @return + */ + public static int compare(T object, T otherObject) + { + int result = 0; + + if ((object != null) && (otherObject != null)) + { + result = object.compareTo(otherObject); + } + else if ((object == null) && (otherObject != null)) + { + result = 1; + } + else if ((object != null) && (otherObject == null)) + { + result = -1; + } + + return result; + } + /** * Method description * diff --git a/scm-core/src/test/java/sonia/scm/resources/ResourceHandlerComparatorTest.java b/scm-core/src/test/java/sonia/scm/resources/ResourceHandlerComparatorTest.java new file mode 100644 index 0000000000..8f3eb8901a --- /dev/null +++ b/scm-core/src/test/java/sonia/scm/resources/ResourceHandlerComparatorTest.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 org.junit.Test; + +import static org.junit.Assert.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.InputStream; + +import java.util.Arrays; + +/** + * + * @author Sebastian Sdorra + */ +public class ResourceHandlerComparatorTest +{ + + /** + * Method description + * + */ + @Test + public void testCompare() + { + ResourceHandler[] handlers = new ResourceHandler[4]; + + handlers[0] = new DummyResourceHandler("xyz"); + handlers[1] = new DummyResourceHandler("abc"); + handlers[2] = new DummyResourceHandler(null); + handlers[3] = new DummyResourceHandler("mno"); + Arrays.sort(handlers, new ResourceHandlerComparator()); + assertEquals("abc", handlers[0].getName()); + assertEquals("mno", handlers[1].getName()); + assertEquals("xyz", handlers[2].getName()); + assertEquals(null, handlers[3].getName()); + } + + //~--- inner classes -------------------------------------------------------- + + /** + * Class description + * + * + * @version Enter version here..., 2011-01-18 + * @author Sebastian Sdorra + */ + private static class DummyResourceHandler implements ResourceHandler + { + + /** + * Constructs ... + * + * + * @param name + */ + public DummyResourceHandler(String name) + { + this.name = name; + } + + //~--- get methods -------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getName() + { + return name; + } + + /** + * Method description + * + * + * @return + */ + @Override + public InputStream getResource() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Method description + * + * + * @return + */ + @Override + public ResourceType getType() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + //~--- fields ------------------------------------------------------------- + + /** Field description */ + private String name; + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/BindingExtensionProcessor.java b/scm-webapp/src/main/java/sonia/scm/BindingExtensionProcessor.java index 05764efb2b..fe9fcc8988 100644 --- a/scm-webapp/src/main/java/sonia/scm/BindingExtensionProcessor.java +++ b/scm-webapp/src/main/java/sonia/scm/BindingExtensionProcessor.java @@ -47,6 +47,7 @@ import sonia.scm.plugin.ext.Extension; import sonia.scm.plugin.ext.ExtensionProcessor; import sonia.scm.repository.RepositoryHandler; import sonia.scm.repository.RepositoryListener; +import sonia.scm.resources.ResourceHandler; import sonia.scm.security.EncryptionHandler; import sonia.scm.user.UserListener; import sonia.scm.web.security.AuthenticationHandler; @@ -97,6 +98,8 @@ public class BindingExtensionProcessor implements ExtensionProcessor Multibinder.newSetBinder(binder, RepositoryHandler.class); Multibinder authenticators = Multibinder.newSetBinder(binder, AuthenticationHandler.class); + Multibinder resourceHandler = + Multibinder.newSetBinder(binder, ResourceHandler.class); authenticators.addBinding().to(XmlAuthenticationHandler.class); @@ -176,6 +179,15 @@ public class BindingExtensionProcessor implements ExtensionProcessor authenticationListeners.add(listener); } + else if (ResourceHandler.class.isAssignableFrom(extensionClass)) + { + if (logger.isInfoEnabled()) + { + logger.info("bind ResourceHandler {}", extensionClass.getName()); + } + + resourceHandler.addBinding().to(extensionClass); + } else { if (logger.isInfoEnabled()) diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java b/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java index b3c7bc5ee1..7f8d31fc1e 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java @@ -39,6 +39,9 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import sonia.scm.boot.BootstrapUtil; +import sonia.scm.resources.ResourceHandler; +import sonia.scm.resources.ResourceHandlerComparator; +import sonia.scm.resources.ResourceType; import sonia.scm.util.IOUtil; import sonia.scm.util.Util; @@ -48,7 +51,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -75,11 +81,14 @@ public class ScriptResourceServlet extends AbstractResourceServlet * * * @param pluginLoader + * @param resourceHandlers */ @Inject - public ScriptResourceServlet(PluginLoader pluginLoader) + public ScriptResourceServlet(PluginLoader pluginLoader, + Set resourceHandlers) { this.pluginLoader = pluginLoader; + this.resourceHandlers = resourceHandlers; } //~--- methods -------------------------------------------------------------- @@ -110,6 +119,22 @@ public class ScriptResourceServlet extends AbstractResourceServlet appendResource(stream, resource); } } + + if (Util.isNotEmpty(resourceHandlers)) + { + List handlerList = + new ArrayList(resourceHandlers); + + Collections.sort(handlerList, new ResourceHandlerComparator()); + + for (ResourceHandler resourceHandler : resourceHandlers) + { + if (resourceHandler.getType() == ResourceType.SCRIPT) + { + appendResource(resourceHandler.getResource(), stream); + } + } + } } //~--- get methods ---------------------------------------------------------- @@ -143,6 +168,21 @@ public class ScriptResourceServlet extends AbstractResourceServlet { InputStream input = getResourceAsStream(script); + appendResource(input, stream); + } + + /** + * Method description + * + * + * @param input + * @param stream + * + * @throws IOException + */ + private void appendResource(InputStream input, OutputStream stream) + throws IOException + { if (input != null) { try @@ -239,4 +279,7 @@ public class ScriptResourceServlet extends AbstractResourceServlet /** Field description */ private PluginLoader pluginLoader; + + /** Field description */ + private Set resourceHandlers; }