]> source.dussan.org Git - gitblit.git/commitdiff
Updated the key generator to build nested key classes
authorJames Moger <james.moger@gitblit.com>
Tue, 24 Apr 2012 21:56:23 +0000 (17:56 -0400)
committerJames Moger <james.moger@gitblit.com>
Tue, 24 Apr 2012 21:56:23 +0000 (17:56 -0400)
src/com/gitblit/GitBlitServer.java
src/com/gitblit/LdapUserService.java
src/com/gitblit/build/Build.java

index 1307bc3d1419ba4fd543cd609637f501f22d8d45..f0dce774b242158e6c57223f0c26c58648686c88 100644 (file)
@@ -280,11 +280,11 @@ public class GitBlitServer {
                        if (StringUtils.isEmpty(params.ldapLdifFile) == false) {\r
                                File ldifFile = new File(params.ldapLdifFile);\r
                                if (ldifFile != null && ldifFile.exists()) {\r
-                                       URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap_server));\r
+                                       URI ldapUrl = new URI(settings.getRequiredString(Keys.realm.ldap.server));\r
                                        String firstLine = new Scanner(ldifFile).nextLine();\r
                                        String rootDN = firstLine.substring(4);\r
-                                       String bindUserName = settings.getString(Keys.realm.ldap_username, "");\r
-                                       String bindPassword = settings.getString(Keys.realm.ldap_password, "");\r
+                                       String bindUserName = settings.getString(Keys.realm.ldap.username, "");\r
+                                       String bindPassword = settings.getString(Keys.realm.ldap.password, "");\r
                                        \r
                                        // Get the port\r
                                        int port = ldapUrl.getPort();\r
index 86b6136406a8fb750475104d9a08d5ca807cab3c..25f4e03b49e0cc400fb40bd4baa3e425cad60e08 100644 (file)
@@ -56,7 +56,7 @@ public class LdapUserService extends GitblitUserService {
        @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
@@ -65,9 +65,9 @@ public class LdapUserService extends GitblitUserService {
        \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
@@ -114,7 +114,7 @@ public class LdapUserService extends GitblitUserService {
         * @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
@@ -135,8 +135,8 @@ public class LdapUserService extends GitblitUserService {
                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
@@ -176,7 +176,7 @@ public class LdapUserService extends GitblitUserService {
 \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
@@ -191,8 +191,8 @@ public class LdapUserService extends GitblitUserService {
                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
index cbe09a9cf7758493f2be6e3f52bbb6441c9e5800..fcf52b9992fb39bfbfc7d617ee26cfe7a0d311be 100644 (file)
@@ -27,9 +27,7 @@ import java.net.URL;
 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
@@ -182,68 +180,141 @@ public class Build {
                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