Skip to content

Update UserManager to support construction of IUserServices with IRuntimeManager as a constructor parameter #1171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/gitblit/IUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* 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
*
*/
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/gitblit/manager/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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;
Expand Down Expand Up @@ -119,15 +121,33 @@ public UserManager start() {
// 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);
}
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")) {
Expand Down