Commits

Sebastian Sdorra committed 79d35dc

use checkedprovider for repository injection

  • Participants
  • Parent commits ee95970
  • Branches issue-77

Comments (0)

Files changed (13)

File scm-core/pom.xml

   <name>scm-core</name>
 
   <dependencies>
+    
+    <!-- servlet api -->
 
     <dependency>
       <groupId>javax.servlet</groupId>
       <version>${servlet.version}</version>
       <scope>provided</scope>
     </dependency>
+    
+    <!-- logging -->
 
     <dependency>
       <artifactId>slf4j-api</artifactId>
       <groupId>org.slf4j</groupId>
       <version>${slf4j.version}</version>
     </dependency>
+    
+    <!-- injection -->
 
     <dependency>
       <groupId>com.google.inject</groupId>
       <artifactId>guice-servlet</artifactId>
       <version>${guice.version}</version>
     </dependency>
+    
+    <dependency>
+      <groupId>com.google.inject.extensions</groupId>
+      <artifactId>guice-throwingproviders</artifactId>
+      <version>${guice.version}</version>
+    </dependency>
+    
+    <!-- rest api -->
 
     <dependency>
       <groupId>com.sun.jersey</groupId>

File scm-core/src/main/java/sonia/scm/repository/RepositoryProvider.java

+/**
+ * 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;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import com.google.inject.throwingproviders.CheckedProvider;
+
+import sonia.scm.security.ScmSecurityException;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ * @since 1.10
+ */
+public interface RepositoryProvider extends CheckedProvider<Repository>
+{
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   *
+   * @throws ScmSecurityException
+   */
+  @Override
+  public Repository get() throws ScmSecurityException;
+}

File scm-core/src/main/java/sonia/scm/web/filter/ProviderPermissionFilter.java

 import com.google.inject.Provider;
 
 import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.web.security.WebSecurityContext;
 
 //~--- JDK imports ------------------------------------------------------------
    */
   public ProviderPermissionFilter(
           Provider<WebSecurityContext> securityContextProvider,
-          Provider<Repository> repositoryProvider)
+          RepositoryProvider repositoryProvider)
   {
     super(securityContextProvider);
     this.repositoryProvider = repositoryProvider;
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
-  private Provider<Repository> repositoryProvider;
+  private RepositoryProvider repositoryProvider;
 }

File scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java

 import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
-import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.web.filter.ProviderPermissionFilter;
 import sonia.scm.web.security.WebSecurityContext;
 
   @Inject
   public GitPermissionFilter(
           Provider<WebSecurityContext> securityContextProvider,
-          Provider<Repository> repositoryProvider)
+          RepositoryProvider repositoryProvider)
   {
     super(securityContextProvider, repositoryProvider);
   }

File scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java

 //~--- non-JDK imports --------------------------------------------------------
 
 import com.google.inject.Inject;
-import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
 import sonia.scm.config.ScmConfiguration;
 import sonia.scm.repository.HgHookManager;
 import sonia.scm.repository.HgRepositoryHandler;
 import sonia.scm.repository.Repository;
-import sonia.scm.repository.RepositoryManager;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.util.AssertUtil;
 import sonia.scm.web.cgi.CGIExecutor;
 import sonia.scm.web.cgi.CGIExecutorFactory;
   @Inject
   public HgCGIServlet(CGIExecutorFactory cgiExecutorFactory,
                       ScmConfiguration configuration,
-                      Provider<Repository> repositoryProvider,
+                      RepositoryProvider repositoryProvider,
                       HgRepositoryHandler handler, HgHookManager hookManager)
   {
     this.cgiExecutorFactory = cgiExecutorFactory;
   private HgHookManager hookManager;
 
   /** Field description */
-  private Provider<Repository> repositoryProvider;
+  private RepositoryProvider repositoryProvider;
 }

File scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgPermissionFilter.java

 import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
-import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.web.filter.ProviderPermissionFilter;
 import sonia.scm.web.security.WebSecurityContext;
 
   @Inject
   public HgPermissionFilter(
           Provider<WebSecurityContext> securityContextProvider,
-          Provider<Repository> repositoryProvider)
+          RepositoryProvider repositoryProvider)
   {
     super(securityContextProvider, repositoryProvider);
   }

File scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnDAVConfig.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
-import com.google.inject.Provider;
-
 import org.tmatesoft.svn.core.internal.server.dav.DAVConfig;
 import org.tmatesoft.svn.core.internal.server.dav.SVNPathBasedAccess;
 
 import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.repository.SvnRepositoryHandler;
 
 //~--- JDK imports ------------------------------------------------------------
    * @param repositoryProvider
    */
   public SvnDAVConfig(DAVConfig davConfig, SvnRepositoryHandler handler,
-                      Provider<Repository> repositoryProvider)
+                      RepositoryProvider repositoryProvider)
   {
     this.davConfig = davConfig;
     this.handler = handler;
   private SvnRepositoryHandler handler;
 
   /** Field description */
-  private Provider<Repository> repositoryProvider;
+  private RepositoryProvider repositoryProvider;
 }

File scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnDAVServlet.java

 //~--- non-JDK imports --------------------------------------------------------
 
 import com.google.inject.Inject;
-import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
 import org.tmatesoft.svn.core.internal.server.dav.DAVConfig;
 import org.tmatesoft.svn.core.internal.server.dav.DAVServlet;
 
 import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.repository.SvnRepositoryHandler;
 import sonia.scm.util.AssertUtil;
 import sonia.scm.util.HttpUtil;
-import sonia.scm.util.IOUtil;
 
 //~--- JDK imports ------------------------------------------------------------
 
    */
   @Inject
   public SvnDAVServlet(SvnRepositoryHandler handler,
-                       Provider<Repository> repositoryProvider)
+                       RepositoryProvider repositoryProvider)
   {
     this.handler = handler;
     this.repositoryProvider = repositoryProvider;
      * @param repositoryProvider
      */
     public SvnHttpServletRequestWrapper(HttpServletRequest request,
-            Provider<Repository> repositoryProvider)
+            RepositoryProvider repositoryProvider)
     {
       super(request);
       this.repositoryProvider = repositoryProvider;
     //~--- fields -------------------------------------------------------------
 
     /** Field description */
-    private Provider<Repository> repositoryProvider;
+    private RepositoryProvider repositoryProvider;
   }
 
 
   private SvnRepositoryHandler handler;
 
   /** Field description */
-  private Provider<Repository> repositoryProvider;
+  private RepositoryProvider repositoryProvider;
 }

File scm-plugins/scm-svn-plugin/src/main/java/sonia/scm/web/SvnPermissionFilter.java

 import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
-import sonia.scm.repository.Repository;
+import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.web.filter.ProviderPermissionFilter;
 import sonia.scm.web.security.WebSecurityContext;
 
   @Inject
   public SvnPermissionFilter(
           Provider<WebSecurityContext> securityContextProvider,
-          Provider<Repository> repository)
+          RepositoryProvider repository)
   {
     super(securityContextProvider, repository);
   }

File scm-webapp/src/main/java/sonia/scm/ScmServletModule.java

 //~--- non-JDK imports --------------------------------------------------------
 
 import com.google.inject.name.Names;
+import com.google.inject.servlet.RequestScoped;
 import com.google.inject.servlet.ServletModule;
+import com.google.inject.throwingproviders.ThrowingProviderBinder;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import sonia.scm.plugin.PluginManager;
 import sonia.scm.plugin.ScriptResourceServlet;
 import sonia.scm.repository.ChangesetViewerUtil;
