From: James Moger Date: Tue, 19 Nov 2013 03:31:37 +0000 (-0500) Subject: Use Dagger to inject managers into all filters and servlets X-Git-Tag: v1.4.0~185 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cacf8bff097fbb66a7be1bfe267b5da2605145f8;p=gitblit.git Use Dagger to inject managers into all filters and servlets Change-Id: I9bb2cc0cbfac9841b13bed15a474fefb24355cd4 --- diff --git a/.classpath b/.classpath index d1b04dad..9d6a6d26 100644 --- a/.classpath +++ b/.classpath @@ -1,78 +1,87 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index f77b6ff2..078de7e0 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ /build-next.xml /*.ps1 /*.sh +/*.factorypath +/src/main/dagger diff --git a/build.moxie b/build.moxie index 7816c927..acc1afb1 100644 --- a/build.moxie +++ b/build.moxie @@ -57,6 +57,7 @@ mainclass: com.gitblit.GitBlitServer # a scope to each directory. sourceDirectories: - compile 'src/main/java' +- compile 'src/main/dagger' apt - test 'src/test/java' # Moxie supports one site-scoped directory for mx:doc - site 'src/site' @@ -120,6 +121,9 @@ properties: { # dependencies: +# Dagger dependency injection library (annotation processor) +- compile 'com.squareup.dagger:dagger:1.1.0' :war apt +- compile 'com.squareup.dagger:dagger-compiler:1.1.0' :war optional apt # Standard dependencies - compile 'com.beust:jcommander:1.17' :fedclient :authority - compile 'log4j:log4j:1.2.17' :war :fedclient :authority diff --git a/build.xml b/build.xml index 860eca90..ab6699bc 100644 --- a/build.xml +++ b/build.xml @@ -247,34 +247,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -368,34 +341,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/gitblit.iml b/gitblit.iml index 67638df6..74b1e85a 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -6,10 +6,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/.gitignore b/src/main/.gitignore new file mode 100644 index 00000000..01c48ab2 --- /dev/null +++ b/src/main/.gitignore @@ -0,0 +1 @@ +/dagger diff --git a/src/main/java/com/gitblit/AccessRestrictionFilter.java b/src/main/java/com/gitblit/AccessRestrictionFilter.java index ac97ba2e..5f0baed2 100644 --- a/src/main/java/com/gitblit/AccessRestrictionFilter.java +++ b/src/main/java/com/gitblit/AccessRestrictionFilter.java @@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; @@ -47,6 +48,19 @@ import com.gitblit.utils.StringUtils; */ public abstract class AccessRestrictionFilter extends AuthenticationFilter { + protected final IRuntimeManager runtimeManager; + + protected final IRepositoryManager repositoryManager; + + protected AccessRestrictionFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + super(sessionManager); + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; + } + /** * Extract the repository name from the url. * @@ -128,9 +142,6 @@ public abstract class AccessRestrictionFilter extends AuthenticationFilter { String fullUrl = getFullUrl(httpRequest); String repository = extractRepositoryName(fullUrl); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - if (repositoryManager.isCollectingGarbage(repository)) { logger.info(MessageFormat.format("ARF: Rejecting request for {0}, busy collecting garbage!", repository)); httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN); diff --git a/src/main/java/com/gitblit/AuthenticationFilter.java b/src/main/java/com/gitblit/AuthenticationFilter.java index 9aeb89f7..96d880f4 100644 --- a/src/main/java/com/gitblit/AuthenticationFilter.java +++ b/src/main/java/com/gitblit/AuthenticationFilter.java @@ -57,6 +57,12 @@ public abstract class AuthenticationFilter implements Filter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); + protected final ISessionManager sessionManager; + + protected AuthenticationFilter(ISessionManager sessionManager) { + this.sessionManager = sessionManager; + } + /** * doFilter does the actual work of preprocessing the request to ensure that * the user may proceed. @@ -101,7 +107,6 @@ public abstract class AuthenticationFilter implements Filter { * @return user */ protected UserModel getUser(HttpServletRequest httpRequest) { - ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class); UserModel user = sessionManager.authenticate(httpRequest, requiresClientCertificate()); return user; } diff --git a/src/main/java/com/gitblit/BranchGraphServlet.java b/src/main/java/com/gitblit/BranchGraphServlet.java index 986560c6..58a57781 100644 --- a/src/main/java/com/gitblit/BranchGraphServlet.java +++ b/src/main/java/com/gitblit/BranchGraphServlet.java @@ -36,6 +36,8 @@ import java.util.Set; import java.util.TreeSet; import javax.imageio.ImageIO; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -61,6 +63,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class BranchGraphServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -74,8 +77,18 @@ public class BranchGraphServlet extends HttpServlet { private final Stroke[] strokeCache; - public BranchGraphServlet() { + private final IStoredSettings settings; + + private final IRepositoryManager repositoryManager; + + @Inject + public BranchGraphServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + super(); + this.settings = runtimeManager.getSettings(); + this.repositoryManager = repositoryManager; strokeCache = new Stroke[4]; for (int i = 1; i < strokeCache.length; i++) @@ -104,7 +117,6 @@ public class BranchGraphServlet extends HttpServlet { protected long getLastModified(HttpServletRequest req) { String repository = req.getParameter("r"); String objectId = req.getParameter("h"); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); Repository r = null; try { r = repositoryManager.getRepository(repository); @@ -131,9 +143,6 @@ public class BranchGraphServlet extends HttpServlet { String objectId = request.getParameter("h"); String length = request.getParameter("l"); - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - r = repositoryManager.getRepository(repository); rw = new PlotWalk(r); diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java new file mode 100644 index 00000000..7ee8ecd6 --- /dev/null +++ b/src/main/java/com/gitblit/DaggerModule.java @@ -0,0 +1,136 @@ +/* + * Copyright 2013 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit; + +import javax.inject.Singleton; + +import org.apache.wicket.protocol.http.WebApplication; + +import com.gitblit.git.GitServlet; +import com.gitblit.manager.IFederationManager; +import com.gitblit.manager.IGitblitManager; +import com.gitblit.manager.INotificationManager; +import com.gitblit.manager.IProjectManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; +import com.gitblit.manager.IUserManager; +import com.gitblit.wicket.GitBlitWebApp; +import com.gitblit.wicket.GitblitWicketFilter; + +import dagger.Module; +import dagger.Provides; + +/** + * DaggerModule references all injectable objects. + * + * @author James Moger + * + */ +@Module( + injects = { + // core managers + IRuntimeManager.class, + INotificationManager.class, + IUserManager.class, + ISessionManager.class, + IRepositoryManager.class, + IProjectManager.class, + IGitblitManager.class, + IFederationManager.class, + + // filters & servlets + GitServlet.class, + GitFilter.class, + PagesServlet.class, + PagesFilter.class, + RpcServlet.class, + RpcFilter.class, + DownloadZipServlet.class, + DownloadZipFilter.class, + SyndicationServlet.class, + SyndicationFilter.class, + FederationServlet.class, + SparkleShareInviteServlet.class, + BranchGraphServlet.class, + RobotsTxtServlet.class, + LogoServlet.class, + EnforceAuthenticationFilter.class, + GitblitWicketFilter.class + } +) +public class DaggerModule { + + final GitBlit gitblit; + + // HACK but necessary for now + public DaggerModule(GitBlit gitblit) { + this.gitblit = gitblit; + } + + @Provides @Singleton IRuntimeManager provideRuntimeManager() { + return gitblit; + } + + @Provides @Singleton INotificationManager provideNotificationManager() { + return gitblit; + } + + @Provides @Singleton IUserManager provideUserManager() { + return gitblit; + } + + @Provides @Singleton ISessionManager provideSessionManager() { + return gitblit; + } + + @Provides @Singleton IRepositoryManager provideRepositoryManager() { + return gitblit; + } + + @Provides @Singleton IProjectManager provideProjectManager() { + return gitblit; + } + + @Provides @Singleton IGitblitManager provideGitblitManager() { + return gitblit; + } + + @Provides @Singleton IFederationManager provideFederationManager() { + return gitblit; + } + + @Provides @Singleton WebApplication provideWebApplication( + IRuntimeManager runtimeManager, + INotificationManager notificationManager, + IUserManager userManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager, + IGitblitManager gitblitManager, + IFederationManager federationManager) { + + return new GitBlitWebApp( + runtimeManager, + notificationManager, + userManager, + sessionManager, + repositoryManager, + projectManager, + gitblitManager, + federationManager); + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/DownloadZipFilter.java b/src/main/java/com/gitblit/DownloadZipFilter.java index e52d9d79..914d89e5 100644 --- a/src/main/java/com/gitblit/DownloadZipFilter.java +++ b/src/main/java/com/gitblit/DownloadZipFilter.java @@ -15,7 +15,13 @@ */ package com.gitblit; +import javax.inject.Inject; +import javax.inject.Singleton; + import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -27,9 +33,16 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class DownloadZipFilter extends AccessRestrictionFilter { - public DownloadZipFilter() { + @Inject + public DownloadZipFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, sessionManager, repositoryManager); } /** diff --git a/src/main/java/com/gitblit/DownloadZipServlet.java b/src/main/java/com/gitblit/DownloadZipServlet.java index 82846dac..d629dcfc 100644 --- a/src/main/java/com/gitblit/DownloadZipServlet.java +++ b/src/main/java/com/gitblit/DownloadZipServlet.java @@ -20,6 +20,8 @@ import java.text.MessageFormat; import java.text.ParseException; import java.util.Date; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletResponse; @@ -43,12 +45,17 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class DownloadZipServlet extends HttpServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(DownloadZipServlet.class); + private final IStoredSettings settings; + + private final IRepositoryManager repositoryManager; + public static enum Format { zip(".zip"), tar(".tar"), gz(".tar.gz"), xz(".tar.xz"), bzip2(".tar.bzip2"); @@ -68,8 +75,14 @@ public class DownloadZipServlet extends HttpServlet { } } - public DownloadZipServlet() { + @Inject + public DownloadZipServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + super(); + this.settings = runtimeManager.getSettings(); + this.repositoryManager = repositoryManager; } /** @@ -103,7 +116,6 @@ public class DownloadZipServlet extends HttpServlet { private void processRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); if (!settings.getBoolean(Keys.web.allowZipDownloads, true)) { logger.warn("Zip downloads are disabled"); response.sendError(HttpServletResponse.SC_FORBIDDEN); @@ -133,7 +145,6 @@ public class DownloadZipServlet extends HttpServlet { name += "-" + objectId; } - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); Repository r = repositoryManager.getRepository(repository); if (r == null) { if (repositoryManager.isCollectingGarbage(repository)) { diff --git a/src/main/java/com/gitblit/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/EnforceAuthenticationFilter.java index 12b07595..48fc0057 100644 --- a/src/main/java/com/gitblit/EnforceAuthenticationFilter.java +++ b/src/main/java/com/gitblit/EnforceAuthenticationFilter.java @@ -18,6 +18,8 @@ import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -42,11 +44,23 @@ import com.gitblit.models.UserModel; * @author Laurens Vrijnsen * */ +@Singleton public class EnforceAuthenticationFilter implements Filter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); - public EnforceAuthenticationFilter() { + private final IStoredSettings settings; + + private final ISessionManager sessionManager; + + @Inject + public EnforceAuthenticationFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager) { + + super(); + this.settings = runtimeManager.getSettings(); + this.sessionManager = sessionManager; } /* @@ -56,7 +70,6 @@ public class EnforceAuthenticationFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } - /* * This does the actual filtering: is the user authenticated? If not, enforce HTTP authentication (401) * @@ -65,8 +78,6 @@ public class EnforceAuthenticationFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class); Boolean mustForceAuth = settings.getBoolean(Keys.web.authenticateViewPages, false) && settings.getBoolean(Keys.web.enforceHttpBasicAuthentication, false); diff --git a/src/main/java/com/gitblit/FederationServlet.java b/src/main/java/com/gitblit/FederationServlet.java index 0d83b4f4..31e3c0e1 100644 --- a/src/main/java/com/gitblit/FederationServlet.java +++ b/src/main/java/com/gitblit/FederationServlet.java @@ -25,6 +25,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.FederationRequest; @@ -48,12 +50,31 @@ import com.gitblit.utils.TimeUtils; * @author James Moger * */ +@Singleton public class FederationServlet extends JsonServlet { private static final long serialVersionUID = 1L; - public FederationServlet() { + private final IStoredSettings settings; + + private final IUserManager userManager; + + private final IRepositoryManager repositoryManager; + + private final IFederationManager federationManager; + + @Inject + public FederationServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager) { + super(); + this.settings = runtimeManager.getSettings(); + this.userManager = userManager; + this.repositoryManager = repositoryManager; + this.federationManager = federationManager; } /** @@ -70,11 +91,6 @@ public class FederationServlet extends JsonServlet { javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IUserManager userManager = GitBlit.getManager(IUserManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - IFederationManager federationManager = GitBlit.getManager(IFederationManager.class); - FederationRequest reqType = FederationRequest.fromName(request.getParameter("req")); logger.info(MessageFormat.format("Federation {0} request from {1}", reqType, request.getRemoteAddr())); diff --git a/src/main/java/com/gitblit/GCExecutor.java b/src/main/java/com/gitblit/GCExecutor.java index 837741f2..3ab98956 100644 --- a/src/main/java/com/gitblit/GCExecutor.java +++ b/src/main/java/com/gitblit/GCExecutor.java @@ -54,14 +54,20 @@ public class GCExecutor implements Runnable { private final IStoredSettings settings; + private final IRepositoryManager repositoryManager; + private AtomicBoolean running = new AtomicBoolean(false); private AtomicBoolean forceClose = new AtomicBoolean(false); private final Map gcCache = new ConcurrentHashMap(); - public GCExecutor(IStoredSettings settings) { + public GCExecutor( + IStoredSettings settings, + IRepositoryManager repositoryManager) { + this.settings = settings; + this.repositoryManager = repositoryManager; } /** @@ -132,8 +138,6 @@ public class GCExecutor implements Runnable { running.set(true); Date now = new Date(); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - for (String repositoryName : repositoryManager.getRepositoryList()) { if (forceClose.get()) { break; diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 31dedf13..8be50fb6 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -65,7 +65,6 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; import javax.servlet.annotation.WebListener; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -95,6 +94,7 @@ import com.gitblit.Constants.FederationStrategy; import com.gitblit.Constants.FederationToken; import com.gitblit.Constants.PermissionType; import com.gitblit.Constants.RegistrantType; +import com.gitblit.dagger.DaggerContextListener; import com.gitblit.fanout.FanoutNioService; import com.gitblit.fanout.FanoutService; import com.gitblit.fanout.FanoutSocketService; @@ -164,7 +164,7 @@ import com.google.gson.reflect.TypeToken; * */ @WebListener -public class GitBlit extends InjectionContextListener +public class GitBlit extends DaggerContextListener implements IRuntimeManager, INotificationManager, IUserManager, @@ -3479,9 +3479,9 @@ public class GitBlit extends InjectionContextListener // prepare service executors mailExecutor = new MailExecutor(settings); - luceneExecutor = new LuceneExecutor(settings, repositoriesFolder); - gcExecutor = new GCExecutor(settings); - mirrorExecutor = new MirrorExecutor(settings); + luceneExecutor = new LuceneExecutor(settings, getManager(IRepositoryManager.class)); + gcExecutor = new GCExecutor(settings, getManager(IRepositoryManager.class)); + mirrorExecutor = new MirrorExecutor(settings, getManager(IRepositoryManager.class)); // initialize utilities String prefix = settings.getString(Keys.git.userRepositoryPrefix, "~"); @@ -3641,7 +3641,7 @@ public class GitBlit extends InjectionContextListener String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); if (port > 0) { try { - gitDaemon = new GitDaemon(bindInterface, port, getRepositoriesFolder()); + gitDaemon = new GitDaemon(this, this, this, this); gitDaemon.start(); } catch (IOException e) { gitDaemon = null; @@ -3854,7 +3854,7 @@ public class GitBlit extends InjectionContextListener * shutting down or because the servlet container is re-deploying Gitblit. */ @Override - public void contextDestroyed(ServletContextEvent contextEvent) { + protected void destroyContext(ServletContext context) { logger.info("Gitblit context destroyed by servlet container."); scheduledExecutor.shutdownNow(); luceneExecutor.close(); @@ -4052,6 +4052,11 @@ public class GitBlit extends InjectionContextListener userService.logout(user); } + @Override + protected Object [] getModules() { + return new Object [] { new DaggerModule(this) }; + } + /** * Instantiate and inject all filters and servlets into the container using * the servlet 3 specification. diff --git a/src/main/java/com/gitblit/GitFilter.java b/src/main/java/com/gitblit/GitFilter.java index a06c7e52..ba8443d0 100644 --- a/src/main/java/com/gitblit/GitFilter.java +++ b/src/main/java/com/gitblit/GitFilter.java @@ -17,10 +17,14 @@ package com.gitblit; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; + import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.AuthorizationControl; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; @@ -33,6 +37,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class GitFilter extends AccessRestrictionFilter { protected static final String gitReceivePack = "/git-receive-pack"; @@ -42,7 +47,16 @@ public class GitFilter extends AccessRestrictionFilter { protected static final String[] suffixes = { gitReceivePack, gitUploadPack, "/info/refs", "/HEAD", "/objects" }; - public GitFilter() { + private final IStoredSettings settings; + + @Inject + public GitFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, sessionManager, repositoryManager); + this.settings = runtimeManager.getSettings(); } /** @@ -105,7 +119,6 @@ public class GitFilter extends AccessRestrictionFilter { */ @Override protected boolean isCreationAllowed() { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); return settings.getBoolean(Keys.git.allowCreateOnPush, true); } @@ -125,7 +138,6 @@ public class GitFilter extends AccessRestrictionFilter { @Override protected boolean requiresClientCertificate() { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); return settings.getBoolean(Keys.git.requiresClientCertificate, false); } @@ -159,7 +171,6 @@ public class GitFilter extends AccessRestrictionFilter { */ @Override protected boolean canAccess(RepositoryModel repository, UserModel user, String action) { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); if (!settings.getBoolean(Keys.git.enableGitServlet, true)) { // Git Servlet disabled return false; @@ -231,14 +242,12 @@ public class GitFilter extends AccessRestrictionFilter { model.accessRestriction = AccessRestrictionType.VIEW; } else { // common repository, user default server settings - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); model.authorizationControl = AuthorizationControl.fromName(settings.getString(Keys.git.defaultAuthorizationControl, "")); model.accessRestriction = AccessRestrictionType.fromName(settings.getString(Keys.git.defaultAccessRestriction, "PUSH")); } // create the repository try { - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); repositoryManager.updateRepositoryModel(model.name, model, true); logger.info(MessageFormat.format("{0} created {1} ON-PUSH", user.username, model.name)); return repositoryManager.getRepositoryModel(model.name); diff --git a/src/main/java/com/gitblit/LogoServlet.java b/src/main/java/com/gitblit/LogoServlet.java index 4222f8ff..17b05cfd 100644 --- a/src/main/java/com/gitblit/LogoServlet.java +++ b/src/main/java/com/gitblit/LogoServlet.java @@ -21,6 +21,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -35,19 +37,23 @@ import com.gitblit.manager.IRuntimeManager; * @author James Moger * */ +@Singleton public class LogoServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final long lastModified = System.currentTimeMillis(); - public LogoServlet() { + private final IRuntimeManager runtimeManager; + + @Inject + public LogoServlet(IRuntimeManager runtimeManager) { super(); + this.runtimeManager = runtimeManager; } @Override protected long getLastModified(HttpServletRequest req) { - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png"); if (file.exists()) { return Math.max(lastModified, file.lastModified()); @@ -62,7 +68,6 @@ public class LogoServlet extends HttpServlet { InputStream is = null; try { String contentType = null; - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File file = runtimeManager.getFileOrFolder(Keys.web.headerLogo, "${baseFolder}/logo.png"); if (file.exists()) { // custom logo diff --git a/src/main/java/com/gitblit/LuceneExecutor.java b/src/main/java/com/gitblit/LuceneExecutor.java index 19395b3b..b7b71c5e 100644 --- a/src/main/java/com/gitblit/LuceneExecutor.java +++ b/src/main/java/com/gitblit/LuceneExecutor.java @@ -128,6 +128,7 @@ public class LuceneExecutor implements Runnable { private final Logger logger = LoggerFactory.getLogger(LuceneExecutor.class); private final IStoredSettings storedSettings; + private final IRepositoryManager repositoryManager; private final File repositoriesFolder; private final Map searchers = new ConcurrentHashMap(); @@ -136,9 +137,13 @@ public class LuceneExecutor implements Runnable { private final String luceneIgnoreExtensions = "7z arc arj bin bmp dll doc docx exe gif gz jar jpg lib lzh odg odf odt pdf ppt png so swf xcf xls xlsx zip"; private Set excludedExtensions; - public LuceneExecutor(IStoredSettings settings, File repositoriesFolder) { + public LuceneExecutor( + IStoredSettings settings, + IRepositoryManager repositoryManager) { + this.storedSettings = settings; - this.repositoriesFolder = repositoriesFolder; + this.repositoryManager = repositoryManager; + this.repositoriesFolder = repositoryManager.getRepositoriesFolder(); String exts = luceneIgnoreExtensions; if (settings != null) { exts = settings.getString(Keys.web.luceneIgnoreExtensions, exts); @@ -161,7 +166,6 @@ public class LuceneExecutor implements Runnable { String exts = storedSettings.getString(Keys.web.luceneIgnoreExtensions, luceneIgnoreExtensions); excludedExtensions = new TreeSet(StringUtils.getStringsFromValue(exts)); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); if (repositoryManager.isCollectingGarbage()) { // busy collecting garbage, try again later return; diff --git a/src/main/java/com/gitblit/MirrorExecutor.java b/src/main/java/com/gitblit/MirrorExecutor.java index a96c955f..6c951b94 100644 --- a/src/main/java/com/gitblit/MirrorExecutor.java +++ b/src/main/java/com/gitblit/MirrorExecutor.java @@ -52,14 +52,20 @@ public class MirrorExecutor implements Runnable { private final IStoredSettings settings; + private final IRepositoryManager repositoryManager; + private AtomicBoolean running = new AtomicBoolean(false); private AtomicBoolean forceClose = new AtomicBoolean(false); private final UserModel gitblitUser; - public MirrorExecutor(IStoredSettings settings) { + public MirrorExecutor( + IStoredSettings settings, + IRepositoryManager repositoryManager) { + this.settings = settings; + this.repositoryManager = repositoryManager; this.gitblitUser = new UserModel("gitblit"); this.gitblitUser.displayName = "Gitblit"; } @@ -84,8 +90,6 @@ public class MirrorExecutor implements Runnable { running.set(true); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - for (String repositoryName : repositoryManager.getRepositoryList()) { if (forceClose.get()) { break; diff --git a/src/main/java/com/gitblit/PagesFilter.java b/src/main/java/com/gitblit/PagesFilter.java index 4c9568dc..a322af2f 100644 --- a/src/main/java/com/gitblit/PagesFilter.java +++ b/src/main/java/com/gitblit/PagesFilter.java @@ -15,10 +15,15 @@ */ package com.gitblit; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -29,9 +34,15 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class PagesFilter extends AccessRestrictionFilter { - public PagesFilter() { + @Inject + public PagesFilter(IRuntimeManager runtimeManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, sessionManager, repositoryManager); } /** @@ -53,7 +64,6 @@ public class PagesFilter extends AccessRestrictionFilter { } else { repository = url.substring(0, slash); } - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); r = repositoryManager.getRepository(repository, false); if (r == null) { // try again diff --git a/src/main/java/com/gitblit/PagesServlet.java b/src/main/java/com/gitblit/PagesServlet.java index 5569ab39..ba919e07 100644 --- a/src/main/java/com/gitblit/PagesServlet.java +++ b/src/main/java/com/gitblit/PagesServlet.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -53,14 +55,25 @@ import com.gitblit.wicket.MarkupProcessor.MarkupDocument; * @author James Moger * */ +@Singleton public class PagesServlet extends HttpServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(PagesServlet.class); - public PagesServlet() { + private final IStoredSettings settings; + + private final IRepositoryManager repositoryManager; + + @Inject + public PagesServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + super(); + this.settings = runtimeManager.getSettings(); + this.repositoryManager = repositoryManager; } /** @@ -101,9 +114,6 @@ public class PagesServlet extends HttpServlet { path = path.substring(1); } - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - // determine repository and resource from url String repository = ""; String resource = ""; diff --git a/src/main/java/com/gitblit/RobotsTxtServlet.java b/src/main/java/com/gitblit/RobotsTxtServlet.java index 6a7c02d3..c07aa1d0 100644 --- a/src/main/java/com/gitblit/RobotsTxtServlet.java +++ b/src/main/java/com/gitblit/RobotsTxtServlet.java @@ -18,6 +18,8 @@ package com.gitblit; import java.io.File; import java.io.IOException; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -32,12 +34,17 @@ import com.gitblit.utils.FileUtils; * @author James Moger * */ +@Singleton public class RobotsTxtServlet extends HttpServlet { private static final long serialVersionUID = 1L; - public RobotsTxtServlet() { + private final IRuntimeManager runtimeManager; + + @Inject + public RobotsTxtServlet(IRuntimeManager runtimeManager) { super(); + this.runtimeManager = runtimeManager; } @Override @@ -55,7 +62,6 @@ public class RobotsTxtServlet extends HttpServlet { protected void processRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File file = runtimeManager.getFileOrFolder(Keys.web.robots.txt, null); String content = ""; if (file.exists()) { diff --git a/src/main/java/com/gitblit/RpcFilter.java b/src/main/java/com/gitblit/RpcFilter.java index b1384ad3..c4b6451c 100644 --- a/src/main/java/com/gitblit/RpcFilter.java +++ b/src/main/java/com/gitblit/RpcFilter.java @@ -18,6 +18,8 @@ package com.gitblit; import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -27,6 +29,7 @@ import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.RpcRequest; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.UserModel; /** @@ -42,9 +45,21 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class RpcFilter extends AuthenticationFilter { - public RpcFilter() { + private final IStoredSettings settings; + + private final IRuntimeManager runtimeManager; + + @Inject + public RpcFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager) { + + super(sessionManager); + this.settings = runtimeManager.getSettings(); + this.runtimeManager = runtimeManager; } /** @@ -68,9 +83,6 @@ public class RpcFilter extends AuthenticationFilter { return; } - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); - IStoredSettings settings = runtimeManager.getSettings(); - boolean adminRequest = requestType.exceeds(RpcRequest.LIST_SETTINGS); // conditionally reject all rpc requests diff --git a/src/main/java/com/gitblit/RpcServlet.java b/src/main/java/com/gitblit/RpcServlet.java index a8fa6f83..a3629b9c 100644 --- a/src/main/java/com/gitblit/RpcServlet.java +++ b/src/main/java/com/gitblit/RpcServlet.java @@ -23,6 +23,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -53,14 +55,41 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class RpcServlet extends JsonServlet { private static final long serialVersionUID = 1L; public static final int PROTOCOL_VERSION = 6; - public RpcServlet() { + private final IStoredSettings settings; + + private final IRuntimeManager runtimeManager; + + private final IUserManager userManager; + + private final IRepositoryManager repositoryManager; + + private final IFederationManager federationManager; + + private final IGitblitManager gitblitManager; + + @Inject + public RpcServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager, + IGitblitManager gitblitManager) { + super(); + + this.settings = runtimeManager.getSettings(); + this.runtimeManager = runtimeManager; + this.userManager = userManager; + this.repositoryManager = repositoryManager; + this.federationManager = federationManager; + this.gitblitManager = gitblitManager; } /** @@ -79,13 +108,6 @@ public class RpcServlet extends JsonServlet { logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType, request.getRemoteAddr())); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); - IUserManager userManager = GitBlit.getManager(IUserManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - IGitblitManager gitblitManager = GitBlit.getManager(IGitblitManager.class); - IFederationManager federationManager = GitBlit.getManager(IFederationManager.class); - IStoredSettings settings = runtimeManager.getSettings(); - UserModel user = (UserModel) request.getUserPrincipal(); boolean allowManagement = user != null && user.canAdmin() diff --git a/src/main/java/com/gitblit/SparkleShareInviteServlet.java b/src/main/java/com/gitblit/SparkleShareInviteServlet.java index 9f6618b2..1cd997d3 100644 --- a/src/main/java/com/gitblit/SparkleShareInviteServlet.java +++ b/src/main/java/com/gitblit/SparkleShareInviteServlet.java @@ -18,6 +18,8 @@ package com.gitblit; import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -37,12 +39,31 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class SparkleShareInviteServlet extends HttpServlet { private static final long serialVersionUID = 1L; - public SparkleShareInviteServlet() { + private final IStoredSettings settings; + + private final IUserManager userManager; + + private final ISessionManager sessionManager; + + private final IRepositoryManager repositoryManager; + + @Inject + public SparkleShareInviteServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + super(); + this.settings = runtimeManager.getSettings(); + this.userManager = userManager; + this.sessionManager = sessionManager; + this.repositoryManager = repositoryManager; } @Override @@ -61,11 +82,6 @@ public class SparkleShareInviteServlet extends HttpServlet { javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IUserManager userManager = GitBlit.getManager(IUserManager.class); - ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - // extract repo name from request String repoUrl = request.getPathInfo().substring(1); diff --git a/src/main/java/com/gitblit/SyndicationFilter.java b/src/main/java/com/gitblit/SyndicationFilter.java index ad908fa8..10b88102 100644 --- a/src/main/java/com/gitblit/SyndicationFilter.java +++ b/src/main/java/com/gitblit/SyndicationFilter.java @@ -18,6 +18,8 @@ package com.gitblit; import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -29,6 +31,7 @@ import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -41,9 +44,24 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class SyndicationFilter extends AuthenticationFilter { - public SyndicationFilter() { + private final IRuntimeManager runtimeManager; + private final IRepositoryManager repositoryManager; + private final IProjectManager projectManager; + + @Inject + public SyndicationFilter( + IRuntimeManager runtimeManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + super(sessionManager); + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; } /** @@ -76,10 +94,6 @@ public class SyndicationFilter extends AuthenticationFilter { String fullUrl = getFullUrl(httpRequest); String name = extractRequestedName(fullUrl); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - IProjectManager projectManager = GitBlit.getManager(IProjectManager.class); - ProjectModel project = projectManager.getProjectModel(name); RepositoryModel model = null; diff --git a/src/main/java/com/gitblit/SyndicationServlet.java b/src/main/java/com/gitblit/SyndicationServlet.java index 11089ee1..397545fa 100644 --- a/src/main/java/com/gitblit/SyndicationServlet.java +++ b/src/main/java/com/gitblit/SyndicationServlet.java @@ -22,6 +22,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServlet; import org.eclipse.jgit.lib.ObjectId; @@ -53,13 +55,29 @@ import com.gitblit.utils.SyndicationUtils; * @author James Moger * */ +@Singleton public class SyndicationServlet extends HttpServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class); - public SyndicationServlet() { + private final IStoredSettings settings; + + private final IRepositoryManager repositoryManager; + + private final IProjectManager projectManager; + + @Inject + public SyndicationServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + super(); + this.settings = runtimeManager.getSettings(); + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; } /** @@ -133,10 +151,6 @@ public class SyndicationServlet extends HttpServlet { javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - IProjectManager projectManager = GitBlit.getManager(IProjectManager.class); - String servletUrl = request.getContextPath() + request.getServletPath(); String url = request.getRequestURI().substring(servletUrl.length()); if (url.charAt(0) == '/' && url.length() > 1) { diff --git a/src/main/java/com/gitblit/dagger/DaggerContextListener.java b/src/main/java/com/gitblit/dagger/DaggerContextListener.java new file mode 100644 index 00000000..3c268860 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerContextListener.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.dagger; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import com.gitblit.InjectionContextListener; + +import dagger.ObjectGraph; + +/** + * Dagger servlet context listener is a context listener that uses Dagger to + * instantiate and inject servlets, filters, and anything else you might want. + * + * @author James Moger + * + */ +public abstract class DaggerContextListener extends InjectionContextListener { + + protected static final String INJECTOR_NAME = ObjectGraph.class.getName(); + + protected abstract Object [] getModules(); + + protected abstract void destroyContext(ServletContext context); + + protected ObjectGraph getInjector(ServletContext context) { + Object o = context.getAttribute(INJECTOR_NAME); + if (o == null) { + logger.debug("instantiating Dagger modules"); + Object [] modules = getModules(); + logger.debug("getting Dagger injector"); + try { + o = ObjectGraph.create(modules); + logger.debug("setting Dagger injector into {} attribute", INJECTOR_NAME); + context.setAttribute(INJECTOR_NAME, o); + } catch (Throwable t) { + logger.error("an error occurred creating the Dagger injector", t); + } + } + return (ObjectGraph) o; + } + + /** + * Instantiates an object. + * + * @param clazz + * @return the object + */ + @Override + protected X instantiate(ServletContext context, Class clazz) { + try { + ObjectGraph injector = getInjector(context); + return injector.get(clazz); + } catch (Throwable t) { + logger.error(null, t); + } + return null; + } + + @Override + public final void contextDestroyed(ServletContextEvent contextEvent) { + ServletContext context = contextEvent.getServletContext(); + context.setAttribute(INJECTOR_NAME, null); + destroyContext(context); + } +} diff --git a/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java b/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java new file mode 100644 index 00000000..b778f1f6 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.dagger; + +import groovy.lang.Singleton; + +import javax.inject.Inject; +import javax.inject.Provider; + +import org.apache.wicket.protocol.http.IWebApplicationFactory; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.protocol.http.WicketFilter; + +/** + * + * A Wicket filter that supports Dagger injection. + * + * @author James Moger + * + */ +@Singleton +public class DaggerWicketFilter extends WicketFilter { + + @Inject + Provider webApplicationProvider; + + @Inject + public DaggerWicketFilter() { + super(); + } + + @Override + protected IWebApplicationFactory getApplicationFactory() { + return new IWebApplicationFactory() { + @Override + public WebApplication createApplication(WicketFilter filter) { + return webApplicationProvider.get(); + } + }; + } +} diff --git a/src/main/java/com/gitblit/git/GitDaemon.java b/src/main/java/com/gitblit/git/GitDaemon.java index bb578efc..40cbe704 100644 --- a/src/main/java/com/gitblit/git/GitDaemon.java +++ b/src/main/java/com/gitblit/git/GitDaemon.java @@ -43,7 +43,6 @@ */ package com.gitblit.git; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; @@ -68,6 +67,12 @@ import org.eclipse.jgit.transport.resolver.UploadPackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; +import com.gitblit.manager.IUserManager; import com.gitblit.utils.StringUtils; /** @@ -106,45 +111,28 @@ public class GitDaemon { private ReceivePackFactory receivePackFactory; - /** Configure a daemon to listen on any available network port. */ - public GitDaemon() { - this(null); - } + public GitDaemon( + IRuntimeManager runtimeManager, + IUserManager userManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { - /** - * Construct the Gitblit Git daemon. - * - * @param bindInterface - * the ip address of the interface to bind - * @param port - * the port to serve on - * @param folder - * the folder to serve from - */ - public GitDaemon(String bindInterface, int port, File folder) { - this(StringUtils.isEmpty(bindInterface) ? new InetSocketAddress(port) - : new InetSocketAddress(bindInterface, port)); + IStoredSettings settings = runtimeManager.getSettings(); + int port = settings.getInteger(Keys.git.daemonPort, 0); + String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); - // set the repository resolver and pack factories - repositoryResolver = new RepositoryResolver(folder); - } + if (StringUtils.isEmpty(bindInterface)) { + myAddress = new InetSocketAddress(port); + } else { + myAddress = new InetSocketAddress(bindInterface, port); + } - /** - * Configure a new daemon for the specified network address. - * - * @param addr - * address to listen for connections on. If null, any available - * port will be chosen on all network interfaces. - */ - public GitDaemon(final InetSocketAddress addr) { - myAddress = addr; - processors = new ThreadGroup("Git-Daemon"); + repositoryResolver = new RepositoryResolver(sessionManager, repositoryManager); + uploadPackFactory = new GitblitUploadPackFactory(sessionManager); + receivePackFactory = new GitblitReceivePackFactory(runtimeManager, userManager, repositoryManager); run = new AtomicBoolean(false); - repositoryResolver = null; - uploadPackFactory = new GitblitUploadPackFactory(); - receivePackFactory = new GitblitReceivePackFactory(); - + processors = new ThreadGroup("Git-Daemon"); services = new GitDaemonService[] { new GitDaemonService("upload-pack", "uploadpack") { { setEnabled(true); diff --git a/src/main/java/com/gitblit/git/GitServlet.java b/src/main/java/com/gitblit/git/GitServlet.java index 23ea3434..e4719d9b 100644 --- a/src/main/java/com/gitblit/git/GitServlet.java +++ b/src/main/java/com/gitblit/git/GitServlet.java @@ -15,12 +15,16 @@ */ package com.gitblit.git; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; -import com.gitblit.GitBlit; import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.ISessionManager; +import com.gitblit.manager.IUserManager; /** * The GitServlet provides http/https access to Git repositories. @@ -29,19 +33,37 @@ import com.gitblit.manager.IRepositoryManager; * @author James Moger * */ +@Singleton public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { private static final long serialVersionUID = 1L; - public GitServlet() { + private final IRuntimeManager runtimeManager; + + private final IUserManager userManager; + + private final ISessionManager sessionManager; + + private final IRepositoryManager repositoryManager; + + @Inject + public GitServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + super(); + this.runtimeManager = runtimeManager; + this.userManager = userManager; + this.sessionManager = sessionManager; + this.repositoryManager = repositoryManager; } @Override public void init(ServletConfig config) throws ServletException { - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - setRepositoryResolver(new RepositoryResolver(repositoryManager.getRepositoriesFolder())); - setUploadPackFactory(new GitblitUploadPackFactory()); - setReceivePackFactory(new GitblitReceivePackFactory()); + setRepositoryResolver(new RepositoryResolver(sessionManager, repositoryManager)); + setUploadPackFactory(new GitblitUploadPackFactory(sessionManager)); + setReceivePackFactory(new GitblitReceivePackFactory(runtimeManager, userManager, repositoryManager)); super.init(config); } } diff --git a/src/main/java/com/gitblit/git/GitblitReceivePack.java b/src/main/java/com/gitblit/git/GitblitReceivePack.java index 8da603ac..44e2bef5 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePack.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePack.java @@ -91,11 +91,20 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P protected GroovyScriptEngine gse; - public GitblitReceivePack(Repository db, RepositoryModel repository, UserModel user) { - super(db); + private final IStoredSettings settings; + + private final IRepositoryManager repositoryManager; - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); + public GitblitReceivePack( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager, + Repository db, + RepositoryModel repository, + UserModel user) { + super(db); + this.settings = runtimeManager.getSettings(); + this.repositoryManager = repositoryManager; this.repository = repository; this.user = user == null ? UserModel.ANONYMOUS : user; this.groovyDir = repositoryManager.getHooksFolder(); @@ -239,7 +248,6 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P } } - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); Set scripts = new LinkedHashSet(); scripts.addAll(repositoryManager.getPreReceiveScriptsInherited(repository)); if (!ArrayUtils.isEmpty(repository.preReceiveScripts)) { @@ -266,8 +274,6 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P return; } - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - // log ref changes for (ReceiveCommand cmd : commands) { @@ -336,8 +342,6 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e); } - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - // run Groovy hook scripts Set scripts = new LinkedHashSet(); scripts.addAll(repositoryManager.getPostReceiveScriptsInherited(repository)); diff --git a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java index a90c36b1..2cdb9856 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePackFactory.java @@ -26,7 +26,6 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.gitblit.GitBlit; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.manager.IRepositoryManager; @@ -48,14 +47,31 @@ public class GitblitReceivePackFactory implements ReceivePackFactory { protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class); + private final IStoredSettings settings; + + private final IRuntimeManager runtimeManager; + + private final IUserManager userManager; + + private final IRepositoryManager repositoryManager; + + public GitblitReceivePackFactory( + IRuntimeManager runtimeManager, + IUserManager userManager, + IRepositoryManager repositoryManager) { + + super(); + this.settings = runtimeManager.getSettings(); + this.runtimeManager = runtimeManager; + this.userManager = userManager; + this.repositoryManager = repositoryManager; + + } + @Override public ReceivePack create(X req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings(); - IUserManager userManager = GitBlit.getManager(IUserManager.class); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - UserModel user = UserModel.ANONYMOUS; String repositoryName = ""; String origin = ""; @@ -97,7 +113,7 @@ public class GitblitReceivePackFactory implements ReceivePackFactory { final RepositoryModel repository = repositoryManager.getRepositoryModel(repositoryName); - final GitblitReceivePack rp = new GitblitReceivePack(db, repository, user); + final GitblitReceivePack rp = new GitblitReceivePack(runtimeManager, repositoryManager, db, repository, user); rp.setGitblitUrl(gitblitUrl); rp.setRepositoryUrl(repositoryUrl); rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin)); diff --git a/src/main/java/com/gitblit/git/GitblitUploadPackFactory.java b/src/main/java/com/gitblit/git/GitblitUploadPackFactory.java index 180e8b55..2afdde1d 100644 --- a/src/main/java/com/gitblit/git/GitblitUploadPackFactory.java +++ b/src/main/java/com/gitblit/git/GitblitUploadPackFactory.java @@ -23,7 +23,6 @@ import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.UploadPackFactory; -import com.gitblit.GitBlit; import com.gitblit.manager.ISessionManager; import com.gitblit.models.UserModel; @@ -37,11 +36,16 @@ import com.gitblit.models.UserModel; */ public class GitblitUploadPackFactory implements UploadPackFactory { + private final ISessionManager sessionManager; + + public GitblitUploadPackFactory(ISessionManager sessionManager) { + this.sessionManager = sessionManager; + } + @Override public UploadPack create(X req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException { - ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class); UserModel user = UserModel.ANONYMOUS; int timeout = 0; diff --git a/src/main/java/com/gitblit/git/RepositoryResolver.java b/src/main/java/com/gitblit/git/RepositoryResolver.java index 5441596d..87795c07 100644 --- a/src/main/java/com/gitblit/git/RepositoryResolver.java +++ b/src/main/java/com/gitblit/git/RepositoryResolver.java @@ -15,7 +15,6 @@ */ package com.gitblit.git; -import java.io.File; import java.io.IOException; import java.text.MessageFormat; @@ -28,7 +27,6 @@ import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.gitblit.GitBlit; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.ISessionManager; import com.gitblit.models.RepositoryModel; @@ -44,8 +42,17 @@ public class RepositoryResolver extends FileResolver { private final Logger logger = LoggerFactory.getLogger(RepositoryResolver.class); - public RepositoryResolver(File repositoriesFolder) { - super(repositoriesFolder, true); + private final ISessionManager sessionManager; + + private final IRepositoryManager repositoryManager; + + public RepositoryResolver( + ISessionManager sessionManager, + IRepositoryManager repositoryManager) { + + super(repositoryManager.getRepositoriesFolder(), true); + this.sessionManager = sessionManager; + this.repositoryManager = repositoryManager; } /** @@ -76,8 +83,6 @@ public class RepositoryResolver extends FileResolver { */ @Override protected boolean isExportOk(X req, String repositoryName, Repository db) throws IOException { - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class); RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName); String scheme = null; diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index 46a3b067..ad13a042 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -28,7 +28,6 @@ import org.apache.wicket.Session; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.protocol.http.WebApplication; -import com.gitblit.GitBlit; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.manager.IFederationManager; @@ -82,14 +81,50 @@ public class GitBlitWebApp extends WebApplication { private final Map cacheablePages = new HashMap(); - private IStoredSettings settings; + private final IStoredSettings settings; + + private final IRuntimeManager runtimeManager; + + private final INotificationManager notificationManager; + + private final IUserManager userManager; + + private final ISessionManager sessionManager; + + private final IRepositoryManager repositoryManager; + + private final IProjectManager projectManager; + + private final IGitblitManager gitblitManager; + + private final IFederationManager federationManager; + + public GitBlitWebApp( + IRuntimeManager runtimeManager, + INotificationManager notificationManager, + IUserManager userManager, + ISessionManager sessionManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager, + IGitblitManager gitblitManager, + IFederationManager federationManager) { + + super(); + this.settings = runtimeManager.getSettings(); + this.runtimeManager = runtimeManager; + this.notificationManager = notificationManager; + this.userManager = userManager; + this.sessionManager = sessionManager; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; + this.gitblitManager = gitblitManager; + this.federationManager = federationManager; + } @Override public void init() { super.init(); - settings = runtime().getSettings(); - // Setup page authorization mechanism boolean useAuthentication = settings.getBoolean(Keys.web.authenticateViewPages, false) || settings.getBoolean(Keys.web.authenticateAdminPages, false); @@ -205,7 +240,7 @@ public class GitBlitWebApp extends WebApplication { * @return true if Gitblit is running in debug mode */ public boolean isDebugMode() { - return runtime().isDebugMode(); + return runtimeManager.isDebugMode(); } /* @@ -213,52 +248,52 @@ public class GitBlitWebApp extends WebApplication { * step towards modularization across multiple commits. */ public Date getBootDate() { - return runtime().getBootDate(); + return runtimeManager.getBootDate(); } public Date getLastActivityDate() { - return repositories().getLastActivityDate(); + return repositoryManager.getLastActivityDate(); } public IRuntimeManager runtime() { - return GitBlit.getManager(IRuntimeManager.class); + return runtimeManager; } public INotificationManager notifier() { - return GitBlit.getManager(INotificationManager.class); + return notificationManager; } public IUserManager users() { - return GitBlit.getManager(IUserManager.class); + return userManager; } public ISessionManager session() { - return GitBlit.getManager(ISessionManager.class); + return sessionManager; } public IRepositoryManager repositories() { - return GitBlit.getManager(IRepositoryManager.class); + return repositoryManager; } public IProjectManager projects() { - return GitBlit.getManager(IProjectManager.class); + return projectManager; } public IFederationManager federation() { - return GitBlit.getManager(IFederationManager.class); + return federationManager; } public IGitblitManager gitblit() { - return GitBlit.getManager(IGitblitManager.class); + return gitblitManager; } public TimeZone getTimezone() { - return runtime().getTimezone(); + return runtimeManager.getTimezone(); } @Override public final String getConfigurationType() { - if (isDebugMode()) { + if (runtimeManager.isDebugMode()) { return Application.DEVELOPMENT; } return Application.DEPLOYMENT; diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java index bc65d77d..87b0a08f 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java @@ -17,18 +17,17 @@ package com.gitblit.wicket; import java.util.Date; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; -import org.apache.wicket.protocol.http.IWebApplicationFactory; -import org.apache.wicket.protocol.http.WebApplication; -import org.apache.wicket.protocol.http.WicketFilter; import org.apache.wicket.util.string.Strings; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; -import com.gitblit.GitBlit; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.dagger.DaggerWicketFilter; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; @@ -45,19 +44,28 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public class GitblitWicketFilter extends WicketFilter { +@Singleton +public class GitblitWicketFilter extends DaggerWicketFilter { - public GitblitWicketFilter() { - } + private final IStoredSettings settings; - @Override - protected IWebApplicationFactory getApplicationFactory() { - return new IWebApplicationFactory() { - @Override - public WebApplication createApplication(WicketFilter filter) { - return new GitBlitWebApp(); - } - }; + private final IRuntimeManager runtimeManager; + + private final IRepositoryManager repositoryManager; + + private final IProjectManager projectManager; + + @Inject + public GitblitWicketFilter( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + super(); + this.settings = runtimeManager.getSettings(); + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; } /** @@ -97,11 +105,6 @@ public class GitblitWicketFilter extends WicketFilter { commitId = servletRequest.getParameter("h"); } - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); - IStoredSettings settings = runtimeManager.getSettings(); - IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class); - IProjectManager projectManager = GitBlit.getManager(IProjectManager.class); - repo = repo.replace("%2f", "/").replace("%2F", "/").replace(settings.getChar(Keys.web.forwardSlashCharacter, '/'), '/'); GitBlitWebApp app = (GitBlitWebApp) getWebApplication(); diff --git a/src/test/java/com/gitblit/tests/GitDaemonStopTest.java b/src/test/java/com/gitblit/tests/GitDaemonStopTest.java deleted file mode 100644 index 6e940cbe..00000000 --- a/src/test/java/com/gitblit/tests/GitDaemonStopTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2013 gitblit.com. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gitblit.tests; - -import org.junit.Test; - -import com.gitblit.git.GitDaemon; - -public class GitDaemonStopTest extends GitblitUnitTest { - - @Test - public void testGitDaemonStop() throws Exception { - GitDaemon daemon = new GitDaemon("localhost", GitDaemon.DEFAULT_PORT + 1, GitBlitSuite.REPOSITORIES); - daemon.setTimeout(5); - daemon.start(); - Thread.sleep(5000); - daemon.stop(); - } -} diff --git a/src/test/java/com/gitblit/tests/LuceneExecutorTest.java b/src/test/java/com/gitblit/tests/LuceneExecutorTest.java index 0f7e55ca..0e1aee1e 100644 --- a/src/test/java/com/gitblit/tests/LuceneExecutorTest.java +++ b/src/test/java/com/gitblit/tests/LuceneExecutorTest.java @@ -16,15 +16,15 @@ package com.gitblit.tests; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.eclipse.jgit.lib.Repository; import org.junit.After; import org.junit.Before; import org.junit.Test; +import com.gitblit.GitBlit; +import com.gitblit.Keys; import com.gitblit.LuceneExecutor; import com.gitblit.models.RefModel; import com.gitblit.models.RepositoryModel; @@ -44,9 +44,10 @@ public class LuceneExecutorTest extends GitblitUnitTest { LuceneExecutor lucene; private LuceneExecutor newLuceneExecutor() { - Map map = new HashMap(); - MemorySettings settings = new MemorySettings(map); - return new LuceneExecutor(settings, GitBlitSuite.REPOSITORIES); + MemorySettings settings = new MemorySettings(); + settings.put(Keys.git.repositoriesFolder, GitBlitSuite.REPOSITORIES); + GitBlit gitblit = new GitBlit(settings, GitBlitSuite.REPOSITORIES); + return new LuceneExecutor(settings, gitblit); } private RepositoryModel newRepositoryModel(Repository repository) { diff --git a/src/test/java/com/gitblit/tests/mock/MemorySettings.java b/src/test/java/com/gitblit/tests/mock/MemorySettings.java index 5b8e60a9..e3f971f0 100644 --- a/src/test/java/com/gitblit/tests/mock/MemorySettings.java +++ b/src/test/java/com/gitblit/tests/mock/MemorySettings.java @@ -16,6 +16,7 @@ */ package com.gitblit.tests.mock; +import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -25,6 +26,10 @@ public class MemorySettings extends IStoredSettings { private Map backingMap; + public MemorySettings() { + this(new HashMap()); + } + public MemorySettings(Map backingMap) { super(MemorySettings.class); this.backingMap = backingMap; @@ -38,8 +43,8 @@ public class MemorySettings extends IStoredSettings { return props; } - public void put(Object key, Object value) { - backingMap.put(key.toString(), value); + public void put(String key, Object value) { + backingMap.put(key, value); } @Override