From 668663dffb73722e2fae12d9f737d80cd54b3f03 Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 24 Apr 2012 17:56:23 -0400 Subject: [PATCH] Updated the key generator to build nested key classes --- src/com/gitblit/GitBlitServer.java | 6 +- src/com/gitblit/LdapUserService.java | 20 +-- src/com/gitblit/build/Build.java | 175 +++++++++++++++++++-------- 3 files changed, 136 insertions(+), 65 deletions(-) diff --git a/src/com/gitblit/GitBlitServer.java b/src/com/gitblit/GitBlitServer.java index 1307bc3d..f0dce774 100644 --- a/src/com/gitblit/GitBlitServer.java +++ b/src/com/gitblit/GitBlitServer.java @@ -280,11 +280,11 @@ public class GitBlitServer { if (StringUtils.isEmpty(params.ldapLdifFile) == false) { File ldifFile = new File(params.ldapLdifFile); if (ldifFile != null && ldifFile.exists()) { - URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap_server)); + URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap.server)); String firstLine = new Scanner(ldifFile).nextLine(); String rootDN = firstLine.substring(4); - String bindUserName = settings.getString(Keys.realm.ldap_username, ""); - String bindPassword = settings.getString(Keys.realm.ldap_password, ""); + String bindUserName = settings.getString(Keys.realm.ldap.username, ""); + String bindPassword = settings.getString(Keys.realm.ldap.password, ""); // Get the port int port = ldapUrl.getPort(); diff --git a/src/com/gitblit/LdapUserService.java b/src/com/gitblit/LdapUserService.java index 86b61364..25f4e03b 100644 --- a/src/com/gitblit/LdapUserService.java +++ b/src/com/gitblit/LdapUserService.java @@ -56,7 +56,7 @@ public class LdapUserService extends GitblitUserService { @Override public void setup(IStoredSettings settings) { this.settings = settings; - String file = settings.getString(Keys.realm.ldap_backingUserService, "users.conf"); + String file = settings.getString(Keys.realm.ldap.backingUserService, "users.conf"); File realmFile = GitBlit.getFileOrFolder(file); serviceImpl = createUserService(realmFile); @@ -65,9 +65,9 @@ public class LdapUserService extends GitblitUserService { private LDAPConnection getLdapConnection() { try { - URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap_server)); - String bindUserName = settings.getString(Keys.realm.ldap_username, ""); - String bindPassword = settings.getString(Keys.realm.ldap_password, ""); + URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap.server)); + String bindUserName = settings.getString(Keys.realm.ldap.username, ""); + String bindPassword = settings.getString(Keys.realm.ldap.password, ""); int ldapPort = ldapUrl.getPort(); if (ldapUrl.getScheme().equalsIgnoreCase("ldaps")) { // SSL @@ -114,7 +114,7 @@ public class LdapUserService extends GitblitUserService { * @since 1.0.0 */ public boolean supportsTeamMembershipChanges() { - return !settings.getBoolean(Keys.realm.ldap_maintainTeams, false); + return !settings.getBoolean(Keys.realm.ldap.maintainTeams, false); } /** @@ -135,8 +135,8 @@ public class LdapUserService extends GitblitUserService { LDAPConnection ldapConnection = getLdapConnection(); if (ldapConnection != null) { // Find the logging in user's DN - String accountBase = settings.getString(Keys.realm.ldap_accountBase, ""); - String accountPattern = settings.getString(Keys.realm.ldap_accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))"); + String accountBase = settings.getString(Keys.realm.ldap.accountBase, ""); + String accountPattern = settings.getString(Keys.realm.ldap.accountPattern, "(&(objectClass=person)(sAMAccountName=${username}))"); accountPattern = StringUtils.replace(accountPattern, "${username}", simpleUsername); SearchResult result = doSearch(ldapConnection, accountBase, accountPattern); @@ -176,7 +176,7 @@ public class LdapUserService extends GitblitUserService { private void setAdminAttribute(UserModel user) { user.canAdmin = false; - List admins = settings.getStrings(Keys.realm.ldap_admins); + List admins = settings.getStrings(Keys.realm.ldap.admins); for (String admin : admins) { if (admin.startsWith("@")) { // Team if (user.getTeam(admin.substring(1)) != null) @@ -191,8 +191,8 @@ public class LdapUserService extends GitblitUserService { String loggingInUserDN = loggingInUser.getDN(); user.teams.clear(); // Clear the users team memberships - we're going to get them from LDAP - String groupBase = settings.getString(Keys.realm.ldap_groupBase, ""); - String groupMemberPattern = settings.getString(Keys.realm.ldap_groupMemberPattern, "(&(objectClass=group)(member=${dn}))"); + String groupBase = settings.getString(Keys.realm.ldap.groupBase, ""); + String groupMemberPattern = settings.getString(Keys.realm.ldap.groupMemberPattern, "(&(objectClass=group)(member=${dn}))"); groupMemberPattern = StringUtils.replace(groupMemberPattern, "${dn}", loggingInUserDN); groupMemberPattern = StringUtils.replace(groupMemberPattern, "${username}", simpleUsername); diff --git a/src/com/gitblit/build/Build.java b/src/com/gitblit/build/Build.java index cbe09a9c..fcf52b99 100644 --- a/src/com/gitblit/build/Build.java +++ b/src/com/gitblit/build/Build.java @@ -27,9 +27,7 @@ import java.net.URL; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Properties; import com.gitblit.Constants; @@ -182,68 +180,141 @@ public class Build { List keys = new ArrayList(properties.stringPropertyNames()); Collections.sort(keys); - // Determine static key group classes - Map> staticClasses = new HashMap>(); - staticClasses.put("", new ArrayList()); + KeyGroup root = new KeyGroup(); for (String key : keys) { - String clazz = ""; - String field = key; - if (key.indexOf('.') > -1) { - clazz = key.substring(0, key.indexOf('.')); - field = key.substring(key.indexOf('.') + 1); - } - if (!staticClasses.containsKey(clazz)) { - staticClasses.put(clazz, new ArrayList()); - } - staticClasses.get(clazz).add(field); + root.addKey(key); } - // Assemble Keys source file - StringBuilder sb = new StringBuilder(); - sb.append("package com.gitblit;\n"); - sb.append('\n'); - sb.append("/*\n"); - sb.append(" * This class is auto-generated from the properties file.\n"); - sb.append(" * Do not version control!\n"); - sb.append(" */\n"); - sb.append("public final class Keys {\n"); - sb.append('\n'); - List classSet = new ArrayList(staticClasses.keySet()); - Collections.sort(classSet); - for (String clazz : classSet) { - List keySet = staticClasses.get(clazz); - if (clazz.equals("")) { - // root keys - for (String key : keySet) { - sb.append(MessageFormat.format( - "\tpublic static final String {0} = \"{1}\";\n\n", - key.replace('.', '_'), key)); - } - } else { - // class keys - sb.append(MessageFormat.format("\tpublic static final class {0} '{'\n\n", clazz)); - sb.append(MessageFormat.format( - "\t\tpublic static final String _ROOT = \"{0}\";\n\n", clazz)); - for (String key : keySet) { - sb.append(MessageFormat.format( - "\t\tpublic static final String {0} = \"{1}\";\n\n", - key.replace('.', '_'), clazz + "." + key)); - } - sb.append("\t}\n\n"); - } - } - sb.append('}'); - // Save Keys class definition try { File file = new File("src/com/gitblit/Keys.java"); FileWriter fw = new FileWriter(file, false); - fw.write(sb.toString()); + fw.write(root.generateClass("com.gitblit", "Keys")); fw.close(); } catch (Throwable t) { t.printStackTrace(); } } + + private static class KeyGroup { + final KeyGroup parent; + final String namespace; + + String name; + List children; + List fields; + + KeyGroup() { + this.parent = null; + this.namespace = ""; + this.name = ""; + } + + KeyGroup(String namespace, KeyGroup parent) { + this.parent = parent; + this.namespace = namespace; + if (parent.children == null) { + parent.children = new ArrayList(); + } + parent.children.add(this); + } + + void addKey(String key) { + String keyspace = ""; + String field = key; + if (key.indexOf('.') > -1) { + keyspace = key.substring(0, key.lastIndexOf('.')); + field = key.substring(key.lastIndexOf('.') + 1); + KeyGroup group = addKeyGroup(keyspace); + group.addKey(field); + } else { + if (fields == null) { + fields = new ArrayList(); + } + fields.add(key); + } + } + + KeyGroup addKeyGroup(String keyspace) { + KeyGroup parent = this; + KeyGroup node = null; + String [] space = keyspace.split("\\."); + for (int i = 0; i < space.length; i++) { + StringBuilder namespace = new StringBuilder(); + for (int j = 0; j <= i; j++) { + namespace.append(space[j]); + if (j < i) { + namespace.append('.'); + } + } + if (parent.children != null) { + for (KeyGroup child : parent.children) { + if (child.name.equals(space[i])) { + node = child; + } + } + } + if (node == null) { + node = new KeyGroup(namespace.toString(), parent); + node.name = space[i]; + } + parent = node; + node = null; + } + return parent; + } + + String fullKey(String field) { + if (namespace.equals("")) { + return field; + } + return namespace + "." + field; + } + + String generateClass(String packageName, String className) { + StringBuilder sb = new StringBuilder(); + sb.append("package ").append(packageName).append(";\n"); + sb.append('\n'); + sb.append("/*\n"); + sb.append(" * This class is auto-generated from the properties file.\n"); + sb.append(" * Do not version control!\n"); + sb.append(" */\n"); + sb.append(MessageFormat.format("public final class {0} '{'\n\n", className)); + sb.append(generateClass(this, 0)); + sb.append("}\n"); + return sb.toString(); + } + + String generateClass(KeyGroup group, int level) { + String classIndent = StringUtils.leftPad("", level, '\t'); + String fieldIndent = StringUtils.leftPad("", level + 1, '\t'); + + // begin class + StringBuilder sb = new StringBuilder(); + if (!group.namespace.equals("")) { + sb.append(classIndent).append(MessageFormat.format("public static final class {0} '{'\n\n", group.name)); + sb.append(fieldIndent).append(MessageFormat.format("public static final String _ROOT = \"{0}\";\n\n", group.namespace)); + } + + if (group.fields != null) { + // fields + for (String field : group.fields) { + sb.append(fieldIndent).append(MessageFormat.format("public static final String {0} = \"{1}\";\n\n", field, group.fullKey(field))); + } + } + if (group.children != null) { + // inner classes + for (KeyGroup child : group.children) { + sb.append(generateClass(child, level + 1)); + } + } + // end class + if (!group.namespace.equals("")) { + sb.append(classIndent).append("}\n\n"); + } + return sb.toString(); + } + } /** * Download a file from the official Apache Maven repository. -- 2.39.5