+import sonia.scm.repository.DefaultRepositoryProvider;
 import sonia.scm.repository.Repository;
 import sonia.scm.repository.RepositoryBrowserUtil;
 import sonia.scm.repository.RepositoryManager;
   @Override
   protected void configureServlets()
   {
+    install(ThrowingProviderBinder.forModule(this));
+
     SCMContextProvider context = SCMContext.getContext();
 
     bind(SCMContextProvider.class).toInstance(context);
     ScmConfiguration config = getScmConfiguration(context);
     CipherUtil cu = CipherUtil.getInstance();
 
-    bind(Repository.class).toProvider(RepositoryProvider.class);
+    // bind repository provider
+    ThrowingProviderBinder.create(binder()).bind(
+        RepositoryProvider.class, Repository.class).to(
+        DefaultRepositoryProvider.class).in(RequestScoped.class);
+
+    // bind core
     bind(StoreFactory.class).to(JAXBStoreFactory.class);
     bind(ScmConfiguration.class).toInstance(config);
     bind(PluginLoader.class).toInstance(pluginLoader);

File scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryProvider.java

+/**
+ * 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;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.servlet.RequestScoped;
+
+import sonia.scm.security.ScmSecurityException;
+
+//~--- JDK imports ------------------------------------------------------------
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+@RequestScoped
+public class DefaultRepositoryProvider implements RepositoryProvider
+{
+
+  /** Field description */
+  public static final String ATTRIBUTE_NAME = "scm.request.repository";
+
+  //~--- constructors ---------------------------------------------------------
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param requestProvider
+   * @param manager
+   */
+  @Inject
+  public DefaultRepositoryProvider(
+          Provider<HttpServletRequest> requestProvider,
+          RepositoryManager manager)
+  {
+    this.requestProvider = requestProvider;
+    this.manager = manager;
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   *
+   * @throws ScmSecurityException
+   */
+  @Override
+  public Repository get() throws ScmSecurityException
+  {
+    Repository repository = null;
+    HttpServletRequest request = requestProvider.get();
+
+    if (request != null)
+    {
+      repository = (Repository) request.getAttribute(ATTRIBUTE_NAME);
+
+      if (repository == null)
+      {
+        repository = manager.getFromRequest(request);
+
+        if (repository != null)
+        {
+          request.setAttribute(ATTRIBUTE_NAME, repository);
+        }
+      }
+    }
+
+    return repository;
+  }
+
+  //~--- fields ---------------------------------------------------------------
+
+  /** Field description */
+  private RepositoryManager manager;
+
+  /** Field description */
+  private Provider<HttpServletRequest> requestProvider;
+}

File scm-webapp/src/main/java/sonia/scm/repository/RepositoryProvider.java

-/**
- * 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;
-
-//~--- non-JDK imports --------------------------------------------------------
-
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.servlet.RequestScoped;
-
-//~--- JDK imports ------------------------------------------------------------
-
-import javax.servlet.http.HttpServletRequest;
-
-/**
- *
- * @author Sebastian Sdorra
- */
-@RequestScoped
-public class RepositoryProvider implements Provider<Repository>
-{
-
-  /** Field description */
-  public static final String ATTRIBUTE_NAME = "scm.request.repository";
-
-  //~--- constructors ---------------------------------------------------------
-
-  /**
-   * Constructs ...
-   *
-   *
-   * @param requestProvider
-   * @param manager
-   */
-  @Inject
-  public RepositoryProvider(Provider<HttpServletRequest> requestProvider,
-                            RepositoryManager manager)
-  {
-    this.requestProvider = requestProvider;
-    this.manager = manager;
-  }
-
-  //~--- get methods ----------------------------------------------------------
-
-  /**
-   * Method description
-   *
-   *
-   * @return
-   */
-  @Override
-  public Repository get()
-  {
-    Repository repository = null;
-    HttpServletRequest request = requestProvider.get();
-
-    if (request != null)
-    {
-      repository = (Repository) request.getAttribute(ATTRIBUTE_NAME);
-
-      if (repository == null)
-      {
-        repository = manager.getFromRequest(request);
-
-        if (repository != null)
-        {
-          request.setAttribute(ATTRIBUTE_NAME, repository);
-        }
-      }
-    }
-
-    return repository;
-  }
-
-  //~--- fields ---------------------------------------------------------------
-
-  /** Field description */
-  private RepositoryManager manager;
-
-  /** Field description */
-  private Provider<HttpServletRequest> requestProvider;
-}

File scm-webapp/src/main/java/sonia/scm/repository/xml/XmlRepositoryManager.java

       {
         if (type.equals(r.getType()) && isNameMatching(r, uri))
         {
-          if (isReader(r))
-          {
-            repository = r.clone();
-          }
+          assertIsReader(r);
+          repository = r.clone();
 
           break;
         }