diff options
author | James Moger <james.moger@gmail.com> | 2017-01-06 09:41:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-06 09:41:44 -0500 |
commit | 5541f37ea97b5f78680344e74c5150831c45a8ab (patch) | |
tree | 0cd254d6ae907fab951fdb6553d7f516a95b6f93 | |
parent | c8c70d74e93283fffe591c07b73b5f44e01b7d4b (diff) | |
parent | d79f5630c82a0d89ec5b2d3a1f0365bf72668a78 (diff) | |
download | gitblit-5541f37ea97b5f78680344e74c5150831c45a8ab.tar.gz gitblit-5541f37ea97b5f78680344e74c5150831c45a8ab.zip |
Merge pull request #1171 from pingunaut/usermanager-file-instantiation
Update UserManager to support construction of IUserServices with IRuntimeManager as a constructor parameter
-rw-r--r-- | src/main/java/com/gitblit/IUserService.java | 3 | ||||
-rw-r--r-- | src/main/java/com/gitblit/manager/UserManager.java | 24 |
2 files changed, 25 insertions, 2 deletions
diff --git a/src/main/java/com/gitblit/IUserService.java b/src/main/java/com/gitblit/IUserService.java index 6f3c5423..468f968f 100644 --- a/src/main/java/com/gitblit/IUserService.java +++ b/src/main/java/com/gitblit/IUserService.java @@ -26,6 +26,9 @@ import com.gitblit.models.UserModel; * Implementations of IUserService control all aspects of UserModel objects and
* user authentication.
*
+ * Plugins implementing this interface (which are instantiated during {@link com.gitblit.manager.UserManager#start()}) can provide
+ * a default constructor or might also use {@link IRuntimeManager} as a constructor argument which will be passed automatically then.
+ *
* @author James Moger
*
*/
diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java index e88ac93c..d661c9b4 100644 --- a/src/main/java/com/gitblit/manager/UserManager.java +++ b/src/main/java/com/gitblit/manager/UserManager.java @@ -17,6 +17,8 @@ package com.gitblit.manager; import java.io.File; import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -119,8 +121,10 @@ public class UserManager implements IUserManager { // typical file path configuration File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf"); service = createUserService(realmFile); - } catch (InstantiationException | IllegalAccessException e) { - logger.error("failed to instantiate user service {}: {}", realm, e.getMessage()); + } catch (InstantiationException | IllegalAccessException e) { + logger.error("failed to instantiate user service {}: {}. Trying once again with IRuntimeManager constructor", realm, e.getMessage()); + //try once again with IRuntimeManager constructor. This adds support for subclasses of ConfigUserService and other custom IUserServices + service = createIRuntimeManagerAwareUserService(realm); } } setUserService(service); @@ -128,6 +132,22 @@ public class UserManager implements IUserManager { return this; } + /** + * Tries to create an {@link IUserService} with {@link #runtimeManager} as a constructor parameter + * + * @param realm the class name of the {@link IUserService} to be instantiated + * @return the {@link IUserService} or {@code null} if instantiation fails + */ + private IUserService createIRuntimeManagerAwareUserService(String realm) { + try { + Constructor<?> constructor = Class.forName(realm).getConstructor(IRuntimeManager.class); + return (IUserService) constructor.newInstance(runtimeManager); + } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + logger.error("failed to instantiate user service {}: {}", realm, e.getMessage()); + return null; + } + } + protected IUserService createUserService(File realmFile) { IUserService service = null; if (realmFile.getName().toLowerCase().endsWith(".conf")) { |