@Override\r
public void setup(IStoredSettings settings) {\r
this.settings = settings;\r
- String file = settings.getString(Keys.realm.ldap_backingUserService, "users.conf");\r
+ String file = settings.getString(Keys.realm.ldap.backingUserService, "users.conf");\r
File realmFile = GitBlit.getFileOrFolder(file);\r
\r
serviceImpl = createUserService(realmFile);\r
\r
private LDAPConnection getLdapConnection() {\r
try {\r
- URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap_server));\r
- String bindUserName = settings.getString(Keys.realm.ldap_username, "");\r
- String bindPassword = settings.getString(Keys.realm.ldap_password, "");\r
+ URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap.server));\r
+ String bindUserName = settings.getString(Keys.realm.ldap.username, "");\r
+ String bindPassword = settings.getString(Keys.realm.ldap.password, "");\r
int ldapPort = ldapUrl.getPort();\r
\r
if (ldapUrl.getScheme().equalsIgnoreCase("ldaps")) { // SSL\r
* @since 1.0.0\r
*/ \r
public boolean supportsTeamMembershipChanges() {\r
- return !settings.getBoolean(Keys.realm.ldap_maintainTeams, false);\r
+ return !settings.getBoolean(Keys.realm.ldap.maintainTeams, false);\r
}\r
\r
/**\r
LDAPConnection ldapConnection = getLdapConnection(); \r
if (ldapConnection != null) {\r
// Find the logging in user's DN\r
- String accountBase = settings.getString(Keys.realm.ldap_accountBase, "");\r
- String accountPattern = settings.getString(Keys.realm.ldap_accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))");\r
+ String accountBase = settings.getString(Keys.realm.ldap.accountBase, "");\r
+ String accountPattern = settings.getString(Keys.realm.ldap.accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))");\r
accountPattern = StringUtils.replace(accountPattern, "${username}", simpleUsername);\r
\r
SearchResult result = doSearch(ldapConnection, accountBase, accountPattern);\r
\r
private void setAdminAttribute(UserModel user) {\r
user.canAdmin = false;\r
- List<String> admins = settings.getStrings(Keys.realm.ldap_admins);\r
+ List<String> admins = settings.getStrings(Keys.realm.ldap.admins);\r
for (String admin : admins) {\r
if (admin.startsWith("@")) { // Team\r
if (user.getTeam(admin.substring(1)) != null)\r
String loggingInUserDN = loggingInUser.getDN();\r
\r
user.teams.clear(); // Clear the users team memberships - we're going to get them from LDAP\r
- String groupBase = settings.getString(Keys.realm.ldap_groupBase, "");\r
- String groupMemberPattern = settings.getString(Keys.realm.ldap_groupMemberPattern, "(&(objectClass=group)(member=${dn}))");\r
+ String groupBase = settings.getString(Keys.realm.ldap.groupBase, "");\r
+ String groupMemberPattern = settings.getString(Keys.realm.ldap.groupMemberPattern, "(&(objectClass=group)(member=${dn}))");\r
\r
groupMemberPattern = StringUtils.replace(groupMemberPattern, "${dn}", loggingInUserDN);\r
groupMemberPattern = StringUtils.replace(groupMemberPattern, "${username}", simpleUsername);\r
import java.text.MessageFormat;\r
import java.util.ArrayList;\r
import java.util.Collections;\r
-import java.util.HashMap;\r
import java.util.List;\r
-import java.util.Map;\r
import java.util.Properties;\r
\r
import com.gitblit.Constants;\r
List<String> keys = new ArrayList<String>(properties.stringPropertyNames());\r
Collections.sort(keys);\r
\r
- // Determine static key group classes\r
- Map<String, List<String>> staticClasses = new HashMap<String, List<String>>();\r
- staticClasses.put("", new ArrayList<String>());\r
+ KeyGroup root = new KeyGroup();\r
for (String key : keys) {\r
- String clazz = "";\r
- String field = key;\r
- if (key.indexOf('.') > -1) {\r
- clazz = key.substring(0, key.indexOf('.'));\r
- field = key.substring(key.indexOf('.') + 1);\r
- }\r
- if (!staticClasses.containsKey(clazz)) {\r
- staticClasses.put(clazz, new ArrayList<String>());\r
- }\r
- staticClasses.get(clazz).add(field);\r
+ root.addKey(key);\r
}\r
\r
- // Assemble Keys source file\r
- StringBuilder sb = new StringBuilder();\r
- sb.append("package com.gitblit;\n");\r
- sb.append('\n');\r
- sb.append("/*\n");\r
- sb.append(" * This class is auto-generated from the properties file.\n");\r
- sb.append(" * Do not version control!\n");\r
- sb.append(" */\n");\r
- sb.append("public final class Keys {\n");\r
- sb.append('\n');\r
- List<String> classSet = new ArrayList<String>(staticClasses.keySet());\r
- Collections.sort(classSet);\r
- for (String clazz : classSet) {\r
- List<String> keySet = staticClasses.get(clazz);\r
- if (clazz.equals("")) {\r
- // root keys\r
- for (String key : keySet) {\r
- sb.append(MessageFormat.format(\r
- "\tpublic static final String {0} = \"{1}\";\n\n",\r
- key.replace('.', '_'), key));\r
- }\r
- } else {\r
- // class keys\r
- sb.append(MessageFormat.format("\tpublic static final class {0} '{'\n\n", clazz));\r
- sb.append(MessageFormat.format(\r
- "\t\tpublic static final String _ROOT = \"{0}\";\n\n", clazz));\r
- for (String key : keySet) {\r
- sb.append(MessageFormat.format(\r
- "\t\tpublic static final String {0} = \"{1}\";\n\n",\r
- key.replace('.', '_'), clazz + "." + key));\r
- }\r
- sb.append("\t}\n\n");\r
- }\r
- }\r
- sb.append('}');\r
-\r
// Save Keys class definition\r
try {\r
File file = new File("src/com/gitblit/Keys.java");\r
FileWriter fw = new FileWriter(file, false);\r
- fw.write(sb.toString());\r
+ fw.write(root.generateClass("com.gitblit", "Keys"));\r
fw.close();\r
} catch (Throwable t) {\r
t.printStackTrace();\r
}\r
}\r
+ \r
+ private static class KeyGroup {\r
+ final KeyGroup parent;\r
+ final String namespace;\r
+ \r
+ String name;\r
+ List<KeyGroup> children; \r
+ List<String> fields; \r
+ \r
+ KeyGroup() {\r
+ this.parent = null;\r
+ this.namespace = "";\r
+ this.name = ""; \r
+ }\r
+ \r
+ KeyGroup(String namespace, KeyGroup parent) {\r
+ this.parent = parent;\r
+ this.namespace = namespace;\r
+ if (parent.children == null) {\r
+ parent.children = new ArrayList<KeyGroup>();\r
+ }\r
+ parent.children.add(this);\r
+ }\r
+ \r
+ void addKey(String key) {\r
+ String keyspace = "";\r
+ String field = key;\r
+ if (key.indexOf('.') > -1) {\r
+ keyspace = key.substring(0, key.lastIndexOf('.'));\r
+ field = key.substring(key.lastIndexOf('.') + 1);\r
+ KeyGroup group = addKeyGroup(keyspace);\r
+ group.addKey(field);\r
+ } else {\r
+ if (fields == null) {\r
+ fields = new ArrayList<String>();\r
+ }\r
+ fields.add(key);\r
+ }\r
+ }\r
+ \r
+ KeyGroup addKeyGroup(String keyspace) {\r
+ KeyGroup parent = this;\r
+ KeyGroup node = null; \r
+ String [] space = keyspace.split("\\.");\r
+ for (int i = 0; i < space.length; i++) {\r
+ StringBuilder namespace = new StringBuilder();\r
+ for (int j = 0; j <= i; j++) {\r
+ namespace.append(space[j]);\r
+ if (j < i) {\r
+ namespace.append('.');\r
+ }\r
+ }\r
+ if (parent.children != null) {\r
+ for (KeyGroup child : parent.children) {\r
+ if (child.name.equals(space[i])) {\r
+ node = child; \r
+ }\r
+ }\r
+ }\r
+ if (node == null) {\r
+ node = new KeyGroup(namespace.toString(), parent);\r
+ node.name = space[i];\r
+ }\r
+ parent = node;\r
+ node = null;\r
+ }\r
+ return parent;\r
+ } \r
+ \r
+ String fullKey(String field) {\r
+ if (namespace.equals("")) {\r
+ return field;\r
+ }\r
+ return namespace + "." + field;\r
+ }\r
+ \r
+ String generateClass(String packageName, String className) {\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("package ").append(packageName).append(";\n");\r
+ sb.append('\n');\r
+ sb.append("/*\n");\r
+ sb.append(" * This class is auto-generated from the properties file.\n");\r
+ sb.append(" * Do not version control!\n");\r
+ sb.append(" */\n");\r
+ sb.append(MessageFormat.format("public final class {0} '{'\n\n", className));\r
+ sb.append(generateClass(this, 0));\r
+ sb.append("}\n");\r
+ return sb.toString();\r
+ }\r
+ \r
+ String generateClass(KeyGroup group, int level) {\r
+ String classIndent = StringUtils.leftPad("", level, '\t');\r
+ String fieldIndent = StringUtils.leftPad("", level + 1, '\t');\r
+ \r
+ // begin class\r
+ StringBuilder sb = new StringBuilder();\r
+ if (!group.namespace.equals("")) {\r
+ sb.append(classIndent).append(MessageFormat.format("public static final class {0} '{'\n\n", group.name));\r
+ sb.append(fieldIndent).append(MessageFormat.format("public static final String _ROOT = \"{0}\";\n\n", group.namespace));\r
+ }\r
+ \r
+ if (group.fields != null) {\r
+ // fields\r
+ for (String field : group.fields) { \r
+ sb.append(fieldIndent).append(MessageFormat.format("public static final String {0} = \"{1}\";\n\n", field, group.fullKey(field)));\r
+ }\r
+ }\r
+ if (group.children != null) {\r
+ // inner classes\r
+ for (KeyGroup child : group.children) {\r
+ sb.append(generateClass(child, level + 1));\r
+ }\r
+ }\r
+ // end class\r
+ if (!group.namespace.equals("")) {\r
+ sb.append(classIndent).append("}\n\n");\r
+ }\r
+ return sb.toString(); \r
+ }\r
+ }\r
\r
/**\r
* Download a file from the official Apache Maven repository.\r