diff --git a/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java b/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java index d4264942c8..90b854900f 100644 --- a/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java +++ b/plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java @@ -40,15 +40,12 @@ import com.google.inject.Provider; import com.google.inject.Singleton; import sonia.scm.repository.GitRepositoryHandler; -import sonia.scm.repository.Repository; -import sonia.scm.web.filter.PermissionFilter; +import sonia.scm.repository.RepositoryManager; +import sonia.scm.web.filter.RegexPermissionFilter; import sonia.scm.web.security.SecurityContext; //~--- JDK imports ------------------------------------------------------------ -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import javax.servlet.http.HttpServletRequest; /** @@ -56,7 +53,7 @@ import javax.servlet.http.HttpServletRequest; * @author Sebastian Sdorra */ @Singleton -public class GitPermissionFilter extends PermissionFilter +public class GitPermissionFilter extends RegexPermissionFilter { /** Field description */ @@ -70,14 +67,13 @@ public class GitPermissionFilter extends PermissionFilter * * * @param securityContextProvider - * @param handler + * @param repositoryManager */ @Inject public GitPermissionFilter(Provider securityContextProvider, - GitRepositoryHandler handler) + RepositoryManager repositoryManager) { - super(securityContextProvider); - this.handler = handler; + super(securityContextProvider, repositoryManager); } //~--- get methods ---------------------------------------------------------- @@ -86,14 +82,12 @@ public class GitPermissionFilter extends PermissionFilter * Method description * * - * @param name - * * @return */ @Override - protected Repository getRepository(String name) + protected String getType() { - return handler.getByName(name); + return GitRepositoryHandler.TYPE_NAME; } /** @@ -109,9 +103,4 @@ public class GitPermissionFilter extends PermissionFilter { return request.getRequestURI().endsWith(PATTERN_WRITEREQUEST); } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private GitRepositoryHandler handler; } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java index 6cdf68a255..df934ed5fe 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHandler.java @@ -66,22 +66,6 @@ public class HgRepositoryHandler /** Field description */ public static final Type TYPE = new Type(TYPE_NAME, TYPE_DISPLAYNAME); - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param repository - * - * @return - */ - @Override - public String createResourcePath(Repository repository) - { - return "/hg/".concat(repository.getName()); - } - //~--- get methods ---------------------------------------------------------- /** diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java index b03b5f3c29..51c530dc5d 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java @@ -40,6 +40,7 @@ import com.google.inject.Singleton; import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryManager; import sonia.scm.web.cgi.AbstractCGIServlet; import sonia.scm.web.cgi.EnvList; @@ -81,11 +82,15 @@ public class HgCGIServlet extends AbstractCGIServlet * Constructs ... * * + * + * @param repositoryManager * @param handler */ @Inject - public HgCGIServlet(HgRepositoryHandler handler) + public HgCGIServlet(RepositoryManager repositoryManager, + HgRepositoryHandler handler) { + this.repositoryManager = repositoryManager; this.handler = handler; } @@ -194,7 +199,7 @@ public class HgCGIServlet extends AbstractCGIServlet */ private Repository getRepository(String repositoryname) { - return handler.getByName(repositoryname); + return repositoryManager.get(HgRepositoryHandler.TYPE_NAME, repositoryname); } //~--- fields --------------------------------------------------------------- @@ -204,4 +209,7 @@ public class HgCGIServlet extends AbstractCGIServlet /** Field description */ private HgRepositoryHandler handler; + + /** Field description */ + private RepositoryManager repositoryManager; } diff --git a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgPermissionFilter.java b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgPermissionFilter.java index 18ddfba451..2b5c44a89d 100644 --- a/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgPermissionFilter.java +++ b/plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgPermissionFilter.java @@ -41,7 +41,9 @@ import com.google.inject.Singleton; import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryManager; import sonia.scm.web.filter.PermissionFilter; +import sonia.scm.web.filter.RegexPermissionFilter; import sonia.scm.web.security.SecurityContext; //~--- JDK imports ------------------------------------------------------------ @@ -53,7 +55,7 @@ import javax.servlet.http.HttpServletRequest; * @author Sebastian Sdorra */ @Singleton -public class HgPermissionFilter extends PermissionFilter +public class HgPermissionFilter extends RegexPermissionFilter { /** @@ -61,14 +63,13 @@ public class HgPermissionFilter extends PermissionFilter * * * @param securityContextProvider - * @param handler + * @param repositoryManager */ @Inject public HgPermissionFilter(Provider securityContextProvider, - HgRepositoryHandler handler) + RepositoryManager repositoryManager) { - super(securityContextProvider); - this.handler = handler; + super(securityContextProvider, repositoryManager); } //~--- get methods ---------------------------------------------------------- @@ -77,14 +78,12 @@ public class HgPermissionFilter extends PermissionFilter * Method description * * - * @param name - * * @return */ @Override - protected Repository getRepository(String name) + protected String getType() { - return handler.getByName(name); + return HgRepositoryHandler.TYPE_NAME; } /** @@ -100,9 +99,4 @@ public class HgPermissionFilter extends PermissionFilter { return !request.getMethod().equalsIgnoreCase("GET"); } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private HgRepositoryHandler handler; } diff --git a/plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnPermissionFilter.java b/plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnPermissionFilter.java index baa14bb158..ed289cf6b7 100644 --- a/plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnPermissionFilter.java +++ b/plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnPermissionFilter.java @@ -40,8 +40,10 @@ import com.google.inject.Provider; import com.google.inject.Singleton; import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.SvnRepositoryHandler; import sonia.scm.web.filter.PermissionFilter; +import sonia.scm.web.filter.RegexPermissionFilter; import sonia.scm.web.security.SecurityContext; //~--- JDK imports ------------------------------------------------------------ @@ -57,7 +59,7 @@ import javax.servlet.http.HttpServletRequest; * @author Sebastian Sdorra */ @Singleton -public class SvnPermissionFilter extends PermissionFilter +public class SvnPermissionFilter extends RegexPermissionFilter { /** Field description */ @@ -74,14 +76,13 @@ public class SvnPermissionFilter extends PermissionFilter * * * @param securityContextProvider - * @param handler + * @param repositoryManager */ @Inject public SvnPermissionFilter(Provider securityContextProvider, - SvnRepositoryHandler handler) + RepositoryManager repositoryManager) { - super(securityContextProvider); - this.handler = handler; + super(securityContextProvider, repositoryManager); } //~--- get methods ---------------------------------------------------------- @@ -90,14 +91,12 @@ public class SvnPermissionFilter extends PermissionFilter * Method description * * - * @param name - * * @return */ @Override - protected Repository getRepository(String name) + protected String getType() { - return handler.getByName(name); + return SvnRepositoryHandler.TYPE_NAME; } /** @@ -113,9 +112,4 @@ public class SvnPermissionFilter extends PermissionFilter { return WRITEMETHOD_SET.contains(request.getMethod().toUpperCase()); } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private SvnRepositoryHandler handler; } diff --git a/scm-core/src/main/java/sonia/scm/HandlerBase.java b/scm-core/src/main/java/sonia/scm/HandlerBase.java index 69cd75bded..cf79f83393 100644 --- a/scm-core/src/main/java/sonia/scm/HandlerBase.java +++ b/scm-core/src/main/java/sonia/scm/HandlerBase.java @@ -1,10 +1,10 @@ /** * 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, @@ -13,7 +13,7 @@ * 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 @@ -24,22 +24,29 @@ * 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; +//~--- JDK imports ------------------------------------------------------------ + import java.io.Closeable; import java.io.IOException; -import java.util.Collection; /** * * @author Sebastian Sdorra + * + * @param + * @param */ -public interface HandlerBase extends Initable, Closeable +public interface HandlerBase + extends Initable, Closeable { /** @@ -74,37 +81,4 @@ public interface HandlerBase extends * @throws IOException */ public void modify(T object) throws E, IOException; - - /** - * Method description - * - * - * @param object - * - * @throws E - * @throws IOException - */ - public void refresh(T object) throws E, IOException; - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * - * @param id - * - * @return - */ - public T get(String id); - - /** - * Method description - * - * - * @return - */ - public Collection getAll(); - } diff --git a/scm-core/src/main/java/sonia/scm/Manager.java b/scm-core/src/main/java/sonia/scm/Manager.java index 53659ab0cc..4862236fa2 100644 --- a/scm-core/src/main/java/sonia/scm/Manager.java +++ b/scm-core/src/main/java/sonia/scm/Manager.java @@ -35,6 +35,8 @@ package sonia.scm; //~--- JDK imports ------------------------------------------------------------ +import java.io.IOException; + import java.util.Collection; /** @@ -48,6 +50,38 @@ public interface Manager extends HandlerBase { + /** + * Method description + * + * + * @param object + * + * @throws E + * @throws IOException + */ + public void refresh(T object) throws E, IOException; + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * + * @param id + * + * @return + */ + public T get(String id); + + /** + * Method description + * + * + * @return + */ + public Collection getAll(); + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java index f1eb7e5481..23743fa6e4 100644 --- a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java +++ b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java @@ -49,7 +49,6 @@ import java.io.IOException; import java.util.HashSet; import java.util.Set; -import java.util.UUID; import javax.xml.bind.JAXB; @@ -212,18 +211,6 @@ public abstract class AbstractRepositoryHandler getAll() - { - List repositories = new ArrayList(); - File[] repositoryFiles = configDirectory.listFiles(new FilenameFilter() - { - @Override - public boolean accept(File dir, String name) - { - return name.endsWith(".xml"); - } - }); - - for (File repositoryFile : repositoryFiles) - { - try - { - Repository repository = getRepositoryFromConfig(repositoryFile); - - if (repository != null) - { - repositories.add(repository); - } - } - catch (Exception ex) - { - logger.error(ex.getMessage(), ex); - } - } - - return repositories; - } - - /** - * Method description - * - * - * @param repositoryname - * - * @return - */ - public Repository getByName(String repositoryname) - { - Repository repository = null; - - for (Repository r : getAll()) - { - if (r.getName().equals(repositoryname)) - { - repository = r; - - break; - } - } - - return repository; - } - /** * Method description * @@ -395,24 +268,6 @@ public abstract class AbstractSimpleRepositoryHandler { + /** + * Method description + * + * + * @param type + * @param name + * + * @return + */ + public Repository get(String type, String name); + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryDatabase.java b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryDatabase.java new file mode 100644 index 0000000000..4820894ff6 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryDatabase.java @@ -0,0 +1,267 @@ +/** + * 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.repository.xml; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.repository.Repository; +import sonia.scm.xml.XmlTimestampDateAdapter; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +/** + * + * @author Sebastian Sdorra + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class XmlRepositoryDatabase +{ + + /** + * Method description + * + * + * @param repository + */ + public void add(Repository repository) + { + repositoryMap.put(createKey(repository), repository); + } + + /** + * Method description + * + * + * + * @param type + * @param name + * + * @return + */ + public boolean contains(String type, String name) + { + return repositoryMap.containsKey(createKey(type, name)); + } + + /** + * Method description + * + * + * @param id + * + * @return + */ + public boolean contains(String id) + { + return get(id) != null; + } + + /** + * Method description + * + * + * @param repository + * + * @return + */ + public boolean contains(Repository repository) + { + return repositoryMap.containsKey(createKey(repository)); + } + + /** + * Method description + * + * + * @param repository + */ + public void remove(Repository repository) + { + repositoryMap.remove(createKey(repository)); + } + + /** + * Method description + * + * + * @return + */ + public Collection values() + { + return repositoryMap.values(); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param type + * @param name + * + * @return + */ + public Repository get(String type, String name) + { + return repositoryMap.get(createKey(type, name)); + } + + /** + * Method description + * + * + * @param id + * + * @return + */ + public Repository get(String id) + { + Repository repository = null; + + for (Repository r : values()) + { + if (r.getId().equals(id)) + { + repository = r; + + break; + } + } + + return repository; + } + + /** + * Method description + * + * + * @return + */ + public long getCreationTime() + { + return creationTime; + } + + /** + * Method description + * + * + * @return + */ + public long getLastModified() + { + return lastModified; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param creationTime + */ + public void setCreationTime(long creationTime) + { + this.creationTime = creationTime; + } + + /** + * Method description + * + * + * @param lastModified + */ + public void setLastModified(long lastModified) + { + this.lastModified = lastModified; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param type + * @param name + * + * @return + */ + static String createKey(String type, String name) + { + return type.concat(":").concat(name); + } + + /** + * Method description + * + * + * @param repository + * + * @return + */ + static String createKey(Repository repository) + { + return createKey(repository.getType(), repository.getName()); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlJavaTypeAdapter(XmlTimestampDateAdapter.class) + private Long creationTime; + + /** Field description */ + @XmlJavaTypeAdapter(XmlTimestampDateAdapter.class) + private Long lastModified; + + /** Field description */ + @XmlJavaTypeAdapter(XmlRepositoryMapAdapter.class) + @XmlElement(name = "repositories") + private Map repositoryMap = new LinkedHashMap(); +} diff --git a/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryList.java b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryList.java new file mode 100644 index 0000000000..d9807e9188 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryList.java @@ -0,0 +1,123 @@ +/** + * 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.repository.xml; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.repository.Repository; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sebastian Sdorra + */ +@XmlRootElement(name = "repositories") +@XmlAccessorType(XmlAccessType.FIELD) +public class XmlRepositoryList implements Iterable +{ + + /** + * Constructs ... + * + */ + public XmlRepositoryList() {} + + /** + * Constructs ... + * + * + * + * @param repositoryMap + */ + public XmlRepositoryList(Map repositoryMap) + { + this.repositories = new LinkedList(repositoryMap.values()); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public Iterator iterator() + { + return repositories.iterator(); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public LinkedList getRepositories() + { + return repositories; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param repositories + */ + public void setRepositories(LinkedList repositories) + { + this.repositories = repositories; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlElement(name = "repository") + private LinkedList repositories; +} diff --git a/scm-core/src/main/java/sonia/scm/repository/BasicRepositoryManager.java b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java similarity index 64% rename from scm-core/src/main/java/sonia/scm/repository/BasicRepositoryManager.java rename to scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java index 0eeab40370..42b9e6ac08 100644 --- a/scm-core/src/main/java/sonia/scm/repository/BasicRepositoryManager.java +++ b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java @@ -31,7 +31,7 @@ -package sonia.scm.repository; +package sonia.scm.repository.xml; //~--- non-JDK imports -------------------------------------------------------- @@ -46,32 +46,45 @@ import sonia.scm.HandlerEvent; import sonia.scm.SCMContext; import sonia.scm.SCMContextProvider; import sonia.scm.Type; +import sonia.scm.repository.AbstractRepositoryManager; +import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryAllreadyExistExeption; +import sonia.scm.repository.RepositoryException; +import sonia.scm.repository.RepositoryHandler; +import sonia.scm.repository.RepositoryHandlerNotFoundException; import sonia.scm.util.AssertUtil; import sonia.scm.util.IOUtil; //~--- JDK imports ------------------------------------------------------------ +import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.LinkedList; import java.util.Map; import java.util.Set; +import java.util.UUID; + +import javax.xml.bind.JAXB; /** * * @author Sebastian Sdorra */ @Singleton -public class BasicRepositoryManager extends AbstractRepositoryManager +public class XmlRepositoryManager extends AbstractRepositoryManager { + /** Field description */ + public static final String DATABASEFILE = + "config".concat(File.separator).concat("repositories.xml"); + /** Field description */ private static final Logger logger = - LoggerFactory.getLogger(BasicRepositoryManager.class); + LoggerFactory.getLogger(XmlRepositoryManager.class); //~--- constructors --------------------------------------------------------- @@ -82,10 +95,10 @@ public class BasicRepositoryManager extends AbstractRepositoryManager * @param handlerSet */ @Inject - public BasicRepositoryManager(Set handlerSet) + public XmlRepositoryManager(Set handlerSet) { handlerMap = new HashMap(); - types = new ArrayList(); + types = new HashSet(); for (RepositoryHandler handler : handlerSet) { @@ -130,7 +143,22 @@ public class BasicRepositoryManager extends AbstractRepositoryManager } AssertUtil.assertIsValid(repository); + + if (repositoryDB.contains(repository)) + { + throw new RepositoryAllreadyExistExeption(); + } + + repository.setId(UUID.randomUUID().toString()); + repository.setCreationDate(System.currentTimeMillis()); getHandler(repository).create(repository); + + synchronized (XmlRepositoryDatabase.class) + { + repositoryDB.add(repository); + storeDB(); + } + fireEvent(repository, HandlerEvent.CREATE); } @@ -153,7 +181,22 @@ public class BasicRepositoryManager extends AbstractRepositoryManager repository.getType()); } - getHandler(repository).delete(repository); + if (repositoryDB.contains(repository)) + { + getHandler(repository).delete(repository); + + synchronized (XmlRepositoryDatabase.class) + { + repositoryDB.remove(repository); + storeDB(); + } + } + else + { + throw new RepositoryException( + "repository ".concat(repository.getName()).concat(" not found")); + } + fireEvent(repository, HandlerEvent.DELETE); } @@ -164,7 +207,23 @@ public class BasicRepositoryManager extends AbstractRepositoryManager * @param context */ @Override - public void init(SCMContextProvider context) {} + public void init(SCMContextProvider context) + { + File directory = context.getBaseDirectory(); + + repositoryDBFile = new File(directory, DATABASEFILE); + + if (repositoryDBFile.exists()) + { + loadDB(); + } + else + { + IOUtil.mkdirs(repositoryDBFile.getParentFile()); + repositoryDB = new XmlRepositoryDatabase(); + repositoryDB.setCreationTime(System.currentTimeMillis()); + } + } /** * Method description @@ -186,7 +245,25 @@ public class BasicRepositoryManager extends AbstractRepositoryManager } AssertUtil.assertIsValid(repository); - getHandler(repository).modify(repository); + + if (repositoryDB.contains(repository)) + { + getHandler(repository).modify(repository); + repository.setLastModified(System.currentTimeMillis()); + + synchronized (XmlRepositoryDatabase.class) + { + repositoryDB.remove(repository); + repositoryDB.add(repository.clone()); + storeDB(); + } + } + else + { + throw new RepositoryException( + "repository ".concat(repository.getName()).concat(" not found")); + } + fireEvent(repository, HandlerEvent.MODIFY); } @@ -203,7 +280,20 @@ public class BasicRepositoryManager extends AbstractRepositoryManager public void refresh(Repository repository) throws RepositoryException, IOException { - getHandler(repository).refresh(repository); + AssertUtil.assertIsNotNull(repository); + + Repository fresh = repositoryDB.get(repository.getType(), + repository.getName()); + + if (repository != null) + { + repository.copyProperties(fresh); + } + else + { + throw new RepositoryException( + "repository ".concat(repository.getName()).concat(" not found")); + } } //~--- get methods ---------------------------------------------------------- @@ -212,7 +302,6 @@ public class BasicRepositoryManager extends AbstractRepositoryManager * Method description * * - * * @param id * * @return @@ -220,19 +309,38 @@ public class BasicRepositoryManager extends AbstractRepositoryManager @Override public Repository get(String id) { - Repository repository = null; + AssertUtil.assertIsNotEmpty(id); - for (RepositoryHandler handler : handlerMap.values()) + Repository repository = repositoryDB.get(id); + + if (repository != null) { - if (handler.isConfigured()) - { - repository = handler.get(id); + repository = repository.clone(); + } - if (repository != null) - { - break; - } - } + return repository; + } + + /** + * Method description + * + * + * @param type + * @param name + * + * @return + */ + @Override + public Repository get(String type, String name) + { + AssertUtil.assertIsNotEmpty(type); + AssertUtil.assertIsNotEmpty(name); + + Repository repository = repositoryDB.get(type, name); + + if (repository != null) + { + repository = repository.clone(); } return repository; @@ -247,29 +355,11 @@ public class BasicRepositoryManager extends AbstractRepositoryManager @Override public Collection getAll() { - if (logger.isDebugEnabled()) + LinkedList repositories = new LinkedList(); + + for (Repository repository : repositoryDB.values()) { - logger.debug("fetch all repositories"); - } - - Set repositories = new HashSet(); - - for (RepositoryHandler handler : handlerMap.values()) - { - if (handler.isConfigured()) - { - Collection handlerRepositories = handler.getAll(); - - if (handlerRepositories != null) - { - repositories.addAll(handlerRepositories); - } - } - } - - if (logger.isDebugEnabled()) - { - logger.debug("fetched {} repositories", repositories.size()); + repositories.add(repository.clone()); } return repositories; @@ -334,6 +424,26 @@ public class BasicRepositoryManager extends AbstractRepositoryManager types.add(type); } + /** + * Method description + * + */ + private void loadDB() + { + repositoryDB = JAXB.unmarshal(repositoryDBFile, + XmlRepositoryDatabase.class); + } + + /** + * Method description + * + */ + private void storeDB() + { + repositoryDB.setLastModified(System.currentTimeMillis()); + JAXB.marshal(repositoryDB, repositoryDBFile); + } + //~--- get methods ---------------------------------------------------------- /** @@ -372,5 +482,11 @@ public class BasicRepositoryManager extends AbstractRepositoryManager private Map handlerMap; /** Field description */ - private List types; + private XmlRepositoryDatabase repositoryDB; + + /** Field description */ + private File repositoryDBFile; + + /** Field description */ + private Set types; } diff --git a/scm-core/src/test/java/sonia/scm/user/XmlUserHandlerTest.java b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryMapAdapter.java similarity index 58% rename from scm-core/src/test/java/sonia/scm/user/XmlUserHandlerTest.java rename to scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryMapAdapter.java index 6bb8475175..a90a2d4fa9 100644 --- a/scm-core/src/test/java/sonia/scm/user/XmlUserHandlerTest.java +++ b/scm-core/src/main/java/sonia/scm/repository/xml/XmlRepositoryMapAdapter.java @@ -31,26 +31,67 @@ -package sonia.scm.user; +package sonia.scm.repository.xml; -import sonia.scm.user.xml.XmlUserHandler; +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.repository.Repository; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.XmlAdapter; /** * * @author Sebastian Sdorra */ -public class XmlUserHandlerTest extends UserHandlerTestBase +public class XmlRepositoryMapAdapter + extends XmlAdapter> { /** * Method description * * + * @param repositoryMap + * * @return + * + * @throws Exception */ @Override - public UserHandler createUserHandler() + public XmlRepositoryList marshal(Map repositoryMap) + throws Exception { - return new XmlUserHandler(); + return new XmlRepositoryList(repositoryMap); + } + + /** + * Method description + * + * + * @param repositories + * + * @return + * + * @throws Exception + */ + @Override + public Map unmarshal(XmlRepositoryList repositories) + throws Exception + { + Map repositoryMap = new LinkedHashMap(); + + for (Repository repository : repositories) + { + repositoryMap.put(XmlRepositoryDatabase.createKey(repository), + repository); + } + + return repositoryMap; } } diff --git a/scm-core/src/main/java/sonia/scm/user/BasicUserManager.java b/scm-core/src/main/java/sonia/scm/user/BasicUserManager.java index 4a072551bc..d8f34ec36e 100644 --- a/scm-core/src/main/java/sonia/scm/user/BasicUserManager.java +++ b/scm-core/src/main/java/sonia/scm/user/BasicUserManager.java @@ -216,7 +216,7 @@ public class BasicUserManager implements UserManager logger.info("refresh user {} of type {}", user.getName(), user.getType()); } - getHandler(user).refresh(user); + // getHandler(user).refresh(user); } /** @@ -250,8 +250,8 @@ public class BasicUserManager implements UserManager { if (handler.isConfigured()) { - user = handler.get(id); + // user = handler.get(id); if (user != null) { break; @@ -278,19 +278,20 @@ public class BasicUserManager implements UserManager Set repositories = new HashSet(); - for (UserHandler handler : handlerMap.values()) - { - if (handler.isConfigured()) - { - Collection handlerRepositories = handler.getAll(); - - if (handlerRepositories != null) - { - repositories.addAll(handlerRepositories); - } - } - } - + /* + * for (UserHandler handler : handlerMap.values()) + * { + * if (handler.isConfigured()) + * { + * Collection handlerRepositories = handler.getAll(); + * + * if (handlerRepositories != null) + * { + * repositories.addAll(handlerRepositories); + * } + * } + * } + */ if (logger.isDebugEnabled()) { logger.debug("fetched {} users", repositories.size()); diff --git a/scm-core/src/main/java/sonia/scm/user/User.java b/scm-core/src/main/java/sonia/scm/user/User.java index 3884f02bd9..d573b4ccbb 100644 --- a/scm-core/src/main/java/sonia/scm/user/User.java +++ b/scm-core/src/main/java/sonia/scm/user/User.java @@ -117,6 +117,22 @@ public class User return user; } + /** + * Method description + * + * + * @param user + */ + public void copyProperties(User user) + { + user.setAdmin(admin); + user.setDisplayName(displayName); + user.setMail(mail); + user.setName(name); + user.setPassword(password); + user.setType(type); + } + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/user/xml/XmlUserHandler.java b/scm-core/src/main/java/sonia/scm/user/xml/XmlUserHandler.java index 42ab029854..4891fd2bd5 100644 --- a/scm-core/src/main/java/sonia/scm/user/xml/XmlUserHandler.java +++ b/scm-core/src/main/java/sonia/scm/user/xml/XmlUserHandler.java @@ -225,7 +225,6 @@ public class XmlUserHandler implements UserHandler * @throws IOException * @throws UserException */ - @Override public void refresh(User user) throws UserException, IOException { User fresh = userDB.get(user.getName()); @@ -235,10 +234,7 @@ public class XmlUserHandler implements UserHandler throw new UserException("user does not exists"); } - user.setDisplayName(fresh.getDisplayName()); - user.setMail(fresh.getMail()); - user.setPassword(fresh.getPassword()); - user.setType(TYPE_NAME); + fresh.copyProperties(user); } //~--- get methods ---------------------------------------------------------- @@ -251,7 +247,6 @@ public class XmlUserHandler implements UserHandler * * @return */ - @Override public User get(String id) { User user = userDB.get(id); @@ -270,7 +265,6 @@ public class XmlUserHandler implements UserHandler * * @return */ - @Override public Collection getAll() { LinkedList users = new LinkedList(); diff --git a/scm-core/src/test/java/sonia/scm/user/UserHandlerTestBase.java b/scm-core/src/test/java/sonia/scm/user/UserHandlerTestBase.java deleted file mode 100644 index fc23a6c2bb..0000000000 --- a/scm-core/src/test/java/sonia/scm/user/UserHandlerTestBase.java +++ /dev/null @@ -1,515 +0,0 @@ -/** - * 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.user; - -//~--- non-JDK imports -------------------------------------------------------- - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import sonia.scm.SCMContextProvider; -import sonia.scm.util.IOUtil; - -import static org.junit.Assert.*; - -import static org.mockito.Mockito.*; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; -import java.io.IOException; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.UUID; - -/** - * - * @author Sebastian Sdorra - */ -public abstract class UserHandlerTestBase -{ - - /** Field description */ - public static final int THREAD_COUNT = 10; - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - public abstract UserHandler createUserHandler(); - - /** - * Method description - * - * - * @throws IOException - */ - @After - public void tearDownTest() throws IOException - { - try - { - handler.close(); - } - finally - { - IOUtil.delete(tempDirectory); - } - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testCreate() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - - User otherUser = handler.get("zaphod"); - - assertNotNull(otherUser); - assertUserEquals(zaphod, otherUser); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test(expected = UserAllreadyExistException.class) - public void testCreateExisting() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - - User sameUser = getTestUser(); - - handler.create(sameUser); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testDelete() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - handler.delete(zaphod); - assertNull(handler.get("zaphod")); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testGet() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - - // test for reference - zaphod.setDisplayName("Tricia McMillan"); - zaphod = handler.get("zaphod"); - assertNotNull(zaphod); - assertEquals("Zaphod Beeblebrox", zaphod.getDisplayName()); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testGetAll() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - - User trillian = new User("trillian", "Tricia McMillan", - "tricia.mcmillan@hitchhiker.com"); - - handler.create(trillian); - assertNotNull(handler.get("trillian")); - - boolean foundZaphod = false; - boolean foundTrillian = false; - Collection users = handler.getAll(); - - assertNotNull(users); - assertFalse(users.isEmpty()); - assertTrue(users.size() >= 2); - - for (User u : users) - { - if (u.getName().equals("zaphod")) - { - foundZaphod = true; - assertUserEquals(zaphod, u); - } - else if (u.getName().equals("trillian")) - { - foundTrillian = true; - assertUserEquals(trillian, u); - } - } - - assertTrue(foundZaphod); - assertTrue(foundTrillian); - - // test for reference - trillian = null; - - for (User u : users) - { - if (u.getName().equals("trillian")) - { - trillian = u; - } - } - - assertNotNull(trillian); - trillian.setDisplayName("Zaphod Beeblebrox"); - - User reference = null; - - for (User u : handler.getAll()) - { - if (u.getName().equals("trillian")) - { - reference = u; - } - } - - assertNotNull(reference); - assertEquals(reference.getDisplayName(), "Tricia McMillan"); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testModify() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - zaphod.setDisplayName("Tricia McMillan"); - handler.modify(zaphod); - - User otherUser = handler.get("zaphod"); - - assertNotNull(otherUser); - assertEquals(otherUser.getDisplayName(), "Tricia McMillan"); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test(expected = UserException.class) - public void testModifyNotExisting() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.modify(zaphod); - } - - /** - * Method description - * - * - * @throws IOException - * @throws InterruptedException - * @throws UserException - */ - @Test - public void testMultiThreaded() - throws UserException, IOException, InterruptedException - { - int initialSize = handler.getAll().size(); - List testers = new ArrayList(); - - for (int i = 0; i < THREAD_COUNT; i++) - { - testers.add(new MultiThreadTester(handler)); - } - - for (MultiThreadTester tester : testers) - { - new Thread(tester).start(); - } - - boolean fin = false; - - while (!fin) - { - Thread.sleep(100l); - fin = true; - - for (MultiThreadTester tester : testers) - { - if (!tester.finished) - { - fin = false; - } - } - } - - assertTrue((initialSize + THREAD_COUNT) == handler.getAll().size()); - } - - /** - * Method description - * - * - * @throws IOException - * @throws UserException - */ - @Test - public void testRefresh() throws UserException, IOException - { - User zaphod = getTestUser(); - - handler.create(zaphod); - assertNotNull(handler.get("zaphod")); - zaphod.setDisplayName("Tricia McMillan"); - handler.refresh(zaphod); - assertEquals(zaphod.getDisplayName(), "Zaphod Beeblebrox"); - } - - //~--- set methods ---------------------------------------------------------- - - /** - * Method description - * - */ - @Before - public void setUpTest() - { - tempDirectory = new File(System.getProperty("java.io.tmpdir"), - UUID.randomUUID().toString()); - assertTrue(tempDirectory.mkdirs()); - handler = createUserHandler(); - - SCMContextProvider provider = mock(SCMContextProvider.class); - - when(provider.getBaseDirectory()).thenReturn(tempDirectory); - handler.init(provider); - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param user - * @param otherUser - */ - private void assertUserEquals(User user, User otherUser) - { - assertEquals(user.getName(), otherUser.getName()); - assertEquals(user.getDisplayName(), otherUser.getDisplayName()); - assertEquals(user.getMail(), otherUser.getMail()); - assertEquals(user.getPassword(), otherUser.getPassword()); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - private User getTestUser() - { - return new User("zaphod", "Zaphod Beeblebrox", - "zaphod.beeblebrox@hitchhiker.com"); - } - - //~--- inner classes -------------------------------------------------------- - - /** - * Class description - * - * - * @version Enter version here..., 2010-11-23 - * @author Sebastian Sdorra - */ - private static class MultiThreadTester implements Runnable - { - - /** - * Constructs ... - * - * - * @param userHandler - */ - public MultiThreadTester(UserHandler userHandler) - { - this.userHandler = userHandler; - } - - //~--- methods ------------------------------------------------------------ - - /** - * Method description - * - */ - @Override - public void run() - { - try - { - User user = createUser(); - - modifyAndDeleteUser(user); - createUser(); - } - catch (Exception ex) - { - throw new RuntimeException(ex); - } - - finished = true; - } - - /** - * Method description - * - * - * @return - * - * @throws IOException - * @throws UserException - */ - private User createUser() throws UserException, IOException - { - String id = UUID.randomUUID().toString(); - User user = new User(id, id.concat(" displayName"), - id.concat("@mail.com")); - - userHandler.create(user); - - return user; - } - - /** - * Method description - * - * - * @param user - * - * @throws IOException - * @throws UserException - */ - private void modifyAndDeleteUser(User user) - throws UserException, IOException - { - String name = user.getName(); - String nd = name.concat(" new displayname"); - - user.setDisplayName(nd); - userHandler.modify(user); - - User otherUser = userHandler.get(name); - - assertNotNull(otherUser); - assertEquals(nd, otherUser.getDisplayName()); - userHandler.delete(user); - otherUser = userHandler.get(name); - assertNull(otherUser); - } - - //~--- fields ------------------------------------------------------------- - - /** Field description */ - private boolean finished = false; - - /** Field description */ - private UserHandler userHandler; - } - - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - private UserHandler handler; - - /** Field description */ - private File tempDirectory; -} diff --git a/scm-web-api/src/main/java/sonia/scm/web/filter/PermissionFilter.java b/scm-web-api/src/main/java/sonia/scm/web/filter/PermissionFilter.java index 44fb94e2db..aa39a88ad9 100644 --- a/scm-web-api/src/main/java/sonia/scm/web/filter/PermissionFilter.java +++ b/scm-web-api/src/main/java/sonia/scm/web/filter/PermissionFilter.java @@ -50,9 +50,6 @@ import sonia.scm.web.security.SecurityContext; import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -69,10 +66,6 @@ public abstract class PermissionFilter extends HttpFilter private static final Logger logger = LoggerFactory.getLogger(PermissionFilter.class); - /** Field description */ - public static final Pattern PATTERN_REPOSITORYNAME = - Pattern.compile("/[^/]+/([^/]+)(?:/.*)?"); - //~--- constructors --------------------------------------------------------- /** @@ -88,6 +81,16 @@ public abstract class PermissionFilter extends HttpFilter //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param request + * + * @return + */ + protected abstract Repository getRepository(HttpServletRequest request); + /** * Method description * @@ -170,50 +173,6 @@ public abstract class PermissionFilter extends HttpFilter } } - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param request - * - * @return - */ - protected Repository getRepository(HttpServletRequest request) - { - Repository repository = null; - String uri = request.getRequestURI(); - - uri = uri.substring(request.getContextPath().length()); - - Matcher m = PATTERN_REPOSITORYNAME.matcher(uri); - - if (m.matches()) - { - String repositoryname = m.group(1); - - repository = getRepository(repositoryname); - } - - return repository; - } - - /** - * Method description - * - * - * @param name - * - * @return - */ - protected Repository getRepository(String name) - { - return null; - } - - ; - //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/scm-web-api/src/main/java/sonia/scm/web/filter/RegexPermissionFilter.java b/scm-web-api/src/main/java/sonia/scm/web/filter/RegexPermissionFilter.java new file mode 100644 index 0000000000..06fba0089e --- /dev/null +++ b/scm-web-api/src/main/java/sonia/scm/web/filter/RegexPermissionFilter.java @@ -0,0 +1,134 @@ +/** + * 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.web.filter; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Provider; + +import sonia.scm.repository.Repository; +import sonia.scm.repository.RepositoryManager; +import sonia.scm.web.security.SecurityContext; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; + +/** + * + * @author Sebastian Sdorra + */ +public abstract class RegexPermissionFilter extends PermissionFilter +{ + + /** Field description */ + public static final Pattern PATTERN_REPOSITORYNAME = + Pattern.compile("/[^/]+/([^/]+)(?:/.*)?"); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param securityContextProvider + * @param repositoryManager + */ + public RegexPermissionFilter( + Provider securityContextProvider, + RepositoryManager repositoryManager) + { + super(securityContextProvider); + this.repositoryManager = repositoryManager; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + protected abstract String getType(); + + /** + * Method description + * + * + * @param request + * + * @return + */ + @Override + protected Repository getRepository(HttpServletRequest request) + { + Repository repository = null; + String uri = request.getRequestURI(); + + uri = uri.substring(request.getContextPath().length()); + + Matcher m = PATTERN_REPOSITORYNAME.matcher(uri); + + if (m.matches()) + { + String repositoryname = m.group(1); + + repository = getRepository(repositoryname); + } + + return repository; + } + + /** + * Method description + * + * + * @param name + * + * @return + */ + protected Repository getRepository(String name) + { + return repositoryManager.get(getType(), name); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private RepositoryManager repositoryManager; +} diff --git a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java index aa6f5532f1..dff09f8caf 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java @@ -43,12 +43,10 @@ import org.slf4j.LoggerFactory; import sonia.scm.api.rest.UriExtensionsConfig; import sonia.scm.cache.CacheManager; -import sonia.scm.cache.CacheRepositoryManagerDecorator; import sonia.scm.cache.EhCacheManager; import sonia.scm.config.ScmConfiguration; import sonia.scm.filter.SecurityFilter; import sonia.scm.plugin.ScriptResourceServlet; -import sonia.scm.repository.BasicRepositoryManager; import sonia.scm.repository.RepositoryHandler; import sonia.scm.repository.RepositoryManager; import sonia.scm.security.EncryptionHandler; @@ -84,6 +82,7 @@ import java.util.Map; import java.util.Set; import javax.xml.bind.JAXB; +import sonia.scm.repository.xml.XmlRepositoryManager; /** * @@ -168,7 +167,7 @@ public class ScmServletModule extends ServletModule bind(CacheManager.class).to(EhCacheManager.class); //bind(RepositoryManager.class).annotatedWith(Undecorated.class).to( // BasicRepositoryManager.class); - bind(RepositoryManager.class).to(BasicRepositoryManager.class); + bind(RepositoryManager.class).to(XmlRepositoryManager.class); bind(UserManager.class).to(BasicUserManager.class); bind(ScmWebPluginContext.class).toInstance(webPluginContext); diff --git a/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java b/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java index 8fef7b6b1d..a5f9f2a74f 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/CacheRepositoryManagerDecorator.java @@ -228,6 +228,21 @@ public class CacheRepositoryManagerDecorator return repository; } + /** + * TODO cache result + * + * + * @param type + * @param name + * + * @return + */ + @Override + public Repository get(String type, String name) + { + return orginal.get(type, name); + } + /** * Method description *