]> source.dussan.org Git - gitblit.git/commitdiff
Eliminate static singleton calls from user services
authorJames Moger <james.moger@gitblit.com>
Tue, 19 Nov 2013 13:01:23 +0000 (08:01 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 29 Nov 2013 16:05:50 +0000 (11:05 -0500)
Change-Id: Ieec34483822f033b19d3ff3259d071c9bc091ed1

14 files changed:
src/main/java/com/gitblit/ConfigUserService.java
src/main/java/com/gitblit/GitBlit.java
src/main/java/com/gitblit/GitblitUserService.java
src/main/java/com/gitblit/HtpasswdUserService.java
src/main/java/com/gitblit/IUserService.java
src/main/java/com/gitblit/LdapUserService.java
src/main/java/com/gitblit/PAMUserService.java
src/main/java/com/gitblit/RedmineUserService.java
src/main/java/com/gitblit/SalesforceUserService.java
src/main/java/com/gitblit/WindowsUserService.java
src/test/java/com/gitblit/tests/HtpasswdUserServiceTest.java
src/test/java/com/gitblit/tests/LdapUserServiceTest.java
src/test/java/com/gitblit/tests/RedmineUserServiceTest.java
src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java [new file with mode: 0644]

index 6b721a832d491b018b665ab4b3e7c314a1208ac9..79cbdba7d9b36288b1088fedd016fed0f129e3ed 100644 (file)
@@ -35,6 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;\r
 \r
 import com.gitblit.Constants.AccessPermission;\r
+import com.gitblit.manager.IRuntimeManager;\r
 import com.gitblit.models.TeamModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.models.UserRepositoryPreferences;\r
@@ -115,11 +116,11 @@ public class ConfigUserService implements IUserService {
        /**\r
         * Setup the user service.\r
         *\r
-        * @param settings\r
-        * @since 0.7.0\r
+        * @param runtimeManager\r
+        * @since 1.4.0\r
         */\r
        @Override\r
-       public void setup(IStoredSettings settings) {\r
+       public void setup(IRuntimeManager runtimeManager) {\r
        }\r
 \r
        /**\r
index 8be50fb6aae93946b38e0ea79d2d70061d77cba7..737167feccb0111055a4d2c9e5d6fb541d119e66 100644 (file)
@@ -611,7 +611,7 @@ public class GitBlit extends DaggerContextListener
        public void setUserService(IUserService userService) {
                logger.info("Setting up user service " + userService.toString());
                this.userService = userService;
-               this.userService.setup(settings);
+               this.userService.setup(getManager(IRuntimeManager.class));
        }
 
        @Override
index ba4c9c5d9285b656e35e4e262bc2769bcd1ef51f..7278b22245437aaeaaac639026462cec7e13a4ea 100644 (file)
@@ -59,8 +59,7 @@ public class GitblitUserService implements IUserService {
        }\r
 \r
        @Override\r
-       public void setup(IStoredSettings settings) {\r
-               IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
+       public void setup(IRuntimeManager runtimeManager) {\r
                File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");\r
                serviceImpl = createUserService(realmFile);\r
                logger.info("GUS delegating to " + serviceImpl.toString());\r
index a98777b94a2efb3caf9cd043de8a194f33a652ed..3b7120f0a1053caadaf6a03058bd83981aea7529 100644 (file)
@@ -80,6 +80,7 @@ public class HtpasswdUserService extends GitblitUserService
 
     private final boolean SUPPORT_PLAINTEXT_PWD;
 
+    private IRuntimeManager runtimeManager;
     private IStoredSettings settings;
     private File htpasswdFile;
 
@@ -117,16 +118,16 @@ public class HtpasswdUserService extends GitblitUserService
      * In addition the setup tries to read and parse the htpasswd file to be used
      * for authentication.
      *
-     * @param settings
-     * @since 0.7.0
+     * @param runtimeManager
+     * @since 1.4.0
      */
     @Override
-    public void setup(IStoredSettings settings)
+    public void setup(IRuntimeManager runtimeManager)
     {
-        this.settings = settings;
+       this.runtimeManager = runtimeManager;
+        this.settings = runtimeManager.getSettings();
 
         // This is done in two steps in order to avoid calling GitBlit.getFileOrFolder(String, String) which will segfault for unit tests.
-        IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
         String file = settings.getString(KEY_BACKING_US, DEFAULT_BACKING_US);
         File realmFile = runtimeManager.getFileOrFolder(file);
         serviceImpl = createUserService(realmFile);
@@ -293,7 +294,6 @@ public class HtpasswdUserService extends GitblitUserService
         if ( !file.equals(htpasswdFilePath) ) {
             // The htpasswd file setting changed. Rediscover the file.
             this.htpasswdFilePath = file;
-            IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
             this.htpasswdFile = runtimeManager.getFileOrFolder(file);
             this.htUsers.clear();
             this.forceReload = true;
index 628c16503beb079d974f85b4bf11d88fa3bc6442..33f519f0e249d7e3d9e29420b995a0ac424a048a 100644 (file)
@@ -18,6 +18,7 @@ package com.gitblit;
 import java.util.Collection;\r
 import java.util.List;\r
 \r
+import com.gitblit.manager.IRuntimeManager;\r
 import com.gitblit.models.TeamModel;\r
 import com.gitblit.models.UserModel;\r
 \r
@@ -35,10 +36,10 @@ public interface IUserService {
         * retrieve settings from gitblit.properties or the web.xml file without\r
         * relying on the GitBlit static singleton.\r
         *\r
-        * @param settings\r
-        * @since 0.7.0\r
+        * @param runtimeManager\r
+        * @since 1.4.0\r
         */\r
-       void setup(IStoredSettings settings);\r
+       void setup(IRuntimeManager runtimeManager);\r
 \r
        /**\r
         * Does the user service support changes to credentials?\r
index e6951b40856b18c7dec5e16ac159fc1bea6241ae..5619dadd42dda31ea6e764c3b57a243ce568d41f 100644 (file)
@@ -81,10 +81,9 @@ public class LdapUserService extends GitblitUserService {
     }\r
 \r
        @Override\r
-       public void setup(IStoredSettings settings) {\r
-               this.settings = settings;\r
+       public void setup(IRuntimeManager runtimeManager) {\r
+               this.settings = runtimeManager.getSettings();\r
                String file = settings.getString(Keys.realm.ldap.backingUserService, "${baseFolder}/users.conf");\r
-               IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
                File realmFile = runtimeManager.getFileOrFolder(file);\r
 \r
                serviceImpl = createUserService(realmFile);\r
index e6ab04a897e010ed4b83412d480e9bb4a789abc3..b348e64c45af6ee75e93a8af39af2f175b104b54 100644 (file)
@@ -45,11 +45,10 @@ public class PAMUserService extends GitblitUserService {
     }\r
 \r
     @Override\r
-    public void setup(IStoredSettings settings) {\r
-        this.settings = settings;\r
+    public void setup(IRuntimeManager runtimeManager) {\r
+        this.settings = runtimeManager.getSettings();\r
 \r
         String file = settings.getString(Keys.realm.pam.backingUserService, "${baseFolder}/users.conf");\r
-        IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
         File realmFile = runtimeManager.getFileOrFolder(file);\r
 \r
         serviceImpl = createUserService(realmFile);\r
index 1148925b61ad576115d9ab1e9cb2bb19314ee0af..62322ebc4f181dc104ebaf389c6c33e482d639f0 100644 (file)
@@ -60,11 +60,10 @@ public class RedmineUserService extends GitblitUserService {
     }\r
 \r
     @Override\r
-    public void setup(IStoredSettings settings) {\r
-        this.settings = settings;\r
+    public void setup(IRuntimeManager runtimeManager) {\r
+        this.settings = runtimeManager.getSettings();\r
 \r
         String file = settings.getString(Keys.realm.redmine.backingUserService, "${baseFolder}/users.conf");\r
-        IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
         File realmFile = runtimeManager.getFileOrFolder(file);\r
 \r
         serviceImpl = createUserService(realmFile);\r
index bfc96be01dd73a0161b4ef63417489d0936e43e8..0eca6c9c526749e04ca3146c44990169d0eb436e 100644 (file)
@@ -27,12 +27,11 @@ public class SalesforceUserService extends GitblitUserService {
        }
 
        @Override
-       public void setup(IStoredSettings settings) {
-               this.settings = settings;
+       public void setup(IRuntimeManager runtimeManager) {
+               this.settings = runtimeManager.getSettings();
                String file = settings.getString(
                                Keys.realm.salesforce.backingUserService,
                                "${baseFolder}/users.conf");
-               IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
                File realmFile = runtimeManager.getFileOrFolder(file);
 
                serviceImpl = createUserService(realmFile);
index 03d65e02354c205e782afc10b5ce100e128ede9f..9b25efbebb1dc378914e15ec2f9928d4685accfb 100644 (file)
@@ -53,11 +53,10 @@ public class WindowsUserService extends GitblitUserService {
     }\r
 \r
     @Override\r
-    public void setup(IStoredSettings settings) {\r
-        this.settings = settings;\r
+    public void setup(IRuntimeManager runtimeManager) {\r
+        this.settings = runtimeManager.getSettings();\r
 \r
         String file = settings.getString(Keys.realm.windows.backingUserService, "${baseFolder}/users.conf");\r
-        IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);\r
         File realmFile = runtimeManager.getFileOrFolder(file);\r
 \r
         serviceImpl = createUserService(realmFile);\r
index e20c82a77640b9d00951385b0a8b393c87711fd6..282debb308d3c93ac4c479b452ac6ed99466ccb1 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.Test;
 import com.gitblit.HtpasswdUserService;
 import com.gitblit.models.UserModel;
 import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.tests.mock.MockRuntimeManager;
 import com.gitblit.utils.StringUtils;
 
 /**
@@ -72,13 +73,13 @@ public class HtpasswdUserServiceTest extends GitblitUnitTest {
     private void setupUS()
     {
         htpwdUserService = new HtpasswdUserService();
-        htpwdUserService.setup(getSettings());
+        htpwdUserService.setup(new MockRuntimeManager(getSettings()));
     }
 
     private void setupUS(boolean overrideLA)
     {
         htpwdUserService = new HtpasswdUserService();
-        htpwdUserService.setup(getSettings(overrideLA));
+        htpwdUserService.setup(new MockRuntimeManager(getSettings(overrideLA)));
     }
 
 
index 86d1f3ca8e52a899660f577549356b90e1564602..8bd90147a3229ca936ed64d610c3a5fca52af18b 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Test;
 import com.gitblit.LdapUserService;
 import com.gitblit.models.UserModel;
 import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.tests.mock.MockRuntimeManager;
 import com.gitblit.utils.StringUtils;
 import com.unboundid.ldap.listener.InMemoryDirectoryServer;
 import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
@@ -60,7 +61,7 @@ public class LdapUserServiceTest extends GitblitUnitTest {
        @Before
        public void createLdapUserService() {
                ldapUserService = new LdapUserService();
-               ldapUserService.setup(getSettings());
+               ldapUserService.setup(new MockRuntimeManager(getSettings()));
        }
 
        private MemorySettings getSettings() {
@@ -118,7 +119,7 @@ public class LdapUserServiceTest extends GitblitUnitTest {
                MemorySettings ms = getSettings();
                ms.put("realm.ldap.displayName", "${personalTitle}. ${givenName} ${surname}");
                ldapUserService = new LdapUserService();
-               ldapUserService.setup(ms);
+               ldapUserService.setup(new MockRuntimeManager(ms));
 
                userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
                assertNotNull(userOneModel);
@@ -135,7 +136,7 @@ public class LdapUserServiceTest extends GitblitUnitTest {
                MemorySettings ms = getSettings();
                ms.put("realm.ldap.email", "${givenName}.${surname}@gitblit.com");
                ldapUserService = new LdapUserService();
-               ldapUserService.setup(ms);
+               ldapUserService.setup(new MockRuntimeManager(ms));
 
                userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
                assertNotNull(userOneModel);
index 753762390c2d1377405e4d81bc5de4682818b9cc..3c6769f09907227b6471e8a41340a24630886564 100644 (file)
@@ -2,13 +2,11 @@ package com.gitblit.tests;
 \r
 import static org.hamcrest.CoreMatchers.is;\r
 \r
-import java.util.HashMap;\r
-\r
 import org.junit.Test;\r
 \r
 import com.gitblit.RedmineUserService;\r
 import com.gitblit.models.UserModel;\r
-import com.gitblit.tests.mock.MemorySettings;\r
+import com.gitblit.tests.mock.MockRuntimeManager;\r
 import com.gitblit.utils.StringUtils;\r
 \r
 public class RedmineUserServiceTest extends GitblitUnitTest {\r
@@ -24,7 +22,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest {
     @Test\r
     public void testAuthenticate() throws Exception {\r
         RedmineUserService redmineUserService = new RedmineUserService();\r
-        redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+        redmineUserService.setup(new MockRuntimeManager());\r
         redmineUserService.setTestingCurrentUserAsJson(JSON);\r
         UserModel userModel = redmineUserService.authenticate("RedmineAdminId", "RedmineAPIKey".toCharArray());\r
         assertThat(userModel.getName(), is("redmineadminid"));\r
@@ -37,7 +35,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest {
     @Test\r
     public void testAuthenticateNotAdminUser() throws Exception {\r
         RedmineUserService redmineUserService = new RedmineUserService();\r
-        redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+        redmineUserService.setup(new MockRuntimeManager());\r
         redmineUserService.setTestingCurrentUserAsJson(NOT_ADMIN_JSON);\r
         UserModel userModel = redmineUserService.authenticate("RedmineUserId", "RedmineAPIKey".toCharArray());\r
         assertThat(userModel.getName(), is("redmineuserid"));\r
@@ -50,7 +48,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest {
     @Test\r
        public void testLocalAccount() {\r
         RedmineUserService redmineUserService = new RedmineUserService();\r
-        redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+        redmineUserService.setup(new MockRuntimeManager());\r
 \r
                UserModel localAccount = new UserModel("bruce");\r
                localAccount.displayName = "Bruce Campbell";\r
diff --git a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java
new file mode 100644 (file)
index 0000000..b42d29a
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2013 gitblit.com.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.tests.mock;
+
+import java.io.File;
+import java.util.Date;
+import java.util.Map;
+import java.util.TimeZone;
+
+import com.gitblit.Constants;
+import com.gitblit.IStoredSettings;
+import com.gitblit.manager.IRuntimeManager;
+import com.gitblit.models.ServerSettings;
+import com.gitblit.models.ServerStatus;
+import com.gitblit.models.SettingModel;
+
+public class MockRuntimeManager implements IRuntimeManager {
+
+       File baseFolder;
+
+       IStoredSettings settings;
+
+       ServerStatus serverStatus;
+
+       ServerSettings serverSettings;
+
+       public MockRuntimeManager() {
+               this(new MemorySettings());
+       }
+
+       public MockRuntimeManager(Map<String, Object> settings) {
+               this(new MemorySettings(settings));
+       }
+
+       public MockRuntimeManager(IStoredSettings settings) {
+               this.settings = settings;
+
+               this.serverStatus = new ServerStatus(true);
+               this.serverStatus.servletContainer = "MockServer";
+
+               this.serverSettings = new ServerSettings();
+       }
+
+       @Override
+       public void setBaseFolder(File folder) {
+               this.baseFolder = folder;
+       }
+
+       @Override
+       public File getBaseFolder() {
+               return baseFolder;
+       }
+
+       @Override
+       public TimeZone getTimezone() {
+               return TimeZone.getDefault();
+       }
+
+       @Override
+       public boolean isServingRepositories() {
+               return true;
+       }
+
+       @Override
+       public boolean isDebugMode() {
+               return true;
+       }
+
+       @Override
+       public Date getBootDate() {
+               return serverStatus.bootDate;
+       }
+
+       @Override
+       public ServerStatus getStatus() {
+               // update heap memory status
+               serverStatus.heapAllocated = Runtime.getRuntime().totalMemory();
+               serverStatus.heapFree = Runtime.getRuntime().freeMemory();
+               return serverStatus;
+       }
+
+       @Override
+       public ServerSettings getSettingsModel() {
+               // ensure that the current values are updated in the setting models
+               for (String key : settings.getAllKeys(null)) {
+                       SettingModel setting = serverSettings.get(key);
+                       if (setting == null) {
+                               // unreferenced setting, create a setting model
+                               setting = new SettingModel();
+                               setting.name = key;
+                               serverSettings.add(setting);
+                       }
+                       setting.currentValue = settings.getString(key, "");
+               }
+               return serverSettings;
+       }
+
+       @Override
+       public File getFileOrFolder(String key, String defaultFileOrFolder) {
+               String fileOrFolder = settings.getString(key, defaultFileOrFolder);
+               return getFileOrFolder(fileOrFolder);
+       }
+
+       @Override
+       public File getFileOrFolder(String fileOrFolder) {
+               return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$,
+                               baseFolder, fileOrFolder);
+       }
+
+       @Override
+       public IStoredSettings getSettings() {
+               return settings;
+       }
+
+       @Override
+       public boolean updateSettings(Map<String, String> updatedSettings) {
+               return settings.saveSettings(updatedSettings);
+       }
+
+}