Skip to content

Commit

Permalink
extracted method
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Spielmann committed Jan 6, 2017
1 parent a6ae27a commit d79f563
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/main/java/com/gitblit/manager/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +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 e1) {
logger.error("failed to instantiate user service {}: {}. Trying once again with IRuntimeManager constructor", realm, e1.getMessage());
//try once again with IRuntimeManager constructor. This adds support for subclasses of ConfigUserService and other custom IUserServices
try {
Constructor<?> constructor = Class.forName(realm).getConstructor(IRuntimeManager.class);
service = (IUserService) constructor.newInstance(runtimeManager);
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e2) {
logger.error("failed to instantiate user service {}: {}", realm, e2.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

0 comments on commit d79f563

Please sign in to comment.