Skip to content

Commit d79f563

Browse files
author
Martin Spielmann
committed
extracted method
1 parent a6ae27a commit d79f563

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,33 @@ public UserManager start() {
121121
// typical file path configuration
122122
File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf");
123123
service = createUserService(realmFile);
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 IRuntimeManager constructor. This adds support for subclasses of ConfigUserService and other custom IUserServices
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-
}
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);
133128
}
134129
}
135130
setUserService(service);
136131
}
137132
return this;
138133
}
139134

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+
140151
protected IUserService createUserService(File realmFile) {
141152
IUserService service = null;
142153
if (realmFile.getName().toLowerCase().endsWith(".conf")) {

0 commit comments

Comments
 (0)