]> source.dussan.org Git - gitblit.git/commitdiff
Revised AvatarGenerator to be injectable 58/258/2
authorJames Moger <james.moger@gitblit.com>
Mon, 15 Jun 2015 16:25:14 +0000 (12:25 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 15 Jun 2015 16:25:14 +0000 (12:25 -0400)
src/main/distrib/data/defaults.properties
src/main/java/com/gitblit/AvatarGenerator.java
src/main/java/com/gitblit/GravatarGenerator.java
src/main/java/com/gitblit/guice/AvatarGeneratorProvider.java [new file with mode: 0644]
src/main/java/com/gitblit/guice/WebModule.java
src/main/java/com/gitblit/utils/ActivityUtils.java
src/main/java/com/gitblit/wicket/GitBlitWebApp.java
src/main/java/com/gitblit/wicket/GitblitWicketApp.java
src/main/java/com/gitblit/wicket/panels/AvatarImage.java
src/test/java/com/gitblit/tests/GitBlitSuite.java
src/test/java/com/gitblit/tests/GravatarTest.java

index aa0d2cfaae891836ba7bce6e55ae5fc5341a692d..0d556fdc6a05d2f9974d8e3e390bceab799c6202 100644 (file)
@@ -976,8 +976,6 @@ web.useResponsiveLayout = true
 web.allowGravatar = true
 
 # Define which class will generate the avatar URL.
-# This class must inherit from com.gitblit.AvatarGenerator
-# It must use a no argument constructor
 #
 # SINCE 1.7.0
 web.avatarClass = com.gitblit.GravatarGenerator
index 76f189deb42133fbe6e3880066fc392832b4728c..0415f92538ba720049e6c65ccba46001618616b1 100644 (file)
  */
 package com.gitblit;
 
-public abstract class AvatarGenerator {
+public interface AvatarGenerator {
 
-       public abstract String getURL(String username, String emailaddress, boolean identicon, int width);
-
-       /**
-        * A method that can extract custom settings for the avatar generator
-        * The default does nothing, it can be overridden
-        *
-        * @param settings
-        */
-       public void configure(IStoredSettings settings) {
-
-       }
+       String getURL(String username, String emailaddress, boolean identicon, int width);
 
 }
index 796a1a88fd999338b74471508a04d0de06f4df6f..1ba02e55a95e7b770aec76866ad7977256358842 100644 (file)
 package com.gitblit;
 
 import com.gitblit.utils.ActivityUtils;
+import com.google.inject.Singleton;
 
-public class GravatarGenerator extends AvatarGenerator {
+@Singleton
+public class GravatarGenerator implements AvatarGenerator {
 
-       public String getURL(String username, String emailaddress,
-                       boolean identicon, int width) {
-               String email = emailaddress == null ? username.toLowerCase() : emailaddress.toLowerCase();
+       @Override
+       public String getURL(String username, String emailaddress, boolean identicon, int width) {
+               String email = emailaddress == null ? username : emailaddress;
                if (identicon) {
                        return ActivityUtils.getGravatarIdenticonUrl(email, width);
                } else {
diff --git a/src/main/java/com/gitblit/guice/AvatarGeneratorProvider.java b/src/main/java/com/gitblit/guice/AvatarGeneratorProvider.java
new file mode 100644 (file)
index 0000000..a18e299
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2015 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.AvatarGenerator;
+import com.gitblit.GravatarGenerator;
+import com.gitblit.IStoredSettings;
+import com.gitblit.Keys;
+import com.gitblit.manager.IRuntimeManager;
+import com.gitblit.utils.StringUtils;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+/**
+ * Provides a lazily-instantiated AvatarGenerator configured from IStoredSettings.
+ *
+ * @author James Moger
+ *
+ */
+@Singleton
+public class AvatarGeneratorProvider implements Provider<AvatarGenerator> {
+
+       private final Logger logger = LoggerFactory.getLogger(getClass());
+
+       private final IRuntimeManager runtimeManager;
+
+       private volatile AvatarGenerator avatarGenerator;
+
+       @Inject
+       public AvatarGeneratorProvider(IRuntimeManager runtimeManager) {
+               this.runtimeManager = runtimeManager;
+       }
+
+       @Override
+       public synchronized AvatarGenerator get() {
+               if (avatarGenerator != null) {
+                       return avatarGenerator;
+               }
+
+               IStoredSettings settings = runtimeManager.getSettings();
+               String clazz = settings.getString(Keys.web.avatarClass, GravatarGenerator.class.getName());
+               if (StringUtils.isEmpty(clazz)) {
+                       clazz = GravatarGenerator.class.getName();
+               }
+               try {
+                       Class<? extends AvatarGenerator> generatorClass = (Class<? extends AvatarGenerator>) Class.forName(clazz);
+                       avatarGenerator = runtimeManager.getInjector().getInstance(generatorClass);
+               } catch (Exception e) {
+                       logger.error("failed to create avatar generator", e);
+                       avatarGenerator = new GravatarGenerator();
+               }
+               return avatarGenerator;
+       }
+}
\ No newline at end of file
index c6172c3dec5ba57217ef532af026b107b7e322af..a40627035a827cde0a354b2d7ea23305355c51a8 100644 (file)
@@ -18,6 +18,7 @@ package com.gitblit.guice;
 import java.util.HashMap;
 import java.util.Map;
 
+import com.gitblit.AvatarGenerator;
 import com.gitblit.Constants;
 import com.gitblit.servlet.AccessDeniedServlet;
 import com.gitblit.servlet.BranchGraphServlet;
@@ -56,6 +57,10 @@ public class WebModule extends ServletModule {
 
        @Override
        protected void configureServlets() {
+
+               // bind web component providers
+               bind(AvatarGenerator.class).toProvider(AvatarGeneratorProvider.class);
+
                // servlets
                serve(fuzzy(Constants.R_PATH), fuzzy(Constants.GIT_PATH)).with(GitServlet.class);
                serve(fuzzy(Constants.RAW_PATH)).with(RawServlet.class);
index ba5599a1e3a293e7f3e24ed5b88e81b9130d03e5..ab1dad96d61a3dd9ac991b9411a0ecdd597d8f6e 100644 (file)
@@ -169,7 +169,7 @@ public class ActivityUtils {
                if (width <= 0) {\r
                        width = 50;\r
                }\r
-               String emailHash = StringUtils.getMD5(email);\r
+               String emailHash = StringUtils.getMD5(email.toLowerCase());\r
                String url = MessageFormat.format(\r
                                "https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=identicon", emailHash, width);\r
                return url;\r
@@ -188,7 +188,7 @@ public class ActivityUtils {
                if (width <= 0) {\r
                        width = 50;\r
                }\r
-               String emailHash = StringUtils.getMD5(email);\r
+               String emailHash = StringUtils.getMD5(email.toLowerCase());\r
                String url = MessageFormat.format(\r
                                "https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=mm", emailHash, width);\r
                return url;\r
index 39cdbb41f0c1227fad8c8d71423af416c60ba6c5..359040b5376b43fa5d2b67f4cb9a5f74288aacfb 100644 (file)
@@ -32,7 +32,6 @@ import org.apache.wicket.protocol.http.WebApplication;
 import ro.fortsoft.pf4j.PluginState;
 import ro.fortsoft.pf4j.PluginWrapper;
 
-import com.gitblit.AvatarGenerator;
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
 import com.gitblit.extensions.GitblitWicketPlugin;
@@ -251,15 +250,12 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp {
                        }
                }
 
-               // customize the Wicket class resolver to load from plugins
+                // customize the Wicket class resolver to load from plugins
                IClassResolver coreResolver = getApplicationSettings().getClassResolver();
-               PluginClassResolver classResolver = new PluginClassResolver(coreResolver, pluginManager);
-               getApplicationSettings().setClassResolver(classResolver);
+        PluginClassResolver classResolver = new PluginClassResolver(coreResolver, pluginManager);
+        getApplicationSettings().setClassResolver(classResolver);
 
                getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
-
-               buildAvatarGenerator();
-
        }
 
        /* (non-Javadoc)
@@ -480,30 +476,4 @@ public class GitBlitWebApp extends WebApplication implements GitblitWicketApp {
        public static GitBlitWebApp get() {
                return (GitBlitWebApp) WebApplication.get();
        }
-
-       AvatarGenerator generator = null;
-       @SuppressWarnings("unchecked")
-       private void buildAvatarGenerator() {
-               Class<AvatarGenerator> clazz;
-               try {
-                       clazz = (Class<AvatarGenerator>) getClass().getClassLoader().loadClass(settings.getString(Keys.web.avatarClass, "com.gitblit.GravatarGenerator"));
-                       generator = clazz.newInstance();
-                       generator.configure(settings);
-               } catch (ClassNotFoundException | SecurityException | InstantiationException | IllegalAccessException e) {
-                       throw new RuntimeException(e);
-               }
-
-       }
-
-       public String buildAvatarUrl(String username, String emailaddress, String cssClass, int width, boolean identicon) {
-               if (width <= 0) {
-                       width = 50;
-               }
-               if(generator != null) {
-                       return (String) generator.getURL(username, emailaddress, identicon, width);
-               }
-               return null;
-       }
-
-
 }
index b3ca20734a5c34cd60322682e3676a196c22e455..3041c5da01203ed544b06b0dd7acc9fc143bbeaf 100644 (file)
@@ -75,6 +75,4 @@ public interface GitblitWicketApp {
 
        public abstract TimeZone getTimezone();
 
-       public String buildAvatarUrl(String username, String emailaddress, String cssClass, int width, boolean identicon);
-
 }
\ No newline at end of file
index 990156126276d7cecdb1b95b7be8b9affb077288..a562042d40466ddda8daea788d9d33adf27e6527 100644 (file)
@@ -17,6 +17,7 @@ package com.gitblit.wicket.panels;
 
 import org.eclipse.jgit.lib.PersonIdent;
 
+import com.gitblit.AvatarGenerator;
 import com.gitblit.Keys;
 import com.gitblit.models.UserModel;
 import com.gitblit.wicket.ExternalImage;
@@ -51,7 +52,8 @@ public class AvatarImage extends BasePanel {
        public AvatarImage(String id, String username, String emailaddress, String cssClass, int width, boolean identicon) {
                super(id);
 
-               String url = app().buildAvatarUrl(username, emailaddress, cssClass, width, identicon);
+               AvatarGenerator avatarGenerator = app().runtime().getInjector().getInstance(AvatarGenerator.class);
+               String url = avatarGenerator.getURL(username, emailaddress, identicon, width);
                ExternalImage image = new ExternalImage("image", url);
                if (cssClass != null) {
                        WicketUtils.setCssClass(image, cssClass);
index 79a0920d8a496214d137ba7f49dd1436da192a2e..af20a48795b4af31364c2a583beaaf82302d70d5 100644 (file)
@@ -65,7 +65,8 @@ import com.gitblit.utils.JGitUtils;
                FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,\r
                ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,\r
                BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,\r
-               SshKeysDispatcherTest.class, UITicketTest.class, PathUtilsTest.class, SshKerberosAuthenticationTest.class })\r
+               SshKeysDispatcherTest.class, UITicketTest.class, PathUtilsTest.class, SshKerberosAuthenticationTest.class,\r
+               GravatarTest.class })\r
 public class GitBlitSuite {\r
 \r
        public static final File BASEFOLDER = new File("data");\r
index 1fea8432961e21c14e70499e3045c0ff546072c8..ba989d56fcbdbdafe9ee19535532e74cec3967b5 100644 (file)
@@ -1,77 +1,74 @@
 package com.gitblit.tests;
 
-import org.junit.Assert;
 import org.junit.Test;
 
+import com.gitblit.AvatarGenerator;
+import com.gitblit.GravatarGenerator;
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
-import com.gitblit.manager.AuthenticationManager;
-import com.gitblit.manager.FederationManager;
-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.guice.AvatarGeneratorProvider;
 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.tests.mock.MemorySettings;
-import com.gitblit.tickets.FileTicketService;
-import com.gitblit.tickets.ITicketService;
-import com.gitblit.transport.ssh.IPublicKeyManager;
-import com.gitblit.transport.ssh.MemoryKeyManager;
-import com.gitblit.utils.IdGenerator;
-import com.gitblit.utils.WorkQueue;
+import com.gitblit.utils.ActivityUtils;
 import com.gitblit.utils.XssFilter;
 import com.gitblit.utils.XssFilter.AllowXssFilter;
-import com.gitblit.wicket.GitBlitWebApp;
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 
 public class GravatarTest extends GitblitUnitTest {
 
-       public static  class SomeModule extends AbstractModule {
-               private final IStoredSettings settings = new MemorySettings();
-               SomeModule() {
-                       settings.overrideSetting(Keys.web.avatarClass, "com.gitblit.GravatarGenerator");
+       public static  class AvatarModule extends AbstractModule {
+               private final IStoredSettings settings;
+
+               AvatarModule(IStoredSettings settings) {
+                       this.settings = settings;
                }
-               @Override 
+
+               @Override
                protected void configure() {
-                       bind(IGitblit.class).to(GitblitManager.class);
-                       bind(IAuthenticationManager.class).to(AuthenticationManager.class);
-                       bind(IFederationManager.class).to(FederationManager.class);
-                       bind(INotificationManager.class).to(NotificationManager.class);
-                       bind(IPluginManager.class).to(PluginManager.class);
-                       bind(IRepositoryManager.class).to(RepositoryManager.class);
-                       bind(IProjectManager.class).to(ProjectManager.class);
-                       bind(IRuntimeManager.class).to(RuntimeManager.class);
-                       bind(IUserManager.class).to(UserManager.class);
-                       bind(ITicketService.class).to(FileTicketService.class);
-                       bind(XssFilter.class).to(AllowXssFilter.class);
                        bind(IStoredSettings.class).toInstance(settings);
-                       bind(IPublicKeyManager.class).to(MemoryKeyManager.class);
-                       bind(IServicesManager.class).to(ServicesManager.class);
-                       bind(WorkQueue.class).toInstance(new WorkQueue(new IdGenerator(), 0));
+                       bind(XssFilter.class).to(AllowXssFilter.class);
+                       bind(IRuntimeManager.class).to(RuntimeManager.class);
+                       bind(AvatarGenerator.class).toProvider(AvatarGeneratorProvider.class);
                }
        }
 
        @Test
-       public void oneTest() {
-               Injector injector = Guice.createInjector(new SomeModule());
-               GitBlitWebApp webapp = injector.getInstance(GitBlitWebApp.class);
-               webapp.init();
-               Assert.assertNotNull(webapp.buildAvatarUrl("username", "emailaddress", "cssClass", 10, true));
+       public void gravatarIdenticonTest() {
+               IStoredSettings settings = new MemorySettings();
+               settings.overrideSetting(Keys.web.avatarClass, GravatarGenerator.class.getName());
+
+               Injector injector = Guice.createInjector(new AvatarModule(settings));
+               AvatarGenerator avatarGenerator = injector.getInstance(AvatarGenerator.class);
+
+               String username = "username";
+               String emailAddress = "emailAddress";
+               int width = 10;
+
+               String url = avatarGenerator.getURL(username, emailAddress, true, width);
+               assertNotNull(url);
+
+               assertEquals(ActivityUtils.getGravatarIdenticonUrl(emailAddress, width), url);
+       }
+
+       @Test
+       public void gravatarThumbnailTest() {
+               IStoredSettings settings = new MemorySettings();
+               settings.overrideSetting(Keys.web.avatarClass, GravatarGenerator.class.getName());
+
+               Injector injector = Guice.createInjector(new AvatarModule(settings));
+               AvatarGenerator avatarGenerator = injector.getInstance(AvatarGenerator.class);
+
+               String username = "username";
+               String emailAddress = "emailAddress";
+               int width = 10;
+
+               String url = avatarGenerator.getURL(username, emailAddress, false, width);
+               assertNotNull(url);
+
+               assertEquals(ActivityUtils.getGravatarThumbnailUrl(emailAddress, width), url);
        }
-       
+
 }