private static void addACLPrivileges(String acl, Map<String, Map<String, List<String[]>>> privileges) {
    int equalIndex = acl.lastIndexOf("=");
    int slashIndex = acl.lastIndexOf("/");
    if (equalIndex == -1) {
      return;
    }

    String user = acl.substring(0, equalIndex);
    String grantor = null;
    if (user.isEmpty()) {
      user = "PUBLIC";
    }
    String privs;
    if (slashIndex != -1) {
      privs = acl.substring(equalIndex + 1, slashIndex);
      grantor = acl.substring(slashIndex + 1, acl.length());
    } else {
      privs = acl.substring(equalIndex + 1, acl.length());
    }

    for (int i = 0; i < privs.length(); i++) {
      char c = privs.charAt(i);
      if (c != '*') {
        String sqlpriv;
        String grantable;
        if (i < privs.length() - 1 && privs.charAt(i + 1) == '*') {
          grantable = "YES";
        } else {
          grantable = "NO";
        }
        switch (c) {
          case 'a':
            sqlpriv = "INSERT";
            break;
          case 'r':
          case 'p':
            sqlpriv = "SELECT";
            break;
          case 'w':
            sqlpriv = "UPDATE";
            break;
          case 'd':
            sqlpriv = "DELETE";
            break;
          case 'D':
            sqlpriv = "TRUNCATE";
            break;
          case 'R':
            sqlpriv = "RULE";
            break;
          case 'x':
            sqlpriv = "REFERENCES";
            break;
          case 't':
            sqlpriv = "TRIGGER";
            break;
          // the following can't be granted to a table, but
          // we'll keep them for completeness.
          case 'X':
            sqlpriv = "EXECUTE";
            break;
          case 'U':
            sqlpriv = "USAGE";
            break;
          case 'C':
            sqlpriv = "CREATE";
            break;
          case 'T':
            sqlpriv = "CREATE TEMP";
            break;
          default:
            sqlpriv = "UNKNOWN";
        }

        Map<String, List<String[]>> usersWithPermission = privileges.get(sqlpriv);
        String[] grant = {grantor, grantable};

        if (usersWithPermission == null) {
          usersWithPermission = new HashMap<String, List<String[]>>();
          List<String[]> permissionByGrantor = new ArrayList<String[]>();
          permissionByGrantor.add(grant);
          usersWithPermission.put(user, permissionByGrantor);
          privileges.put(sqlpriv, usersWithPermission);
        } else {
          List<String[]> permissionByGrantor = usersWithPermission.get(user);
          if (permissionByGrantor == null) {
            permissionByGrantor = new ArrayList<String[]>();
            permissionByGrantor.add(grant);
            usersWithPermission.put(user, permissionByGrantor);
          } else {
            permissionByGrantor.add(grant);
          }
        }
      }
    }
  }
private static void addACLPrivileges ( String acl , Map < String , Map < String , List < String [ ] > > > privileges ) { int equalIndex = acl . lastIndexOf ( "=" ) ; int slashIndex = acl . lastIndexOf ( "/" ) ; if ( equalIndex == - 1 ) { return ; } String user = acl . substring ( 0 , equalIndex ) ; String grantor = null ; if ( user . isEmpty ( ) ) { user = "PUBLIC" ; } String privs ; if ( slashIndex != - 1 ) { privs = acl . substring ( equalIndex + 1 , slashIndex ) ; grantor = acl . substring ( slashIndex + 1 , acl . length ( ) ) ; } else { privs = acl . substring ( equalIndex + 1 , acl . length ( ) ) ; } for ( int i = 0 ; i < privs . length ( ) ; i ++ ) { char c = privs . charAt ( i ) ; if ( c != '*' ) { String sqlpriv ; String grantable ; if ( i < privs . length ( ) - 1 && privs . charAt ( i + 1 ) == '*' ) { grantable = "YES" ; } else { grantable = "NO" ; } switch ( c ) { case 'a' : sqlpriv = "INSERT" ; break ; case 'r' : case 'p' : sqlpriv = "SELECT" ; break ; case 'w' : sqlpriv = "UPDATE" ; break ; case 'd' : sqlpriv = "DELETE" ; break ; case 'D' : sqlpriv = "TRUNCATE" ; break ; case 'R' : sqlpriv = "RULE" ; break ; case 'x' : sqlpriv = "REFERENCES" ; break ; case 't' : sqlpriv = "TRIGGER" ; break ; case 'X' : sqlpriv = "EXECUTE" ; break ; case 'U' : sqlpriv = "USAGE" ; break ; case 'C' : sqlpriv = "CREATE" ; break ; case 'T' : sqlpriv = "CREATE TEMP" ; break ; default : sqlpriv = "UNKNOWN" ; } Map < String , List < String [ ] > > usersWithPermission = privileges . get ( sqlpriv ) ; String [ ] grant = { grantor , grantable } ; if ( usersWithPermission == null ) { usersWithPermission = new HashMap < String , List < String [ ] > > ( ) ; List < String [ ] > permissionByGrantor = new ArrayList < String [ ] > ( ) ; permissionByGrantor . add ( grant ) ; usersWithPermission . put ( user , permissionByGrantor ) ; privileges . put ( sqlpriv , usersWithPermission ) ; } else { List < String [ ] > permissionByGrantor = usersWithPermission . get ( user ) ; if ( permissionByGrantor == null ) { permissionByGrantor = new ArrayList < String [ ] > ( ) ; permissionByGrantor . add ( grant ) ; usersWithPermission . put ( user , permissionByGrantor ) ; } else { permissionByGrantor . add ( grant ) ; } } } } }
Add the user described by the given acl to the Lists of users with the privileges described by
the acl.