From 17e2d3407065a7433c7512c37a7cf7e7df63f337 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 16 May 2014 10:34:35 -0400 Subject: Return of Servlet3 servlet and filter loading This is a quick return of the servlet3-style code which was reverted mid-December 2013. It is not completely tested, but a casual review was done and it's looks good. The next steps should be to restore `@Inject` annotations, simplify *DaggerModule* boilerplate, and run this on a JEE container with CDI - like JBoss AS 7. --- src/main/java/WEB-INF/beans.xml | 10 + src/main/java/WEB-INF/web.xml | 394 ++------------------- src/main/java/com/gitblit/Constants.java | 2 + src/main/java/com/gitblit/DaggerModule.java | 331 ++++++++++++++++- .../java/com/gitblit/dagger/DaggerContext.java | 22 +- .../java/com/gitblit/servlet/GitblitContext.java | 64 +++- .../gitblit/servlet/InjectionContextListener.java | 241 +++++++++++++ 7 files changed, 683 insertions(+), 381 deletions(-) create mode 100644 src/main/java/WEB-INF/beans.xml create mode 100644 src/main/java/com/gitblit/servlet/InjectionContextListener.java diff --git a/src/main/java/WEB-INF/beans.xml b/src/main/java/WEB-INF/beans.xml new file mode 100644 index 00000000..0165c2cb --- /dev/null +++ b/src/main/java/WEB-INF/beans.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/WEB-INF/web.xml b/src/main/java/WEB-INF/web.xml index 13f612e4..2a570e32 100644 --- a/src/main/java/WEB-INF/web.xml +++ b/src/main/java/WEB-INF/web.xml @@ -1,361 +1,35 @@ - - - - - - The base folder is used to specify the root location of your Gitblit data. - baseFolder - java.lang.String - ${contextFolder}/WEB-INF/data - - - - Gitblit - @gb.version@ - - - - - - - - GitServlet - com.gitblit.servlet.GitServlet - - - GitServlet - /git/* - - - GitServlet - /r/* - - - - - - SparkleShareInviteServlet - com.gitblit.servlet.SparkleShareInviteServlet - - - SparkleShareInviteServlet - /sparkleshare/* - - - - - - SyndicationServlet - com.gitblit.servlet.SyndicationServlet - - - SyndicationServlet - /feed/* - - - - - - ZipServlet - com.gitblit.servlet.DownloadZipServlet - - - ZipServlet - /zip/* - - - - - - FederationServlet - com.gitblit.servlet.FederationServlet - - - FederationServlet - /federation/* - - - - - - RpcServlet - com.gitblit.servlet.RpcServlet - - - RpcServlet - /rpc/* - - - - - - RawServlet - com.gitblit.servlet.RawServlet - - - RawServlet - /raw/* - - - - - - PagesServlet - com.gitblit.servlet.PagesServlet - - - PagesServlet - /pages/* - - - - - - LogoServlet - com.gitblit.servlet.LogoServlet - - - LogoServlet - /logo.png - - - - - - PtServlet - com.gitblit.servlet.PtServlet - - - PtServlet - /pt - - - - - - BranchGraphServlet - com.gitblit.servlet.BranchGraphServlet - - - BranchGraphServlet - /graph/* - - - - - RobotsTxtServlet - com.gitblit.servlet.RobotsTxtServlet - - - RobotsTxtServlet - /robots.txt - - - - ProxyFilter - com.gitblit.servlet.ProxyFilter - - - ProxyFilter - /* - - - - - GitFilter - com.gitblit.servlet.GitFilter - - - GitFilter - /git/* - - - GitFilter - /r/* - - - - - - SyndicationFilter - com.gitblit.servlet.SyndicationFilter - - - SyndicationFilter - /feed/* - - - - - - ZipFilter - com.gitblit.servlet.DownloadZipFilter - - - ZipFilter - /zip/* - - - - - - RpcFilter - com.gitblit.servlet.RpcFilter - - - RpcFilter - /rpc/* - - - - - - RawFilter - com.gitblit.servlet.RawFilter - - - RawFilter - /raw/* - - - - - - PagesFilter - com.gitblit.servlet.PagesFilter - - - PagesFilter - /pages/* - - - - EnforceAuthenticationFilter - com.gitblit.servlet.EnforceAuthenticationFilter - - - EnforceAuthenticationFilter - /* - - - - - - wicketFilter - - com.gitblit.wicket.GitblitWicketFilter - - - ignorePaths - - r/,git/,pt,feed/,zip/,federation/,rpc/,raw/,pages/,robots.txt,logo.png,graph/,sparkleshare/ - - - - wicketFilter - /* - - + + + + + + The base folder is used to specify the root location of your Gitblit data. + baseFolder + java.lang.String + ${contextFolder}/WEB-INF/data + + + + Gitblit - @gb.version@ + \ No newline at end of file diff --git a/src/main/java/com/gitblit/Constants.java b/src/main/java/com/gitblit/Constants.java index 8d6e249a..279d3c92 100644 --- a/src/main/java/com/gitblit/Constants.java +++ b/src/main/java/com/gitblit/Constants.java @@ -70,6 +70,8 @@ public class Constants { public static final String RAW_PATH = "/raw/"; + public static final String PT_PATH = "/pt"; + public static final String BRANCH_GRAPH_PATH = "/graph/"; public static final String BORDER = "*****************************************************************"; diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java index 6ad3fe63..aa10198c 100644 --- a/src/main/java/com/gitblit/DaggerModule.java +++ b/src/main/java/com/gitblit/DaggerModule.java @@ -34,12 +34,33 @@ import com.gitblit.manager.ProjectManager; import com.gitblit.manager.RepositoryManager; import com.gitblit.manager.RuntimeManager; import com.gitblit.manager.UserManager; +import com.gitblit.servlet.BranchGraphServlet; +import com.gitblit.servlet.DownloadZipFilter; +import com.gitblit.servlet.DownloadZipServlet; +import com.gitblit.servlet.EnforceAuthenticationFilter; +import com.gitblit.servlet.FederationServlet; +import com.gitblit.servlet.GitFilter; +import com.gitblit.servlet.GitServlet; +import com.gitblit.servlet.LogoServlet; +import com.gitblit.servlet.PagesFilter; +import com.gitblit.servlet.PagesServlet; +import com.gitblit.servlet.ProxyFilter; +import com.gitblit.servlet.PtServlet; +import com.gitblit.servlet.RawFilter; +import com.gitblit.servlet.RawServlet; +import com.gitblit.servlet.RobotsTxtServlet; +import com.gitblit.servlet.RpcFilter; +import com.gitblit.servlet.RpcServlet; +import com.gitblit.servlet.SparkleShareInviteServlet; +import com.gitblit.servlet.SyndicationFilter; +import com.gitblit.servlet.SyndicationServlet; import com.gitblit.transport.ssh.FileKeyManager; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.transport.ssh.MemoryKeyManager; import com.gitblit.transport.ssh.NullKeyManager; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebApp; +import com.gitblit.wicket.GitblitWicketFilter; import dagger.Module; import dagger.Provides; @@ -70,7 +91,30 @@ import dagger.Provides; IGitblit.class, // the Gitblit Wicket app - GitBlitWebApp.class + GitBlitWebApp.class, + + // filters & servlets + GitServlet.class, + GitFilter.class, + RawServlet.class, + RawFilter.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, + PtServlet.class, + ProxyFilter.class, + EnforceAuthenticationFilter.class, + GitblitWicketFilter.class } ) public class DaggerModule { @@ -212,4 +256,289 @@ public class DaggerModule { federationManager, gitblit); } +// +// @Provides @Singleton GitblitWicketFilter provideGitblitWicketFilter(GitBlitWebApp webapp) { +// return new GitblitWicketFilter(webapp); +// } +// +// @Provides GitServlet provideGitServlet(IGitblit gitblit) { +// return new GitServlet(gitblit); +// } +// +// @Provides GitFilter provideGitFilter( +// IRuntimeManager runtimeManager, +// IUserManager userManager, +// IAuthenticationManager authenticationManager, +// IRepositoryManager repositoryManager, +// IFederationManager federationManager) { +// +// return new GitFilter( +// runtimeManager, +// userManager, +// authenticationManager, +// repositoryManager, +// federationManager); +// } +// +// @Provides @Singleton PagesServlet providePagesServlet( +// IRuntimeManager runtimeManager, +// IRepositoryManager repositoryManager) { +// +// return new PagesServlet(runtimeManager, repositoryManager); +// } +// +// @Provides @Singleton PagesFilter providePagesFilter( +// IRuntimeManager runtimeManager, +// IAuthenticationManager authenticationManager, +// IRepositoryManager repositoryManager) { +// +// return new PagesFilter( +// runtimeManager, +// authenticationManager, +// repositoryManager); +// } +// +// @Provides @Singleton RpcServlet provideRpcServlet(IGitblit gitblit) { +// return new RpcServlet(gitblit); +// } +// +// @Provides @Singleton RpcFilter provideRpcFilter( +// IRuntimeManager runtimeManager, +// IAuthenticationManager authenticationManager) { +// +// return new RpcFilter(runtimeManager, authenticationManager); +// } +// +// @Provides @Singleton DownloadZipServlet provideDownloadZipServlet( +// IRuntimeManager runtimeManager, +// IRepositoryManager repositoryManager) { +// +// return new DownloadZipServlet(runtimeManager, repositoryManager); +// } +// +// @Provides @Singleton DownloadZipFilter provideDownloadZipFilter( +// IRuntimeManager runtimeManager, +// IAuthenticationManager authenticationManager, +// IRepositoryManager repositoryManager) { +// +// return new DownloadZipFilter( +// runtimeManager, +// authenticationManager, +// repositoryManager); +// } +// +// @Provides @Singleton SyndicationServlet provideSyndicationServlet( +// IRuntimeManager runtimeManager, +// IRepositoryManager repositoryManager, +// IProjectManager projectManager) { +// +// return new SyndicationServlet( +// runtimeManager, +// repositoryManager, +// projectManager); +// } +// +// @Provides @Singleton SyndicationFilter provideSyndicationFilter( +// IRuntimeManager runtimeManager, +// IAuthenticationManager authenticationManager, +// IRepositoryManager repositoryManager, +// IProjectManager projectManager) { +// +// return new SyndicationFilter( +// runtimeManager, +// authenticationManager, +// repositoryManager, +// projectManager); +// } +// +// @Provides @Singleton FederationServlet provideFederationServlet( +// IRuntimeManager runtimeManager, +// IUserManager userManager, +// IRepositoryManager repositoryManager, +// IFederationManager federationManager) { +// +// return new FederationServlet( +// runtimeManager, +// userManager, +// repositoryManager, +// federationManager); +// } +// +// @Provides @Singleton SparkleShareInviteServlet provideSparkleshareInviteServlet( +// IRuntimeManager runtimeManager, +// IUserManager userManager, +// IAuthenticationManager authenticationManager, +// IRepositoryManager repositoryManager) { +// +// return new SparkleShareInviteServlet( +// runtimeManager, +// userManager, +// authenticationManager, +// repositoryManager); +// } +// +// @Provides @Singleton BranchGraphServlet provideBranchGraphServlet( +// IRuntimeManager runtimeManager, +// IRepositoryManager repositoryManager) { +// +// return new BranchGraphServlet(runtimeManager, repositoryManager); +// } +// +// @Provides @Singleton RobotsTxtServlet provideRobotsTxtServlet(IRuntimeManager runtimeManager) { +// return new RobotsTxtServlet(runtimeManager); +// } +// +// @Provides @Singleton LogoServlet provideLogoServlet(IRuntimeManager runtimeManager) { +// return new LogoServlet(runtimeManager); +// } +// +// @Provides @Singleton EnforceAuthenticationFilter provideEnforceAuthenticationFilter( +// IRuntimeManager runtimeManager, +// IAuthenticationManager authenticationManager) { +// +// return new EnforceAuthenticationFilter(runtimeManager, authenticationManager); +// } + + + @Provides @Singleton GitblitWicketFilter provideGitblitWicketFilter(GitBlitWebApp webapp) { + return new GitblitWicketFilter(); + } + + @Provides GitServlet provideGitServlet(IGitblit gitblit) { + return new GitServlet(); + } + + @Provides GitFilter provideGitFilter( + IRuntimeManager runtimeManager, + IUserManager userManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager) { + + return new GitFilter(); + } + + @Provides @Singleton RawServlet provideRawServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + return new RawServlet(); + } + + @Provides @Singleton RawFilter provideRawFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + return new RawFilter(); + } + + @Provides @Singleton PagesServlet providePagesServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + return new PagesServlet(); + } + + @Provides @Singleton PagesFilter providePagesFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + return new PagesFilter(); + } + + @Provides @Singleton RpcServlet provideRpcServlet(IGitblit gitblit) { + return new RpcServlet(); + } + + @Provides @Singleton RpcFilter provideRpcFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager) { + + return new RpcFilter(); + } + + @Provides @Singleton DownloadZipServlet provideDownloadZipServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + return new DownloadZipServlet(); + } + + @Provides @Singleton DownloadZipFilter provideDownloadZipFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + return new DownloadZipFilter(); + } + + @Provides @Singleton SyndicationServlet provideSyndicationServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + return new SyndicationServlet(); + } + + @Provides @Singleton SyndicationFilter provideSyndicationFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + return new SyndicationFilter(); + } + + @Provides @Singleton FederationServlet provideFederationServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager) { + + return new FederationServlet(); + } + + @Provides @Singleton SparkleShareInviteServlet provideSparkleshareInviteServlet( + IRuntimeManager runtimeManager, + IUserManager userManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + return new SparkleShareInviteServlet(); + } + + @Provides @Singleton BranchGraphServlet provideBranchGraphServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + return new BranchGraphServlet(); + } + + @Provides @Singleton RobotsTxtServlet provideRobotsTxtServlet(IRuntimeManager runtimeManager) { + return new RobotsTxtServlet(); + } + + @Provides @Singleton LogoServlet provideLogoServlet(IRuntimeManager runtimeManager) { + return new LogoServlet(); + } + + @Provides @Singleton PtServlet providePtServlet(IRuntimeManager runtimeManager) { + return new PtServlet(); + } + + @Provides @Singleton ProxyFilter provideProxyFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager) { + + return new ProxyFilter(); + } + + @Provides @Singleton EnforceAuthenticationFilter provideEnforceAuthenticationFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager) { + + return new EnforceAuthenticationFilter(); + } } \ No newline at end of file diff --git a/src/main/java/com/gitblit/dagger/DaggerContext.java b/src/main/java/com/gitblit/dagger/DaggerContext.java index 0e6a3fc4..ef570804 100644 --- a/src/main/java/com/gitblit/dagger/DaggerContext.java +++ b/src/main/java/com/gitblit/dagger/DaggerContext.java @@ -17,11 +17,12 @@ package com.gitblit.dagger; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.gitblit.servlet.InjectionContextListener; + import dagger.ObjectGraph; /** @@ -31,7 +32,7 @@ import dagger.ObjectGraph; * @author James Moger * */ -public abstract class DaggerContext implements ServletContextListener { +public abstract class DaggerContext extends InjectionContextListener { public static final String INJECTOR_NAME = ObjectGraph.class.getName(); @@ -58,6 +59,23 @@ public abstract class DaggerContext implements ServletContextListener { 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(); diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index d5b4092c..ac547914 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -23,13 +23,15 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; 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 com.gitblit.Constants; import com.gitblit.DaggerModule; @@ -52,20 +54,22 @@ import com.gitblit.manager.IUserManager; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.ContainerUtils; import com.gitblit.utils.StringUtils; +import com.gitblit.wicket.GitblitWicketFilter; import dagger.ObjectGraph; /** * This class is the main entry point for the entire webapp. It is a singleton * created manually by Gitblit GO or dynamically by the WAR/Express servlet - * container. This class instantiates and starts all managers. Servlets and - * filters are instantiated defined in web.xml and instantiated by the servlet - * container, but those servlets and filters use Dagger to manually inject their - * dependencies. + * container. This class instantiates and starts all managers. + * + * Servlets and filters are injected which allows Gitblit to be completely + * code-driven. * * @author James Moger * */ +@WebListener public class GitblitContext extends DaggerContext { private static GitblitContext gitblit; @@ -119,22 +123,11 @@ public class GitblitContext extends DaggerContext { return new Object [] { new DaggerModule() }; } - /** - * Configure Gitblit from the web.xml, if no configuration has already been - * specified. - * - * @see ServletContextListener.contextInitialize(ServletContextEvent) - */ - @Override - public final void contextInitialized(ServletContextEvent contextEvent) { - ServletContext context = contextEvent.getServletContext(); - configureContext(context); - } - /** * Prepare runtime settings and start all manager instances. */ - protected void configureContext(ServletContext context) { + @Override + protected void beforeServletInjection(ServletContext context) { ObjectGraph injector = getInjector(context); // create the runtime settings object @@ -236,6 +229,41 @@ public class GitblitContext extends DaggerContext { logger.info("----[{}]----", clazz.getName()); } + /** + * Instantiate and inject all filters and servlets into the container using + * the servlet 3 specification. + */ + @Override + protected void injectServlets(ServletContext context) { + // access restricted servlets + serve(context, Constants.R_PATH, GitServlet.class, GitFilter.class); + serve(context, Constants.GIT_PATH, GitServlet.class, GitFilter.class); + serve(context, Constants.RAW_PATH, RawServlet.class, RawFilter.class); + serve(context, Constants.PAGES, PagesServlet.class, PagesFilter.class); + serve(context, Constants.RPC_PATH, RpcServlet.class, RpcFilter.class); + serve(context, Constants.ZIP_PATH, DownloadZipServlet.class, DownloadZipFilter.class); + serve(context, Constants.SYNDICATION_PATH, SyndicationServlet.class, SyndicationFilter.class); + + // servlets + serve(context, Constants.FEDERATION_PATH, FederationServlet.class); + serve(context, Constants.SPARKLESHARE_INVITE_PATH, SparkleShareInviteServlet.class); + serve(context, Constants.BRANCH_GRAPH_PATH, BranchGraphServlet.class); + serve(context, Constants.PT_PATH, PtServlet.class); + file(context, "/robots.txt", RobotsTxtServlet.class); + file(context, "/logo.png", LogoServlet.class); + + // global filters + filter(context, "/*", ProxyFilter.class, null); + filter(context, "/*", EnforceAuthenticationFilter.class, null); + + // Wicket + String toIgnore = StringUtils.flattenStrings(getRegisteredPaths(), ","); + Map params = new HashMap(); + params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, "/*"); + params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore); + filter(context, "/*", GitblitWicketFilter.class, params); + } + /** * Gitblit is being shutdown either because the servlet container is * shutting down or because the servlet container is re-deploying Gitblit. diff --git a/src/main/java/com/gitblit/servlet/InjectionContextListener.java b/src/main/java/com/gitblit/servlet/InjectionContextListener.java new file mode 100644 index 00000000..17de6dad --- /dev/null +++ b/src/main/java/com/gitblit/servlet/InjectionContextListener.java @@ -0,0 +1,241 @@ +/* + * Copyright 2014 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.servlet; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; + +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import javax.servlet.FilterRegistration; +import javax.servlet.Servlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletRegistration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Injection context listener instantiates and injects servlets, filters, and + * anything else you might want into a servlet context. This class provides + * convenience methods for servlet & filter registration and also tracks + * registered paths. + * + * @author James Moger + * + */ +public abstract class InjectionContextListener implements ServletContextListener { + + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + private final List registeredPaths = new ArrayList(); + + protected final List getRegisteredPaths() { + return registeredPaths; + } + + /** + * Hook for subclasses to manipulate context initialization before + * standard initialization procedure. + * + * @param context + */ + protected void beforeServletInjection(ServletContext context) { + // NOOP + } + + /** + * Hook for subclasses to instantiate and inject servlets and filters + * into the servlet context. + * + * @param context + */ + protected abstract void injectServlets(ServletContext context); + + /** + * Hook for subclasses to manipulate context initialization after + * servlet registration. + * + * @param context + */ + protected void afterServletInjection(ServletContext context) { + // NOOP + } + + /** + * Configure Gitblit from the web.xml, if no configuration has already been + * specified. + * + * @see ServletContextListener.contextInitialize(ServletContextEvent) + */ + @Override + public final void contextInitialized(ServletContextEvent contextEvent) { + ServletContext context = contextEvent.getServletContext(); + beforeServletInjection(context); + injectServlets(context); + afterServletInjection(context); + } + + + /** + * Registers a file path. + * + * @param context + * @param file + * @param servletClass + */ + protected void file(ServletContext context, String file, Class servletClass) { + file(context, file, servletClass, null); + } + + /** + * Registers a file path with init parameters. + * + * @param context + * @param file + * @param servletClass + * @param initParams + */ + protected void file(ServletContext context, String file, Class servletClass, Map initParams) { + Servlet servlet = instantiate(context, servletClass); + ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + file), servlet); + d.addMapping(file); + if (initParams != null) { + d.setInitParameters(initParams); + } + registeredPaths.add(file); + } + + /** + * Serves a path (trailing wildcard will be appended). + * + * @param context + * @param route + * @param servletClass + */ + protected void serve(ServletContext context, String route, Class servletClass) { + serve(context, route, servletClass, (Class) null); + } + + /** + * Serves a path (trailing wildcard will be appended) with init parameters. + * + * @param context + * @param route + * @param servletClass + * @param initParams + */ + protected void serve(ServletContext context, String route, Class servletClass, Map initParams) { + Servlet servlet = instantiate(context, servletClass); + ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); + d.addMapping(route + "*"); + if (initParams != null) { + d.setInitParameters(initParams); + } + registeredPaths.add(route); + } + + /** + * Serves a path (trailing wildcard will be appended) and also maps a filter + * to that path. + * + * @param context + * @param route + * @param servletClass + * @param filterClass + */ + protected void serve(ServletContext context, String route, Class servletClass, Class filterClass) { + Servlet servlet = instantiate(context, servletClass); + ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); + d.addMapping(route + "*"); + if (filterClass != null) { + filter(context, route + "*", filterClass); + } + registeredPaths.add(route); + } + + /** + * Registers a path filter. + * + * @param context + * @param route + * @param filterClass + */ + protected void filter(ServletContext context, String route, Class filterClass) { + filter(context, route, filterClass, null); + } + + /** + * Registers a path filter with init parameters. + * + * @param context + * @param route + * @param filterClass + * @param initParams + */ + protected void filter(ServletContext context, String route, Class filterClass, Map initParams) { + Filter filter = instantiate(context, filterClass); + FilterRegistration.Dynamic d = context.addFilter(sanitize(filterClass.getSimpleName() + route), filter); + d.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, route); + if (initParams != null) { + d.setInitParameters(initParams); + } + } + + /** + * Limit the generated servlet/filter names to alpha-numeric values with a + * handful of acceptable other characters. + * + * @param name + * @return a sanitized name + */ + protected String sanitize(String name) { + StringBuilder sb = new StringBuilder(); + for (char c : name.toCharArray()) { + if (Character.isLetterOrDigit(c)) { + sb.append(c); + } else if ('-' == c) { + sb.append(c); + } else if ('*' == c) { + sb.append("all"); + } else if ('.' == c) { + sb.append('.'); + } else { + sb.append('_'); + } + } + return sb.toString(); + } + + /** + * Instantiates an object. + * + * @param clazz + * @return the object + */ + protected X instantiate(ServletContext context, Class clazz) { + try { + return clazz.newInstance(); + } catch (Throwable t) { + logger.error(null, t); + } + return null; + } +} -- cgit v1.2.3 From 1fc1bda84e06f36a787ed84d681025e1e921c7f9 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 16 May 2014 13:50:01 -0400 Subject: Update to Dagger 1.2.1 and Guava 15 --- .classpath | 8 ++++---- build.moxie | 5 +++-- gitblit.iml | 40 ++++++++++++++++++++-------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/.classpath b/.classpath index c5fe4e56..6d6011c4 100644 --- a/.classpath +++ b/.classpath @@ -5,10 +5,11 @@ - + - - + + + @@ -70,7 +71,6 @@ - diff --git a/build.moxie b/build.moxie index 2f86a3c7..c5ad4423 100644 --- a/build.moxie +++ b/build.moxie @@ -127,8 +127,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 +- compile 'com.squareup.dagger:dagger:1.2.1' :war apt +- compile 'com.squareup.dagger:dagger-compiler:1.2.1' :war optional apt +- compile 'com.google.guava:guava:15.0' :war # Standard dependencies - compile 'com.intellij:annotations:12.0' :war - compile 'log4j:log4j:1.2.17' :war :fedclient :authority diff --git a/gitblit.iml b/gitblit.iml index 5f985a5b..cd6e14a2 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -14,13 +14,13 @@ - + - + - + @@ -36,24 +36,35 @@ - + - + - + - + - + - + + + + + + + + + + + + @@ -724,17 +735,6 @@ - - - - - - - - - - - -- cgit v1.2.3 From 1b34b05f5b8d2d0fd51819b286e22d7ddd27cb27 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 16 May 2014 13:50:59 -0400 Subject: Embrace @Inject for Managers, Servlets, and Filters --- src/main/java/com/gitblit/DaggerModule.java | 395 +-------------------- src/main/java/com/gitblit/GitBlit.java | 2 + .../com/gitblit/dagger/DaggerWicketFilter.java | 45 --- .../com/gitblit/manager/AuthenticationManager.java | 2 + .../com/gitblit/manager/FederationManager.java | 2 + .../com/gitblit/manager/NotificationManager.java | 2 + .../java/com/gitblit/manager/PluginManager.java | 3 + .../java/com/gitblit/manager/ProjectManager.java | 3 + .../com/gitblit/manager/RepositoryManager.java | 3 + .../java/com/gitblit/manager/RuntimeManager.java | 3 + src/main/java/com/gitblit/manager/UserManager.java | 3 + .../gitblit/servlet/AccessRestrictionFilter.java | 18 +- .../com/gitblit/servlet/AuthenticationFilter.java | 17 +- .../com/gitblit/servlet/BranchGraphServlet.java | 25 +- .../com/gitblit/servlet/DownloadZipFilter.java | 16 + .../com/gitblit/servlet/DownloadZipServlet.java | 17 +- .../servlet/EnforceAuthenticationFilter.java | 34 +- .../com/gitblit/servlet/FederationServlet.java | 22 +- src/main/java/com/gitblit/servlet/GitFilter.java | 26 +- src/main/java/com/gitblit/servlet/GitServlet.java | 18 +- src/main/java/com/gitblit/servlet/JsonServlet.java | 4 +- src/main/java/com/gitblit/servlet/LogoServlet.java | 15 +- src/main/java/com/gitblit/servlet/PagesFilter.java | 17 + .../java/com/gitblit/servlet/PagesServlet.java | 13 + src/main/java/com/gitblit/servlet/ProxyFilter.java | 33 +- src/main/java/com/gitblit/servlet/PtServlet.java | 15 +- src/main/java/com/gitblit/servlet/RawFilter.java | 16 + src/main/java/com/gitblit/servlet/RawServlet.java | 24 +- .../java/com/gitblit/servlet/RobotsTxtServlet.java | 15 +- src/main/java/com/gitblit/servlet/RpcFilter.java | 22 +- src/main/java/com/gitblit/servlet/RpcServlet.java | 13 +- .../gitblit/servlet/SparkleShareInviteServlet.java | 26 +- .../com/gitblit/servlet/SyndicationFilter.java | 24 +- .../com/gitblit/servlet/SyndicationServlet.java | 24 +- .../java/com/gitblit/wicket/GitBlitWebApp.java | 5 + .../com/gitblit/wicket/GitblitWicketFilter.java | 28 +- 36 files changed, 358 insertions(+), 592 deletions(-) delete mode 100644 src/main/java/com/gitblit/dagger/DaggerWicketFilter.java diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java index aa10198c..662ca033 100644 --- a/src/main/java/com/gitblit/DaggerModule.java +++ b/src/main/java/com/gitblit/DaggerModule.java @@ -72,7 +72,6 @@ import dagger.Provides; * */ @Module( - library = true, injects = { IStoredSettings.class, @@ -123,32 +122,24 @@ public class DaggerModule { return new FileSettings(); } - @Provides @Singleton IRuntimeManager provideRuntimeManager(IStoredSettings settings) { - return new RuntimeManager(settings); + @Provides @Singleton IRuntimeManager provideRuntimeManager(RuntimeManager manager) { + return manager; } - @Provides @Singleton IPluginManager providePluginManager(IRuntimeManager runtimeManager) { - return new PluginManager(runtimeManager); + @Provides @Singleton IPluginManager providePluginManager(PluginManager manager) { + return manager; } - @Provides @Singleton INotificationManager provideNotificationManager(IStoredSettings settings) { - return new NotificationManager(settings); + @Provides @Singleton INotificationManager provideNotificationManager(NotificationManager manager) { + return manager; } - @Provides @Singleton IUserManager provideUserManager( - IRuntimeManager runtimeManager, - IPluginManager pluginManager) { - - return new UserManager(runtimeManager, pluginManager); + @Provides @Singleton IUserManager provideUserManager(UserManager manager) { + return manager; } - @Provides @Singleton IAuthenticationManager provideAuthenticationManager( - IRuntimeManager runtimeManager, - IUserManager userManager) { - - return new AuthenticationManager( - runtimeManager, - userManager); + @Provides @Singleton IAuthenticationManager provideAuthenticationManager(AuthenticationManager manager) { + return manager; } @Provides @Singleton IPublicKeyManager providePublicKeyManager( @@ -176,369 +167,19 @@ public class DaggerModule { } } - @Provides @Singleton IRepositoryManager provideRepositoryManager( - IRuntimeManager runtimeManager, - IPluginManager pluginManager, - IUserManager userManager) { - - return new RepositoryManager( - runtimeManager, - pluginManager, - userManager); - } - - @Provides @Singleton IProjectManager provideProjectManager( - IRuntimeManager runtimeManager, - IUserManager userManager, - IRepositoryManager repositoryManager) { - - return new ProjectManager( - runtimeManager, - userManager, - repositoryManager); - } - - @Provides @Singleton IFederationManager provideFederationManager( - IRuntimeManager runtimeManager, - INotificationManager notificationManager, - IRepositoryManager repositoryManager) { - - return new FederationManager( - runtimeManager, - notificationManager, - repositoryManager); - } - - @Provides @Singleton IGitblit provideGitblit( - IRuntimeManager runtimeManager, - IPluginManager pluginManager, - INotificationManager notificationManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IPublicKeyManager publicKeyManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager, - IFederationManager federationManager) { - - return new GitBlit( - runtimeManager, - pluginManager, - notificationManager, - userManager, - authenticationManager, - publicKeyManager, - repositoryManager, - projectManager, - federationManager); - } - - @Provides @Singleton GitBlitWebApp provideWebApplication( - IRuntimeManager runtimeManager, - IPluginManager pluginManager, - INotificationManager notificationManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IPublicKeyManager publicKeyManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager, - IFederationManager federationManager, - IGitblit gitblit) { - - return new GitBlitWebApp( - runtimeManager, - pluginManager, - notificationManager, - userManager, - authenticationManager, - publicKeyManager, - repositoryManager, - projectManager, - federationManager, - gitblit); - } -// -// @Provides @Singleton GitblitWicketFilter provideGitblitWicketFilter(GitBlitWebApp webapp) { -// return new GitblitWicketFilter(webapp); -// } -// -// @Provides GitServlet provideGitServlet(IGitblit gitblit) { -// return new GitServlet(gitblit); -// } -// -// @Provides GitFilter provideGitFilter( -// IRuntimeManager runtimeManager, -// IUserManager userManager, -// IAuthenticationManager authenticationManager, -// IRepositoryManager repositoryManager, -// IFederationManager federationManager) { -// -// return new GitFilter( -// runtimeManager, -// userManager, -// authenticationManager, -// repositoryManager, -// federationManager); -// } -// -// @Provides @Singleton PagesServlet providePagesServlet( -// IRuntimeManager runtimeManager, -// IRepositoryManager repositoryManager) { -// -// return new PagesServlet(runtimeManager, repositoryManager); -// } -// -// @Provides @Singleton PagesFilter providePagesFilter( -// IRuntimeManager runtimeManager, -// IAuthenticationManager authenticationManager, -// IRepositoryManager repositoryManager) { -// -// return new PagesFilter( -// runtimeManager, -// authenticationManager, -// repositoryManager); -// } -// -// @Provides @Singleton RpcServlet provideRpcServlet(IGitblit gitblit) { -// return new RpcServlet(gitblit); -// } -// -// @Provides @Singleton RpcFilter provideRpcFilter( -// IRuntimeManager runtimeManager, -// IAuthenticationManager authenticationManager) { -// -// return new RpcFilter(runtimeManager, authenticationManager); -// } -// -// @Provides @Singleton DownloadZipServlet provideDownloadZipServlet( -// IRuntimeManager runtimeManager, -// IRepositoryManager repositoryManager) { -// -// return new DownloadZipServlet(runtimeManager, repositoryManager); -// } -// -// @Provides @Singleton DownloadZipFilter provideDownloadZipFilter( -// IRuntimeManager runtimeManager, -// IAuthenticationManager authenticationManager, -// IRepositoryManager repositoryManager) { -// -// return new DownloadZipFilter( -// runtimeManager, -// authenticationManager, -// repositoryManager); -// } -// -// @Provides @Singleton SyndicationServlet provideSyndicationServlet( -// IRuntimeManager runtimeManager, -// IRepositoryManager repositoryManager, -// IProjectManager projectManager) { -// -// return new SyndicationServlet( -// runtimeManager, -// repositoryManager, -// projectManager); -// } -// -// @Provides @Singleton SyndicationFilter provideSyndicationFilter( -// IRuntimeManager runtimeManager, -// IAuthenticationManager authenticationManager, -// IRepositoryManager repositoryManager, -// IProjectManager projectManager) { -// -// return new SyndicationFilter( -// runtimeManager, -// authenticationManager, -// repositoryManager, -// projectManager); -// } -// -// @Provides @Singleton FederationServlet provideFederationServlet( -// IRuntimeManager runtimeManager, -// IUserManager userManager, -// IRepositoryManager repositoryManager, -// IFederationManager federationManager) { -// -// return new FederationServlet( -// runtimeManager, -// userManager, -// repositoryManager, -// federationManager); -// } -// -// @Provides @Singleton SparkleShareInviteServlet provideSparkleshareInviteServlet( -// IRuntimeManager runtimeManager, -// IUserManager userManager, -// IAuthenticationManager authenticationManager, -// IRepositoryManager repositoryManager) { -// -// return new SparkleShareInviteServlet( -// runtimeManager, -// userManager, -// authenticationManager, -// repositoryManager); -// } -// -// @Provides @Singleton BranchGraphServlet provideBranchGraphServlet( -// IRuntimeManager runtimeManager, -// IRepositoryManager repositoryManager) { -// -// return new BranchGraphServlet(runtimeManager, repositoryManager); -// } -// -// @Provides @Singleton RobotsTxtServlet provideRobotsTxtServlet(IRuntimeManager runtimeManager) { -// return new RobotsTxtServlet(runtimeManager); -// } -// -// @Provides @Singleton LogoServlet provideLogoServlet(IRuntimeManager runtimeManager) { -// return new LogoServlet(runtimeManager); -// } -// -// @Provides @Singleton EnforceAuthenticationFilter provideEnforceAuthenticationFilter( -// IRuntimeManager runtimeManager, -// IAuthenticationManager authenticationManager) { -// -// return new EnforceAuthenticationFilter(runtimeManager, authenticationManager); -// } - - - @Provides @Singleton GitblitWicketFilter provideGitblitWicketFilter(GitBlitWebApp webapp) { - return new GitblitWicketFilter(); + @Provides @Singleton IRepositoryManager provideRepositoryManager(RepositoryManager manager) { + return manager; } - @Provides GitServlet provideGitServlet(IGitblit gitblit) { - return new GitServlet(); + @Provides @Singleton IProjectManager provideProjectManager(ProjectManager manager) { + return manager; } - @Provides GitFilter provideGitFilter( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { - - return new GitFilter(); - } - - @Provides @Singleton RawServlet provideRawServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new RawServlet(); + @Provides @Singleton IFederationManager provideFederationManager(FederationManager manager) { + return manager; } - @Provides @Singleton RawFilter provideRawFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new RawFilter(); - } - - @Provides @Singleton PagesServlet providePagesServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new PagesServlet(); - } - - @Provides @Singleton PagesFilter providePagesFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new PagesFilter(); - } - - @Provides @Singleton RpcServlet provideRpcServlet(IGitblit gitblit) { - return new RpcServlet(); - } - - @Provides @Singleton RpcFilter provideRpcFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { - - return new RpcFilter(); - } - - @Provides @Singleton DownloadZipServlet provideDownloadZipServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new DownloadZipServlet(); - } - - @Provides @Singleton DownloadZipFilter provideDownloadZipFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new DownloadZipFilter(); - } - - @Provides @Singleton SyndicationServlet provideSyndicationServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { - - return new SyndicationServlet(); - } - - @Provides @Singleton SyndicationFilter provideSyndicationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager, - IProjectManager projectManager) { - - return new SyndicationFilter(); - } - - @Provides @Singleton FederationServlet provideFederationServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IRepositoryManager repositoryManager, - IFederationManager federationManager) { - - return new FederationServlet(); - } - - @Provides @Singleton SparkleShareInviteServlet provideSparkleshareInviteServlet( - IRuntimeManager runtimeManager, - IUserManager userManager, - IAuthenticationManager authenticationManager, - IRepositoryManager repositoryManager) { - - return new SparkleShareInviteServlet(); - } - - @Provides @Singleton BranchGraphServlet provideBranchGraphServlet( - IRuntimeManager runtimeManager, - IRepositoryManager repositoryManager) { - - return new BranchGraphServlet(); - } - - @Provides @Singleton RobotsTxtServlet provideRobotsTxtServlet(IRuntimeManager runtimeManager) { - return new RobotsTxtServlet(); - } - - @Provides @Singleton LogoServlet provideLogoServlet(IRuntimeManager runtimeManager) { - return new LogoServlet(); - } - - @Provides @Singleton PtServlet providePtServlet(IRuntimeManager runtimeManager) { - return new PtServlet(); - } - - @Provides @Singleton ProxyFilter provideProxyFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { - - return new ProxyFilter(); - } - - @Provides @Singleton EnforceAuthenticationFilter provideEnforceAuthenticationFilter( - IRuntimeManager runtimeManager, - IAuthenticationManager authenticationManager) { - - return new EnforceAuthenticationFilter(); + @Provides @Singleton IGitblit provideGitblit(GitBlit gitblit) { + return gitblit; } } \ No newline at end of file diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index f9d9be9f..8bfa043a 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import javax.inject.Inject; import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; @@ -70,6 +71,7 @@ public class GitBlit extends GitblitManager { private ITicketService ticketService; + @Inject public GitBlit( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java b/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java deleted file mode 100644 index c2fd4d67..00000000 --- a/src/main/java/com/gitblit/dagger/DaggerWicketFilter.java +++ /dev/null @@ -1,45 +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.dagger; - -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; - -import org.apache.wicket.protocol.http.WicketFilter; - -import dagger.ObjectGraph; - -/** - * Uses Dagger to manually inject dependencies into a Wicket filter. - * This class is useful for servlet containers that offer CDI and are - * confused by Dagger. - * - * @author James Moger - * - */ -public abstract class DaggerWicketFilter extends WicketFilter { - - @Override - public final void init(FilterConfig filterConfig) throws ServletException { - ServletContext context = filterConfig.getServletContext(); - ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); - inject(objectGraph); - super.init(filterConfig); - } - - protected abstract void inject(ObjectGraph dagger); -} diff --git a/src/main/java/com/gitblit/manager/AuthenticationManager.java b/src/main/java/com/gitblit/manager/AuthenticationManager.java index d1b1af0a..2aa2d197 100644 --- a/src/main/java/com/gitblit/manager/AuthenticationManager.java +++ b/src/main/java/com/gitblit/manager/AuthenticationManager.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -76,6 +77,7 @@ public class AuthenticationManager implements IAuthenticationManager { private final Map legacyRedirects; + @Inject public AuthenticationManager( IRuntimeManager runtimeManager, IUserManager userManager) { diff --git a/src/main/java/com/gitblit/manager/FederationManager.java b/src/main/java/com/gitblit/manager/FederationManager.java index 95d38af1..32193525 100644 --- a/src/main/java/com/gitblit/manager/FederationManager.java +++ b/src/main/java/com/gitblit/manager/FederationManager.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; @@ -70,6 +71,7 @@ public class FederationManager implements IFederationManager { private final IRepositoryManager repositoryManager; + @Inject public FederationManager( IRuntimeManager runtimeManager, INotificationManager notificationManager, diff --git a/src/main/java/com/gitblit/manager/NotificationManager.java b/src/main/java/com/gitblit/manager/NotificationManager.java index 69a611bb..63f064e5 100644 --- a/src/main/java/com/gitblit/manager/NotificationManager.java +++ b/src/main/java/com/gitblit/manager/NotificationManager.java @@ -20,6 +20,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import javax.inject.Inject; import javax.mail.Message; import org.slf4j.Logger; @@ -48,6 +49,7 @@ public class NotificationManager implements INotificationManager { private final MailService mailService; + @Inject public NotificationManager(IStoredSettings settings) { this.settings = settings; this.mailService = new MailService(settings); diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index 2ee48558..a0c873e8 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -34,6 +34,8 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import javax.inject.Inject; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,6 +83,7 @@ public class PluginManager implements IPluginManager, PluginStateListener { private int readTimeout = 12800; + @Inject public PluginManager(IRuntimeManager runtimeManager) { File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); dir.mkdirs(); diff --git a/src/main/java/com/gitblit/manager/ProjectManager.java b/src/main/java/com/gitblit/manager/ProjectManager.java index 666f5210..4da7f801 100644 --- a/src/main/java/com/gitblit/manager/ProjectManager.java +++ b/src/main/java/com/gitblit/manager/ProjectManager.java @@ -27,6 +27,8 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import javax.inject.Inject; + import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.util.FS; import org.slf4j.Logger; @@ -68,6 +70,7 @@ public class ProjectManager implements IProjectManager { private FileBasedConfig projectConfigs; + @Inject public ProjectManager( IRuntimeManager runtimeManager, IUserManager userManager, diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 0160363e..a82cd016 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -44,6 +44,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import javax.inject.Inject; + import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryCache.FileKey; @@ -127,6 +129,7 @@ public class RepositoryManager implements IRepositoryManager { private MirrorService mirrorExecutor; + @Inject public RepositoryManager( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/manager/RuntimeManager.java b/src/main/java/com/gitblit/manager/RuntimeManager.java index 9cdc64eb..00fe1c91 100644 --- a/src/main/java/com/gitblit/manager/RuntimeManager.java +++ b/src/main/java/com/gitblit/manager/RuntimeManager.java @@ -22,6 +22,8 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import javax.inject.Inject; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,6 +49,7 @@ public class RuntimeManager implements IRuntimeManager { private TimeZone timezone; + @Inject public RuntimeManager(IStoredSettings settings) { this(settings, null); } diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java index 2b82ffb6..fe21b063 100644 --- a/src/main/java/com/gitblit/manager/UserManager.java +++ b/src/main/java/com/gitblit/manager/UserManager.java @@ -24,6 +24,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.inject.Inject; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +59,7 @@ public class UserManager implements IUserManager { private IUserService userService; + @Inject public UserManager(IRuntimeManager runtimeManager, IPluginManager pluginManager) { this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; diff --git a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java index 0e6d323d..6d2efa4f 100644 --- a/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java +++ b/src/main/java/com/gitblit/servlet/AccessRestrictionFilter.java @@ -19,21 +19,19 @@ import java.io.IOException; import java.text.MessageFormat; import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * The AccessRestrictionFilter is an AuthenticationFilter that confirms that the * requested repository can be accessed by the anonymous or named user. @@ -54,11 +52,15 @@ public abstract class AccessRestrictionFilter extends AuthenticationFilter { protected IRepositoryManager repositoryManager; - @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - super.inject(dagger, filterConfig); - this.runtimeManager = dagger.get(IRuntimeManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); + protected AccessRestrictionFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + super(authenticationManager); + + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; } /** diff --git a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java index c21f8692..6f13252a 100644 --- a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java @@ -21,6 +21,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; +import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; @@ -35,14 +36,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants; -import com.gitblit.dagger.DaggerFilter; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.models.UserModel; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * The AuthenticationFilter is a servlet filter that preprocesses requests that * match its url pattern definition in the web.xml file. @@ -52,7 +50,7 @@ import dagger.ObjectGraph; * @author James Moger * */ -public abstract class AuthenticationFilter extends DaggerFilter { +public abstract class AuthenticationFilter implements Filter { protected static final String CHALLENGE = "Basic realm=\"" + Constants.NAME + "\""; @@ -62,9 +60,16 @@ public abstract class AuthenticationFilter extends DaggerFilter { protected IAuthenticationManager authenticationManager; + protected AuthenticationFilter(IAuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - this.authenticationManager = dagger.get(IAuthenticationManager.class); + public void destroy() { } /** diff --git a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java index 0abe347f..c1ad0806 100644 --- a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java +++ b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java @@ -36,7 +36,10 @@ 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; import javax.servlet.http.HttpServletResponse; @@ -52,20 +55,18 @@ import org.eclipse.jgit.revwalk.RevCommit; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * Handles requests for branch graphs * * @author James Moger * */ -public class BranchGraphServlet extends DaggerServlet { +@Singleton +public class BranchGraphServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -82,20 +83,20 @@ public class BranchGraphServlet extends DaggerServlet { private IRepositoryManager repositoryManager; - public BranchGraphServlet() { - super(); + @Inject + public BranchGraphServlet( + IStoredSettings settings, + IRepositoryManager repositoryManager) { + + this.settings = settings; + this.repositoryManager = repositoryManager; + strokeCache = new Stroke[4]; for (int i = 1; i < strokeCache.length; i++) { strokeCache[i] = new BasicStroke(i); } } - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); - } - /** * Returns an url to this servlet for the specified parameters. * diff --git a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java index 0c7b3e56..3f20c2b7 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java @@ -15,7 +15,13 @@ */ package com.gitblit.servlet; +import javax.inject.Inject; +import javax.inject.Singleton; + import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -27,8 +33,18 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class DownloadZipFilter extends AccessRestrictionFilter { + @Inject + public DownloadZipFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, authenticationManager, repositoryManager); + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java index 6a64778c..4b0e3bf5 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java @@ -20,7 +20,10 @@ 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; import org.eclipse.jgit.lib.Repository; @@ -31,15 +34,12 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; import com.gitblit.utils.CompressionUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * Streams out a zip file from the specified repository for any tree path at any * revision. @@ -47,7 +47,8 @@ import dagger.ObjectGraph; * @author James Moger * */ -public class DownloadZipServlet extends DaggerServlet { +@Singleton +public class DownloadZipServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -76,10 +77,10 @@ public class DownloadZipServlet extends DaggerServlet { } } - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); + @Inject + public DownloadZipServlet(IStoredSettings settings, IRepositoryManager repositoryManager) { + this.settings = settings; + this.repositoryManager = repositoryManager; } /** diff --git a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java index c015021d..a9e6554e 100644 --- a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java @@ -18,6 +18,9 @@ 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; import javax.servlet.ServletException; @@ -31,12 +34,9 @@ import org.slf4j.LoggerFactory; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerFilter; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.models.UserModel; -import dagger.ObjectGraph; - /** * This filter enforces authentication via HTTP Basic Authentication, if the settings indicate so. * It looks at the settings "web.authenticateViewPages" and "web.enforceHttpBasicAuthentication"; if @@ -45,7 +45,8 @@ import dagger.ObjectGraph; * @author Laurens Vrijnsen * */ -public class EnforceAuthenticationFilter extends DaggerFilter { +@Singleton +public class EnforceAuthenticationFilter implements Filter { protected transient Logger logger = LoggerFactory.getLogger(getClass()); @@ -53,10 +54,21 @@ public class EnforceAuthenticationFilter extends DaggerFilter { private IAuthenticationManager authenticationManager; + @Inject + public EnforceAuthenticationFilter( + IStoredSettings settings, + IAuthenticationManager authenticationManager) { + + this.settings = settings; + this.authenticationManager = authenticationManager; + } + @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - this.settings = dagger.get(IStoredSettings.class); - this.authenticationManager = dagger.get(IAuthenticationManager.class); + public void init(FilterConfig config) { + } + + @Override + public void destroy() { } /* @@ -87,12 +99,4 @@ public class EnforceAuthenticationFilter extends DaggerFilter { chain.doFilter(request, response); } } - - - /* - * @see javax.servlet.Filter#destroy() - */ - @Override - public void destroy() { - } } diff --git a/src/main/java/com/gitblit/servlet/FederationServlet.java b/src/main/java/com/gitblit/servlet/FederationServlet.java index 8dbf0e16..8a03f089 100644 --- a/src/main/java/com/gitblit/servlet/FederationServlet.java +++ b/src/main/java/com/gitblit/servlet/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; @@ -43,14 +45,13 @@ import com.gitblit.utils.HttpUtils; import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; -import dagger.ObjectGraph; - /** * Handles federation requests. * * @author James Moger * */ +@Singleton public class FederationServlet extends JsonServlet { private static final long serialVersionUID = 1L; @@ -63,12 +64,17 @@ public class FederationServlet extends JsonServlet { private IFederationManager federationManager; - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.userManager = dagger.get(IUserManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); - this.federationManager = dagger.get(IFederationManager.class); + @Inject + public FederationServlet( + IStoredSettings settings, + IUserManager userManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager) { + + this.settings = settings; + this.userManager = userManager; + this.repositoryManager = repositoryManager; + this.federationManager = federationManager; } /** diff --git a/src/main/java/com/gitblit/servlet/GitFilter.java b/src/main/java/com/gitblit/servlet/GitFilter.java index bb3d3216..e2c45898 100644 --- a/src/main/java/com/gitblit/servlet/GitFilter.java +++ b/src/main/java/com/gitblit/servlet/GitFilter.java @@ -17,7 +17,8 @@ package com.gitblit.servlet; import java.text.MessageFormat; -import javax.servlet.FilterConfig; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import com.gitblit.Constants.AccessRestrictionType; @@ -25,13 +26,14 @@ import com.gitblit.Constants.AuthorizationControl; import com.gitblit.GitBlitException; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * The GitFilter is an AccessRestrictionFilter which ensures that Git client * requests for push, clone, or view restricted repositories are authenticated @@ -40,6 +42,7 @@ import dagger.ObjectGraph; * @author James Moger * */ +@Singleton public class GitFilter extends AccessRestrictionFilter { protected static final String gitReceivePack = "/git-receive-pack"; @@ -53,11 +56,18 @@ public class GitFilter extends AccessRestrictionFilter { private IFederationManager federationManager; - @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - super.inject(dagger, filterConfig); - this.settings = dagger.get(IStoredSettings.class); - this.federationManager = dagger.get(IFederationManager.class); + @Inject + public GitFilter( + IStoredSettings settings, + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager, + IFederationManager federationManager) { + + super(runtimeManager, authenticationManager, repositoryManager); + + this.settings = settings; + this.federationManager = federationManager; } /** diff --git a/src/main/java/com/gitblit/servlet/GitServlet.java b/src/main/java/com/gitblit/servlet/GitServlet.java index 93fe31d0..e9c5242f 100644 --- a/src/main/java/com/gitblit/servlet/GitServlet.java +++ b/src/main/java/com/gitblit/servlet/GitServlet.java @@ -20,6 +20,8 @@ import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; import java.io.IOException; import java.util.Enumeration; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletConfig; @@ -33,14 +35,11 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jgit.http.server.GitFilter; -import com.gitblit.dagger.DaggerContext; import com.gitblit.git.GitblitReceivePackFactory; import com.gitblit.git.GitblitUploadPackFactory; import com.gitblit.git.RepositoryResolver; import com.gitblit.manager.IGitblit; -import dagger.ObjectGraph; - /** * The GitServlet provides http/https access to Git repositories. * Access to this servlet is protected by the GitFilter. @@ -48,24 +47,23 @@ import dagger.ObjectGraph; * @author James Moger * */ +@Singleton public class GitServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final GitFilter gitFilter; - public GitServlet() { + @Inject + public GitServlet(IGitblit gitblit) { gitFilter = new GitFilter(); + gitFilter.setRepositoryResolver(new RepositoryResolver(gitblit)); + gitFilter.setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); + gitFilter.setReceivePackFactory(new GitblitReceivePackFactory(gitblit)); } @Override public void init(final ServletConfig config) throws ServletException { - ServletContext context = config.getServletContext(); - ObjectGraph dagger = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); - IGitblit gitblit = dagger.get(IGitblit.class); - gitFilter.setRepositoryResolver(new RepositoryResolver(gitblit)); - gitFilter.setUploadPackFactory(new GitblitUploadPackFactory(gitblit)); - gitFilter.setReceivePackFactory(new GitblitReceivePackFactory(gitblit)); gitFilter.init(new FilterConfig() { @Override diff --git a/src/main/java/com/gitblit/servlet/JsonServlet.java b/src/main/java/com/gitblit/servlet/JsonServlet.java index 4378c8a6..abc0f292 100644 --- a/src/main/java/com/gitblit/servlet/JsonServlet.java +++ b/src/main/java/com/gitblit/servlet/JsonServlet.java @@ -21,6 +21,7 @@ import java.lang.reflect.Type; import java.text.MessageFormat; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -28,7 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.utils.JsonUtils; import com.gitblit.utils.StringUtils; @@ -38,7 +38,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ -public abstract class JsonServlet extends DaggerServlet { +public abstract class JsonServlet extends HttpServlet { private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/gitblit/servlet/LogoServlet.java b/src/main/java/com/gitblit/servlet/LogoServlet.java index 96f34afd..489e98b2 100644 --- a/src/main/java/com/gitblit/servlet/LogoServlet.java +++ b/src/main/java/com/gitblit/servlet/LogoServlet.java @@ -21,24 +21,25 @@ 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRuntimeManager; -import dagger.ObjectGraph; - /** * Handles requests for logo.png * * @author James Moger * */ -public class LogoServlet extends DaggerServlet { +@Singleton +public class LogoServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -46,9 +47,9 @@ public class LogoServlet extends DaggerServlet { private IRuntimeManager runtimeManager; - @Override - protected void inject(ObjectGraph dagger) { - this.runtimeManager = dagger.get(IRuntimeManager.class); + @Inject + public LogoServlet(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; } @Override diff --git a/src/main/java/com/gitblit/servlet/PagesFilter.java b/src/main/java/com/gitblit/servlet/PagesFilter.java index e07d9b3b..fa8c9abe 100644 --- a/src/main/java/com/gitblit/servlet/PagesFilter.java +++ b/src/main/java/com/gitblit/servlet/PagesFilter.java @@ -15,6 +15,13 @@ */ package com.gitblit.servlet; +import javax.inject.Inject; +import javax.inject.Singleton; + +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; + /** * The PagesFilter is an AccessRestrictionFilter which ensures the gh-pages @@ -23,7 +30,17 @@ package com.gitblit.servlet; * @author James Moger * */ + +@Singleton public class PagesFilter extends RawFilter { + @Inject + public PagesFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, authenticationManager, repositoryManager); + } } diff --git a/src/main/java/com/gitblit/servlet/PagesServlet.java b/src/main/java/com/gitblit/servlet/PagesServlet.java index f578f86f..4cf43024 100644 --- a/src/main/java/com/gitblit/servlet/PagesServlet.java +++ b/src/main/java/com/gitblit/servlet/PagesServlet.java @@ -15,10 +15,14 @@ */ package com.gitblit.servlet; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; /** * Serves the content of a gh-pages branch. @@ -26,6 +30,7 @@ import com.gitblit.Constants; * @author James Moger * */ +@Singleton public class PagesServlet extends RawServlet { private static final long serialVersionUID = 1L; @@ -46,6 +51,14 @@ public class PagesServlet extends RawServlet { return baseURL + Constants.PAGES + repository + "/" + (path == null ? "" : ("/" + path)); } + @Inject + public PagesServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, repositoryManager); + } + @Override protected String getBranch(String repository, HttpServletRequest request) { return "gh-pages"; diff --git a/src/main/java/com/gitblit/servlet/ProxyFilter.java b/src/main/java/com/gitblit/servlet/ProxyFilter.java index 46f59de9..778c8187 100644 --- a/src/main/java/com/gitblit/servlet/ProxyFilter.java +++ b/src/main/java/com/gitblit/servlet/ProxyFilter.java @@ -16,9 +16,13 @@ package com.gitblit.servlet; import java.io.IOException; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; @@ -27,13 +31,10 @@ import javax.servlet.ServletResponse; import ro.fortsoft.pf4j.PluginWrapper; -import com.gitblit.dagger.DaggerFilter; import com.gitblit.extensions.HttpRequestFilter; import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IRuntimeManager; -import dagger.ObjectGraph; - /** * A request filter than allows registered extension request filters to access * request data. The intended purpose is for server monitoring plugins. @@ -41,15 +42,29 @@ import dagger.ObjectGraph; * @author David Ostrovsky * @since 1.6.0 */ -public class ProxyFilter extends DaggerFilter { - private List filters; +@Singleton +public class ProxyFilter implements Filter { + private final IRuntimeManager runtimeManager; + + private final IPluginManager pluginManager; + + private final List filters; + + @Inject + public ProxyFilter( + IRuntimeManager runtimeManager, + IPluginManager pluginManager) { + + this.runtimeManager = runtimeManager; + this.pluginManager = pluginManager; + this.filters = new ArrayList<>(); + + } @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException { - IRuntimeManager runtimeManager = dagger.get(IRuntimeManager.class); - IPluginManager pluginManager = dagger.get(IPluginManager.class); + public void init(FilterConfig filterConfig) throws ServletException { - filters = pluginManager.getExtensions(HttpRequestFilter.class); + filters.addAll(pluginManager.getExtensions(HttpRequestFilter.class)); for (HttpRequestFilter f : filters) { // wrap the filter config for Gitblit settings retrieval PluginWrapper pluginWrapper = pluginManager.whichPlugin(f.getClass()); diff --git a/src/main/java/com/gitblit/servlet/PtServlet.java b/src/main/java/com/gitblit/servlet/PtServlet.java index e9cbaa5b..65d3de17 100644 --- a/src/main/java/com/gitblit/servlet/PtServlet.java +++ b/src/main/java/com/gitblit/servlet/PtServlet.java @@ -21,7 +21,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -34,11 +37,8 @@ import org.apache.commons.compress.compressors.CompressorStreamFactory; import org.apache.wicket.util.io.ByteArrayOutputStream; import org.eclipse.jgit.lib.FileMode; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRuntimeManager; -import dagger.ObjectGraph; - /** * Handles requests for the Barnum pt (patchset tool). * @@ -47,7 +47,8 @@ import dagger.ObjectGraph; * @author James Moger * */ -public class PtServlet extends DaggerServlet { +@Singleton +public class PtServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -55,9 +56,9 @@ public class PtServlet extends DaggerServlet { private IRuntimeManager runtimeManager; - @Override - protected void inject(ObjectGraph dagger) { - this.runtimeManager = dagger.get(IRuntimeManager.class); + @Inject + public PtServlet(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; } @Override diff --git a/src/main/java/com/gitblit/servlet/RawFilter.java b/src/main/java/com/gitblit/servlet/RawFilter.java index 34989c98..7dddd9b7 100644 --- a/src/main/java/com/gitblit/servlet/RawFilter.java +++ b/src/main/java/com/gitblit/servlet/RawFilter.java @@ -15,9 +15,15 @@ */ package com.gitblit.servlet; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; @@ -28,8 +34,18 @@ import com.gitblit.models.UserModel; * @author James Moger * */ +@Singleton public class RawFilter extends AccessRestrictionFilter { + @Inject + public RawFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + super(runtimeManager, authenticationManager, repositoryManager); + } + /** * Extract the repository name from the url. * diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java index 0def062a..d856ef89 100644 --- a/src/main/java/com/gitblit/servlet/RawServlet.java +++ b/src/main/java/com/gitblit/servlet/RawServlet.java @@ -28,8 +28,11 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.ServletContext; import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -48,7 +51,6 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.PathModel; @@ -57,28 +59,30 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * Serves the content of a branch. * * @author James Moger * */ -public class RawServlet extends DaggerServlet { +@Singleton +public class RawServlet extends HttpServlet { private static final long serialVersionUID = 1L; private transient Logger logger = LoggerFactory.getLogger(RawServlet.class); - private IRuntimeManager runtimeManager; + private final IRuntimeManager runtimeManager; - private IRepositoryManager repositoryManager; + private final IRepositoryManager repositoryManager; - @Override - protected void inject(ObjectGraph dagger) { - this.runtimeManager = dagger.get(IRuntimeManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); + @Inject + public RawServlet( + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager) { + + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; } /** diff --git a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java index 9bd3b3c3..e08c6bbd 100644 --- a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java +++ b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java @@ -18,32 +18,33 @@ package com.gitblit.servlet; 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; import javax.servlet.http.HttpServletResponse; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IRuntimeManager; import com.gitblit.utils.FileUtils; -import dagger.ObjectGraph; - /** * Handles requests for robots.txt * * @author James Moger * */ -public class RobotsTxtServlet extends DaggerServlet { +@Singleton +public class RobotsTxtServlet extends HttpServlet { private static final long serialVersionUID = 1L; private IRuntimeManager runtimeManager; - @Override - protected void inject(ObjectGraph dagger) { - this.runtimeManager = dagger.get(IRuntimeManager.class); + @Inject + public RobotsTxtServlet(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; } @Override diff --git a/src/main/java/com/gitblit/servlet/RpcFilter.java b/src/main/java/com/gitblit/servlet/RpcFilter.java index 23bf956e..73f9dcb3 100644 --- a/src/main/java/com/gitblit/servlet/RpcFilter.java +++ b/src/main/java/com/gitblit/servlet/RpcFilter.java @@ -18,8 +18,9 @@ package com.gitblit.servlet; import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -29,11 +30,10 @@ import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.RpcRequest; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.UserModel; -import dagger.ObjectGraph; - /** * The RpcFilter is a servlet filter that secures the RpcServlet. * @@ -47,17 +47,23 @@ import dagger.ObjectGraph; * @author James Moger * */ +@Singleton public class RpcFilter extends AuthenticationFilter { private IStoredSettings settings; private IRuntimeManager runtimeManager; - @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - super.inject(dagger, filterConfig); - this.settings = dagger.get(IStoredSettings.class); - this.runtimeManager = dagger.get(IRuntimeManager.class); + @Inject + public RpcFilter( + IStoredSettings settings, + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager) { + + super(authenticationManager); + + this.settings = settings; + this.runtimeManager = runtimeManager; } /** diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java index b8cdfb04..aaf7d5fb 100644 --- a/src/main/java/com/gitblit/servlet/RpcServlet.java +++ b/src/main/java/com/gitblit/servlet/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; @@ -47,13 +49,12 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.RpcUtils; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * Handles remote procedure calls. * * @author James Moger */ +@Singleton public class RpcServlet extends JsonServlet { private static final long serialVersionUID = 1L; @@ -64,10 +65,10 @@ public class RpcServlet extends JsonServlet { private IGitblit gitblit; - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.gitblit = dagger.get(IGitblit.class); + @Inject + public RpcServlet(IStoredSettings settings, IGitblit gitblit) { + this.settings = settings; + this.gitblit = gitblit; } /** diff --git a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java index 150dd68a..e596cfd3 100644 --- a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java +++ b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java @@ -19,13 +19,15 @@ import java.io.IOException; import java.net.URL; 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; import javax.servlet.http.HttpServletResponse; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IUserManager; @@ -33,15 +35,14 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * Handles requests for Sparkleshare Invites * * @author James Moger * */ -public class SparkleShareInviteServlet extends DaggerServlet { +@Singleton +public class SparkleShareInviteServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -53,12 +54,17 @@ public class SparkleShareInviteServlet extends DaggerServlet { private IRepositoryManager repositoryManager; - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.userManager = dagger.get(IUserManager.class); - this.authenticationManager = dagger.get(IAuthenticationManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); + @Inject + public SparkleShareInviteServlet( + IStoredSettings settings, + IUserManager userManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager) { + + this.settings = settings; + this.userManager = userManager; + this.authenticationManager = authenticationManager; + this.repositoryManager = repositoryManager; } @Override diff --git a/src/main/java/com/gitblit/servlet/SyndicationFilter.java b/src/main/java/com/gitblit/servlet/SyndicationFilter.java index 78da47e9..3b9753fa 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationFilter.java +++ b/src/main/java/com/gitblit/servlet/SyndicationFilter.java @@ -18,8 +18,9 @@ package com.gitblit.servlet; import java.io.IOException; import java.text.MessageFormat; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -27,6 +28,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.AccessRestrictionType; +import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; @@ -34,8 +36,6 @@ import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; -import dagger.ObjectGraph; - /** * The SyndicationFilter is an AuthenticationFilter which ensures that feed * requests for projects or view-restricted repositories have proper authentication @@ -44,18 +44,24 @@ import dagger.ObjectGraph; * @author James Moger * */ +@Singleton public class SyndicationFilter extends AuthenticationFilter { private IRuntimeManager runtimeManager; private IRepositoryManager repositoryManager; private IProjectManager projectManager; - @Override - protected void inject(ObjectGraph dagger, FilterConfig filterConfig) { - super.inject(dagger, filterConfig); - this.runtimeManager = dagger.get(IRuntimeManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); - this.projectManager = dagger.get(IProjectManager.class); + @Inject + public SyndicationFilter( + IRuntimeManager runtimeManager, + IAuthenticationManager authenticationManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + super(authenticationManager); + + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; } /** diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index 631df781..c48f25f9 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -22,6 +22,10 @@ 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; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; @@ -31,7 +35,6 @@ import org.slf4j.LoggerFactory; import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.Keys; -import com.gitblit.dagger.DaggerServlet; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.models.FeedEntryModel; @@ -46,8 +49,6 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; import com.gitblit.utils.SyndicationUtils; -import dagger.ObjectGraph; - /** * SyndicationServlet generates RSS 2.0 feeds and feed links. * @@ -56,7 +57,8 @@ import dagger.ObjectGraph; * @author James Moger * */ -public class SyndicationServlet extends DaggerServlet { +@Singleton +public class SyndicationServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -68,11 +70,15 @@ public class SyndicationServlet extends DaggerServlet { private IProjectManager projectManager; - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); - this.projectManager = dagger.get(IProjectManager.class); + @Inject + public SyndicationServlet( + IStoredSettings settings, + IRepositoryManager repositoryManager, + IProjectManager projectManager) { + + this.settings = settings; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; } /** diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index f63ff3d9..e5b65508 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -21,6 +21,9 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.apache.wicket.Application; import org.apache.wicket.Request; import org.apache.wicket.Response; @@ -90,6 +93,7 @@ import com.gitblit.wicket.pages.TreePage; import com.gitblit.wicket.pages.UserPage; import com.gitblit.wicket.pages.UsersPage; +@Singleton public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final Class homePageClass = MyDashboardPage.class; @@ -120,6 +124,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final IGitblit gitblit; + @Inject public GitBlitWebApp( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java index 7865fb3b..72adbf51 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java @@ -17,6 +17,8 @@ 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; @@ -28,7 +30,6 @@ import org.eclipse.jgit.revwalk.RevCommit; 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; @@ -37,8 +38,6 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; -import dagger.ObjectGraph; - /** * * Customization of the WicketFilter to allow smart browser-side caching of @@ -47,7 +46,8 @@ import dagger.ObjectGraph; * @author James Moger * */ -public class GitblitWicketFilter extends DaggerWicketFilter { +@Singleton +public class GitblitWicketFilter extends WicketFilter { private IStoredSettings settings; @@ -59,13 +59,19 @@ public class GitblitWicketFilter extends DaggerWicketFilter { private GitBlitWebApp webapp; - @Override - protected void inject(ObjectGraph dagger) { - this.settings = dagger.get(IStoredSettings.class); - this.runtimeManager = dagger.get(IRuntimeManager.class); - this.repositoryManager = dagger.get(IRepositoryManager.class); - this.projectManager = dagger.get(IProjectManager.class); - this.webapp = dagger.get(GitBlitWebApp.class); + @Inject + public GitblitWicketFilter( + IStoredSettings settings, + IRuntimeManager runtimeManager, + IRepositoryManager repositoryManager, + IProjectManager projectManager, + GitBlitWebApp webapp) { + + this.settings = settings; + this.runtimeManager = runtimeManager; + this.repositoryManager = repositoryManager; + this.projectManager = projectManager; + this.webapp = webapp; } @Override -- cgit v1.2.3 From 6720653097764f7c51fac88b26682f6fe683fc7d Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 16 May 2014 14:52:21 -0400 Subject: Move the DaggerModule to the Dagger package --- src/main/java/com/gitblit/DaggerModule.java | 185 -------------------- src/main/java/com/gitblit/dagger/DaggerModule.java | 189 +++++++++++++++++++++ .../java/com/gitblit/servlet/GitblitContext.java | 2 +- 3 files changed, 190 insertions(+), 186 deletions(-) delete mode 100644 src/main/java/com/gitblit/DaggerModule.java create mode 100644 src/main/java/com/gitblit/dagger/DaggerModule.java diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java deleted file mode 100644 index 662ca033..00000000 --- a/src/main/java/com/gitblit/DaggerModule.java +++ /dev/null @@ -1,185 +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; - -import javax.inject.Singleton; - -import com.gitblit.manager.AuthenticationManager; -import com.gitblit.manager.FederationManager; -import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IFederationManager; -import com.gitblit.manager.IGitblit; -import com.gitblit.manager.INotificationManager; -import com.gitblit.manager.IPluginManager; -import com.gitblit.manager.IProjectManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; -import com.gitblit.manager.IUserManager; -import com.gitblit.manager.NotificationManager; -import com.gitblit.manager.PluginManager; -import com.gitblit.manager.ProjectManager; -import com.gitblit.manager.RepositoryManager; -import com.gitblit.manager.RuntimeManager; -import com.gitblit.manager.UserManager; -import com.gitblit.servlet.BranchGraphServlet; -import com.gitblit.servlet.DownloadZipFilter; -import com.gitblit.servlet.DownloadZipServlet; -import com.gitblit.servlet.EnforceAuthenticationFilter; -import com.gitblit.servlet.FederationServlet; -import com.gitblit.servlet.GitFilter; -import com.gitblit.servlet.GitServlet; -import com.gitblit.servlet.LogoServlet; -import com.gitblit.servlet.PagesFilter; -import com.gitblit.servlet.PagesServlet; -import com.gitblit.servlet.ProxyFilter; -import com.gitblit.servlet.PtServlet; -import com.gitblit.servlet.RawFilter; -import com.gitblit.servlet.RawServlet; -import com.gitblit.servlet.RobotsTxtServlet; -import com.gitblit.servlet.RpcFilter; -import com.gitblit.servlet.RpcServlet; -import com.gitblit.servlet.SparkleShareInviteServlet; -import com.gitblit.servlet.SyndicationFilter; -import com.gitblit.servlet.SyndicationServlet; -import com.gitblit.transport.ssh.FileKeyManager; -import com.gitblit.transport.ssh.IPublicKeyManager; -import com.gitblit.transport.ssh.MemoryKeyManager; -import com.gitblit.transport.ssh.NullKeyManager; -import com.gitblit.utils.StringUtils; -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 = { - IStoredSettings.class, - - // core managers - IRuntimeManager.class, - IPluginManager.class, - INotificationManager.class, - IUserManager.class, - IAuthenticationManager.class, - IPublicKeyManager.class, - IRepositoryManager.class, - IProjectManager.class, - IFederationManager.class, - - // the monolithic manager - IGitblit.class, - - // the Gitblit Wicket app - GitBlitWebApp.class, - - // filters & servlets - GitServlet.class, - GitFilter.class, - RawServlet.class, - RawFilter.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, - PtServlet.class, - ProxyFilter.class, - EnforceAuthenticationFilter.class, - GitblitWicketFilter.class - } -) -public class DaggerModule { - - @Provides @Singleton IStoredSettings provideSettings() { - return new FileSettings(); - } - - @Provides @Singleton IRuntimeManager provideRuntimeManager(RuntimeManager manager) { - return manager; - } - - @Provides @Singleton IPluginManager providePluginManager(PluginManager manager) { - return manager; - } - - @Provides @Singleton INotificationManager provideNotificationManager(NotificationManager manager) { - return manager; - } - - @Provides @Singleton IUserManager provideUserManager(UserManager manager) { - return manager; - } - - @Provides @Singleton IAuthenticationManager provideAuthenticationManager(AuthenticationManager manager) { - return manager; - } - - @Provides @Singleton IPublicKeyManager providePublicKeyManager( - IStoredSettings settings, - IRuntimeManager runtimeManager) { - - String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); - if (StringUtils.isEmpty(clazz)) { - clazz = FileKeyManager.class.getName(); - } - if (FileKeyManager.class.getName().equals(clazz)) { - return new FileKeyManager(runtimeManager); - } else if (NullKeyManager.class.getName().equals(clazz)) { - return new NullKeyManager(); - } else if (MemoryKeyManager.class.getName().equals(clazz)) { - return new MemoryKeyManager(); - } else { - try { - Class mgrClass = Class.forName(clazz); - return (IPublicKeyManager) mgrClass.newInstance(); - } catch (Exception e) { - - } - return null; - } - } - - @Provides @Singleton IRepositoryManager provideRepositoryManager(RepositoryManager manager) { - return manager; - } - - @Provides @Singleton IProjectManager provideProjectManager(ProjectManager manager) { - return manager; - } - - @Provides @Singleton IFederationManager provideFederationManager(FederationManager manager) { - return manager; - } - - @Provides @Singleton IGitblit provideGitblit(GitBlit gitblit) { - return gitblit; - } -} \ No newline at end of file diff --git a/src/main/java/com/gitblit/dagger/DaggerModule.java b/src/main/java/com/gitblit/dagger/DaggerModule.java new file mode 100644 index 00000000..9f001fa7 --- /dev/null +++ b/src/main/java/com/gitblit/dagger/DaggerModule.java @@ -0,0 +1,189 @@ +/* + * 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.inject.Singleton; + +import com.gitblit.FileSettings; +import com.gitblit.GitBlit; +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.AuthenticationManager; +import com.gitblit.manager.FederationManager; +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IFederationManager; +import com.gitblit.manager.IGitblit; +import com.gitblit.manager.INotificationManager; +import com.gitblit.manager.IPluginManager; +import com.gitblit.manager.IProjectManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IUserManager; +import com.gitblit.manager.NotificationManager; +import com.gitblit.manager.PluginManager; +import com.gitblit.manager.ProjectManager; +import com.gitblit.manager.RepositoryManager; +import com.gitblit.manager.RuntimeManager; +import com.gitblit.manager.UserManager; +import com.gitblit.servlet.BranchGraphServlet; +import com.gitblit.servlet.DownloadZipFilter; +import com.gitblit.servlet.DownloadZipServlet; +import com.gitblit.servlet.EnforceAuthenticationFilter; +import com.gitblit.servlet.FederationServlet; +import com.gitblit.servlet.GitFilter; +import com.gitblit.servlet.GitServlet; +import com.gitblit.servlet.LogoServlet; +import com.gitblit.servlet.PagesFilter; +import com.gitblit.servlet.PagesServlet; +import com.gitblit.servlet.ProxyFilter; +import com.gitblit.servlet.PtServlet; +import com.gitblit.servlet.RawFilter; +import com.gitblit.servlet.RawServlet; +import com.gitblit.servlet.RobotsTxtServlet; +import com.gitblit.servlet.RpcFilter; +import com.gitblit.servlet.RpcServlet; +import com.gitblit.servlet.SparkleShareInviteServlet; +import com.gitblit.servlet.SyndicationFilter; +import com.gitblit.servlet.SyndicationServlet; +import com.gitblit.transport.ssh.FileKeyManager; +import com.gitblit.transport.ssh.IPublicKeyManager; +import com.gitblit.transport.ssh.MemoryKeyManager; +import com.gitblit.transport.ssh.NullKeyManager; +import com.gitblit.utils.StringUtils; +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 = { + IStoredSettings.class, + + // core managers + IRuntimeManager.class, + IPluginManager.class, + INotificationManager.class, + IUserManager.class, + IAuthenticationManager.class, + IPublicKeyManager.class, + IRepositoryManager.class, + IProjectManager.class, + IFederationManager.class, + + // the monolithic manager + IGitblit.class, + + // the Gitblit Wicket app + GitBlitWebApp.class, + + // filters & servlets + GitServlet.class, + GitFilter.class, + RawServlet.class, + RawFilter.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, + PtServlet.class, + ProxyFilter.class, + EnforceAuthenticationFilter.class, + GitblitWicketFilter.class + } +) +public class DaggerModule { + + @Provides @Singleton IStoredSettings provideSettings() { + return new FileSettings(); + } + + @Provides @Singleton IRuntimeManager provideRuntimeManager(RuntimeManager manager) { + return manager; + } + + @Provides @Singleton IPluginManager providePluginManager(PluginManager manager) { + return manager; + } + + @Provides @Singleton INotificationManager provideNotificationManager(NotificationManager manager) { + return manager; + } + + @Provides @Singleton IUserManager provideUserManager(UserManager manager) { + return manager; + } + + @Provides @Singleton IAuthenticationManager provideAuthenticationManager(AuthenticationManager manager) { + return manager; + } + + @Provides @Singleton IPublicKeyManager providePublicKeyManager( + IStoredSettings settings, + IRuntimeManager runtimeManager) { + + String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); + if (StringUtils.isEmpty(clazz)) { + clazz = FileKeyManager.class.getName(); + } + if (FileKeyManager.class.getName().equals(clazz)) { + return new FileKeyManager(runtimeManager); + } else if (NullKeyManager.class.getName().equals(clazz)) { + return new NullKeyManager(); + } else if (MemoryKeyManager.class.getName().equals(clazz)) { + return new MemoryKeyManager(); + } else { + try { + Class mgrClass = Class.forName(clazz); + return (IPublicKeyManager) mgrClass.newInstance(); + } catch (Exception e) { + + } + return null; + } + } + + @Provides @Singleton IRepositoryManager provideRepositoryManager(RepositoryManager manager) { + return manager; + } + + @Provides @Singleton IProjectManager provideProjectManager(ProjectManager manager) { + return manager; + } + + @Provides @Singleton IFederationManager provideFederationManager(FederationManager manager) { + return manager; + } + + @Provides @Singleton IGitblit provideGitblit(GitBlit gitblit) { + return gitblit; + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index ac547914..c3a1fadd 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -34,12 +34,12 @@ import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import com.gitblit.Constants; -import com.gitblit.DaggerModule; import com.gitblit.FileSettings; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.WebXmlSettings; import com.gitblit.dagger.DaggerContext; +import com.gitblit.dagger.DaggerModule; import com.gitblit.extensions.LifeCycleListener; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; -- cgit v1.2.3 From aa1361d04cfe09f90e7d8bece90c00dd6e4185bb Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 16 May 2014 16:16:32 -0400 Subject: Replace Dagger with Guice 4.0 beta and update Guava to 16.0.1 --- .classpath | 7 +- build.moxie | 5 +- gitblit.iml | 29 +--- src/main/java/com/gitblit/GitBlit.java | 140 +++------------ .../java/com/gitblit/dagger/DaggerContext.java | 85 --------- src/main/java/com/gitblit/dagger/DaggerFilter.java | 47 ----- src/main/java/com/gitblit/dagger/DaggerModule.java | 189 --------------------- .../java/com/gitblit/dagger/DaggerServlet.java | 44 ----- src/main/java/com/gitblit/guice/GuiceContext.java | 86 ++++++++++ src/main/java/com/gitblit/guice/GuiceModule.java | 100 +++++++++++ .../java/com/gitblit/servlet/GitblitContext.java | 30 ++-- .../com/gitblit/tickets/BranchTicketService.java | 5 + .../com/gitblit/tickets/FileTicketService.java | 5 + .../com/gitblit/tickets/NullTicketService.java | 5 + .../com/gitblit/tickets/RedisTicketService.java | 5 + 15 files changed, 259 insertions(+), 523 deletions(-) delete mode 100644 src/main/java/com/gitblit/dagger/DaggerContext.java delete mode 100644 src/main/java/com/gitblit/dagger/DaggerFilter.java delete mode 100644 src/main/java/com/gitblit/dagger/DaggerModule.java delete mode 100644 src/main/java/com/gitblit/dagger/DaggerServlet.java create mode 100644 src/main/java/com/gitblit/guice/GuiceContext.java create mode 100644 src/main/java/com/gitblit/guice/GuiceModule.java diff --git a/.classpath b/.classpath index 6d6011c4..ec9495fb 100644 --- a/.classpath +++ b/.classpath @@ -5,11 +5,10 @@ - + - - - + + diff --git a/build.moxie b/build.moxie index c5ad4423..0501b779 100644 --- a/build.moxie +++ b/build.moxie @@ -127,9 +127,8 @@ properties: { dependencies: # Dagger dependency injection library (annotation processor) -- compile 'com.squareup.dagger:dagger:1.2.1' :war apt -- compile 'com.squareup.dagger:dagger-compiler:1.2.1' :war optional apt -- compile 'com.google.guava:guava:15.0' :war +- compile 'com.google.inject:guice:4.0-beta4' :war +- compile 'com.google.guava:guava:16.0.1' :war # Standard dependencies - compile 'com.intellij:annotations:12.0' :war - compile 'log4j:log4j:1.2.17' :war :fedclient :authority diff --git a/gitblit.iml b/gitblit.iml index cd6e14a2..9a689dab 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -14,13 +14,13 @@ - + - + - + @@ -36,35 +36,24 @@ - + - + - + - + - + - - - - - - - - - - - - + diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 8bfa043a..df2f502e 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Set; import javax.inject.Inject; -import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import com.gitblit.Constants.AccessPermission; @@ -44,17 +43,13 @@ import com.gitblit.manager.ServicesManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.RepositoryUrl; import com.gitblit.models.UserModel; -import com.gitblit.tickets.BranchTicketService; -import com.gitblit.tickets.FileTicketService; import com.gitblit.tickets.ITicketService; import com.gitblit.tickets.NullTicketService; -import com.gitblit.tickets.RedisTicketService; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.StringUtils; - -import dagger.Module; -import dagger.ObjectGraph; -import dagger.Provides; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; /** * GitBlit is the aggregate manager for the Gitblit webapp. It provides all @@ -65,7 +60,7 @@ import dagger.Provides; */ public class GitBlit extends GitblitManager { - private final ObjectGraph injector; + private final Injector injector; private final ServicesManager servicesManager; @@ -93,7 +88,7 @@ public class GitBlit extends GitblitManager { projectManager, federationManager); - this.injector = ObjectGraph.create(getModules()); + this.injector = Guice.createInjector(getModules()); this.servicesManager = new ServicesManager(this); } @@ -135,8 +130,8 @@ public class GitBlit extends GitblitManager { return servicesManager.isServingSSH(); } - protected Object [] getModules() { - return new Object [] { new GitBlitModule()}; + protected AbstractModule [] getModules() { + return new AbstractModule [] { new GitBlitModule()}; } protected boolean acceptPush(Transport byTransport) { @@ -360,7 +355,7 @@ public class GitBlit extends GitblitManager { } try { Class serviceClass = (Class) Class.forName(clazz); - ticketService = injector.get(serviceClass).start(); + ticketService = injector.getInstance(serviceClass).start(); if (ticketService instanceof NullTicketService) { logger.warn("No ticket service configured."); } else if (ticketService.isReady()) { @@ -370,118 +365,31 @@ public class GitBlit extends GitblitManager { } } catch (Exception e) { logger.error("failed to create ticket service " + clazz, e); - ticketService = injector.get(NullTicketService.class).start(); + ticketService = injector.getInstance(NullTicketService.class).start(); } } /** - * A nested Dagger graph is used for constructor dependency injection of + * A nested Guice Module is used for constructor dependency injection of * complex classes. * * @author James Moger * */ - @Module( - library = true, - injects = { - IStoredSettings.class, - - // core managers - IRuntimeManager.class, - IPluginManager.class, - INotificationManager.class, - IUserManager.class, - IAuthenticationManager.class, - IRepositoryManager.class, - IProjectManager.class, - IFederationManager.class, - - // the monolithic manager - IGitblit.class, - - // ticket services - NullTicketService.class, - FileTicketService.class, - BranchTicketService.class, - RedisTicketService.class - } - ) - class GitBlitModule { - - @Provides @Singleton IStoredSettings provideSettings() { - return settings; - } - - @Provides @Singleton IRuntimeManager provideRuntimeManager() { - return runtimeManager; - } - - @Provides @Singleton IPluginManager providePluginManager() { - return pluginManager; - } - - @Provides @Singleton INotificationManager provideNotificationManager() { - return notificationManager; - } - - @Provides @Singleton IUserManager provideUserManager() { - return userManager; - } - - @Provides @Singleton IAuthenticationManager provideAuthenticationManager() { - return authenticationManager; - } - - @Provides @Singleton IRepositoryManager provideRepositoryManager() { - return repositoryManager; - } - - @Provides @Singleton IProjectManager provideProjectManager() { - return projectManager; - } - - @Provides @Singleton IFederationManager provideFederationManager() { - return federationManager; - } - - @Provides @Singleton IGitblit provideGitblit() { - return GitBlit.this; - } - - @Provides @Singleton NullTicketService provideNullTicketService() { - return new NullTicketService( - runtimeManager, - pluginManager, - notificationManager, - userManager, - repositoryManager); - } - - @Provides @Singleton FileTicketService provideFileTicketService() { - return new FileTicketService( - runtimeManager, - pluginManager, - notificationManager, - userManager, - repositoryManager); - } - - @Provides @Singleton BranchTicketService provideBranchTicketService() { - return new BranchTicketService( - runtimeManager, - pluginManager, - notificationManager, - userManager, - repositoryManager); - } - - @Provides @Singleton RedisTicketService provideRedisTicketService() { - return new RedisTicketService( - runtimeManager, - pluginManager, - notificationManager, - userManager, - repositoryManager); + class GitBlitModule extends AbstractModule { + + @Override + protected void configure() { + bind(IStoredSettings.class).toInstance(settings); + bind(IRuntimeManager.class).toInstance(runtimeManager); + bind(IPluginManager.class).toInstance(pluginManager); + bind(INotificationManager.class).toInstance(notificationManager); + bind(IUserManager.class).toInstance(userManager); + bind(IAuthenticationManager.class).toInstance(authenticationManager); + bind(IRepositoryManager.class).toInstance(repositoryManager); + bind(IProjectManager.class).toInstance(projectManager); + bind(IFederationManager.class).toInstance(federationManager); + bind(IGitblit.class).toInstance(GitBlit.this); } } } diff --git a/src/main/java/com/gitblit/dagger/DaggerContext.java b/src/main/java/com/gitblit/dagger/DaggerContext.java deleted file mode 100644 index ef570804..00000000 --- a/src/main/java/com/gitblit/dagger/DaggerContext.java +++ /dev/null @@ -1,85 +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.dagger; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.gitblit.servlet.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 DaggerContext extends InjectionContextListener { - - public static final String INJECTOR_NAME = ObjectGraph.class.getName(); - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - 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.removeAttribute(INJECTOR_NAME); - destroyContext(context); - } -} diff --git a/src/main/java/com/gitblit/dagger/DaggerFilter.java b/src/main/java/com/gitblit/dagger/DaggerFilter.java deleted file mode 100644 index 01c07a4a..00000000 --- a/src/main/java/com/gitblit/dagger/DaggerFilter.java +++ /dev/null @@ -1,47 +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.dagger; - -import javax.servlet.Filter; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; - -import dagger.ObjectGraph; - -/** - * Uses Dagger to manually inject dependencies into a servlet filter. - * This class is useful for servlet containers that offer CDI and are - * confused by Dagger. - * - * @author James Moger - * - */ -public abstract class DaggerFilter implements Filter { - - @Override - public final void init(FilterConfig filterConfig) throws ServletException { - ServletContext context = filterConfig.getServletContext(); - ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); - inject(objectGraph, filterConfig); - } - - protected abstract void inject(ObjectGraph dagger, FilterConfig filterConfig) throws ServletException; - - @Override - public void destroy() { - } -} diff --git a/src/main/java/com/gitblit/dagger/DaggerModule.java b/src/main/java/com/gitblit/dagger/DaggerModule.java deleted file mode 100644 index 9f001fa7..00000000 --- a/src/main/java/com/gitblit/dagger/DaggerModule.java +++ /dev/null @@ -1,189 +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.dagger; - -import javax.inject.Singleton; - -import com.gitblit.FileSettings; -import com.gitblit.GitBlit; -import com.gitblit.IStoredSettings; -import com.gitblit.Keys; -import com.gitblit.manager.AuthenticationManager; -import com.gitblit.manager.FederationManager; -import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IFederationManager; -import com.gitblit.manager.IGitblit; -import com.gitblit.manager.INotificationManager; -import com.gitblit.manager.IPluginManager; -import com.gitblit.manager.IProjectManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; -import com.gitblit.manager.IUserManager; -import com.gitblit.manager.NotificationManager; -import com.gitblit.manager.PluginManager; -import com.gitblit.manager.ProjectManager; -import com.gitblit.manager.RepositoryManager; -import com.gitblit.manager.RuntimeManager; -import com.gitblit.manager.UserManager; -import com.gitblit.servlet.BranchGraphServlet; -import com.gitblit.servlet.DownloadZipFilter; -import com.gitblit.servlet.DownloadZipServlet; -import com.gitblit.servlet.EnforceAuthenticationFilter; -import com.gitblit.servlet.FederationServlet; -import com.gitblit.servlet.GitFilter; -import com.gitblit.servlet.GitServlet; -import com.gitblit.servlet.LogoServlet; -import com.gitblit.servlet.PagesFilter; -import com.gitblit.servlet.PagesServlet; -import com.gitblit.servlet.ProxyFilter; -import com.gitblit.servlet.PtServlet; -import com.gitblit.servlet.RawFilter; -import com.gitblit.servlet.RawServlet; -import com.gitblit.servlet.RobotsTxtServlet; -import com.gitblit.servlet.RpcFilter; -import com.gitblit.servlet.RpcServlet; -import com.gitblit.servlet.SparkleShareInviteServlet; -import com.gitblit.servlet.SyndicationFilter; -import com.gitblit.servlet.SyndicationServlet; -import com.gitblit.transport.ssh.FileKeyManager; -import com.gitblit.transport.ssh.IPublicKeyManager; -import com.gitblit.transport.ssh.MemoryKeyManager; -import com.gitblit.transport.ssh.NullKeyManager; -import com.gitblit.utils.StringUtils; -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 = { - IStoredSettings.class, - - // core managers - IRuntimeManager.class, - IPluginManager.class, - INotificationManager.class, - IUserManager.class, - IAuthenticationManager.class, - IPublicKeyManager.class, - IRepositoryManager.class, - IProjectManager.class, - IFederationManager.class, - - // the monolithic manager - IGitblit.class, - - // the Gitblit Wicket app - GitBlitWebApp.class, - - // filters & servlets - GitServlet.class, - GitFilter.class, - RawServlet.class, - RawFilter.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, - PtServlet.class, - ProxyFilter.class, - EnforceAuthenticationFilter.class, - GitblitWicketFilter.class - } -) -public class DaggerModule { - - @Provides @Singleton IStoredSettings provideSettings() { - return new FileSettings(); - } - - @Provides @Singleton IRuntimeManager provideRuntimeManager(RuntimeManager manager) { - return manager; - } - - @Provides @Singleton IPluginManager providePluginManager(PluginManager manager) { - return manager; - } - - @Provides @Singleton INotificationManager provideNotificationManager(NotificationManager manager) { - return manager; - } - - @Provides @Singleton IUserManager provideUserManager(UserManager manager) { - return manager; - } - - @Provides @Singleton IAuthenticationManager provideAuthenticationManager(AuthenticationManager manager) { - return manager; - } - - @Provides @Singleton IPublicKeyManager providePublicKeyManager( - IStoredSettings settings, - IRuntimeManager runtimeManager) { - - String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); - if (StringUtils.isEmpty(clazz)) { - clazz = FileKeyManager.class.getName(); - } - if (FileKeyManager.class.getName().equals(clazz)) { - return new FileKeyManager(runtimeManager); - } else if (NullKeyManager.class.getName().equals(clazz)) { - return new NullKeyManager(); - } else if (MemoryKeyManager.class.getName().equals(clazz)) { - return new MemoryKeyManager(); - } else { - try { - Class mgrClass = Class.forName(clazz); - return (IPublicKeyManager) mgrClass.newInstance(); - } catch (Exception e) { - - } - return null; - } - } - - @Provides @Singleton IRepositoryManager provideRepositoryManager(RepositoryManager manager) { - return manager; - } - - @Provides @Singleton IProjectManager provideProjectManager(ProjectManager manager) { - return manager; - } - - @Provides @Singleton IFederationManager provideFederationManager(FederationManager manager) { - return manager; - } - - @Provides @Singleton IGitblit provideGitblit(GitBlit gitblit) { - return gitblit; - } -} \ No newline at end of file diff --git a/src/main/java/com/gitblit/dagger/DaggerServlet.java b/src/main/java/com/gitblit/dagger/DaggerServlet.java deleted file mode 100644 index 88331a43..00000000 --- a/src/main/java/com/gitblit/dagger/DaggerServlet.java +++ /dev/null @@ -1,44 +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.dagger; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; - -import dagger.ObjectGraph; - -/** - * Uses Dagger to manually inject dependencies into a servlet. - * This class is useful for servlet containers that offer CDI and are - * confused by Dagger. - * - * @author James Moger - * - */ -public abstract class DaggerServlet extends HttpServlet { - - private static final long serialVersionUID = 1L; - - @Override - public final void init() throws ServletException { - ServletContext context = getServletContext(); - ObjectGraph objectGraph = (ObjectGraph) context.getAttribute(DaggerContext.INJECTOR_NAME); - inject(objectGraph); - } - - protected abstract void inject(ObjectGraph dagger); -} diff --git a/src/main/java/com/gitblit/guice/GuiceContext.java b/src/main/java/com/gitblit/guice/GuiceContext.java new file mode 100644 index 00000000..4361b7bd --- /dev/null +++ b/src/main/java/com/gitblit/guice/GuiceContext.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 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.guice; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.gitblit.servlet.InjectionContextListener; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Guice servlet context listener is a context listener that uses Guice to + * instantiate and inject servlets, filters, and anything else you might want. + * + * @author James Moger + * + */ +public abstract class GuiceContext extends InjectionContextListener { + + public static final String INJECTOR_NAME = Injector.class.getName(); + + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + protected abstract AbstractModule [] getModules(); + + protected abstract void destroyContext(ServletContext context); + + protected Injector getInjector(ServletContext context) { + Object o = context.getAttribute(INJECTOR_NAME); + if (o == null) { + logger.debug("instantiating Guice modules"); + AbstractModule [] modules = getModules(); + logger.debug("getting Guice injector"); + try { + o = Guice.createInjector(modules); + logger.debug("setting Guice injector into {} attribute", INJECTOR_NAME); + context.setAttribute(INJECTOR_NAME, o); + } catch (Throwable t) { + logger.error("an error occurred creating the Guice injector", t); + } + } + return (Injector) o; + } + + /** + * Instantiates an object. + * + * @param clazz + * @return the object + */ + @Override + protected X instantiate(ServletContext context, Class clazz) { + try { + Injector injector = getInjector(context); + return injector.getInstance(clazz); + } catch (Throwable t) { + logger.error(null, t); + } + return null; + } + + @Override + public final void contextDestroyed(ServletContextEvent contextEvent) { + ServletContext context = contextEvent.getServletContext(); + context.removeAttribute(INJECTOR_NAME); + destroyContext(context); + } +} diff --git a/src/main/java/com/gitblit/guice/GuiceModule.java b/src/main/java/com/gitblit/guice/GuiceModule.java new file mode 100644 index 00000000..254e0680 --- /dev/null +++ b/src/main/java/com/gitblit/guice/GuiceModule.java @@ -0,0 +1,100 @@ +/* + * Copyright 2014 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.guice; + +import javax.inject.Singleton; + +import com.gitblit.FileSettings; +import com.gitblit.GitBlit; +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.AuthenticationManager; +import com.gitblit.manager.FederationManager; +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IFederationManager; +import com.gitblit.manager.IGitblit; +import com.gitblit.manager.INotificationManager; +import com.gitblit.manager.IPluginManager; +import com.gitblit.manager.IProjectManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IUserManager; +import com.gitblit.manager.NotificationManager; +import com.gitblit.manager.PluginManager; +import com.gitblit.manager.ProjectManager; +import com.gitblit.manager.RepositoryManager; +import com.gitblit.manager.RuntimeManager; +import com.gitblit.manager.UserManager; +import com.gitblit.transport.ssh.FileKeyManager; +import com.gitblit.transport.ssh.IPublicKeyManager; +import com.gitblit.transport.ssh.MemoryKeyManager; +import com.gitblit.transport.ssh.NullKeyManager; +import com.gitblit.utils.StringUtils; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; + +/** + * GuiceModule references all injectable objects. + * + * @author James Moger + * + */ +public class GuiceModule extends AbstractModule { + + @Override + protected void configure() { + + bind(IStoredSettings.class).toInstance(new FileSettings()); + + // core managers + bind(IRuntimeManager.class).to(RuntimeManager.class).in(Singleton.class); + bind(IPluginManager.class).to(PluginManager.class).in(Singleton.class); + bind(INotificationManager.class).to(NotificationManager.class).in(Singleton.class); + bind(IUserManager.class).to(UserManager.class).in(Singleton.class); + bind(IAuthenticationManager.class).to(AuthenticationManager.class).in(Singleton.class); + bind(IRepositoryManager.class).to(RepositoryManager.class).in(Singleton.class); + bind(IProjectManager.class).to(ProjectManager.class).in(Singleton.class); + bind(IFederationManager.class).to(FederationManager.class).in(Singleton.class); + + // the monolithic manager + bind(IGitblit.class).to(GitBlit.class).in(Singleton.class); + } + + @Provides + @Singleton + IPublicKeyManager providePublicKeyManager(IStoredSettings settings, IRuntimeManager runtimeManager) { + + String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); + if (StringUtils.isEmpty(clazz)) { + clazz = FileKeyManager.class.getName(); + } + if (FileKeyManager.class.getName().equals(clazz)) { + return new FileKeyManager(runtimeManager); + } else if (NullKeyManager.class.getName().equals(clazz)) { + return new NullKeyManager(); + } else if (MemoryKeyManager.class.getName().equals(clazz)) { + return new MemoryKeyManager(); + } else { + try { + Class mgrClass = Class.forName(clazz); + return (IPublicKeyManager) mgrClass.newInstance(); + } catch (Exception e) { + + } + return null; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index c3a1fadd..fb16d32d 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -38,9 +38,9 @@ import com.gitblit.FileSettings; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.WebXmlSettings; -import com.gitblit.dagger.DaggerContext; -import com.gitblit.dagger.DaggerModule; import com.gitblit.extensions.LifeCycleListener; +import com.gitblit.guice.GuiceContext; +import com.gitblit.guice.GuiceModule; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; import com.gitblit.manager.IGitblit; @@ -55,8 +55,8 @@ import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.ContainerUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitblitWicketFilter; - -import dagger.ObjectGraph; +import com.google.inject.AbstractModule; +import com.google.inject.Injector; /** * This class is the main entry point for the entire webapp. It is a singleton @@ -70,7 +70,7 @@ import dagger.ObjectGraph; * */ @WebListener -public class GitblitContext extends DaggerContext { +public class GitblitContext extends GuiceContext { private static GitblitContext gitblit; @@ -116,11 +116,11 @@ public class GitblitContext extends DaggerContext { } /** - * Returns Gitblit's Dagger injection modules. + * Returns Gitblit's Guice injection modules. */ @Override - protected Object [] getModules() { - return new Object [] { new DaggerModule() }; + protected AbstractModule [] getModules() { + return new AbstractModule [] { new GuiceModule() }; } /** @@ -128,10 +128,10 @@ public class GitblitContext extends DaggerContext { */ @Override protected void beforeServletInjection(ServletContext context) { - ObjectGraph injector = getInjector(context); + Injector injector = getInjector(context); // create the runtime settings object - IStoredSettings runtimeSettings = injector.get(IStoredSettings.class); + IStoredSettings runtimeSettings = injector.getInstance(IStoredSettings.class); final File baseFolder; if (goSettings != null) { @@ -161,7 +161,7 @@ public class GitblitContext extends DaggerContext { // Manually configure IRuntimeManager logManager(IRuntimeManager.class); - IRuntimeManager runtime = injector.get(IRuntimeManager.class); + IRuntimeManager runtime = injector.getInstance(IRuntimeManager.class); runtime.setBaseFolder(baseFolder); runtime.getStatus().isGO = goSettings != null; runtime.getStatus().servletContainer = context.getServerInfo(); @@ -189,7 +189,7 @@ public class GitblitContext extends DaggerContext { logger.info("All managers started."); logger.info(""); - IPluginManager pluginManager = injector.get(IPluginManager.class); + IPluginManager pluginManager = injector.getInstance(IPluginManager.class); for (LifeCycleListener listener : pluginManager.getExtensions(LifeCycleListener.class)) { try { listener.onStartup(); @@ -211,12 +211,12 @@ public class GitblitContext extends DaggerContext { return null; } - protected X loadManager(ObjectGraph injector, Class clazz) { - X x = injector.get(clazz); + protected X loadManager(Injector injector, Class clazz) { + X x = injector.getInstance(clazz); return x; } - protected X startManager(ObjectGraph injector, Class clazz) { + protected X startManager(Injector injector, Class clazz) { X x = loadManager(injector, clazz); logManager(clazz); x.start(); diff --git a/src/main/java/com/gitblit/tickets/BranchTicketService.java b/src/main/java/com/gitblit/tickets/BranchTicketService.java index 8c000550..a555ece3 100644 --- a/src/main/java/com/gitblit/tickets/BranchTicketService.java +++ b/src/main/java/com/gitblit/tickets/BranchTicketService.java @@ -30,6 +30,9 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.dircache.DirCache; @@ -81,6 +84,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class BranchTicketService extends ITicketService implements RefsChangedListener { public static final String BRANCH = "refs/meta/gitblit/tickets"; @@ -91,6 +95,7 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi private final Map lastAssignedId; + @Inject public BranchTicketService( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java index b3d8838e..c10d7855 100644 --- a/src/main/java/com/gitblit/tickets/FileTicketService.java +++ b/src/main/java/com/gitblit/tickets/FileTicketService.java @@ -27,6 +27,9 @@ import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants; @@ -51,6 +54,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class FileTicketService extends ITicketService { private static final String JOURNAL = "journal.json"; @@ -59,6 +63,7 @@ public class FileTicketService extends ITicketService { private final Map lastAssignedId; + @Inject public FileTicketService( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/tickets/NullTicketService.java b/src/main/java/com/gitblit/tickets/NullTicketService.java index d410cdd0..b8882788 100644 --- a/src/main/java/com/gitblit/tickets/NullTicketService.java +++ b/src/main/java/com/gitblit/tickets/NullTicketService.java @@ -19,6 +19,9 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import javax.inject.Inject; +import javax.inject.Singleton; + import com.gitblit.manager.INotificationManager; import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IRepositoryManager; @@ -35,8 +38,10 @@ import com.gitblit.models.TicketModel.Change; * @author James Moger * */ +@Singleton public class NullTicketService extends ITicketService { + @Inject public NullTicketService( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/tickets/RedisTicketService.java b/src/main/java/com/gitblit/tickets/RedisTicketService.java index d773b0bd..6b8817b6 100644 --- a/src/main/java/com/gitblit/tickets/RedisTicketService.java +++ b/src/main/java/com/gitblit/tickets/RedisTicketService.java @@ -22,6 +22,9 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; +import javax.inject.Inject; +import javax.inject.Singleton; + import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.Client; @@ -53,6 +56,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class RedisTicketService extends ITicketService { private final JedisPool pool; @@ -61,6 +65,7 @@ public class RedisTicketService extends ITicketService { journal, ticket, counter } + @Inject public RedisTicketService( IRuntimeManager runtimeManager, IPluginManager pluginManager, -- cgit v1.2.3 From c828cf2db88956094a31a79741145688876879df Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 23 May 2014 08:17:11 -0400 Subject: Use Guice-Servlet rather than custom code and expose the Injector This is a fairly functional variation of Gitblit with one notable exception: The security filters are not working properly. This is a design flaw in Guice that I have reported upstream [1]. The general idea is that Guice-Servlet filters are not properly wrapping the ServletRequest. This has historically been a problem for Guice-Servlet servlets but Google has fixed most of those issues. Unfortunately, all the same flaws reported against the servlet delegation also exist in Guice-Servlet filter delegation. :( [1]: https://code.google.com/p/google-guice/issues/detail?id=807 --- .classpath | 1 + build.moxie | 4 +- gitblit.iml | 11 + src/main/java/WEB-INF/web.xml | 18 +- src/main/java/com/gitblit/guice/CoreModule.java | 100 +++++++++ src/main/java/com/gitblit/guice/GuiceContext.java | 86 -------- src/main/java/com/gitblit/guice/GuiceModule.java | 100 --------- src/main/java/com/gitblit/guice/WebModule.java | 99 +++++++++ .../java/com/gitblit/manager/GitblitManager.java | 6 + .../java/com/gitblit/manager/IRuntimeManager.java | 3 + .../java/com/gitblit/manager/RuntimeManager.java | 9 + .../com/gitblit/servlet/AuthenticationFilter.java | 8 +- .../java/com/gitblit/servlet/GitblitContext.java | 81 ++++--- .../gitblit/servlet/InjectionContextListener.java | 241 --------------------- .../com/gitblit/tests/mock/MockRuntimeManager.java | 6 + 15 files changed, 297 insertions(+), 476 deletions(-) create mode 100644 src/main/java/com/gitblit/guice/CoreModule.java delete mode 100644 src/main/java/com/gitblit/guice/GuiceContext.java delete mode 100644 src/main/java/com/gitblit/guice/GuiceModule.java create mode 100644 src/main/java/com/gitblit/guice/WebModule.java delete mode 100644 src/main/java/com/gitblit/servlet/InjectionContextListener.java diff --git a/.classpath b/.classpath index ec9495fb..9c3e7b8c 100644 --- a/.classpath +++ b/.classpath @@ -9,6 +9,7 @@ + diff --git a/build.moxie b/build.moxie index 0501b779..b25a4222 100644 --- a/build.moxie +++ b/build.moxie @@ -112,6 +112,7 @@ properties: { wikitext.version : 1.4 sshd.version: 0.11.1-atlassian-1 mina.version: 2.0.7 + guice.version : 4.0-beta4 } # Dependencies @@ -127,7 +128,8 @@ properties: { dependencies: # Dagger dependency injection library (annotation processor) -- compile 'com.google.inject:guice:4.0-beta4' :war +- compile 'com.google.inject:guice:${guice.version}' :war +- compile 'com.google.inject.extensions:guice-servlet:${guice.version}' :war - compile 'com.google.guava:guava:16.0.1' :war # Standard dependencies - compile 'com.intellij:annotations:12.0' :war diff --git a/gitblit.iml b/gitblit.iml index 9a689dab..f8a8a674 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -57,6 +57,17 @@ + + + + + + + + + + + diff --git a/src/main/java/WEB-INF/web.xml b/src/main/java/WEB-INF/web.xml index 2a570e32..88422249 100644 --- a/src/main/java/WEB-INF/web.xml +++ b/src/main/java/WEB-INF/web.xml @@ -1,7 +1,7 @@ - + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> Gitblit - @gb.version@ + + com.gitblit.servlet.GitblitContext + + + + guiceFilter + com.google.inject.servlet.GuiceFilter + + + + guiceFilter + /* + + \ No newline at end of file diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java new file mode 100644 index 00000000..9b479c09 --- /dev/null +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -0,0 +1,100 @@ +/* + * Copyright 2014 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.guice; + +import javax.inject.Singleton; + +import com.gitblit.FileSettings; +import com.gitblit.GitBlit; +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.AuthenticationManager; +import com.gitblit.manager.FederationManager; +import com.gitblit.manager.IAuthenticationManager; +import com.gitblit.manager.IFederationManager; +import com.gitblit.manager.IGitblit; +import com.gitblit.manager.INotificationManager; +import com.gitblit.manager.IPluginManager; +import com.gitblit.manager.IProjectManager; +import com.gitblit.manager.IRepositoryManager; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IUserManager; +import com.gitblit.manager.NotificationManager; +import com.gitblit.manager.PluginManager; +import com.gitblit.manager.ProjectManager; +import com.gitblit.manager.RepositoryManager; +import com.gitblit.manager.RuntimeManager; +import com.gitblit.manager.UserManager; +import com.gitblit.transport.ssh.FileKeyManager; +import com.gitblit.transport.ssh.IPublicKeyManager; +import com.gitblit.transport.ssh.MemoryKeyManager; +import com.gitblit.transport.ssh.NullKeyManager; +import com.gitblit.utils.StringUtils; +import com.google.inject.AbstractModule; +import com.google.inject.Provides; + +/** + * CoreModule references all the core business objects. + * + * @author James Moger + * + */ +public class CoreModule extends AbstractModule { + + @Override + protected void configure() { + + bind(IStoredSettings.class).toInstance(new FileSettings()); + + // core managers + bind(IRuntimeManager.class).to(RuntimeManager.class).in(Singleton.class); + bind(IPluginManager.class).to(PluginManager.class).in(Singleton.class); + bind(INotificationManager.class).to(NotificationManager.class).in(Singleton.class); + bind(IUserManager.class).to(UserManager.class).in(Singleton.class); + bind(IAuthenticationManager.class).to(AuthenticationManager.class).in(Singleton.class); + bind(IRepositoryManager.class).to(RepositoryManager.class).in(Singleton.class); + bind(IProjectManager.class).to(ProjectManager.class).in(Singleton.class); + bind(IFederationManager.class).to(FederationManager.class).in(Singleton.class); + + // the monolithic manager + bind(IGitblit.class).to(GitBlit.class).in(Singleton.class); + } + + @Provides + @Singleton + IPublicKeyManager providePublicKeyManager(IStoredSettings settings, IRuntimeManager runtimeManager) { + + String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); + if (StringUtils.isEmpty(clazz)) { + clazz = FileKeyManager.class.getName(); + } + if (FileKeyManager.class.getName().equals(clazz)) { + return new FileKeyManager(runtimeManager); + } else if (NullKeyManager.class.getName().equals(clazz)) { + return new NullKeyManager(); + } else if (MemoryKeyManager.class.getName().equals(clazz)) { + return new MemoryKeyManager(); + } else { + try { + Class mgrClass = Class.forName(clazz); + return (IPublicKeyManager) mgrClass.newInstance(); + } catch (Exception e) { + + } + return null; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/guice/GuiceContext.java b/src/main/java/com/gitblit/guice/GuiceContext.java deleted file mode 100644 index 4361b7bd..00000000 --- a/src/main/java/com/gitblit/guice/GuiceContext.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2014 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.guice; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.gitblit.servlet.InjectionContextListener; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; - -/** - * Guice servlet context listener is a context listener that uses Guice to - * instantiate and inject servlets, filters, and anything else you might want. - * - * @author James Moger - * - */ -public abstract class GuiceContext extends InjectionContextListener { - - public static final String INJECTOR_NAME = Injector.class.getName(); - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - protected abstract AbstractModule [] getModules(); - - protected abstract void destroyContext(ServletContext context); - - protected Injector getInjector(ServletContext context) { - Object o = context.getAttribute(INJECTOR_NAME); - if (o == null) { - logger.debug("instantiating Guice modules"); - AbstractModule [] modules = getModules(); - logger.debug("getting Guice injector"); - try { - o = Guice.createInjector(modules); - logger.debug("setting Guice injector into {} attribute", INJECTOR_NAME); - context.setAttribute(INJECTOR_NAME, o); - } catch (Throwable t) { - logger.error("an error occurred creating the Guice injector", t); - } - } - return (Injector) o; - } - - /** - * Instantiates an object. - * - * @param clazz - * @return the object - */ - @Override - protected X instantiate(ServletContext context, Class clazz) { - try { - Injector injector = getInjector(context); - return injector.getInstance(clazz); - } catch (Throwable t) { - logger.error(null, t); - } - return null; - } - - @Override - public final void contextDestroyed(ServletContextEvent contextEvent) { - ServletContext context = contextEvent.getServletContext(); - context.removeAttribute(INJECTOR_NAME); - destroyContext(context); - } -} diff --git a/src/main/java/com/gitblit/guice/GuiceModule.java b/src/main/java/com/gitblit/guice/GuiceModule.java deleted file mode 100644 index 254e0680..00000000 --- a/src/main/java/com/gitblit/guice/GuiceModule.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2014 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.guice; - -import javax.inject.Singleton; - -import com.gitblit.FileSettings; -import com.gitblit.GitBlit; -import com.gitblit.IStoredSettings; -import com.gitblit.Keys; -import com.gitblit.manager.AuthenticationManager; -import com.gitblit.manager.FederationManager; -import com.gitblit.manager.IAuthenticationManager; -import com.gitblit.manager.IFederationManager; -import com.gitblit.manager.IGitblit; -import com.gitblit.manager.INotificationManager; -import com.gitblit.manager.IPluginManager; -import com.gitblit.manager.IProjectManager; -import com.gitblit.manager.IRepositoryManager; -import com.gitblit.manager.IRuntimeManager; -import com.gitblit.manager.IUserManager; -import com.gitblit.manager.NotificationManager; -import com.gitblit.manager.PluginManager; -import com.gitblit.manager.ProjectManager; -import com.gitblit.manager.RepositoryManager; -import com.gitblit.manager.RuntimeManager; -import com.gitblit.manager.UserManager; -import com.gitblit.transport.ssh.FileKeyManager; -import com.gitblit.transport.ssh.IPublicKeyManager; -import com.gitblit.transport.ssh.MemoryKeyManager; -import com.gitblit.transport.ssh.NullKeyManager; -import com.gitblit.utils.StringUtils; -import com.google.inject.AbstractModule; -import com.google.inject.Provides; - -/** - * GuiceModule references all injectable objects. - * - * @author James Moger - * - */ -public class GuiceModule extends AbstractModule { - - @Override - protected void configure() { - - bind(IStoredSettings.class).toInstance(new FileSettings()); - - // core managers - bind(IRuntimeManager.class).to(RuntimeManager.class).in(Singleton.class); - bind(IPluginManager.class).to(PluginManager.class).in(Singleton.class); - bind(INotificationManager.class).to(NotificationManager.class).in(Singleton.class); - bind(IUserManager.class).to(UserManager.class).in(Singleton.class); - bind(IAuthenticationManager.class).to(AuthenticationManager.class).in(Singleton.class); - bind(IRepositoryManager.class).to(RepositoryManager.class).in(Singleton.class); - bind(IProjectManager.class).to(ProjectManager.class).in(Singleton.class); - bind(IFederationManager.class).to(FederationManager.class).in(Singleton.class); - - // the monolithic manager - bind(IGitblit.class).to(GitBlit.class).in(Singleton.class); - } - - @Provides - @Singleton - IPublicKeyManager providePublicKeyManager(IStoredSettings settings, IRuntimeManager runtimeManager) { - - String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); - if (StringUtils.isEmpty(clazz)) { - clazz = FileKeyManager.class.getName(); - } - if (FileKeyManager.class.getName().equals(clazz)) { - return new FileKeyManager(runtimeManager); - } else if (NullKeyManager.class.getName().equals(clazz)) { - return new NullKeyManager(); - } else if (MemoryKeyManager.class.getName().equals(clazz)) { - return new MemoryKeyManager(); - } else { - try { - Class mgrClass = Class.forName(clazz); - return (IPublicKeyManager) mgrClass.newInstance(); - } catch (Exception e) { - - } - return null; - } - } -} \ No newline at end of file diff --git a/src/main/java/com/gitblit/guice/WebModule.java b/src/main/java/com/gitblit/guice/WebModule.java new file mode 100644 index 00000000..b4f9899f --- /dev/null +++ b/src/main/java/com/gitblit/guice/WebModule.java @@ -0,0 +1,99 @@ +/* + * Copyright 2014 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.guice; + +import java.util.HashMap; +import java.util.Map; + +import com.gitblit.Constants; +import com.gitblit.servlet.BranchGraphServlet; +import com.gitblit.servlet.DownloadZipServlet; +import com.gitblit.servlet.EnforceAuthenticationFilter; +import com.gitblit.servlet.FederationServlet; +import com.gitblit.servlet.GitServlet; +import com.gitblit.servlet.LogoServlet; +import com.gitblit.servlet.PagesServlet; +import com.gitblit.servlet.ProxyFilter; +import com.gitblit.servlet.PtServlet; +import com.gitblit.servlet.RawServlet; +import com.gitblit.servlet.RobotsTxtServlet; +import com.gitblit.servlet.RpcServlet; +import com.gitblit.servlet.SparkleShareInviteServlet; +import com.gitblit.servlet.SyndicationServlet; +import com.gitblit.wicket.GitblitWicketFilter; +import com.google.common.base.Joiner; +import com.google.inject.servlet.ServletModule; + +/** + * Defines all the web servlets & filters. + * + * @author James Moger + * + */ +public class WebModule extends ServletModule { + + final static String ALL = "/*"; + + @Override + protected void configureServlets() { + // servlets + serve(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).with(GitServlet.class); + serve(fuzzy(Constants.RAW_PATH)).with(RawServlet.class); + serve(fuzzy(Constants.PAGES)).with(PagesServlet.class); + serve(fuzzy(Constants.RPC_PATH)).with(RpcServlet.class); + serve(fuzzy(Constants.ZIP_PATH)).with(DownloadZipServlet.class); + serve(fuzzy(Constants.SYNDICATION_PATH)).with(SyndicationServlet.class); + + serve(fuzzy(Constants.FEDERATION_PATH)).with(FederationServlet.class); + serve(fuzzy(Constants.SPARKLESHARE_INVITE_PATH)).with(SparkleShareInviteServlet.class); + serve(fuzzy(Constants.BRANCH_GRAPH_PATH)).with(BranchGraphServlet.class); + serve(Constants.PT_PATH).with(PtServlet.class); + serve("/robots.txt").with(RobotsTxtServlet.class); + serve("/logo.png").with(LogoServlet.class); + + // global filters + filter(ALL).through(ProxyFilter.class); + filter(ALL).through(EnforceAuthenticationFilter.class); + + // security filters +// filter(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).through(GitFilter.class); +// filter(fuzzy(Constants.RAW_PATH)).through(RawFilter.class); +// filter(fuzzy(Constants.PAGES)).through(PagesFilter.class); +// filter(fuzzy(Constants.RPC_PATH)).through(RpcFilter.class); +// filter(fuzzy(Constants.ZIP_PATH)).through(DownloadZipFilter.class); +// filter(fuzzy(Constants.SYNDICATION_PATH)).through(SyndicationFilter.class); + + // Wicket + String toIgnore = Joiner.on(",").join(Constants.R_PATH, Constants.GIT_PATH, Constants.RAW_PATH, + Constants.PAGES, Constants.RPC_PATH, Constants.ZIP_PATH, Constants.SYNDICATION_PATH, + Constants.FEDERATION_PATH, Constants.SPARKLESHARE_INVITE_PATH, Constants.BRANCH_GRAPH_PATH, + Constants.PT_PATH, "/robots.txt", "/logo.png"); + + Map params = new HashMap(); + params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, ALL); + params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore); + filter(ALL).through(GitblitWicketFilter.class, params); + } + + private String fuzzy(String path) { + if (path.endsWith(ALL)) { + return path; + } else if (path.endsWith("/")) { + return path + "*"; + } + return path + ALL; + } +} diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 98ad33e7..a0718f77 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -87,6 +87,7 @@ import com.google.gson.Gson; import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; +import com.google.inject.Injector; /** * GitblitManager is an aggregate interface delegate. It implements all the manager @@ -656,6 +657,11 @@ public class GitblitManager implements IGitblit { return runtimeManager.getStatus(); } + @Override + public Injector getInjector() { + return runtimeManager.getInjector(); + } + /* * NOTIFICATION MANAGER */ diff --git a/src/main/java/com/gitblit/manager/IRuntimeManager.java b/src/main/java/com/gitblit/manager/IRuntimeManager.java index b2d7a2b3..f736a057 100644 --- a/src/main/java/com/gitblit/manager/IRuntimeManager.java +++ b/src/main/java/com/gitblit/manager/IRuntimeManager.java @@ -24,9 +24,12 @@ import java.util.TimeZone; import com.gitblit.IStoredSettings; import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; +import com.google.inject.Injector; public interface IRuntimeManager extends IManager { + Injector getInjector(); + void setBaseFolder(File folder); File getBaseFolder(); diff --git a/src/main/java/com/gitblit/manager/RuntimeManager.java b/src/main/java/com/gitblit/manager/RuntimeManager.java index 00fe1c91..f2520085 100644 --- a/src/main/java/com/gitblit/manager/RuntimeManager.java +++ b/src/main/java/com/gitblit/manager/RuntimeManager.java @@ -34,6 +34,7 @@ import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; import com.gitblit.models.SettingModel; import com.gitblit.utils.StringUtils; +import com.google.inject.Injector; public class RuntimeManager implements IRuntimeManager { @@ -49,6 +50,9 @@ public class RuntimeManager implements IRuntimeManager { private TimeZone timezone; + @Inject + private Injector injector; + @Inject public RuntimeManager(IStoredSettings settings) { this(settings, null); @@ -77,6 +81,11 @@ public class RuntimeManager implements IRuntimeManager { return this; } + @Override + public Injector getInjector() { + return injector; + } + @Override public File getBaseFolder() { return baseFolder; diff --git a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java index 6f13252a..35a62365 100644 --- a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java @@ -99,8 +99,12 @@ public abstract class AuthenticationFilter implements Filter { * @return url */ protected String getFullUrl(HttpServletRequest httpRequest) { - String servletUrl = httpRequest.getContextPath() + httpRequest.getServletPath(); - String url = httpRequest.getRequestURI().substring(servletUrl.length()); + String contextPath = httpRequest.getContextPath(); + String servletPath = httpRequest.getServletPath(); + String pathInfo = httpRequest.getPathInfo(); + String servletUrl = contextPath + servletPath; + String requestURI = httpRequest.getRequestURI(); + String url = requestURI.substring(servletUrl.length()); String params = httpRequest.getQueryString(); if (url.length() > 0 && url.charAt(0) == '/') { url = url.substring(1); diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index fb16d32d..86b2fb3a 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -23,24 +23,26 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.gitblit.Constants; import com.gitblit.FileSettings; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.WebXmlSettings; import com.gitblit.extensions.LifeCycleListener; -import com.gitblit.guice.GuiceContext; -import com.gitblit.guice.GuiceModule; +import com.gitblit.guice.CoreModule; +import com.gitblit.guice.WebModule; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; import com.gitblit.manager.IGitblit; @@ -54,9 +56,10 @@ import com.gitblit.manager.IUserManager; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.ContainerUtils; import com.gitblit.utils.StringUtils; -import com.gitblit.wicket.GitblitWicketFilter; import com.google.inject.AbstractModule; +import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.servlet.GuiceServletContextListener; /** * This class is the main entry point for the entire webapp. It is a singleton @@ -70,10 +73,12 @@ import com.google.inject.Injector; * */ @WebListener -public class GitblitContext extends GuiceContext { +public class GitblitContext extends GuiceServletContextListener { private static GitblitContext gitblit; + protected final Logger logger = LoggerFactory.getLogger(getClass()); + private final List managers = new ArrayList(); private final IStoredSettings goSettings; @@ -115,20 +120,37 @@ public class GitblitContext extends GuiceContext { return null; } + @Override + protected Injector getInjector() { + return Guice.createInjector(getModules()); + } + /** * Returns Gitblit's Guice injection modules. */ - @Override protected AbstractModule [] getModules() { - return new AbstractModule [] { new GuiceModule() }; + return new AbstractModule [] { new CoreModule(), new WebModule() }; } /** - * Prepare runtime settings and start all manager instances. + * Configure Gitblit from the web.xml, if no configuration has already been + * specified. + * + * @see ServletContextListener.contextInitialize(ServletContextEvent) */ @Override - protected void beforeServletInjection(ServletContext context) { - Injector injector = getInjector(context); + public final void contextInitialized(ServletContextEvent contextEvent) { + super.contextInitialized(contextEvent); + + ServletContext context = contextEvent.getServletContext(); + startCore(context); + } + + /** + * Prepare runtime settings and start all manager instances. + */ + protected void startCore(ServletContext context) { + Injector injector = (Injector) context.getAttribute(Injector.class.getName()); // create the runtime settings object IStoredSettings runtimeSettings = injector.getInstance(IStoredSettings.class); @@ -229,46 +251,17 @@ public class GitblitContext extends GuiceContext { logger.info("----[{}]----", clazz.getName()); } - /** - * Instantiate and inject all filters and servlets into the container using - * the servlet 3 specification. - */ @Override - protected void injectServlets(ServletContext context) { - // access restricted servlets - serve(context, Constants.R_PATH, GitServlet.class, GitFilter.class); - serve(context, Constants.GIT_PATH, GitServlet.class, GitFilter.class); - serve(context, Constants.RAW_PATH, RawServlet.class, RawFilter.class); - serve(context, Constants.PAGES, PagesServlet.class, PagesFilter.class); - serve(context, Constants.RPC_PATH, RpcServlet.class, RpcFilter.class); - serve(context, Constants.ZIP_PATH, DownloadZipServlet.class, DownloadZipFilter.class); - serve(context, Constants.SYNDICATION_PATH, SyndicationServlet.class, SyndicationFilter.class); - - // servlets - serve(context, Constants.FEDERATION_PATH, FederationServlet.class); - serve(context, Constants.SPARKLESHARE_INVITE_PATH, SparkleShareInviteServlet.class); - serve(context, Constants.BRANCH_GRAPH_PATH, BranchGraphServlet.class); - serve(context, Constants.PT_PATH, PtServlet.class); - file(context, "/robots.txt", RobotsTxtServlet.class); - file(context, "/logo.png", LogoServlet.class); - - // global filters - filter(context, "/*", ProxyFilter.class, null); - filter(context, "/*", EnforceAuthenticationFilter.class, null); - - // Wicket - String toIgnore = StringUtils.flattenStrings(getRegisteredPaths(), ","); - Map params = new HashMap(); - params.put(GitblitWicketFilter.FILTER_MAPPING_PARAM, "/*"); - params.put(GitblitWicketFilter.IGNORE_PATHS_PARAM, toIgnore); - filter(context, "/*", GitblitWicketFilter.class, params); + public final void contextDestroyed(ServletContextEvent contextEvent) { + super.contextDestroyed(contextEvent); + ServletContext context = contextEvent.getServletContext(); + destroyContext(context); } /** * Gitblit is being shutdown either because the servlet container is * shutting down or because the servlet container is re-deploying Gitblit. */ - @Override protected void destroyContext(ServletContext context) { logger.info("Gitblit context destroyed by servlet container."); diff --git a/src/main/java/com/gitblit/servlet/InjectionContextListener.java b/src/main/java/com/gitblit/servlet/InjectionContextListener.java deleted file mode 100644 index 17de6dad..00000000 --- a/src/main/java/com/gitblit/servlet/InjectionContextListener.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright 2014 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.servlet; - -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.List; -import java.util.Map; - -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterRegistration; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.ServletRegistration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Injection context listener instantiates and injects servlets, filters, and - * anything else you might want into a servlet context. This class provides - * convenience methods for servlet & filter registration and also tracks - * registered paths. - * - * @author James Moger - * - */ -public abstract class InjectionContextListener implements ServletContextListener { - - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - private final List registeredPaths = new ArrayList(); - - protected final List getRegisteredPaths() { - return registeredPaths; - } - - /** - * Hook for subclasses to manipulate context initialization before - * standard initialization procedure. - * - * @param context - */ - protected void beforeServletInjection(ServletContext context) { - // NOOP - } - - /** - * Hook for subclasses to instantiate and inject servlets and filters - * into the servlet context. - * - * @param context - */ - protected abstract void injectServlets(ServletContext context); - - /** - * Hook for subclasses to manipulate context initialization after - * servlet registration. - * - * @param context - */ - protected void afterServletInjection(ServletContext context) { - // NOOP - } - - /** - * Configure Gitblit from the web.xml, if no configuration has already been - * specified. - * - * @see ServletContextListener.contextInitialize(ServletContextEvent) - */ - @Override - public final void contextInitialized(ServletContextEvent contextEvent) { - ServletContext context = contextEvent.getServletContext(); - beforeServletInjection(context); - injectServlets(context); - afterServletInjection(context); - } - - - /** - * Registers a file path. - * - * @param context - * @param file - * @param servletClass - */ - protected void file(ServletContext context, String file, Class servletClass) { - file(context, file, servletClass, null); - } - - /** - * Registers a file path with init parameters. - * - * @param context - * @param file - * @param servletClass - * @param initParams - */ - protected void file(ServletContext context, String file, Class servletClass, Map initParams) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + file), servlet); - d.addMapping(file); - if (initParams != null) { - d.setInitParameters(initParams); - } - registeredPaths.add(file); - } - - /** - * Serves a path (trailing wildcard will be appended). - * - * @param context - * @param route - * @param servletClass - */ - protected void serve(ServletContext context, String route, Class servletClass) { - serve(context, route, servletClass, (Class) null); - } - - /** - * Serves a path (trailing wildcard will be appended) with init parameters. - * - * @param context - * @param route - * @param servletClass - * @param initParams - */ - protected void serve(ServletContext context, String route, Class servletClass, Map initParams) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); - d.addMapping(route + "*"); - if (initParams != null) { - d.setInitParameters(initParams); - } - registeredPaths.add(route); - } - - /** - * Serves a path (trailing wildcard will be appended) and also maps a filter - * to that path. - * - * @param context - * @param route - * @param servletClass - * @param filterClass - */ - protected void serve(ServletContext context, String route, Class servletClass, Class filterClass) { - Servlet servlet = instantiate(context, servletClass); - ServletRegistration.Dynamic d = context.addServlet(sanitize(servletClass.getSimpleName() + route), servlet); - d.addMapping(route + "*"); - if (filterClass != null) { - filter(context, route + "*", filterClass); - } - registeredPaths.add(route); - } - - /** - * Registers a path filter. - * - * @param context - * @param route - * @param filterClass - */ - protected void filter(ServletContext context, String route, Class filterClass) { - filter(context, route, filterClass, null); - } - - /** - * Registers a path filter with init parameters. - * - * @param context - * @param route - * @param filterClass - * @param initParams - */ - protected void filter(ServletContext context, String route, Class filterClass, Map initParams) { - Filter filter = instantiate(context, filterClass); - FilterRegistration.Dynamic d = context.addFilter(sanitize(filterClass.getSimpleName() + route), filter); - d.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, route); - if (initParams != null) { - d.setInitParameters(initParams); - } - } - - /** - * Limit the generated servlet/filter names to alpha-numeric values with a - * handful of acceptable other characters. - * - * @param name - * @return a sanitized name - */ - protected String sanitize(String name) { - StringBuilder sb = new StringBuilder(); - for (char c : name.toCharArray()) { - if (Character.isLetterOrDigit(c)) { - sb.append(c); - } else if ('-' == c) { - sb.append(c); - } else if ('*' == c) { - sb.append("all"); - } else if ('.' == c) { - sb.append('.'); - } else { - sb.append('_'); - } - } - return sb.toString(); - } - - /** - * Instantiates an object. - * - * @param clazz - * @return the object - */ - protected X instantiate(ServletContext context, Class clazz) { - try { - return clazz.newInstance(); - } catch (Throwable t) { - logger.error(null, t); - } - return null; - } -} diff --git a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java index 54be539f..36a0218e 100644 --- a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java +++ b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java @@ -28,6 +28,7 @@ import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; import com.gitblit.models.SettingModel; +import com.google.inject.Injector; public class MockRuntimeManager implements IRuntimeManager { @@ -56,6 +57,11 @@ public class MockRuntimeManager implements IRuntimeManager { this.serverSettings = new ServerSettings(); } + @Override + public Injector getInjector() { + return null; + } + @Override public void setBaseFolder(File folder) { this.baseFolder = folder; -- cgit v1.2.3 From 8f50853cf767c869d63f39c23073b6beb7e0593d Mon Sep 17 00:00:00 2001 From: James Moger Date: Sat, 24 May 2014 09:20:59 -0400 Subject: Dropped WebListener annotation --- src/main/java/com/gitblit/servlet/GitblitContext.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index 86b2fb3a..e0f6003c 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -30,7 +30,6 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; -import javax.servlet.annotation.WebListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,7 +71,6 @@ import com.google.inject.servlet.GuiceServletContextListener; * @author James Moger * */ -@WebListener public class GitblitContext extends GuiceServletContextListener { private static GitblitContext gitblit; -- cgit v1.2.3 From cdb2fe64280a517cf1d13013614bb8ab73cd0ac0 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 12 Jun 2014 18:12:46 -0400 Subject: Use Guice annotations, not javax.inject annotations --- src/main/java/com/gitblit/GitBlit.java | 2 +- src/main/java/com/gitblit/guice/CoreModule.java | 2 +- src/main/java/com/gitblit/guice/WebModule.java | 3 ++- src/main/java/com/gitblit/manager/AuthenticationManager.java | 2 +- src/main/java/com/gitblit/manager/FederationManager.java | 2 +- src/main/java/com/gitblit/manager/NotificationManager.java | 2 +- src/main/java/com/gitblit/manager/PluginManager.java | 2 +- src/main/java/com/gitblit/manager/ProjectManager.java | 2 +- src/main/java/com/gitblit/manager/RepositoryManager.java | 2 +- src/main/java/com/gitblit/manager/RuntimeManager.java | 2 +- src/main/java/com/gitblit/manager/UserManager.java | 2 +- src/main/java/com/gitblit/servlet/BranchGraphServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/DownloadZipFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/DownloadZipServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/FederationServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/GitFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/GitServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/LogoServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/PagesFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/PagesServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/ProxyFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/PtServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/RawFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/RawServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/RobotsTxtServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/RpcFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/RpcServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java | 4 ++-- src/main/java/com/gitblit/servlet/SyndicationFilter.java | 4 ++-- src/main/java/com/gitblit/servlet/SyndicationServlet.java | 4 ++-- src/main/java/com/gitblit/tickets/BranchTicketService.java | 4 ++-- src/main/java/com/gitblit/tickets/FileTicketService.java | 4 ++-- src/main/java/com/gitblit/tickets/NullTicketService.java | 4 ++-- src/main/java/com/gitblit/tickets/RedisTicketService.java | 4 ++-- src/main/java/com/gitblit/wicket/GitBlitWebApp.java | 4 ++-- src/main/java/com/gitblit/wicket/GitblitWicketFilter.java | 4 ++-- 37 files changed, 64 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index df2f502e..a39b0fcd 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -24,7 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; -import javax.inject.Inject; +import com.google.inject.Inject; import javax.servlet.http.HttpServletRequest; import com.gitblit.Constants.AccessPermission; diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index 9b479c09..89be94f7 100644 --- a/src/main/java/com/gitblit/guice/CoreModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -15,7 +15,7 @@ */ package com.gitblit.guice; -import javax.inject.Singleton; +import com.google.inject.Singleton; import com.gitblit.FileSettings; import com.gitblit.GitBlit; diff --git a/src/main/java/com/gitblit/guice/WebModule.java b/src/main/java/com/gitblit/guice/WebModule.java index b4f9899f..bfea987f 100644 --- a/src/main/java/com/gitblit/guice/WebModule.java +++ b/src/main/java/com/gitblit/guice/WebModule.java @@ -32,6 +32,7 @@ import com.gitblit.servlet.RawServlet; import com.gitblit.servlet.RobotsTxtServlet; import com.gitblit.servlet.RpcServlet; import com.gitblit.servlet.SparkleShareInviteServlet; +import com.gitblit.servlet.SyndicationFilter; import com.gitblit.servlet.SyndicationServlet; import com.gitblit.wicket.GitblitWicketFilter; import com.google.common.base.Joiner; @@ -74,7 +75,7 @@ public class WebModule extends ServletModule { // filter(fuzzy(Constants.PAGES)).through(PagesFilter.class); // filter(fuzzy(Constants.RPC_PATH)).through(RpcFilter.class); // filter(fuzzy(Constants.ZIP_PATH)).through(DownloadZipFilter.class); -// filter(fuzzy(Constants.SYNDICATION_PATH)).through(SyndicationFilter.class); + filter(fuzzy(Constants.SYNDICATION_PATH)).through(SyndicationFilter.class); // Wicket String toIgnore = Joiner.on(",").join(Constants.R_PATH, Constants.GIT_PATH, Constants.RAW_PATH, diff --git a/src/main/java/com/gitblit/manager/AuthenticationManager.java b/src/main/java/com/gitblit/manager/AuthenticationManager.java index 2aa2d197..de7e34cb 100644 --- a/src/main/java/com/gitblit/manager/AuthenticationManager.java +++ b/src/main/java/com/gitblit/manager/AuthenticationManager.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import javax.inject.Inject; +import com.google.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/src/main/java/com/gitblit/manager/FederationManager.java b/src/main/java/com/gitblit/manager/FederationManager.java index 32193525..b4dc5010 100644 --- a/src/main/java/com/gitblit/manager/FederationManager.java +++ b/src/main/java/com/gitblit/manager/FederationManager.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import javax.inject.Inject; +import com.google.inject.Inject; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; diff --git a/src/main/java/com/gitblit/manager/NotificationManager.java b/src/main/java/com/gitblit/manager/NotificationManager.java index 63f064e5..ebc21b3a 100644 --- a/src/main/java/com/gitblit/manager/NotificationManager.java +++ b/src/main/java/com/gitblit/manager/NotificationManager.java @@ -20,7 +20,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import javax.inject.Inject; +import com.google.inject.Inject; import javax.mail.Message; import org.slf4j.Logger; diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index a0c873e8..268eef14 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -34,7 +34,7 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import javax.inject.Inject; +import com.google.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/gitblit/manager/ProjectManager.java b/src/main/java/com/gitblit/manager/ProjectManager.java index 4da7f801..80675a76 100644 --- a/src/main/java/com/gitblit/manager/ProjectManager.java +++ b/src/main/java/com/gitblit/manager/ProjectManager.java @@ -27,7 +27,7 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; -import javax.inject.Inject; +import com.google.inject.Inject; import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.util.FS; diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index a82cd016..9b061e25 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -44,7 +44,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import javax.inject.Inject; +import com.google.inject.Inject; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; diff --git a/src/main/java/com/gitblit/manager/RuntimeManager.java b/src/main/java/com/gitblit/manager/RuntimeManager.java index f2520085..383b1dce 100644 --- a/src/main/java/com/gitblit/manager/RuntimeManager.java +++ b/src/main/java/com/gitblit/manager/RuntimeManager.java @@ -22,7 +22,7 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import javax.inject.Inject; +import com.google.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java index fe21b063..8d6d4c14 100644 --- a/src/main/java/com/gitblit/manager/UserManager.java +++ b/src/main/java/com/gitblit/manager/UserManager.java @@ -24,7 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.inject.Inject; +import com.google.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java index c1ad0806..2c77553a 100644 --- a/src/main/java/com/gitblit/servlet/BranchGraphServlet.java +++ b/src/main/java/com/gitblit/servlet/BranchGraphServlet.java @@ -36,8 +36,8 @@ import java.util.Set; import java.util.TreeSet; import javax.imageio.ImageIO; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java index 3f20c2b7..13703a81 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipFilter.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipFilter.java @@ -15,8 +15,8 @@ */ package com.gitblit.servlet; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.manager.IAuthenticationManager; diff --git a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java index 4b0e3bf5..07562561 100644 --- a/src/main/java/com/gitblit/servlet/DownloadZipServlet.java +++ b/src/main/java/com/gitblit/servlet/DownloadZipServlet.java @@ -20,8 +20,8 @@ import java.text.MessageFormat; import java.text.ParseException; import java.util.Date; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletResponse; diff --git a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java index a9e6554e..8a3f782a 100644 --- a/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/EnforceAuthenticationFilter.java @@ -18,8 +18,8 @@ import java.io.IOException; import java.text.MessageFormat; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; diff --git a/src/main/java/com/gitblit/servlet/FederationServlet.java b/src/main/java/com/gitblit/servlet/FederationServlet.java index 8a03f089..89ff2312 100644 --- a/src/main/java/com/gitblit/servlet/FederationServlet.java +++ b/src/main/java/com/gitblit/servlet/FederationServlet.java @@ -25,8 +25,8 @@ import java.util.List; import java.util.Map; import java.util.Set; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.http.HttpServletResponse; import com.gitblit.Constants.FederationRequest; diff --git a/src/main/java/com/gitblit/servlet/GitFilter.java b/src/main/java/com/gitblit/servlet/GitFilter.java index e2c45898..b29fdb6a 100644 --- a/src/main/java/com/gitblit/servlet/GitFilter.java +++ b/src/main/java/com/gitblit/servlet/GitFilter.java @@ -17,8 +17,8 @@ package com.gitblit.servlet; import java.text.MessageFormat; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.http.HttpServletRequest; import com.gitblit.Constants.AccessRestrictionType; diff --git a/src/main/java/com/gitblit/servlet/GitServlet.java b/src/main/java/com/gitblit/servlet/GitServlet.java index e9c5242f..941b4c5e 100644 --- a/src/main/java/com/gitblit/servlet/GitServlet.java +++ b/src/main/java/com/gitblit/servlet/GitServlet.java @@ -20,8 +20,8 @@ import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; import java.io.IOException; import java.util.Enumeration; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletConfig; diff --git a/src/main/java/com/gitblit/servlet/LogoServlet.java b/src/main/java/com/gitblit/servlet/LogoServlet.java index 489e98b2..d5d298b1 100644 --- a/src/main/java/com/gitblit/servlet/LogoServlet.java +++ b/src/main/java/com/gitblit/servlet/LogoServlet.java @@ -21,8 +21,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; diff --git a/src/main/java/com/gitblit/servlet/PagesFilter.java b/src/main/java/com/gitblit/servlet/PagesFilter.java index fa8c9abe..1d6c3db9 100644 --- a/src/main/java/com/gitblit/servlet/PagesFilter.java +++ b/src/main/java/com/gitblit/servlet/PagesFilter.java @@ -15,8 +15,8 @@ */ package com.gitblit.servlet; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IRepositoryManager; diff --git a/src/main/java/com/gitblit/servlet/PagesServlet.java b/src/main/java/com/gitblit/servlet/PagesServlet.java index 4cf43024..ad34b3ca 100644 --- a/src/main/java/com/gitblit/servlet/PagesServlet.java +++ b/src/main/java/com/gitblit/servlet/PagesServlet.java @@ -15,8 +15,8 @@ */ package com.gitblit.servlet; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/src/main/java/com/gitblit/servlet/ProxyFilter.java b/src/main/java/com/gitblit/servlet/ProxyFilter.java index 778c8187..d7f096ac 100644 --- a/src/main/java/com/gitblit/servlet/ProxyFilter.java +++ b/src/main/java/com/gitblit/servlet/ProxyFilter.java @@ -20,8 +20,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; diff --git a/src/main/java/com/gitblit/servlet/PtServlet.java b/src/main/java/com/gitblit/servlet/PtServlet.java index 65d3de17..313b284c 100644 --- a/src/main/java/com/gitblit/servlet/PtServlet.java +++ b/src/main/java/com/gitblit/servlet/PtServlet.java @@ -21,8 +21,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/com/gitblit/servlet/RawFilter.java b/src/main/java/com/gitblit/servlet/RawFilter.java index 7dddd9b7..fe4af040 100644 --- a/src/main/java/com/gitblit/servlet/RawFilter.java +++ b/src/main/java/com/gitblit/servlet/RawFilter.java @@ -15,8 +15,8 @@ */ package com.gitblit.servlet; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import org.eclipse.jgit.lib.Repository; diff --git a/src/main/java/com/gitblit/servlet/RawServlet.java b/src/main/java/com/gitblit/servlet/RawServlet.java index d856ef89..7731a95f 100644 --- a/src/main/java/com/gitblit/servlet/RawServlet.java +++ b/src/main/java/com/gitblit/servlet/RawServlet.java @@ -28,8 +28,8 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; diff --git a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java index e08c6bbd..4e58d4de 100644 --- a/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java +++ b/src/main/java/com/gitblit/servlet/RobotsTxtServlet.java @@ -18,8 +18,8 @@ package com.gitblit.servlet; import java.io.File; import java.io.IOException; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/com/gitblit/servlet/RpcFilter.java b/src/main/java/com/gitblit/servlet/RpcFilter.java index 73f9dcb3..34474d55 100644 --- a/src/main/java/com/gitblit/servlet/RpcFilter.java +++ b/src/main/java/com/gitblit/servlet/RpcFilter.java @@ -18,8 +18,8 @@ package com.gitblit.servlet; import java.io.IOException; import java.text.MessageFormat; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java index aaf7d5fb..9809a252 100644 --- a/src/main/java/com/gitblit/servlet/RpcServlet.java +++ b/src/main/java/com/gitblit/servlet/RpcServlet.java @@ -23,8 +23,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; diff --git a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java index e596cfd3..69c254cb 100644 --- a/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java +++ b/src/main/java/com/gitblit/servlet/SparkleShareInviteServlet.java @@ -19,8 +19,8 @@ import java.io.IOException; import java.net.URL; import java.text.MessageFormat; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/com/gitblit/servlet/SyndicationFilter.java b/src/main/java/com/gitblit/servlet/SyndicationFilter.java index 3b9753fa..49348d0d 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationFilter.java +++ b/src/main/java/com/gitblit/servlet/SyndicationFilter.java @@ -18,8 +18,8 @@ package com.gitblit.servlet; import java.io.IOException; import java.text.MessageFormat; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index c48f25f9..29db54d4 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -22,8 +22,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.http.HttpServlet; import org.eclipse.jgit.lib.ObjectId; diff --git a/src/main/java/com/gitblit/tickets/BranchTicketService.java b/src/main/java/com/gitblit/tickets/BranchTicketService.java index a555ece3..0633751a 100644 --- a/src/main/java/com/gitblit/tickets/BranchTicketService.java +++ b/src/main/java/com/gitblit/tickets/BranchTicketService.java @@ -30,8 +30,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.JGitInternalException; diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java index c10d7855..74311f53 100644 --- a/src/main/java/com/gitblit/tickets/FileTicketService.java +++ b/src/main/java/com/gitblit/tickets/FileTicketService.java @@ -27,8 +27,8 @@ import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import org.eclipse.jgit.lib.Repository; diff --git a/src/main/java/com/gitblit/tickets/NullTicketService.java b/src/main/java/com/gitblit/tickets/NullTicketService.java index b8882788..0980d29d 100644 --- a/src/main/java/com/gitblit/tickets/NullTicketService.java +++ b/src/main/java/com/gitblit/tickets/NullTicketService.java @@ -19,8 +19,8 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import com.gitblit.manager.INotificationManager; import com.gitblit.manager.IPluginManager; diff --git a/src/main/java/com/gitblit/tickets/RedisTicketService.java b/src/main/java/com/gitblit/tickets/RedisTicketService.java index 6b8817b6..02c19e63 100644 --- a/src/main/java/com/gitblit/tickets/RedisTicketService.java +++ b/src/main/java/com/gitblit/tickets/RedisTicketService.java @@ -22,8 +22,8 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index e5b65508..a9073110 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -21,8 +21,8 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import org.apache.wicket.Application; import org.apache.wicket.Request; diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java index 72adbf51..68ad84a5 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketFilter.java @@ -17,8 +17,8 @@ package com.gitblit.wicket; import java.util.Date; -import javax.inject.Inject; -import javax.inject.Singleton; +import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.servlet.http.HttpServletRequest; import org.apache.wicket.protocol.http.IWebApplicationFactory; -- cgit v1.2.3 From 52daaea33dab5d10ec7bd7164a4ce6560c56c8d4 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 12 Jun 2014 19:19:26 -0400 Subject: Restore all security filters --- src/main/java/com/gitblit/guice/WebModule.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gitblit/guice/WebModule.java b/src/main/java/com/gitblit/guice/WebModule.java index bfea987f..5b569182 100644 --- a/src/main/java/com/gitblit/guice/WebModule.java +++ b/src/main/java/com/gitblit/guice/WebModule.java @@ -20,16 +20,21 @@ import java.util.Map; import com.gitblit.Constants; import com.gitblit.servlet.BranchGraphServlet; +import com.gitblit.servlet.DownloadZipFilter; import com.gitblit.servlet.DownloadZipServlet; import com.gitblit.servlet.EnforceAuthenticationFilter; import com.gitblit.servlet.FederationServlet; +import com.gitblit.servlet.GitFilter; import com.gitblit.servlet.GitServlet; import com.gitblit.servlet.LogoServlet; +import com.gitblit.servlet.PagesFilter; import com.gitblit.servlet.PagesServlet; import com.gitblit.servlet.ProxyFilter; import com.gitblit.servlet.PtServlet; +import com.gitblit.servlet.RawFilter; import com.gitblit.servlet.RawServlet; import com.gitblit.servlet.RobotsTxtServlet; +import com.gitblit.servlet.RpcFilter; import com.gitblit.servlet.RpcServlet; import com.gitblit.servlet.SparkleShareInviteServlet; import com.gitblit.servlet.SyndicationFilter; @@ -70,11 +75,11 @@ public class WebModule extends ServletModule { filter(ALL).through(EnforceAuthenticationFilter.class); // security filters -// filter(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).through(GitFilter.class); -// filter(fuzzy(Constants.RAW_PATH)).through(RawFilter.class); -// filter(fuzzy(Constants.PAGES)).through(PagesFilter.class); -// filter(fuzzy(Constants.RPC_PATH)).through(RpcFilter.class); -// filter(fuzzy(Constants.ZIP_PATH)).through(DownloadZipFilter.class); + filter(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).through(GitFilter.class); + filter(fuzzy(Constants.RAW_PATH)).through(RawFilter.class); + filter(fuzzy(Constants.PAGES)).through(PagesFilter.class); + filter(fuzzy(Constants.RPC_PATH)).through(RpcFilter.class); + filter(fuzzy(Constants.ZIP_PATH)).through(DownloadZipFilter.class); filter(fuzzy(Constants.SYNDICATION_PATH)).through(SyndicationFilter.class); // Wicket -- cgit v1.2.3 From 575d4f690b8eaedfdd3981fe7c00ecc41d358e73 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 12 Jun 2014 19:25:12 -0400 Subject: Temporarily depend on patched guice-servlet (guice-807) --- .classpath | 2 +- build.moxie | 3 ++- gitblit.iml | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.classpath b/.classpath index 9c3e7b8c..a7a89634 100644 --- a/.classpath +++ b/.classpath @@ -9,7 +9,7 @@ - + diff --git a/build.moxie b/build.moxie index b25a4222..3514577e 100644 --- a/build.moxie +++ b/build.moxie @@ -113,6 +113,7 @@ properties: { sshd.version: 0.11.1-atlassian-1 mina.version: 2.0.7 guice.version : 4.0-beta4 + guice-servlet.version : 4.0-SNAPSHOT } # Dependencies @@ -129,7 +130,7 @@ properties: { dependencies: # Dagger dependency injection library (annotation processor) - compile 'com.google.inject:guice:${guice.version}' :war -- compile 'com.google.inject.extensions:guice-servlet:${guice.version}' :war +- compile 'com.google.inject.extensions:guice-servlet:${guice-servlet.version}' :war - compile 'com.google.guava:guava:16.0.1' :war # Standard dependencies - compile 'com.intellij:annotations:12.0' :war diff --git a/gitblit.iml b/gitblit.iml index f8a8a674..8912282a 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -58,13 +58,13 @@ - + - + - + -- cgit v1.2.3 From 94146dd495aad8157c423bfd9a1ae47c8e26b32c Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jun 2014 21:29:01 -0400 Subject: Use forked guice-servlet jar rather than stock jar from Google --- .classpath | 2 +- build.moxie | 6 ++++-- gitblit.iml | 6 +++--- src/main/java/com/gitblit/servlet/AuthenticationFilter.java | 8 ++------ 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.classpath b/.classpath index a7a89634..274dee2b 100644 --- a/.classpath +++ b/.classpath @@ -9,7 +9,7 @@ - + diff --git a/build.moxie b/build.moxie index 3514577e..8ca52755 100644 --- a/build.moxie +++ b/build.moxie @@ -96,9 +96,10 @@ registeredRepositories: - { id: eclipse, url: 'http://repo.eclipse.org/content/groups/releases' } - { id: eclipse-snapshots, url: 'http://repo.eclipse.org/content/groups/snapshots' } - { id: atlassian-contrib, url: 'https://maven.atlassian.com/content/repositories/atlassian-3rdparty' } +- { id: gitblit, url: 'http://gitblit.github.io/gitblit-maven' } # Source all dependencies from the following repositories in the specified order -repositories: central, eclipse-snapshots, eclipse, atlassian-contrib +repositories: central, eclipse-snapshots, eclipse, atlassian-contrib, gitblit # Convenience properties for dependencies properties: { @@ -113,7 +114,8 @@ properties: { sshd.version: 0.11.1-atlassian-1 mina.version: 2.0.7 guice.version : 4.0-beta4 - guice-servlet.version : 4.0-SNAPSHOT + # Gitblit maintains a fork of guice-servlet + guice-servlet.version : 4.0-gb1 } # Dependencies diff --git a/gitblit.iml b/gitblit.iml index 8912282a..cc9324f5 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -58,13 +58,13 @@ - + - + - + diff --git a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java index 35a62365..6f13252a 100644 --- a/src/main/java/com/gitblit/servlet/AuthenticationFilter.java +++ b/src/main/java/com/gitblit/servlet/AuthenticationFilter.java @@ -99,12 +99,8 @@ public abstract class AuthenticationFilter implements Filter { * @return url */ protected String getFullUrl(HttpServletRequest httpRequest) { - String contextPath = httpRequest.getContextPath(); - String servletPath = httpRequest.getServletPath(); - String pathInfo = httpRequest.getPathInfo(); - String servletUrl = contextPath + servletPath; - String requestURI = httpRequest.getRequestURI(); - String url = requestURI.substring(servletUrl.length()); + String servletUrl = httpRequest.getContextPath() + httpRequest.getServletPath(); + String url = httpRequest.getRequestURI().substring(servletUrl.length()); String params = httpRequest.getQueryString(); if (url.length() > 0 && url.charAt(0) == '/') { url = url.substring(1); -- cgit v1.2.3 From 9bc44161b6985c2f39cc86bf989351cb4281919f Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jun 2014 22:29:48 -0400 Subject: Removed beans.xml --- src/main/java/WEB-INF/beans.xml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/main/java/WEB-INF/beans.xml diff --git a/src/main/java/WEB-INF/beans.xml b/src/main/java/WEB-INF/beans.xml deleted file mode 100644 index 0165c2cb..00000000 --- a/src/main/java/WEB-INF/beans.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file -- cgit v1.2.3 From 35fe70c73a4341d9288ead5ca4792a86b485b788 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jun 2014 22:45:52 -0400 Subject: Adjust builds for appropriate dependencies --- build.moxie | 26 ++++++++++++-------------- build.xml | 10 ++++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/build.moxie b/build.moxie index 8ca52755..735e1857 100644 --- a/build.moxie +++ b/build.moxie @@ -130,16 +130,14 @@ properties: { # dependencies: -# Dagger dependency injection library (annotation processor) -- compile 'com.google.inject:guice:${guice.version}' :war +- compile 'com.google.inject:guice:${guice.version}' :war :fedclient - compile 'com.google.inject.extensions:guice-servlet:${guice-servlet.version}' :war -- compile 'com.google.guava:guava:16.0.1' :war -# Standard dependencies +- compile 'com.google.guava:guava:16.0.1' :war :fedclient - compile 'com.intellij:annotations:12.0' :war -- compile 'log4j:log4j:1.2.17' :war :fedclient :authority -- compile 'org.slf4j:slf4j-api:1.7.7' :war :fedclient :authority -- compile 'org.slf4j:slf4j-log4j12:1.7.7' :war :fedclient :authority -- compile 'com.sun.mail:javax.mail:1.5.1' :war :authority +- compile 'log4j:log4j:1.2.17' :war :fedclient +- compile 'org.slf4j:slf4j-api:1.7.7' :war :fedclient +- compile 'org.slf4j:slf4j-log4j12:1.7.7' :war :fedclient +- compile 'com.sun.mail:javax.mail:1.5.1' :war - compile 'javax.servlet:javax.servlet-api:3.1.0' :fedclient - compile 'org.eclipse.jetty.aggregate:jetty-all:${jetty.version}' @jar - compile 'org.apache.wicket:wicket:${wicket.version}' :war !org.mockito @@ -157,11 +155,11 @@ dependencies: - compile 'org.fusesource.wikitext:tracwiki-core:${wikitext.version}' :war - compile 'org.fusesource.wikitext:mediawiki-core:${wikitext.version}' :war - compile 'org.fusesource.wikitext:confluence-core:${wikitext.version}' :war -- compile 'org.eclipse.jgit:org.eclipse.jgit:${jgit.version}' :war :fedclient :manager :authority !junit -- compile 'org.eclipse.jgit:org.eclipse.jgit.http.server:${jgit.version}' :war :manager :authority !junit -- compile 'org.bouncycastle:bcprov-jdk15on:${bouncycastle.version}' :war :authority -- compile 'org.bouncycastle:bcmail-jdk15on:${bouncycastle.version}' :war :authority -- compile 'org.bouncycastle:bcpkix-jdk15on:${bouncycastle.version}' :war :authority +- compile 'org.eclipse.jgit:org.eclipse.jgit:${jgit.version}' :war :fedclient :manager !junit +- compile 'org.eclipse.jgit:org.eclipse.jgit.http.server:${jgit.version}' :war :manager !junit +- compile 'org.bouncycastle:bcprov-jdk15on:${bouncycastle.version}' :war +- compile 'org.bouncycastle:bcmail-jdk15on:${bouncycastle.version}' :war +- compile 'org.bouncycastle:bcpkix-jdk15on:${bouncycastle.version}' :war - compile 'org.apache.sshd:sshd-core:${sshd.version}' :war !org.easymock - compile 'org.apache.mina:mina-core:${mina.version}' :war !org.easymock - compile 'rome:rome:0.9' :war :manager :api @@ -176,7 +174,7 @@ dependencies: - compile 'org.freemarker:freemarker:2.3.20' :war - compile 'com.github.dblock.waffle:waffle-jna:1.5' :war - compile 'org.kohsuke:libpam4j:1.7' :war -- compile 'args4j:args4j:2.0.26' :war :fedclient :authority +- compile 'args4j:args4j:2.0.26' :war :fedclient - compile 'commons-codec:commons-codec:1.7' :war - compile 'redis.clients:jedis:2.3.1' :war - compile 'ro.fortsoft.pf4j:pf4j:0.8.0' :war diff --git a/build.xml b/build.xml index c88bd8fd..9a94cda8 100644 --- a/build.xml +++ b/build.xml @@ -295,7 +295,7 @@ classes, exclude any classes in classpath jars --> + excludes="**/.class, **/*.java, **/Thumbs.db, **/*.mkd, **/*.md, **/*.css, com/gitblit/wicket/**"> @@ -335,7 +335,8 @@ + destfile="${project.targetDirectory}/manager.jar" + excludes="**/.class, **/*.java, **/Thumbs.db, **/*.mkd, **/*.md, **/*.css, com/gitblit/wicket/**"> @@ -411,9 +412,10 @@ + destfile="${project.targetDirectory}/gbapi-${project.version}.jar" + excludes="**/.class, **/*.java, **/Thumbs.db, **/*.mkd, **/*.md, **/*.css, com/gitblit/wicket/**"> + - -- cgit v1.2.3 From e6ae536d3a5ec04eea1421f3f5422f7ea5011e33 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 30 Jun 2014 23:25:01 -0400 Subject: Documentation --- NOTICE | 9 +++++++++ releases.moxie | 1 + src/site/design.mkd | 1 + 3 files changed, 11 insertions(+) diff --git a/NOTICE b/NOTICE index da61b201..69d7c744 100644 --- a/NOTICE +++ b/NOTICE @@ -358,3 +358,12 @@ pf4j Apache License 2.0 https://github.com/decebals/pf4j + +--------------------------------------------------------------------------- +google-guice +--------------------------------------------------------------------------- + google-guice, release under the + Apache License 2.0 + + https://code.google.com/p/google-guice + \ No newline at end of file diff --git a/releases.moxie b/releases.moxie index 61c43c4d..e99cbd55 100644 --- a/releases.moxie +++ b/releases.moxie @@ -24,6 +24,7 @@ r25: { - Jetty 9.2.1 (pr-202) - Lucene 4.8.1 (pr-201) - JGit 3.4.0 (pr-200) + - Guice 4.0-beta4 (ticket-80) - SLF4j 1.7.7 (ticket-122) - gson 2.2.2 (ticket-123) - Freemarker 2.3.20 (ticket-124) diff --git a/src/site/design.mkd b/src/site/design.mkd index cd4b1b71..9ef302c1 100644 --- a/src/site/design.mkd +++ b/src/site/design.mkd @@ -57,6 +57,7 @@ The following dependencies are automatically downloaded by Gitblit GO (or alread - [jedis](https://github.com/xetorthio/jedis) (MIT) - [Mina SSHD](https://mina.apache.org) (Apache 2.0) - [pf4j](https://github.com/decebals/pf4j) (Apache 2.0) +- [google-guice](https://code.google.com/p/google-guice) (Apache 2.0) ### Other Build Dependencies - [Fancybox image viewer](http://fancybox.net) (MIT and GPL dual-licensed) -- cgit v1.2.3 From 7d3a31514afbe88664081b4ea57cd7939de99014 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 16:21:16 -0400 Subject: Extract services manager into a top-level injectable manager --- src/main/java/com/gitblit/GitBlit.java | 181 ----------------- src/main/java/com/gitblit/guice/CoreModule.java | 9 + .../java/com/gitblit/guice/WorkQueueProvider.java | 57 ++++++ .../java/com/gitblit/manager/GitblitManager.java | 84 -------- src/main/java/com/gitblit/manager/IGitblit.java | 15 -- .../java/com/gitblit/manager/IRuntimeManager.java | 36 ---- .../java/com/gitblit/manager/IServicesManager.java | 75 +++++++ .../com/gitblit/manager/RepositoryManager.java | 26 ++- .../java/com/gitblit/manager/RuntimeManager.java | 51 +---- .../java/com/gitblit/manager/ServicesManager.java | 226 +++++++++++++++++++-- .../java/com/gitblit/servlet/GitblitContext.java | 2 + .../java/com/gitblit/wicket/GitBlitWebApp.java | 20 +- .../java/com/gitblit/wicket/GitblitWicketApp.java | 3 + .../gitblit/wicket/pages/EmptyRepositoryPage.java | 2 +- .../java/com/gitblit/wicket/pages/TicketPage.java | 2 +- .../java/com/gitblit/wicket/pages/UserPage.java | 8 +- .../gitblit/wicket/panels/RepositoryUrlPanel.java | 4 +- .../com/gitblit/tests/mock/MockRuntimeManager.java | 20 -- 18 files changed, 397 insertions(+), 424 deletions(-) create mode 100644 src/main/java/com/gitblit/guice/WorkQueueProvider.java create mode 100644 src/main/java/com/gitblit/manager/IServicesManager.java diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index a39b0fcd..edad32ef 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -25,10 +25,7 @@ import java.util.List; import java.util.Set; import com.google.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import com.gitblit.Constants.AccessPermission; -import com.gitblit.Constants.Transport; import com.gitblit.manager.GitblitManager; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; @@ -39,9 +36,7 @@ import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IUserManager; -import com.gitblit.manager.ServicesManager; import com.gitblit.models.RepositoryModel; -import com.gitblit.models.RepositoryUrl; import com.gitblit.models.UserModel; import com.gitblit.tickets.ITicketService; import com.gitblit.tickets.NullTicketService; @@ -62,8 +57,6 @@ public class GitBlit extends GitblitManager { private final Injector injector; - private final ServicesManager servicesManager; - private ITicketService ticketService; @Inject @@ -89,15 +82,11 @@ public class GitBlit extends GitblitManager { federationManager); this.injector = Guice.createInjector(getModules()); - - this.servicesManager = new ServicesManager(this); } @Override public GitBlit start() { super.start(); - logger.info("Starting services manager..."); - servicesManager.start(); configureTicketService(); return this; } @@ -105,184 +94,14 @@ public class GitBlit extends GitblitManager { @Override public GitBlit stop() { super.stop(); - servicesManager.stop(); ticketService.stop(); return this; } - @Override - public boolean isServingRepositories() { - return servicesManager.isServingRepositories(); - } - - @Override - public boolean isServingHTTP() { - return servicesManager.isServingHTTP(); - } - - @Override - public boolean isServingGIT() { - return servicesManager.isServingGIT(); - } - - @Override - public boolean isServingSSH() { - return servicesManager.isServingSSH(); - } - protected AbstractModule [] getModules() { return new AbstractModule [] { new GitBlitModule()}; } - protected boolean acceptPush(Transport byTransport) { - if (byTransport == null) { - logger.info("Unknown transport, push rejected!"); - return false; - } - - Set transports = new HashSet(); - for (String value : getSettings().getStrings(Keys.git.acceptedPushTransports)) { - Transport transport = Transport.fromString(value); - if (transport == null) { - logger.info(String.format("Ignoring unknown registered transport %s", value)); - continue; - } - - transports.add(transport); - } - - if (transports.isEmpty()) { - // no transports are explicitly specified, all are acceptable - return true; - } - - // verify that the transport is permitted - return transports.contains(byTransport); - } - - /** - * Returns a list of repository URLs and the user access permission. - * - * @param request - * @param user - * @param repository - * @return a list of repository urls - */ - @Override - public List getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) { - if (user == null) { - user = UserModel.ANONYMOUS; - } - String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username); - - List list = new ArrayList(); - - // http/https url - if (settings.getBoolean(Keys.git.enableGitServlet, true)) { - AccessPermission permission = user.getRepositoryPermission(repository).permission; - if (permission.exceeds(AccessPermission.NONE)) { - Transport transport = Transport.fromString(request.getScheme()); - if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(transport)) { - // downgrade the repo permission for this transport - // because it is not an acceptable PUSH transport - permission = AccessPermission.CLONE; - } - list.add(new RepositoryUrl(getRepositoryUrl(request, username, repository), permission)); - } - } - - // ssh daemon url - String sshDaemonUrl = servicesManager.getSshDaemonUrl(request, user, repository); - if (!StringUtils.isEmpty(sshDaemonUrl)) { - AccessPermission permission = user.getRepositoryPermission(repository).permission; - if (permission.exceeds(AccessPermission.NONE)) { - if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(Transport.SSH)) { - // downgrade the repo permission for this transport - // because it is not an acceptable PUSH transport - permission = AccessPermission.CLONE; - } - - list.add(new RepositoryUrl(sshDaemonUrl, permission)); - } - } - - // git daemon url - String gitDaemonUrl = servicesManager.getGitDaemonUrl(request, user, repository); - if (!StringUtils.isEmpty(gitDaemonUrl)) { - AccessPermission permission = servicesManager.getGitDaemonAccessPermission(user, repository); - if (permission.exceeds(AccessPermission.NONE)) { - if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(Transport.GIT)) { - // downgrade the repo permission for this transport - // because it is not an acceptable PUSH transport - permission = AccessPermission.CLONE; - } - list.add(new RepositoryUrl(gitDaemonUrl, permission)); - } - } - - // add all other urls - // {0} = repository - // {1} = username - for (String url : settings.getStrings(Keys.web.otherUrls)) { - if (url.contains("{1}")) { - // external url requires username, only add url IF we have one - if (!StringUtils.isEmpty(username)) { - list.add(new RepositoryUrl(MessageFormat.format(url, repository.name, username), null)); - } - } else { - // external url does not require username - list.add(new RepositoryUrl(MessageFormat.format(url, repository.name), null)); - } - } - - // sort transports by highest permission and then by transport security - Collections.sort(list, new Comparator() { - - @Override - public int compare(RepositoryUrl o1, RepositoryUrl o2) { - if (!o1.isExternal() && o2.isExternal()) { - // prefer Gitblit over external - return -1; - } else if (o1.isExternal() && !o2.isExternal()) { - // prefer Gitblit over external - return 1; - } else if (o1.isExternal() && o2.isExternal()) { - // sort by Transport ordinal - return o1.transport.compareTo(o2.transport); - } else if (o1.permission.exceeds(o2.permission)) { - // prefer highest permission - return -1; - } else if (o2.permission.exceeds(o1.permission)) { - // prefer highest permission - return 1; - } - - // prefer more secure transports - return o1.transport.compareTo(o2.transport); - } - }); - - // consider the user's transport preference - RepositoryUrl preferredUrl = null; - Transport preferredTransport = user.getPreferences().getTransport(); - if (preferredTransport != null) { - Iterator itr = list.iterator(); - while (itr.hasNext()) { - RepositoryUrl url = itr.next(); - if (url.transport.equals(preferredTransport)) { - itr.remove(); - preferredUrl = url; - break; - } - } - } - if (preferredUrl != null) { - list.add(0, preferredUrl); - } - - return list; - } - /** * Detect renames and reindex as appropriate. */ diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index 89be94f7..03cdbf22 100644 --- a/src/main/java/com/gitblit/guice/CoreModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -31,18 +31,21 @@ import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IServicesManager; import com.gitblit.manager.IUserManager; import com.gitblit.manager.NotificationManager; import com.gitblit.manager.PluginManager; import com.gitblit.manager.ProjectManager; import com.gitblit.manager.RepositoryManager; import com.gitblit.manager.RuntimeManager; +import com.gitblit.manager.ServicesManager; import com.gitblit.manager.UserManager; import com.gitblit.transport.ssh.FileKeyManager; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.transport.ssh.MemoryKeyManager; import com.gitblit.transport.ssh.NullKeyManager; import com.gitblit.utils.StringUtils; +import com.gitblit.utils.WorkQueue; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -59,6 +62,9 @@ public class CoreModule extends AbstractModule { bind(IStoredSettings.class).toInstance(new FileSettings()); + // bind complex providers + bind(WorkQueue.class).toProvider(WorkQueueProvider.class); + // core managers bind(IRuntimeManager.class).to(RuntimeManager.class).in(Singleton.class); bind(IPluginManager.class).to(PluginManager.class).in(Singleton.class); @@ -71,6 +77,9 @@ public class CoreModule extends AbstractModule { // the monolithic manager bind(IGitblit.class).to(GitBlit.class).in(Singleton.class); + + // manager for long-running daemons and services + bind(IServicesManager.class).to(ServicesManager.class); } @Provides diff --git a/src/main/java/com/gitblit/guice/WorkQueueProvider.java b/src/main/java/com/gitblit/guice/WorkQueueProvider.java new file mode 100644 index 00000000..cde27ea9 --- /dev/null +++ b/src/main/java/com/gitblit/guice/WorkQueueProvider.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 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.guice; + +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.utils.IdGenerator; +import com.gitblit.utils.WorkQueue; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; + +/** + * Provides a lazily-instantiated WorkQueue configured from IStoredSettings. + * + * @author James Moger + * + */ +@Singleton +public class WorkQueueProvider implements Provider { + + private final IRuntimeManager runtimeManager; + + private volatile WorkQueue workQueue; + + @Inject + public WorkQueueProvider(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; + } + + @Override + public synchronized WorkQueue get() { + if (workQueue != null) { + return workQueue; + } + + IStoredSettings settings = runtimeManager.getSettings(); + int defaultThreadPoolSize = settings.getInteger(Keys.execution.defaultThreadPoolSize, 1); + IdGenerator idGenerator = new IdGenerator(); + workQueue = new WorkQueue(idGenerator, defaultThreadPoolSize); + return workQueue; + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index a0718f77..2c88e405 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -49,12 +49,10 @@ import ro.fortsoft.pf4j.Version; import com.gitblit.Constants; import com.gitblit.Constants.AccessPermission; -import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.FederationRequest; import com.gitblit.Constants.FederationToken; import com.gitblit.GitBlitException; import com.gitblit.IStoredSettings; -import com.gitblit.Keys; import com.gitblit.models.FederationModel; import com.gitblit.models.FederationProposal; import com.gitblit.models.FederationSet; @@ -68,7 +66,6 @@ import com.gitblit.models.PluginRegistry.PluginRelease; import com.gitblit.models.ProjectModel; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.RepositoryModel; -import com.gitblit.models.RepositoryUrl; import com.gitblit.models.SearchResult; import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; @@ -79,7 +76,6 @@ import com.gitblit.tickets.ITicketService; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.transport.ssh.SshKey; import com.gitblit.utils.ArrayUtils; -import com.gitblit.utils.HttpUtils; import com.gitblit.utils.JsonUtils; import com.gitblit.utils.ObjectCache; import com.gitblit.utils.StringUtils; @@ -350,66 +346,6 @@ public class GitblitManager implements IGitblit { } } - /** - * Returns a list of repository URLs and the user access permission. - * - * @param request - * @param user - * @param repository - * @return a list of repository urls - */ - @Override - public List getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) { - if (user == null) { - user = UserModel.ANONYMOUS; - } - String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username); - - List list = new ArrayList(); - // http/https url - if (settings.getBoolean(Keys.git.enableGitServlet, true)) { - AccessPermission permission = user.getRepositoryPermission(repository).permission; - if (permission.exceeds(AccessPermission.NONE)) { - list.add(new RepositoryUrl(getRepositoryUrl(request, username, repository), permission)); - } - } - - // add all other urls - // {0} = repository - // {1} = username - for (String url : settings.getStrings(Keys.web.otherUrls)) { - if (url.contains("{1}")) { - // external url requires username, only add url IF we have one - if (!StringUtils.isEmpty(username)) { - list.add(new RepositoryUrl(MessageFormat.format(url, repository.name, username), null)); - } - } else { - // external url does not require username - list.add(new RepositoryUrl(MessageFormat.format(url, repository.name), null)); - } - } - return list; - } - - protected String getRepositoryUrl(HttpServletRequest request, String username, RepositoryModel repository) { - String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); - if (StringUtils.isEmpty(gitblitUrl)) { - gitblitUrl = HttpUtils.getGitblitURL(request); - } - StringBuilder sb = new StringBuilder(); - sb.append(gitblitUrl); - sb.append(Constants.R_PATH); - sb.append(repository.name); - - // inject username into repository url if authentication is required - if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE) - && !StringUtils.isEmpty(username)) { - sb.insert(sb.indexOf("://") + 3, username + "@"); - } - return sb.toString(); - } - - /** * Returns the list of custom client applications to be used for the * repository url panel; @@ -597,26 +533,6 @@ public class GitblitManager implements IGitblit { return runtimeManager.getSettingsModel(); } - @Override - public boolean isServingRepositories() { - return runtimeManager.isServingRepositories(); - } - - @Override - public boolean isServingHTTP() { - return runtimeManager.isServingHTTP(); - } - - @Override - public boolean isServingGIT() { - return runtimeManager.isServingGIT(); - } - - @Override - public boolean isServingSSH() { - return runtimeManager.isServingSSH(); - } - @Override public TimeZone getTimezone() { return runtimeManager.getTimezone(); diff --git a/src/main/java/com/gitblit/manager/IGitblit.java b/src/main/java/com/gitblit/manager/IGitblit.java index 50ec8b1f..6c5b374c 100644 --- a/src/main/java/com/gitblit/manager/IGitblit.java +++ b/src/main/java/com/gitblit/manager/IGitblit.java @@ -16,14 +16,10 @@ package com.gitblit.manager; import java.util.Collection; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; import com.gitblit.GitBlitException; import com.gitblit.models.GitClientApplication; import com.gitblit.models.RepositoryModel; -import com.gitblit.models.RepositoryUrl; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; import com.gitblit.tickets.ITicketService; @@ -39,17 +35,6 @@ public interface IGitblit extends IManager, IProjectManager, IFederationManager { - /** - * Returns a list of repository URLs and the user access permission. - * - * @param request - * @param user - * @param repository - * @return a list of repository urls - * @since 1.4.0 - */ - List getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository); - /** * Creates a complete user object. * diff --git a/src/main/java/com/gitblit/manager/IRuntimeManager.java b/src/main/java/com/gitblit/manager/IRuntimeManager.java index f736a057..8322d34f 100644 --- a/src/main/java/com/gitblit/manager/IRuntimeManager.java +++ b/src/main/java/com/gitblit/manager/IRuntimeManager.java @@ -50,42 +50,6 @@ public interface IRuntimeManager extends IManager { */ Locale getLocale(); - /** - * Determine if this Gitblit instance is actively serving git repositories - * or if it is merely a repository viewer. - * - * @return true if Gitblit is serving repositories - * @since 1.4.0 - */ - boolean isServingRepositories(); - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over HTTP. - * - * @return true if Gitblit is serving repositories over HTTP - * @since 1.6.0 - */ - boolean isServingHTTP(); - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over the GIT Daemon protocol. - * - * @return true if Gitblit is serving repositories over the GIT Daemon protocol - * @since 1.6.0 - */ - boolean isServingGIT(); - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over the SSH protocol. - * - * @return true if Gitblit is serving repositories over the SSH protocol - * @since 1.6.0 - */ - boolean isServingSSH(); - /** * Determine if this Gitblit instance is running in debug mode * diff --git a/src/main/java/com/gitblit/manager/IServicesManager.java b/src/main/java/com/gitblit/manager/IServicesManager.java new file mode 100644 index 00000000..5bb135dd --- /dev/null +++ b/src/main/java/com/gitblit/manager/IServicesManager.java @@ -0,0 +1,75 @@ +/* + * Copyright 2014 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.manager; + +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import com.gitblit.models.RepositoryModel; +import com.gitblit.models.RepositoryUrl; +import com.gitblit.models.UserModel; + +public interface IServicesManager extends IManager { + + /** + * Determine if this Gitblit instance is actively serving git repositories + * or if it is merely a repository viewer. + * + * @return true if Gitblit is serving repositories + * @since 1.7.0 + */ + boolean isServingRepositories(); + + /** + * Determine if this Gitblit instance is actively serving git repositories + * over HTTP. + * + * @return true if Gitblit is serving repositories over HTTP + * @since 1.7.0 + */ + boolean isServingHTTP(); + + /** + * Determine if this Gitblit instance is actively serving git repositories + * over the GIT Daemon protocol. + * + * @return true if Gitblit is serving repositories over the GIT Daemon protocol + * @since 1.7.0 + */ + boolean isServingGIT(); + + /** + * Determine if this Gitblit instance is actively serving git repositories + * over the SSH protocol. + * + * @return true if Gitblit is serving repositories over the SSH protocol + * @since 1.7.0 + */ + boolean isServingSSH(); + + /** + * Returns a list of repository URLs and the user access permission. + * + * @param request + * @param user + * @param repository + * @return a list of repository urls + * @since 1.7.0 + */ + List getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository); + +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 9b061e25..a417c025 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -1937,21 +1937,19 @@ public class RepositoryManager implements IRepositoryManager { } protected void confirmWriteAccess() { - if (runtimeManager.isServingRepositories()) { - try { - if (!getRepositoriesFolder().exists()) { - getRepositoriesFolder().mkdirs(); - } - File file = File.createTempFile(".test-", ".txt", getRepositoriesFolder()); - file.delete(); - } catch (Exception e) { - logger.error(""); - logger.error(Constants.BORDER2); - logger.error("Please check filesystem permissions!"); - logger.error("FAILED TO WRITE TO REPOSITORIES FOLDER!!", e); - logger.error(Constants.BORDER2); - logger.error(""); + try { + if (!getRepositoriesFolder().exists()) { + getRepositoriesFolder().mkdirs(); } + File file = File.createTempFile(".test-", ".txt", getRepositoriesFolder()); + file.delete(); + } catch (Exception e) { + logger.error(""); + logger.error(Constants.BORDER2); + logger.error("Please check filesystem permissions!"); + logger.error("FAILED TO WRITE TO REPOSITORIES FOLDER!!", e); + logger.error(Constants.BORDER2); + logger.error(""); } } } diff --git a/src/main/java/com/gitblit/manager/RuntimeManager.java b/src/main/java/com/gitblit/manager/RuntimeManager.java index 383b1dce..95a363f6 100644 --- a/src/main/java/com/gitblit/manager/RuntimeManager.java +++ b/src/main/java/com/gitblit/manager/RuntimeManager.java @@ -22,8 +22,6 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import com.google.inject.Inject; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,8 +32,11 @@ import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; import com.gitblit.models.SettingModel; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; import com.google.inject.Injector; +import com.google.inject.Singleton; +@Singleton public class RuntimeManager implements IRuntimeManager { private final Logger logger = LoggerFactory.getLogger(getClass()); @@ -123,52 +124,6 @@ public class RuntimeManager implements IRuntimeManager { return settingsModel; } - /** - * Determine if this Gitblit instance is actively serving git repositories - * or if it is merely a repository viewer. - * - * @return true if Gitblit is serving repositories - */ - @Override - public boolean isServingRepositories() { - return isServingHTTP() - || isServingGIT() - || isServingSSH(); - } - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over the HTTP protocol. - * - * @return true if Gitblit is serving repositories over the HTTP protocol - */ - @Override - public boolean isServingHTTP() { - return settings.getBoolean(Keys.git.enableGitServlet, true); - } - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over the Git Daemon protocol. - * - * @return true if Gitblit is serving repositories over the Git Daemon protocol - */ - @Override - public boolean isServingGIT() { - return settings.getInteger(Keys.git.daemonPort, 0) > 0; - } - - /** - * Determine if this Gitblit instance is actively serving git repositories - * over the SSH protocol. - * - * @return true if Gitblit is serving repositories over the SSH protocol - */ - @Override - public boolean isServingSSH() { - return settings.getInteger(Keys.git.sshPort, 0) > 0; - } - /** * Returns the preferred timezone for the Gitblit instance. * diff --git a/src/main/java/com/gitblit/manager/ServicesManager.java b/src/main/java/com/gitblit/manager/ServicesManager.java index 37215786..2550f66f 100644 --- a/src/main/java/com/gitblit/manager/ServicesManager.java +++ b/src/main/java/com/gitblit/manager/ServicesManager.java @@ -18,9 +18,15 @@ package com.gitblit.manager; import java.io.IOException; import java.net.URI; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -30,9 +36,11 @@ import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.gitblit.Constants; import com.gitblit.Constants.AccessPermission; import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.FederationToken; +import com.gitblit.Constants.Transport; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.fanout.FanoutNioService; @@ -40,14 +48,18 @@ import com.gitblit.fanout.FanoutService; import com.gitblit.fanout.FanoutSocketService; import com.gitblit.models.FederationModel; import com.gitblit.models.RepositoryModel; +import com.gitblit.models.RepositoryUrl; import com.gitblit.models.UserModel; import com.gitblit.service.FederationPullService; import com.gitblit.transport.git.GitDaemon; import com.gitblit.transport.ssh.SshDaemon; -import com.gitblit.utils.IdGenerator; +import com.gitblit.utils.HttpUtils; import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; import com.gitblit.utils.WorkQueue; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; /** * Services manager manages long-running services/processes that either have no @@ -57,32 +69,35 @@ import com.gitblit.utils.WorkQueue; * @author James Moger * */ -public class ServicesManager implements IManager { +@Singleton +public class ServicesManager implements IServicesManager { private final Logger logger = LoggerFactory.getLogger(getClass()); private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(5); + private final Provider workQueueProvider; + private final IStoredSettings settings; private final IGitblit gitblit; - private final IdGenerator idGenerator; - - private final WorkQueue workQueue; - private FanoutService fanoutService; private GitDaemon gitDaemon; private SshDaemon sshDaemon; - public ServicesManager(IGitblit gitblit) { - this.settings = gitblit.getSettings(); + @Inject + public ServicesManager( + Provider workQueueProvider, + IStoredSettings settings, + IGitblit gitblit) { + + this.workQueueProvider = workQueueProvider; + + this.settings = settings; this.gitblit = gitblit; - int defaultThreadPoolSize = settings.getInteger(Keys.execution.defaultThreadPoolSize, 1); - this.idGenerator = new IdGenerator(); - this.workQueue = new WorkQueue(idGenerator, defaultThreadPoolSize); } @Override @@ -107,24 +122,181 @@ public class ServicesManager implements IManager { if (sshDaemon != null) { sshDaemon.stop(); } - workQueue.stop(); + workQueueProvider.get().stop(); return this; } + protected String getRepositoryUrl(HttpServletRequest request, String username, RepositoryModel repository) { + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); + if (StringUtils.isEmpty(gitblitUrl)) { + gitblitUrl = HttpUtils.getGitblitURL(request); + } + StringBuilder sb = new StringBuilder(); + sb.append(gitblitUrl); + sb.append(Constants.R_PATH); + sb.append(repository.name); + + // inject username into repository url if authentication is required + if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE) + && !StringUtils.isEmpty(username)) { + sb.insert(sb.indexOf("://") + 3, username + "@"); + } + return sb.toString(); + } + + /** + * Returns a list of repository URLs and the user access permission. + * + * @param request + * @param user + * @param repository + * @return a list of repository urls + */ + @Override + public List getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) { + if (user == null) { + user = UserModel.ANONYMOUS; + } + String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username); + + List list = new ArrayList(); + + // http/https url + if (settings.getBoolean(Keys.git.enableGitServlet, true)) { + AccessPermission permission = user.getRepositoryPermission(repository).permission; + if (permission.exceeds(AccessPermission.NONE)) { + Transport transport = Transport.fromString(request.getScheme()); + if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(transport)) { + // downgrade the repo permission for this transport + // because it is not an acceptable PUSH transport + permission = AccessPermission.CLONE; + } + list.add(new RepositoryUrl(getRepositoryUrl(request, username, repository), permission)); + } + } + + // ssh daemon url + String sshDaemonUrl = getSshDaemonUrl(request, user, repository); + if (!StringUtils.isEmpty(sshDaemonUrl)) { + AccessPermission permission = user.getRepositoryPermission(repository).permission; + if (permission.exceeds(AccessPermission.NONE)) { + if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(Transport.SSH)) { + // downgrade the repo permission for this transport + // because it is not an acceptable PUSH transport + permission = AccessPermission.CLONE; + } + + list.add(new RepositoryUrl(sshDaemonUrl, permission)); + } + } + + // git daemon url + String gitDaemonUrl = getGitDaemonUrl(request, user, repository); + if (!StringUtils.isEmpty(gitDaemonUrl)) { + AccessPermission permission = getGitDaemonAccessPermission(user, repository); + if (permission.exceeds(AccessPermission.NONE)) { + if (permission.atLeast(AccessPermission.PUSH) && !acceptPush(Transport.GIT)) { + // downgrade the repo permission for this transport + // because it is not an acceptable PUSH transport + permission = AccessPermission.CLONE; + } + list.add(new RepositoryUrl(gitDaemonUrl, permission)); + } + } + + // add all other urls + // {0} = repository + // {1} = username + for (String url : settings.getStrings(Keys.web.otherUrls)) { + if (url.contains("{1}")) { + // external url requires username, only add url IF we have one + if (!StringUtils.isEmpty(username)) { + list.add(new RepositoryUrl(MessageFormat.format(url, repository.name, username), null)); + } + } else { + // external url does not require username + list.add(new RepositoryUrl(MessageFormat.format(url, repository.name), null)); + } + } + + // sort transports by highest permission and then by transport security + Collections.sort(list, new Comparator() { + + @Override + public int compare(RepositoryUrl o1, RepositoryUrl o2) { + if (!o1.isExternal() && o2.isExternal()) { + // prefer Gitblit over external + return -1; + } else if (o1.isExternal() && !o2.isExternal()) { + // prefer Gitblit over external + return 1; + } else if (o1.isExternal() && o2.isExternal()) { + // sort by Transport ordinal + return o1.transport.compareTo(o2.transport); + } else if (o1.permission.exceeds(o2.permission)) { + // prefer highest permission + return -1; + } else if (o2.permission.exceeds(o1.permission)) { + // prefer highest permission + return 1; + } + + // prefer more secure transports + return o1.transport.compareTo(o2.transport); + } + }); + + // consider the user's transport preference + RepositoryUrl preferredUrl = null; + Transport preferredTransport = user.getPreferences().getTransport(); + if (preferredTransport != null) { + Iterator itr = list.iterator(); + while (itr.hasNext()) { + RepositoryUrl url = itr.next(); + if (url.transport.equals(preferredTransport)) { + itr.remove(); + preferredUrl = url; + break; + } + } + } + if (preferredUrl != null) { + list.add(0, preferredUrl); + } + + return list; + } + + /* (non-Javadoc) + * @see com.gitblit.manager.IServicesManager#isServingRepositories() + */ + @Override public boolean isServingRepositories() { return isServingHTTP() || isServingGIT() || isServingSSH(); } + /* (non-Javadoc) + * @see com.gitblit.manager.IServicesManager#isServingHTTP() + */ + @Override public boolean isServingHTTP() { return settings.getBoolean(Keys.git.enableGitServlet, true); } + /* (non-Javadoc) + * @see com.gitblit.manager.IServicesManager#isServingGIT() + */ + @Override public boolean isServingGIT() { return gitDaemon != null && gitDaemon.isRunning(); } + /* (non-Javadoc) + * @see com.gitblit.manager.IServicesManager#isServingSSH() + */ + @Override public boolean isServingSSH() { return sshDaemon != null && sshDaemon.isRunning(); } @@ -158,6 +330,32 @@ public class ServicesManager implements IManager { } } + protected boolean acceptPush(Transport byTransport) { + if (byTransport == null) { + logger.info("Unknown transport, push rejected!"); + return false; + } + + Set transports = new HashSet(); + for (String value : settings.getStrings(Keys.git.acceptedPushTransports)) { + Transport transport = Transport.fromString(value); + if (transport == null) { + logger.info(String.format("Ignoring unknown registered transport %s", value)); + continue; + } + + transports.add(transport); + } + + if (transports.isEmpty()) { + // no transports are explicitly specified, all are acceptable + return true; + } + + // verify that the transport is permitted + return transports.contains(byTransport); + } + protected void configureGitDaemon() { int port = settings.getInteger(Keys.git.daemonPort, 0); String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); @@ -179,7 +377,7 @@ public class ServicesManager implements IManager { String bindInterface = settings.getString(Keys.git.sshBindInterface, "localhost"); if (port > 0) { try { - sshDaemon = new SshDaemon(gitblit, workQueue); + sshDaemon = new SshDaemon(gitblit, workQueueProvider.get()); sshDaemon.start(); } catch (IOException e) { sshDaemon = null; @@ -285,7 +483,7 @@ public class ServicesManager implements IManager { */ protected String getHostname(HttpServletRequest request) { String hostname = request.getServerName(); - String canonicalUrl = gitblit.getSettings().getString(Keys.web.canonicalUrl, null); + String canonicalUrl = settings.getString(Keys.web.canonicalUrl, null); if (!StringUtils.isEmpty(canonicalUrl)) { try { URI uri = new URI(canonicalUrl); diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index e0f6003c..44a857ca 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -51,6 +51,7 @@ import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IServicesManager; import com.gitblit.manager.IUserManager; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.ContainerUtils; @@ -200,6 +201,7 @@ public class GitblitContext extends GuiceServletContextListener { startManager(injector, IProjectManager.class); startManager(injector, IFederationManager.class); startManager(injector, IGitblit.class); + startManager(injector, IServicesManager.class); // start the plugin manager last so that plugins can depend on // deterministic access to all other managers in their start() methods diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index a9073110..24468c0b 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -21,9 +21,6 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import com.google.inject.Inject; -import com.google.inject.Singleton; - import org.apache.wicket.Application; import org.apache.wicket.Request; import org.apache.wicket.Response; @@ -46,6 +43,7 @@ import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IServicesManager; import com.gitblit.manager.IUserManager; import com.gitblit.tickets.ITicketService; import com.gitblit.transport.ssh.IPublicKeyManager; @@ -92,6 +90,8 @@ import com.gitblit.wicket.pages.TicketsPage; import com.gitblit.wicket.pages.TreePage; import com.gitblit.wicket.pages.UserPage; import com.gitblit.wicket.pages.UsersPage; +import com.google.inject.Inject; +import com.google.inject.Singleton; @Singleton public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { @@ -124,6 +124,8 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final IGitblit gitblit; + private final IServicesManager services; + @Inject public GitBlitWebApp( IRuntimeManager runtimeManager, @@ -135,7 +137,8 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { IRepositoryManager repositoryManager, IProjectManager projectManager, IFederationManager federationManager, - IGitblit gitblit) { + IGitblit gitblit, + IServicesManager services) { super(); this.settings = runtimeManager.getSettings(); @@ -149,6 +152,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { this.projectManager = projectManager; this.federationManager = federationManager; this.gitblit = gitblit; + this.services = services; } @Override @@ -420,6 +424,14 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { return gitblit; } + /* (non-Javadoc) + * @see com.gitblit.wicket.Webapp#services() + */ + @Override + public IServicesManager services() { + return services; + } + /* (non-Javadoc) * @see com.gitblit.wicket.Webapp#tickets() */ diff --git a/src/main/java/com/gitblit/wicket/GitblitWicketApp.java b/src/main/java/com/gitblit/wicket/GitblitWicketApp.java index a56e6996..7013097c 100644 --- a/src/main/java/com/gitblit/wicket/GitblitWicketApp.java +++ b/src/main/java/com/gitblit/wicket/GitblitWicketApp.java @@ -14,6 +14,7 @@ import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; +import com.gitblit.manager.IServicesManager; import com.gitblit.manager.IUserManager; import com.gitblit.tickets.ITicketService; import com.gitblit.transport.ssh.IPublicKeyManager; @@ -65,6 +66,8 @@ public interface GitblitWicketApp { public abstract IGitblit gitblit(); + public abstract IServicesManager services(); + public abstract ITicketService tickets(); public abstract TimeZone getTimezone(); diff --git a/src/main/java/com/gitblit/wicket/pages/EmptyRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/EmptyRepositoryPage.java index b3c52436..72d1e1a4 100644 --- a/src/main/java/com/gitblit/wicket/pages/EmptyRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EmptyRepositoryPage.java @@ -55,7 +55,7 @@ public class EmptyRepositoryPage extends RepositoryPage { } HttpServletRequest req = ((WebRequest) getRequest()).getHttpServletRequest(); - List repositoryUrls = app().gitblit().getRepositoryUrls(req, user, repository); + List repositoryUrls = app().services().getRepositoryUrls(req, user, repository); RepositoryUrl primaryUrl = repositoryUrls.size() == 0 ? null : repositoryUrls.get(0); String url = primaryUrl != null ? primaryUrl.url : ""; diff --git a/src/main/java/com/gitblit/wicket/pages/TicketPage.java b/src/main/java/com/gitblit/wicket/pages/TicketPage.java index f5f63d23..e1e81628 100644 --- a/src/main/java/com/gitblit/wicket/pages/TicketPage.java +++ b/src/main/java/com/gitblit/wicket/pages/TicketPage.java @@ -1505,7 +1505,7 @@ public class TicketPage extends RepositoryPage { */ protected RepositoryUrl getRepositoryUrl(UserModel user, RepositoryModel repository) { HttpServletRequest req = ((WebRequest) getRequest()).getHttpServletRequest(); - List urls = app().gitblit().getRepositoryUrls(req, user, repository); + List urls = app().services().getRepositoryUrls(req, user, repository); if (ArrayUtils.isEmpty(urls)) { return null; } diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index 8931d5e3..e21431d9 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -104,7 +104,7 @@ public class UserPage extends RootPage { if (isMyProfile) { addPreferences(user); - if (app().gitblit().isServingSSH()) { + if (app().services().isServingSSH()) { // show the SSH key management tab addSshKeys(user); } else { @@ -248,14 +248,14 @@ public class UserPage extends RootPage { emailMeOnMyTicketChanges).setVisible(app().notifier().isSendingMail())); List availableTransports = new ArrayList<>(); - if (app().gitblit().isServingSSH()) { + if (app().services().isServingSSH()) { availableTransports.add(Transport.SSH); } - if (app().gitblit().isServingHTTP()) { + if (app().services().isServingHTTP()) { availableTransports.add(Transport.HTTPS); availableTransports.add(Transport.HTTP); } - if (app().gitblit().isServingGIT()) { + if (app().services().isServingGIT()) { availableTransports.add(Transport.GIT); } diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java index 938226a6..0666fcd8 100644 --- a/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/RepositoryUrlPanel.java @@ -80,7 +80,7 @@ public class RepositoryUrlPanel extends BasePanel { HttpServletRequest req = ((WebRequest) getRequest()).getHttpServletRequest(); - List repositoryUrls = app().gitblit().getRepositoryUrls(req, user, repository); + List repositoryUrls = app().services().getRepositoryUrls(req, user, repository); // grab primary url from the top of the list primaryUrl = repositoryUrls.size() == 0 ? null : repositoryUrls.get(0); @@ -165,7 +165,7 @@ public class RepositoryUrlPanel extends BasePanel { if (repository.isMirror) { urlPanel.add(WicketUtils.newImage("accessRestrictionIcon", "mirror_16x16.png", getString("gb.isMirror"))); - } else if (app().gitblit().isServingRepositories()) { + } else if (app().services().isServingRepositories()) { switch (repository.accessRestriction) { case NONE: urlPanel.add(WicketUtils.newClearPixel("accessRestrictionIcon").setVisible(false)); diff --git a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java index 36a0218e..9a71c884 100644 --- a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java +++ b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java @@ -82,26 +82,6 @@ public class MockRuntimeManager implements IRuntimeManager { return Locale.getDefault(); } - @Override - public boolean isServingRepositories() { - return true; - } - - @Override - public boolean isServingHTTP() { - return true; - } - - @Override - public boolean isServingGIT() { - return true; - } - - @Override - public boolean isServingSSH() { - return true; - } - @Override public boolean isDebugMode() { return true; -- cgit v1.2.3 From f9980ea7a6615f8bfef89fae7e6569c54bc749c3 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 16:30:55 -0400 Subject: Annotate managers with @Singleton --- src/main/java/com/gitblit/GitBlit.java | 5 +++-- src/main/java/com/gitblit/guice/CoreModule.java | 20 +++++++++----------- .../com/gitblit/manager/AuthenticationManager.java | 4 +++- .../java/com/gitblit/manager/FederationManager.java | 4 +++- .../java/com/gitblit/manager/GitblitManager.java | 4 ++++ .../com/gitblit/manager/NotificationManager.java | 4 +++- src/main/java/com/gitblit/manager/PluginManager.java | 5 +++-- .../java/com/gitblit/manager/ProjectManager.java | 5 +++-- .../java/com/gitblit/manager/RepositoryManager.java | 5 +++-- src/main/java/com/gitblit/manager/UserManager.java | 5 +++-- 10 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index edad32ef..7b9b904c 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -24,8 +24,6 @@ import java.util.Iterator; import java.util.List; import java.util.Set; -import com.google.inject.Inject; - import com.gitblit.manager.GitblitManager; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; @@ -44,7 +42,9 @@ import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.StringUtils; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Inject; import com.google.inject.Injector; +import com.google.inject.Singleton; /** * GitBlit is the aggregate manager for the Gitblit webapp. It provides all @@ -53,6 +53,7 @@ import com.google.inject.Injector; * @author James Moger * */ +@Singleton public class GitBlit extends GitblitManager { private final Injector injector; diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index 03cdbf22..cc73f095 100644 --- a/src/main/java/com/gitblit/guice/CoreModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -15,8 +15,6 @@ */ package com.gitblit.guice; -import com.google.inject.Singleton; - import com.gitblit.FileSettings; import com.gitblit.GitBlit; import com.gitblit.IStoredSettings; @@ -66,17 +64,17 @@ public class CoreModule extends AbstractModule { bind(WorkQueue.class).toProvider(WorkQueueProvider.class); // core managers - bind(IRuntimeManager.class).to(RuntimeManager.class).in(Singleton.class); - bind(IPluginManager.class).to(PluginManager.class).in(Singleton.class); - bind(INotificationManager.class).to(NotificationManager.class).in(Singleton.class); - bind(IUserManager.class).to(UserManager.class).in(Singleton.class); - bind(IAuthenticationManager.class).to(AuthenticationManager.class).in(Singleton.class); - bind(IRepositoryManager.class).to(RepositoryManager.class).in(Singleton.class); - bind(IProjectManager.class).to(ProjectManager.class).in(Singleton.class); - bind(IFederationManager.class).to(FederationManager.class).in(Singleton.class); + bind(IRuntimeManager.class).to(RuntimeManager.class); + bind(IPluginManager.class).to(PluginManager.class); + bind(INotificationManager.class).to(NotificationManager.class); + bind(IUserManager.class).to(UserManager.class); + bind(IAuthenticationManager.class).to(AuthenticationManager.class); + bind(IRepositoryManager.class).to(RepositoryManager.class); + bind(IProjectManager.class).to(ProjectManager.class); + bind(IFederationManager.class).to(FederationManager.class); // the monolithic manager - bind(IGitblit.class).to(GitBlit.class).in(Singleton.class); + bind(IGitblit.class).to(GitBlit.class); // manager for long-running daemons and services bind(IServicesManager.class).to(ServicesManager.class); diff --git a/src/main/java/com/gitblit/manager/AuthenticationManager.java b/src/main/java/com/gitblit/manager/AuthenticationManager.java index de7e34cb..ccf03c0b 100644 --- a/src/main/java/com/gitblit/manager/AuthenticationManager.java +++ b/src/main/java/com/gitblit/manager/AuthenticationManager.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import com.google.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -54,6 +53,8 @@ import com.gitblit.utils.HttpUtils; import com.gitblit.utils.StringUtils; import com.gitblit.utils.X509Utils.X509Metadata; import com.gitblit.wicket.GitBlitWebSession; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * The authentication manager handles user login & logout. @@ -61,6 +62,7 @@ import com.gitblit.wicket.GitBlitWebSession; * @author James Moger * */ +@Singleton public class AuthenticationManager implements IAuthenticationManager { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/FederationManager.java b/src/main/java/com/gitblit/manager/FederationManager.java index b4dc5010..f009c1c8 100644 --- a/src/main/java/com/gitblit/manager/FederationManager.java +++ b/src/main/java/com/gitblit/manager/FederationManager.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import com.google.inject.Inject; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; @@ -46,6 +45,8 @@ import com.gitblit.utils.Base64; import com.gitblit.utils.FederationUtils; import com.gitblit.utils.JsonUtils; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Federation manager controls all aspects of handling federation sets, tokens, @@ -54,6 +55,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class FederationManager implements IFederationManager { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 2c88e405..5c2eccf0 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -83,7 +83,9 @@ import com.google.gson.Gson; import com.google.gson.JsonIOException; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; +import com.google.inject.Inject; import com.google.inject.Injector; +import com.google.inject.Singleton; /** * GitblitManager is an aggregate interface delegate. It implements all the manager @@ -97,6 +99,7 @@ import com.google.inject.Injector; * @author James Moger * */ +@Singleton public class GitblitManager implements IGitblit { protected final Logger logger = LoggerFactory.getLogger(getClass()); @@ -123,6 +126,7 @@ public class GitblitManager implements IGitblit { protected final IFederationManager federationManager; + @Inject public GitblitManager( IRuntimeManager runtimeManager, IPluginManager pluginManager, diff --git a/src/main/java/com/gitblit/manager/NotificationManager.java b/src/main/java/com/gitblit/manager/NotificationManager.java index ebc21b3a..4bbc2abf 100644 --- a/src/main/java/com/gitblit/manager/NotificationManager.java +++ b/src/main/java/com/gitblit/manager/NotificationManager.java @@ -20,7 +20,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import com.google.inject.Inject; import javax.mail.Message; import org.slf4j.Logger; @@ -30,6 +29,8 @@ import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.models.Mailing; import com.gitblit.service.MailService; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * The notification manager dispatches notifications. Currently, email is the @@ -39,6 +40,7 @@ import com.gitblit.service.MailService; * @author James Moger * */ +@Singleton public class NotificationManager implements INotificationManager { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index 268eef14..dcf12b26 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -34,8 +34,6 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import com.google.inject.Inject; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,6 +58,8 @@ import com.gitblit.utils.JsonUtils; import com.gitblit.utils.StringUtils; import com.google.common.io.Files; import com.google.common.io.InputSupplier; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * The plugin manager maintains the lifecycle of plugins. It is exposed as @@ -70,6 +70,7 @@ import com.google.common.io.InputSupplier; * @author James Moger * */ +@Singleton public class PluginManager implements IPluginManager, PluginStateListener { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/ProjectManager.java b/src/main/java/com/gitblit/manager/ProjectManager.java index 80675a76..ae46bdfc 100644 --- a/src/main/java/com/gitblit/manager/ProjectManager.java +++ b/src/main/java/com/gitblit/manager/ProjectManager.java @@ -27,8 +27,6 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; -import com.google.inject.Inject; - import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.util.FS; import org.slf4j.Logger; @@ -43,6 +41,8 @@ import com.gitblit.utils.DeepCopier; import com.gitblit.utils.ModelUtils; import com.gitblit.utils.ObjectCache; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Project manager handles project-related functions. @@ -50,6 +50,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class ProjectManager implements IProjectManager { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index a417c025..24f35734 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -44,8 +44,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import com.google.inject.Inject; - import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryCache.FileKey; @@ -91,6 +89,8 @@ import com.gitblit.utils.ModelUtils; import com.gitblit.utils.ObjectCache; import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Repository manager creates, updates, deletes and caches git repositories. It @@ -99,6 +99,7 @@ import com.gitblit.utils.TimeUtils; * @author James Moger * */ +@Singleton public class RepositoryManager implements IRepositoryManager { private final Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java index 8d6d4c14..86be8bcd 100644 --- a/src/main/java/com/gitblit/manager/UserManager.java +++ b/src/main/java/com/gitblit/manager/UserManager.java @@ -24,8 +24,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import com.google.inject.Inject; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +36,8 @@ import com.gitblit.extensions.UserTeamLifeCycleListener; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * The user manager manages persistence and retrieval of users and teams. @@ -45,6 +45,7 @@ import com.gitblit.utils.StringUtils; * @author James Moger * */ +@Singleton public class UserManager implements IUserManager { private final Logger logger = LoggerFactory.getLogger(getClass()); -- cgit v1.2.3 From 241f57365649ffbbcc1923a1712ab86f6a443bc6 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 16:39:47 -0400 Subject: Implement custom IPublicKeyManager provider --- src/main/java/com/gitblit/FederationClient.java | 378 ++++++++++----------- src/main/java/com/gitblit/GitBlit.java | 26 +- src/main/java/com/gitblit/guice/CoreModule.java | 34 +- .../gitblit/guice/IPublicKeyManagerProvider.java | 72 ++++ .../java/com/gitblit/manager/GitblitManager.java | 33 +- .../com/gitblit/transport/ssh/FileKeyManager.java | 2 + .../gitblit/transport/ssh/MemoryKeyManager.java | 3 + .../com/gitblit/transport/ssh/NullKeyManager.java | 3 + .../java/com/gitblit/wicket/GitBlitWebApp.java | 11 +- 9 files changed, 304 insertions(+), 258 deletions(-) create mode 100644 src/main/java/com/gitblit/guice/IPublicKeyManagerProvider.java diff --git a/src/main/java/com/gitblit/FederationClient.java b/src/main/java/com/gitblit/FederationClient.java index 29cdefe6..af92b5f9 100644 --- a/src/main/java/com/gitblit/FederationClient.java +++ b/src/main/java/com/gitblit/FederationClient.java @@ -1,189 +1,189 @@ -/* - * Copyright 2011 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 java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.kohsuke.args4j.CmdLineException; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.Option; - -import com.gitblit.manager.FederationManager; -import com.gitblit.manager.GitblitManager; -import com.gitblit.manager.IGitblit; -import com.gitblit.manager.INotificationManager; -import com.gitblit.manager.RepositoryManager; -import com.gitblit.manager.RuntimeManager; -import com.gitblit.manager.UserManager; -import com.gitblit.models.FederationModel; -import com.gitblit.models.Mailing; -import com.gitblit.service.FederationPullService; -import com.gitblit.utils.FederationUtils; -import com.gitblit.utils.StringUtils; - -/** - * Command-line client to pull federated Gitblit repositories. - * - * @author James Moger - * - */ -public class FederationClient { - - public static void main(String[] args) { - Params params = new Params(); - CmdLineParser parser = new CmdLineParser(params); - try { - parser.parseArgument(args); - } catch (CmdLineException t) { - usage(parser, t); - } - - System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")"); - - // command-line specified base folder - File baseFolder = new File(System.getProperty("user.dir")); - if (!StringUtils.isEmpty(params.baseFolder)) { - baseFolder = new File(params.baseFolder); - } - - File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile); - FileSettings settings = new FileSettings(regFile.getAbsolutePath()); - List registrations = new ArrayList(); - if (StringUtils.isEmpty(params.url)) { - registrations.addAll(FederationUtils.getFederationRegistrations(settings)); - } else { - if (StringUtils.isEmpty(params.token)) { - System.out.println("Must specify --token parameter!"); - System.exit(0); - } - FederationModel model = new FederationModel("Gitblit"); - model.url = params.url; - model.token = params.token; - model.mirror = params.mirror; - model.bare = params.bare; - model.folder = ""; - registrations.add(model); - } - if (registrations.size() == 0) { - System.out.println("No Federation Registrations! Nothing to do."); - System.exit(0); - } - - // command-line specified repositories folder - if (!StringUtils.isEmpty(params.repositoriesFolder)) { - settings.overrideSetting(Keys.git.repositoriesFolder, new File( - params.repositoriesFolder).getAbsolutePath()); - } - - // configure the Gitblit singleton for minimal, non-server operation - RuntimeManager runtime = new RuntimeManager(settings, baseFolder).start(); - NoopNotificationManager notifications = new NoopNotificationManager().start(); - UserManager users = new UserManager(runtime, null).start(); - RepositoryManager repositories = new RepositoryManager(runtime, null, users).start(); - FederationManager federation = new FederationManager(runtime, notifications, repositories).start(); - IGitblit gitblit = new GitblitManager(runtime, null, notifications, users, null, null, repositories, null, federation); - - FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) { - @Override - public void reschedule(FederationModel registration) { - // NOOP - } - }; - puller.run(); - - System.out.println("Finished."); - System.exit(0); - } - - private static void usage(CmdLineParser parser, CmdLineException t) { - System.out.println(Constants.getGitBlitVersion()); - System.out.println(); - if (t != null) { - System.out.println(t.getMessage()); - System.out.println(); - } - - if (parser != null) { - parser.printUsage(System.out); - } - System.exit(0); - } - - /** - * Parameters class for FederationClient. - */ - private static class Params { - - @Option(name = "--registrations", usage = "Gitblit Federation Registrations File", metaVar = "FILE") - public String registrationsFile = "${baseFolder}/federation.properties"; - - @Option(name = "--url", usage = "URL of Gitblit instance to mirror from", metaVar = "URL") - public String url; - - @Option(name = "--mirror", usage = "Mirror repositories") - public boolean mirror; - - @Option(name = "--bare", usage = "Create bare repositories") - public boolean bare; - - @Option(name = "--token", usage = "Federation Token", metaVar = "TOKEN") - public String token; - - @Option(name = "--baseFolder", usage = "Base folder for received data", metaVar = "PATH") - public String baseFolder; - - @Option(name = "--repositoriesFolder", usage = "Destination folder for cloned repositories", metaVar = "PATH") - public String repositoriesFolder; - - } - - private static class NoopNotificationManager implements INotificationManager { - - @Override - public NoopNotificationManager start() { - return this; - } - - @Override - public NoopNotificationManager stop() { - return this; - } - - @Override - public boolean isSendingMail() { - return false; - } - - @Override - public void sendMailToAdministrators(String subject, String message) { - } - - @Override - public void sendMail(String subject, String message, Collection toAddresses) { - } - - @Override - public void sendHtmlMail(String subject, String message, Collection toAddresses) { - } - - @Override - public void send(Mailing mailing) { - } - } -} +/* + * Copyright 2011 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 java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.Option; + +import com.gitblit.manager.FederationManager; +import com.gitblit.manager.GitblitManager; +import com.gitblit.manager.IGitblit; +import com.gitblit.manager.INotificationManager; +import com.gitblit.manager.RepositoryManager; +import com.gitblit.manager.RuntimeManager; +import com.gitblit.manager.UserManager; +import com.gitblit.models.FederationModel; +import com.gitblit.models.Mailing; +import com.gitblit.service.FederationPullService; +import com.gitblit.utils.FederationUtils; +import com.gitblit.utils.StringUtils; + +/** + * Command-line client to pull federated Gitblit repositories. + * + * @author James Moger + * + */ +public class FederationClient { + + public static void main(String[] args) { + Params params = new Params(); + CmdLineParser parser = new CmdLineParser(params); + try { + parser.parseArgument(args); + } catch (CmdLineException t) { + usage(parser, t); + } + + System.out.println("Gitblit Federation Client v" + Constants.getVersion() + " (" + Constants.getBuildDate() + ")"); + + // command-line specified base folder + File baseFolder = new File(System.getProperty("user.dir")); + if (!StringUtils.isEmpty(params.baseFolder)) { + baseFolder = new File(params.baseFolder); + } + + File regFile = com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, baseFolder, params.registrationsFile); + FileSettings settings = new FileSettings(regFile.getAbsolutePath()); + List registrations = new ArrayList(); + if (StringUtils.isEmpty(params.url)) { + registrations.addAll(FederationUtils.getFederationRegistrations(settings)); + } else { + if (StringUtils.isEmpty(params.token)) { + System.out.println("Must specify --token parameter!"); + System.exit(0); + } + FederationModel model = new FederationModel("Gitblit"); + model.url = params.url; + model.token = params.token; + model.mirror = params.mirror; + model.bare = params.bare; + model.folder = ""; + registrations.add(model); + } + if (registrations.size() == 0) { + System.out.println("No Federation Registrations! Nothing to do."); + System.exit(0); + } + + // command-line specified repositories folder + if (!StringUtils.isEmpty(params.repositoriesFolder)) { + settings.overrideSetting(Keys.git.repositoriesFolder, new File( + params.repositoriesFolder).getAbsolutePath()); + } + + // configure the Gitblit singleton for minimal, non-server operation + RuntimeManager runtime = new RuntimeManager(settings, baseFolder).start(); + NoopNotificationManager notifications = new NoopNotificationManager().start(); + UserManager users = new UserManager(runtime, null).start(); + RepositoryManager repositories = new RepositoryManager(runtime, null, users).start(); + FederationManager federation = new FederationManager(runtime, notifications, repositories).start(); + IGitblit gitblit = new GitblitManager(null, runtime, null, notifications, users, null, repositories, null, federation); + + FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) { + @Override + public void reschedule(FederationModel registration) { + // NOOP + } + }; + puller.run(); + + System.out.println("Finished."); + System.exit(0); + } + + private static void usage(CmdLineParser parser, CmdLineException t) { + System.out.println(Constants.getGitBlitVersion()); + System.out.println(); + if (t != null) { + System.out.println(t.getMessage()); + System.out.println(); + } + + if (parser != null) { + parser.printUsage(System.out); + } + System.exit(0); + } + + /** + * Parameters class for FederationClient. + */ + private static class Params { + + @Option(name = "--registrations", usage = "Gitblit Federation Registrations File", metaVar = "FILE") + public String registrationsFile = "${baseFolder}/federation.properties"; + + @Option(name = "--url", usage = "URL of Gitblit instance to mirror from", metaVar = "URL") + public String url; + + @Option(name = "--mirror", usage = "Mirror repositories") + public boolean mirror; + + @Option(name = "--bare", usage = "Create bare repositories") + public boolean bare; + + @Option(name = "--token", usage = "Federation Token", metaVar = "TOKEN") + public String token; + + @Option(name = "--baseFolder", usage = "Base folder for received data", metaVar = "PATH") + public String baseFolder; + + @Option(name = "--repositoriesFolder", usage = "Destination folder for cloned repositories", metaVar = "PATH") + public String repositoriesFolder; + + } + + private static class NoopNotificationManager implements INotificationManager { + + @Override + public NoopNotificationManager start() { + return this; + } + + @Override + public NoopNotificationManager stop() { + return this; + } + + @Override + public boolean isSendingMail() { + return false; + } + + @Override + public void sendMailToAdministrators(String subject, String message) { + } + + @Override + public void sendMail(String subject, String message, Collection toAddresses) { + } + + @Override + public void sendHtmlMail(String subject, String message, Collection toAddresses) { + } + + @Override + public void send(Mailing mailing) { + } + } +} diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 7b9b904c..9a01d3ce 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -44,6 +44,7 @@ import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; +import com.google.inject.Provider; import com.google.inject.Singleton; /** @@ -62,22 +63,23 @@ public class GitBlit extends GitblitManager { @Inject public GitBlit( + Provider publicKeyManagerProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, IUserManager userManager, IAuthenticationManager authenticationManager, - IPublicKeyManager publicKeyManager, IRepositoryManager repositoryManager, IProjectManager projectManager, IFederationManager federationManager) { - super(runtimeManager, + super( + publicKeyManagerProvider, + runtimeManager, pluginManager, notificationManager, userManager, authenticationManager, - publicKeyManager, repositoryManager, projectManager, federationManager); @@ -122,24 +124,6 @@ public class GitBlit extends GitblitManager { } } - /** - * Delete the user and all associated public ssh keys. - */ - @Override - public boolean deleteUser(String username) { - UserModel user = userManager.getUserModel(username); - return deleteUserModel(user); - } - - @Override - public boolean deleteUserModel(UserModel model) { - boolean success = userManager.deleteUserModel(model); - if (success) { - getPublicKeyManager().removeAllKeys(model.username); - } - return success; - } - /** * Delete the repository and all associated tickets. */ diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index cc73f095..6dbe6150 100644 --- a/src/main/java/com/gitblit/guice/CoreModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -18,7 +18,6 @@ package com.gitblit.guice; import com.gitblit.FileSettings; import com.gitblit.GitBlit; import com.gitblit.IStoredSettings; -import com.gitblit.Keys; import com.gitblit.manager.AuthenticationManager; import com.gitblit.manager.FederationManager; import com.gitblit.manager.IAuthenticationManager; @@ -38,14 +37,9 @@ import com.gitblit.manager.RepositoryManager; import com.gitblit.manager.RuntimeManager; import com.gitblit.manager.ServicesManager; import com.gitblit.manager.UserManager; -import com.gitblit.transport.ssh.FileKeyManager; import com.gitblit.transport.ssh.IPublicKeyManager; -import com.gitblit.transport.ssh.MemoryKeyManager; -import com.gitblit.transport.ssh.NullKeyManager; -import com.gitblit.utils.StringUtils; import com.gitblit.utils.WorkQueue; import com.google.inject.AbstractModule; -import com.google.inject.Provides; /** * CoreModule references all the core business objects. @@ -61,8 +55,9 @@ public class CoreModule extends AbstractModule { bind(IStoredSettings.class).toInstance(new FileSettings()); // bind complex providers + bind(IPublicKeyManager.class).toProvider(IPublicKeyManagerProvider.class); bind(WorkQueue.class).toProvider(WorkQueueProvider.class); - + // core managers bind(IRuntimeManager.class).to(RuntimeManager.class); bind(IPluginManager.class).to(PluginManager.class); @@ -79,29 +74,4 @@ public class CoreModule extends AbstractModule { // manager for long-running daemons and services bind(IServicesManager.class).to(ServicesManager.class); } - - @Provides - @Singleton - IPublicKeyManager providePublicKeyManager(IStoredSettings settings, IRuntimeManager runtimeManager) { - - String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); - if (StringUtils.isEmpty(clazz)) { - clazz = FileKeyManager.class.getName(); - } - if (FileKeyManager.class.getName().equals(clazz)) { - return new FileKeyManager(runtimeManager); - } else if (NullKeyManager.class.getName().equals(clazz)) { - return new NullKeyManager(); - } else if (MemoryKeyManager.class.getName().equals(clazz)) { - return new MemoryKeyManager(); - } else { - try { - Class mgrClass = Class.forName(clazz); - return (IPublicKeyManager) mgrClass.newInstance(); - } catch (Exception e) { - - } - return null; - } - } } \ No newline at end of file diff --git a/src/main/java/com/gitblit/guice/IPublicKeyManagerProvider.java b/src/main/java/com/gitblit/guice/IPublicKeyManagerProvider.java new file mode 100644 index 00000000..8075aa94 --- /dev/null +++ b/src/main/java/com/gitblit/guice/IPublicKeyManagerProvider.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014 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.guice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.transport.ssh.FileKeyManager; +import com.gitblit.transport.ssh.IPublicKeyManager; +import com.gitblit.transport.ssh.NullKeyManager; +import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; + +/** + * Provides a lazily-instantiated IPublicKeyManager configured from IStoredSettings. + * + * @author James Moger + * + */ +@Singleton +public class IPublicKeyManagerProvider implements Provider { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final IRuntimeManager runtimeManager; + + private volatile IPublicKeyManager manager; + + @Inject + public IPublicKeyManagerProvider(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; + } + + @Override + public synchronized IPublicKeyManager get() { + if (manager != null) { + return manager; + } + + IStoredSettings settings = runtimeManager.getSettings(); + String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName()); + if (StringUtils.isEmpty(clazz)) { + clazz = FileKeyManager.class.getName(); + } + try { + Class mgrClass = (Class) Class.forName(clazz); + manager = runtimeManager.getInjector().getInstance(mgrClass); + } catch (Exception e) { + logger.error("failed to create public key manager", e); + manager = new NullKeyManager(); + } + return manager; + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index 5c2eccf0..fbbafacb 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -86,6 +86,7 @@ import com.google.gson.reflect.TypeToken; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Singleton; +import com.google.inject.Provider; /** * GitblitManager is an aggregate interface delegate. It implements all the manager @@ -106,6 +107,8 @@ public class GitblitManager implements IGitblit { protected final ObjectCache> clientApplications = new ObjectCache>(); + protected final Provider publicKeyManagerProvider; + protected final IStoredSettings settings; protected final IRuntimeManager runtimeManager; @@ -118,8 +121,6 @@ public class GitblitManager implements IGitblit { protected final IAuthenticationManager authenticationManager; - protected final IPublicKeyManager publicKeyManager; - protected final IRepositoryManager repositoryManager; protected final IProjectManager projectManager; @@ -128,23 +129,24 @@ public class GitblitManager implements IGitblit { @Inject public GitblitManager( + Provider publicKeyManagerProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, IUserManager userManager, IAuthenticationManager authenticationManager, - IPublicKeyManager publicKeyManager, IRepositoryManager repositoryManager, IProjectManager projectManager, IFederationManager federationManager) { + this.publicKeyManagerProvider = publicKeyManagerProvider; + this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; this.pluginManager = pluginManager; this.notificationManager = notificationManager; this.userManager = userManager; this.authenticationManager = authenticationManager; - this.publicKeyManager = publicKeyManager; this.repositoryManager = repositoryManager; this.projectManager = projectManager; this.federationManager = federationManager; @@ -487,7 +489,7 @@ public class GitblitManager implements IGitblit { @Override public IPublicKeyManager getPublicKeyManager() { - return publicKeyManager; + return publicKeyManagerProvider.get(); } /* @@ -706,11 +708,6 @@ public class GitblitManager implements IGitblit { return userManager.getAllUsers(); } - @Override - public boolean deleteUser(String username) { - return userManager.deleteUser(username); - } - @Override public UserModel getUserModel(String username) { return userManager.getUserModel(username); @@ -751,9 +748,23 @@ public class GitblitManager implements IGitblit { return userManager.updateUserModel(username, model); } + @Override + public boolean deleteUser(String username) { + // delegate to deleteUserModel() to delete public ssh keys + UserModel user = userManager.getUserModel(username); + return deleteUserModel(user); + } + + /** + * Delete the user and all associated public ssh keys. + */ @Override public boolean deleteUserModel(UserModel model) { - return userManager.deleteUserModel(model); + boolean success = userManager.deleteUserModel(model); + if (success) { + getPublicKeyManager().removeAllKeys(model.username); + } + return success; } @Override diff --git a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java index a063dc7d..1a2cd682 100644 --- a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java +++ b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java @@ -29,6 +29,7 @@ import com.gitblit.manager.IRuntimeManager; import com.google.common.base.Charsets; import com.google.common.base.Joiner; import com.google.common.io.Files; +import com.google.inject.Inject; /** * Manages public keys on the filesystem. @@ -42,6 +43,7 @@ public class FileKeyManager extends IPublicKeyManager { protected final Map lastModifieds; + @Inject public FileKeyManager(IRuntimeManager runtimeManager) { this.runtimeManager = runtimeManager; this.lastModifieds = new ConcurrentHashMap(); diff --git a/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java b/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java index 357b34a2..bf783786 100644 --- a/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java +++ b/src/main/java/com/gitblit/transport/ssh/MemoryKeyManager.java @@ -20,6 +20,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.inject.Inject; + /** * Memory public key manager. * @@ -30,6 +32,7 @@ public class MemoryKeyManager extends IPublicKeyManager { final Map> keys; + @Inject public MemoryKeyManager() { keys = new HashMap>(); } diff --git a/src/main/java/com/gitblit/transport/ssh/NullKeyManager.java b/src/main/java/com/gitblit/transport/ssh/NullKeyManager.java index 0761d842..fcd3e197 100644 --- a/src/main/java/com/gitblit/transport/ssh/NullKeyManager.java +++ b/src/main/java/com/gitblit/transport/ssh/NullKeyManager.java @@ -17,6 +17,8 @@ package com.gitblit.transport.ssh; import java.util.List; +import com.google.inject.Inject; + /** * Rejects all public key management requests. * @@ -25,6 +27,7 @@ import java.util.List; */ public class NullKeyManager extends IPublicKeyManager { + @Inject public NullKeyManager() { } diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index 24468c0b..e09799d1 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -91,6 +91,7 @@ import com.gitblit.wicket.pages.TreePage; import com.gitblit.wicket.pages.UserPage; import com.gitblit.wicket.pages.UsersPage; import com.google.inject.Inject; +import com.google.inject.Provider; import com.google.inject.Singleton; @Singleton @@ -102,6 +103,8 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final Map cacheablePages = new HashMap(); + private final Provider publicKeyManagerProvider; + private final IStoredSettings settings; private final IRuntimeManager runtimeManager; @@ -114,8 +117,6 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final IAuthenticationManager authenticationManager; - private final IPublicKeyManager publicKeyManager; - private final IRepositoryManager repositoryManager; private final IProjectManager projectManager; @@ -128,12 +129,12 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { @Inject public GitBlitWebApp( + Provider publicKeyManagerProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, IUserManager userManager, IAuthenticationManager authenticationManager, - IPublicKeyManager publicKeyManager, IRepositoryManager repositoryManager, IProjectManager projectManager, IFederationManager federationManager, @@ -141,13 +142,13 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { IServicesManager services) { super(); + this.publicKeyManagerProvider = publicKeyManagerProvider; this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; this.pluginManager = pluginManager; this.notificationManager = notificationManager; this.userManager = userManager; this.authenticationManager = authenticationManager; - this.publicKeyManager = publicKeyManager; this.repositoryManager = repositoryManager; this.projectManager = projectManager; this.federationManager = federationManager; @@ -389,7 +390,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { */ @Override public IPublicKeyManager keys() { - return publicKeyManager; + return publicKeyManagerProvider.get(); } /* (non-Javadoc) -- cgit v1.2.3 From c42032df0911f51c81a91a961eff2066b380607c Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 16:45:37 -0400 Subject: Extract ticket service into an injectable object with a custom provider --- src/main/java/com/gitblit/FederationClient.java | 2 +- src/main/java/com/gitblit/GitBlit.java | 140 +-------------------- src/main/java/com/gitblit/guice/CoreModule.java | 2 + .../com/gitblit/guice/ITicketServiceProvider.java | 73 +++++++++++ .../java/com/gitblit/manager/GitblitManager.java | 36 ++++-- .../java/com/gitblit/servlet/GitblitContext.java | 2 + .../com/gitblit/tickets/BranchTicketService.java | 6 +- .../com/gitblit/tickets/FileTicketService.java | 6 +- .../java/com/gitblit/tickets/ITicketService.java | 5 +- .../com/gitblit/tickets/NullTicketService.java | 6 +- .../com/gitblit/tickets/RedisTicketService.java | 9 +- .../java/com/gitblit/wicket/GitBlitWebApp.java | 6 +- 12 files changed, 136 insertions(+), 157 deletions(-) create mode 100644 src/main/java/com/gitblit/guice/ITicketServiceProvider.java diff --git a/src/main/java/com/gitblit/FederationClient.java b/src/main/java/com/gitblit/FederationClient.java index af92b5f9..822e8a7f 100644 --- a/src/main/java/com/gitblit/FederationClient.java +++ b/src/main/java/com/gitblit/FederationClient.java @@ -97,7 +97,7 @@ public class FederationClient { UserManager users = new UserManager(runtime, null).start(); RepositoryManager repositories = new RepositoryManager(runtime, null, users).start(); FederationManager federation = new FederationManager(runtime, notifications, repositories).start(); - IGitblit gitblit = new GitblitManager(null, runtime, null, notifications, users, null, repositories, null, federation); + IGitblit gitblit = new GitblitManager(null, null, runtime, null, notifications, users, null, repositories, null, federation); FederationPullService puller = new FederationPullService(gitblit, federation.getFederationRegistrations()) { @Override diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 9a01d3ce..68a91bb5 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -15,55 +15,36 @@ */ package com.gitblit; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - import com.gitblit.manager.GitblitManager; import com.gitblit.manager.IAuthenticationManager; import com.gitblit.manager.IFederationManager; -import com.gitblit.manager.IGitblit; import com.gitblit.manager.INotificationManager; import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IProjectManager; import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IUserManager; -import com.gitblit.models.RepositoryModel; -import com.gitblit.models.UserModel; import com.gitblit.tickets.ITicketService; -import com.gitblit.tickets.NullTicketService; import com.gitblit.transport.ssh.IPublicKeyManager; -import com.gitblit.utils.StringUtils; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; import com.google.inject.Inject; -import com.google.inject.Injector; import com.google.inject.Provider; import com.google.inject.Singleton; /** - * GitBlit is the aggregate manager for the Gitblit webapp. It provides all - * management functions and also manages some long-running services. + * GitBlit is the aggregate manager for the Gitblit webapp. The parent class provides all + * functionality. This class exists to not break existing Groovy push hooks. * * @author James Moger * */ @Singleton +@Deprecated public class GitBlit extends GitblitManager { - private final Injector injector; - - private ITicketService ticketService; - @Inject public GitBlit( Provider publicKeyManagerProvider, + Provider ticketServiceProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, @@ -75,6 +56,7 @@ public class GitBlit extends GitblitManager { super( publicKeyManagerProvider, + ticketServiceProvider, runtimeManager, pluginManager, notificationManager, @@ -83,117 +65,5 @@ public class GitBlit extends GitblitManager { repositoryManager, projectManager, federationManager); - - this.injector = Guice.createInjector(getModules()); - } - - @Override - public GitBlit start() { - super.start(); - configureTicketService(); - return this; - } - - @Override - public GitBlit stop() { - super.stop(); - ticketService.stop(); - return this; - } - - protected AbstractModule [] getModules() { - return new AbstractModule [] { new GitBlitModule()}; - } - - /** - * Detect renames and reindex as appropriate. - */ - @Override - public void updateRepositoryModel(String repositoryName, RepositoryModel repository, - boolean isCreate) throws GitBlitException { - RepositoryModel oldModel = null; - boolean isRename = !isCreate && !repositoryName.equalsIgnoreCase(repository.name); - if (isRename) { - oldModel = repositoryManager.getRepositoryModel(repositoryName); - } - - super.updateRepositoryModel(repositoryName, repository, isCreate); - - if (isRename && ticketService != null) { - ticketService.rename(oldModel, repository); - } - } - - /** - * Delete the repository and all associated tickets. - */ - @Override - public boolean deleteRepository(String repositoryName) { - RepositoryModel repository = repositoryManager.getRepositoryModel(repositoryName); - return deleteRepositoryModel(repository); - } - - @Override - public boolean deleteRepositoryModel(RepositoryModel model) { - boolean success = repositoryManager.deleteRepositoryModel(model); - if (success && ticketService != null) { - ticketService.deleteAll(model); - } - return success; - } - - /** - * Returns the configured ticket service. - * - * @return a ticket service - */ - @Override - public ITicketService getTicketService() { - return ticketService; - } - - protected void configureTicketService() { - String clazz = settings.getString(Keys.tickets.service, NullTicketService.class.getName()); - if (StringUtils.isEmpty(clazz)) { - clazz = NullTicketService.class.getName(); - } - try { - Class serviceClass = (Class) Class.forName(clazz); - ticketService = injector.getInstance(serviceClass).start(); - if (ticketService instanceof NullTicketService) { - logger.warn("No ticket service configured."); - } else if (ticketService.isReady()) { - logger.info("{} is ready.", ticketService); - } else { - logger.warn("{} is disabled.", ticketService); - } - } catch (Exception e) { - logger.error("failed to create ticket service " + clazz, e); - ticketService = injector.getInstance(NullTicketService.class).start(); - } - } - - /** - * A nested Guice Module is used for constructor dependency injection of - * complex classes. - * - * @author James Moger - * - */ - class GitBlitModule extends AbstractModule { - - @Override - protected void configure() { - bind(IStoredSettings.class).toInstance(settings); - bind(IRuntimeManager.class).toInstance(runtimeManager); - bind(IPluginManager.class).toInstance(pluginManager); - bind(INotificationManager.class).toInstance(notificationManager); - bind(IUserManager.class).toInstance(userManager); - bind(IAuthenticationManager.class).toInstance(authenticationManager); - bind(IRepositoryManager.class).toInstance(repositoryManager); - bind(IProjectManager.class).toInstance(projectManager); - bind(IFederationManager.class).toInstance(federationManager); - bind(IGitblit.class).toInstance(GitBlit.this); - } } } diff --git a/src/main/java/com/gitblit/guice/CoreModule.java b/src/main/java/com/gitblit/guice/CoreModule.java index 6dbe6150..c0d39e99 100644 --- a/src/main/java/com/gitblit/guice/CoreModule.java +++ b/src/main/java/com/gitblit/guice/CoreModule.java @@ -37,6 +37,7 @@ import com.gitblit.manager.RepositoryManager; import com.gitblit.manager.RuntimeManager; import com.gitblit.manager.ServicesManager; import com.gitblit.manager.UserManager; +import com.gitblit.tickets.ITicketService; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.WorkQueue; import com.google.inject.AbstractModule; @@ -56,6 +57,7 @@ public class CoreModule extends AbstractModule { // bind complex providers bind(IPublicKeyManager.class).toProvider(IPublicKeyManagerProvider.class); + bind(ITicketService.class).toProvider(ITicketServiceProvider.class); bind(WorkQueue.class).toProvider(WorkQueueProvider.class); // core managers diff --git a/src/main/java/com/gitblit/guice/ITicketServiceProvider.java b/src/main/java/com/gitblit/guice/ITicketServiceProvider.java new file mode 100644 index 00000000..fd39955d --- /dev/null +++ b/src/main/java/com/gitblit/guice/ITicketServiceProvider.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014 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.guice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.tickets.ITicketService; +import com.gitblit.tickets.NullTicketService; +import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; + +/** + * Provides a lazily-instantiated ITicketService configured from IStoredSettings. + * + * @author James Moger + * + */ +@Singleton +public class ITicketServiceProvider implements Provider { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final IRuntimeManager runtimeManager; + + private volatile ITicketService service; + + @Inject + public ITicketServiceProvider(IRuntimeManager runtimeManager) { + this.runtimeManager = runtimeManager; + } + + @Override + public synchronized ITicketService get() { + if (service != null) { + return service; + } + + IStoredSettings settings = runtimeManager.getSettings(); + String clazz = settings.getString(Keys.tickets.service, NullTicketService.class.getName()); + if (StringUtils.isEmpty(clazz)) { + clazz = NullTicketService.class.getName(); + } + + try { + Class serviceClass = (Class) Class.forName(clazz); + service = runtimeManager.getInjector().getInstance(serviceClass); + } catch (Exception e) { + logger.error("failed to create ticket service", e); + service = runtimeManager.getInjector().getInstance(NullTicketService.class); + } + + return service; + } +} \ No newline at end of file diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java index fbbafacb..02b2d675 100644 --- a/src/main/java/com/gitblit/manager/GitblitManager.java +++ b/src/main/java/com/gitblit/manager/GitblitManager.java @@ -109,6 +109,8 @@ public class GitblitManager implements IGitblit { protected final Provider publicKeyManagerProvider; + protected final Provider ticketServiceProvider; + protected final IStoredSettings settings; protected final IRuntimeManager runtimeManager; @@ -130,6 +132,7 @@ public class GitblitManager implements IGitblit { @Inject public GitblitManager( Provider publicKeyManagerProvider, + Provider ticketServiceProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, @@ -140,6 +143,7 @@ public class GitblitManager implements IGitblit { IFederationManager federationManager) { this.publicKeyManagerProvider = publicKeyManagerProvider; + this.ticketServiceProvider = ticketServiceProvider; this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; @@ -478,13 +482,9 @@ public class GitblitManager implements IGitblit { } } - /** - * Throws an exception if trying to get a ticket service. - * - */ @Override public ITicketService getTicketService() { - throw new RuntimeException("This class does not have a ticket service!"); + return ticketServiceProvider.get(); } @Override @@ -960,10 +960,23 @@ public class GitblitManager implements IGitblit { return repositoryManager.getRepositoryDefaultMetrics(model, repository); } + /** + * Detect renames and reindex as appropriate. + */ @Override public void updateRepositoryModel(String repositoryName, RepositoryModel repository, boolean isCreate) throws GitBlitException { + RepositoryModel oldModel = null; + boolean isRename = !isCreate && !repositoryName.equalsIgnoreCase(repository.name); + if (isRename) { + oldModel = repositoryManager.getRepositoryModel(repositoryName); + } + repositoryManager.updateRepositoryModel(repositoryName, repository, isCreate); + + if (isRename && ticketServiceProvider.get() != null) { + ticketServiceProvider.get().rename(oldModel, repository); + } } @Override @@ -976,14 +989,23 @@ public class GitblitManager implements IGitblit { return repositoryManager.canDelete(model); } + /** + * Delete the repository and all associated tickets. + */ @Override public boolean deleteRepositoryModel(RepositoryModel model) { - return repositoryManager.deleteRepositoryModel(model); + boolean success = repositoryManager.deleteRepositoryModel(model); + if (success && ticketServiceProvider.get() != null) { + ticketServiceProvider.get().deleteAll(model); + } + return success; } @Override public boolean deleteRepository(String repositoryName) { - return repositoryManager.deleteRepository(repositoryName); + // delegate to deleteRepositoryModel() to destroy indexed tickets + RepositoryModel repository = repositoryManager.getRepositoryModel(repositoryName); + return deleteRepositoryModel(repository); } @Override diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index 44a857ca..115af656 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -53,6 +53,7 @@ import com.gitblit.manager.IRepositoryManager; import com.gitblit.manager.IRuntimeManager; import com.gitblit.manager.IServicesManager; import com.gitblit.manager.IUserManager; +import com.gitblit.tickets.ITicketService; import com.gitblit.transport.ssh.IPublicKeyManager; import com.gitblit.utils.ContainerUtils; import com.gitblit.utils.StringUtils; @@ -200,6 +201,7 @@ public class GitblitContext extends GuiceServletContextListener { startManager(injector, IRepositoryManager.class); startManager(injector, IProjectManager.class); startManager(injector, IFederationManager.class); + startManager(injector, ITicketService.class); startManager(injector, IGitblit.class); startManager(injector, IServicesManager.class); diff --git a/src/main/java/com/gitblit/tickets/BranchTicketService.java b/src/main/java/com/gitblit/tickets/BranchTicketService.java index 0633751a..8a2bc951 100644 --- a/src/main/java/com/gitblit/tickets/BranchTicketService.java +++ b/src/main/java/com/gitblit/tickets/BranchTicketService.java @@ -30,9 +30,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import com.google.inject.Inject; -import com.google.inject.Singleton; - import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.dircache.DirCache; @@ -75,6 +72,8 @@ import com.gitblit.models.TicketModel.Change; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Implementation of a ticket service based on an orphan branch. All tickets @@ -117,6 +116,7 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi @Override public BranchTicketService start() { + log.info("{} started", getClass().getSimpleName()); return this; } diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java index 74311f53..c8346d14 100644 --- a/src/main/java/com/gitblit/tickets/FileTicketService.java +++ b/src/main/java/com/gitblit/tickets/FileTicketService.java @@ -27,9 +27,6 @@ import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -import com.google.inject.Inject; -import com.google.inject.Singleton; - import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants; @@ -45,6 +42,8 @@ import com.gitblit.models.TicketModel.Change; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.FileUtils; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Implementation of a ticket service based on a directory within the repository. @@ -82,6 +81,7 @@ public class FileTicketService extends ITicketService { @Override public FileTicketService start() { + log.info("{} started", getClass().getSimpleName()); return this; } diff --git a/src/main/java/com/gitblit/tickets/ITicketService.java b/src/main/java/com/gitblit/tickets/ITicketService.java index 668d0bcc..7e7ea9e8 100644 --- a/src/main/java/com/gitblit/tickets/ITicketService.java +++ b/src/main/java/com/gitblit/tickets/ITicketService.java @@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.extensions.TicketHook; +import com.gitblit.manager.IManager; import com.gitblit.manager.INotificationManager; import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IRepositoryManager; @@ -63,7 +64,7 @@ import com.google.common.cache.CacheBuilder; * @author James Moger * */ -public abstract class ITicketService { +public abstract class ITicketService implements IManager { public static final String SETTING_UPDATE_DIFFSTATS = "migration.updateDiffstats"; @@ -176,12 +177,14 @@ public abstract class ITicketService { * Start the service. * @since 1.4.0 */ + @Override public abstract ITicketService start(); /** * Stop the service. * @since 1.4.0 */ + @Override public final ITicketService stop() { indexer.close(); ticketsCache.invalidateAll(); diff --git a/src/main/java/com/gitblit/tickets/NullTicketService.java b/src/main/java/com/gitblit/tickets/NullTicketService.java index 0980d29d..3947b945 100644 --- a/src/main/java/com/gitblit/tickets/NullTicketService.java +++ b/src/main/java/com/gitblit/tickets/NullTicketService.java @@ -19,9 +19,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import com.google.inject.Inject; -import com.google.inject.Singleton; - import com.gitblit.manager.INotificationManager; import com.gitblit.manager.IPluginManager; import com.gitblit.manager.IRepositoryManager; @@ -31,6 +28,8 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.models.TicketModel; import com.gitblit.models.TicketModel.Attachment; import com.gitblit.models.TicketModel.Change; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Implementation of a ticket service that rejects everything. @@ -63,6 +62,7 @@ public class NullTicketService extends ITicketService { @Override public NullTicketService start() { + log.info("{} started", getClass().getSimpleName()); return this; } diff --git a/src/main/java/com/gitblit/tickets/RedisTicketService.java b/src/main/java/com/gitblit/tickets/RedisTicketService.java index 02c19e63..0f9ad174 100644 --- a/src/main/java/com/gitblit/tickets/RedisTicketService.java +++ b/src/main/java/com/gitblit/tickets/RedisTicketService.java @@ -22,9 +22,6 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; -import com.google.inject.Inject; -import com.google.inject.Singleton; - import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.Client; @@ -46,6 +43,8 @@ import com.gitblit.models.TicketModel.Attachment; import com.gitblit.models.TicketModel.Change; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.StringUtils; +import com.google.inject.Inject; +import com.google.inject.Singleton; /** * Implementation of a ticket service based on a Redis key-value store. All @@ -85,6 +84,10 @@ public class RedisTicketService extends ITicketService { @Override public RedisTicketService start() { + log.info("{} started", getClass().getSimpleName()); + if (!isReady()) { + log.warn("{} is not ready!", getClass().getSimpleName()); + } return this; } diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java index e09799d1..036a05a5 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.java +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.java @@ -105,6 +105,8 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { private final Provider publicKeyManagerProvider; + private final Provider ticketServiceProvider; + private final IStoredSettings settings; private final IRuntimeManager runtimeManager; @@ -130,6 +132,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { @Inject public GitBlitWebApp( Provider publicKeyManagerProvider, + Provider ticketServiceProvider, IRuntimeManager runtimeManager, IPluginManager pluginManager, INotificationManager notificationManager, @@ -143,6 +146,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { super(); this.publicKeyManagerProvider = publicKeyManagerProvider; + this.ticketServiceProvider = ticketServiceProvider; this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; this.pluginManager = pluginManager; @@ -438,7 +442,7 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp { */ @Override public ITicketService tickets() { - return gitblit.getTicketService(); + return ticketServiceProvider.get(); } /* (non-Javadoc) -- cgit v1.2.3 From 3610dc445e01ee07faae64acaabcdc00aac5e1b5 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 16:50:45 -0400 Subject: Delay pf4j instantiation and setup to start() --- .../java/com/gitblit/manager/PluginManager.java | 25 +++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index dcf12b26..a35ff6fe 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -75,10 +75,10 @@ public class PluginManager implements IPluginManager, PluginStateListener { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final DefaultPluginManager pf4j; - private final IRuntimeManager runtimeManager; + private DefaultPluginManager pf4j; + // timeout defaults of Maven 3.0.4 in seconds private int connectTimeout = 20; @@ -86,18 +86,7 @@ public class PluginManager implements IPluginManager, PluginStateListener { @Inject public PluginManager(IRuntimeManager runtimeManager) { - File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); - dir.mkdirs(); this.runtimeManager = runtimeManager; - - this.pf4j = new DefaultPluginManager(dir); - - try { - Version systemVersion = Version.createVersion(Constants.getVersion()); - pf4j.setSystemVersion(systemVersion); - } catch (Exception e) { - logger.error(null, e); - } } @Override @@ -112,6 +101,16 @@ public class PluginManager implements IPluginManager, PluginStateListener { @Override public PluginManager start() { + File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); + dir.mkdirs(); + pf4j = new DefaultPluginManager(dir); + + try { + Version systemVersion = Version.createVersion(Constants.getVersion()); + pf4j.setSystemVersion(systemVersion); + } catch (Exception e) { + logger.error(null, e); + } pf4j.loadPlugins(); logger.debug("Starting plugins"); pf4j.startPlugins(); -- cgit v1.2.3 From 34ea0170815ade4a456fe23c1b4472852ed26107 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 2 Jul 2014 17:51:35 -0400 Subject: Delay setup of the repositories folder to start() --- src/main/java/com/gitblit/manager/RepositoryManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 24f35734..626dac4c 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -122,7 +122,7 @@ public class RepositoryManager implements IRepositoryManager { private final IUserManager userManager; - private final File repositoriesFolder; + private File repositoriesFolder; private LuceneService luceneExecutor; @@ -140,11 +140,11 @@ public class RepositoryManager implements IRepositoryManager { this.runtimeManager = runtimeManager; this.pluginManager = pluginManager; this.userManager = userManager; - this.repositoriesFolder = runtimeManager.getFileOrFolder(Keys.git.repositoriesFolder, "${baseFolder}/git"); } @Override public RepositoryManager start() { + repositoriesFolder = runtimeManager.getFileOrFolder(Keys.git.repositoriesFolder, "${baseFolder}/git"); logger.info("Repositories folder : {}", repositoriesFolder.getAbsolutePath()); // initialize utilities -- cgit v1.2.3 From 1c301ba07b13c1cd941c44805ee09d1ac8759bd5 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 3 Jul 2014 14:30:22 -0400 Subject: Support injection of plugin Extensions --- .../java/com/gitblit/manager/PluginManager.java | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gitblit/manager/PluginManager.java b/src/main/java/com/gitblit/manager/PluginManager.java index a35ff6fe..874e24b5 100644 --- a/src/main/java/com/gitblit/manager/PluginManager.java +++ b/src/main/java/com/gitblit/manager/PluginManager.java @@ -37,7 +37,9 @@ import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ro.fortsoft.pf4j.DefaultExtensionFinder; import ro.fortsoft.pf4j.DefaultPluginManager; +import ro.fortsoft.pf4j.ExtensionFinder; import ro.fortsoft.pf4j.PluginClassLoader; import ro.fortsoft.pf4j.PluginState; import ro.fortsoft.pf4j.PluginStateEvent; @@ -103,7 +105,33 @@ public class PluginManager implements IPluginManager, PluginStateListener { public PluginManager start() { File dir = runtimeManager.getFileOrFolder(Keys.plugins.folder, "${baseFolder}/plugins"); dir.mkdirs(); - pf4j = new DefaultPluginManager(dir); + pf4j = new DefaultPluginManager(dir) { + @Override + protected ExtensionFinder createExtensionFinder() { + DefaultExtensionFinder extensionFinder = new DefaultExtensionFinder(this) { + @Override + protected ExtensionFactory createExtensionFactory() { + return new ExtensionFactory() { + @Override + public Object create(Class extensionType) { + // instantiate && inject the extension + logger.debug("Create instance for extension '{}'", extensionType.getName()); + try { + return runtimeManager.getInjector().getInstance(extensionType); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + return null; + } + + }; + } + }; + addPluginStateListener(extensionFinder); + + return extensionFinder; + } + }; try { Version systemVersion = Version.createVersion(Constants.getVersion()); -- cgit v1.2.3 From 9991de4d121dc6124d40a10d86d879b17b39de68 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 3 Jul 2014 16:19:49 -0400 Subject: Update to Guava 17 --- .classpath | 2 +- build.moxie | 2 +- gitblit.iml | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.classpath b/.classpath index 274dee2b..9873ddb4 100644 --- a/.classpath +++ b/.classpath @@ -8,7 +8,7 @@ - + diff --git a/build.moxie b/build.moxie index 735e1857..3005b0ee 100644 --- a/build.moxie +++ b/build.moxie @@ -132,7 +132,7 @@ properties: { dependencies: - compile 'com.google.inject:guice:${guice.version}' :war :fedclient - compile 'com.google.inject.extensions:guice-servlet:${guice-servlet.version}' :war -- compile 'com.google.guava:guava:16.0.1' :war :fedclient +- compile 'com.google.guava:guava:17.0' :war :fedclient - compile 'com.intellij:annotations:12.0' :war - compile 'log4j:log4j:1.2.17' :war :fedclient - compile 'org.slf4j:slf4j-api:1.7.7' :war :fedclient diff --git a/gitblit.iml b/gitblit.iml index cc9324f5..878fdc3c 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -47,13 +47,13 @@ - + - + - + -- cgit v1.2.3