Skip to content

Commit da6a261

Browse files
update user manager to support instantiation if IUserService with IRuntimeManager as a parameter
1 parent 99b4a18 commit da6a261

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/main/java/com/gitblit/IUserService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* Implementations of IUserService control all aspects of UserModel objects and
2727
* user authentication.
2828
*
29+
* Plugins implementing this interface (which are instantiated during {@link com.gitblit.manager.UserManager#start()}) can provide
30+
* a default constructor or might also use {@link IRuntimeManager} as a constructor argument which will be passed automatically then.
31+
*
2932
* @author James Moger
3033
*
3134
*/

src/main/java/com/gitblit/manager/UserManager.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.File;
1919
import java.io.IOException;
20+
import java.lang.reflect.Constructor;
21+
import java.lang.reflect.InvocationTargetException;
2022
import java.text.MessageFormat;
2123
import java.util.ArrayList;
2224
import java.util.Collection;
@@ -119,8 +121,15 @@ public UserManager start() {
119121
// typical file path configuration
120122
File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");
121123
service = createUserService(realmFile);
122-
} catch (InstantiationException | IllegalAccessException e) {
123-
logger.error("failed to instantiate user service {}: {}", realm, e.getMessage());
124+
} catch (InstantiationException | IllegalAccessException e1) {
125+
logger.error("failed to instantiate user service {}: {}. Trying once again with IRuntimeManager constructor", realm, e1.getMessage());
126+
//try once again with file constructor. this adds support for subclasses of ConfigUserService
127+
try {
128+
Constructor<?> constructor = Class.forName(realm).getConstructor(IRuntimeManager.class);
129+
service = (IUserService) constructor.newInstance(runtimeManager);
130+
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e2) {
131+
logger.error("failed to instantiate user service {}: {}", realm, e2.getMessage());
132+
}
124133
}
125134
}
126135
setUserService(service);

0 commit comments

Comments
 (0)