Skip to content

Commit 5541f37

Browse files
authored
Merge pull request #1171 from pingunaut/usermanager-file-instantiation
Update UserManager to support construction of IUserServices with IRuntimeManager as a constructor parameter
2 parents c8c70d7 + d79f563 commit 5541f37

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-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: 22 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,15 +121,33 @@ 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 e) {
125+
logger.error("failed to instantiate user service {}: {}. Trying once again with IRuntimeManager constructor", realm, e.getMessage());
126+
//try once again with IRuntimeManager constructor. This adds support for subclasses of ConfigUserService and other custom IUserServices
127+
service = createIRuntimeManagerAwareUserService(realm);
124128
}
125129
}
126130
setUserService(service);
127131
}
128132
return this;
129133
}
130134

135+
/**
136+
* Tries to create an {@link IUserService} with {@link #runtimeManager} as a constructor parameter
137+
*
138+
* @param realm the class name of the {@link IUserService} to be instantiated
139+
* @return the {@link IUserService} or {@code null} if instantiation fails
140+
*/
141+
private IUserService createIRuntimeManagerAwareUserService(String realm) {
142+
try {
143+
Constructor<?> constructor = Class.forName(realm).getConstructor(IRuntimeManager.class);
144+
return (IUserService) constructor.newInstance(runtimeManager);
145+
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
146+
logger.error("failed to instantiate user service {}: {}", realm, e.getMessage());
147+
return null;
148+
}
149+
}
150+
131151
protected IUserService createUserService(File realmFile) {
132152
IUserService service = null;
133153
if (realmFile.getName().toLowerCase().endsWith(".conf")) {

0 commit comments

Comments
 (0)