private void saveUsers(PrintStream ps) {
    Set s = _userCache.getUsers(0);
    Iterator it = s.iterator();
    while (it.hasNext()) {
      String uid = (String) it.next();
      Map m = null;
      try {
        m = _userCache.getUser(uid);
      } catch (UserServiceException e) {
        _log.warn("Unable to get user data", e);
      }
      if (m != null) {
        ps.println("  <user>");
        printField(ps, m, UserEntries.FIELD_UID);
        printField(ps, m, UserEntries.FIELD_PASSWORD);
        printField(ps, m, UserEntries.FIELD_ENABLE_TIME);
        printField(ps, m, UserEntries.FIELD_CERT_OK);
        printField(ps, m, UserEntries.FIELD_AUTH);
        printField(ps, m, UserEntries.FIELD_FNAME);
        printField(ps, m, UserEntries.FIELD_LNAME);
        printField(ps, m, UserEntries.FIELD_NAME);
        printField(ps, m, UserEntries.FIELD_MAIL);
        printField(ps, m, UserEntries.FIELD_ROLE_LIST);

        ps.println("  </user>");
      }
    }
  }
 private void saveRoles(PrintStream ps) {
   Set s = null;
   try {
     s = _userCache.getRoles(0);
   } catch (UserServiceException e) {
     _log.warn("Unable to get roles", e);
   }
   if (s != null) {
     Iterator it = s.iterator();
     while (it.hasNext()) {
       Map m = null;
       try {
         m = _userCache.getRole((String) it.next());
       } catch (UserServiceException e) {
         _log.warn("Unable to get role ID", e);
       }
       if (m != null) {
         ps.println("  <role>");
         printField(ps, m, UserEntries.FIELD_RID);
         printField(ps, m, UserEntries.FIELD_DESCRIPTION);
         ps.println("  </role>");
       }
     }
   }
 }
  private void buildDigestedUserFile(String args[]) {
    // Parse command-line arguments.
    String domain = null;
    String fileName = null;
    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-d")) {
        if (++i == args.length) {
          usage();
        }
        domain = args[i];
      } else if (args[i].equals("-f")) {
        if (++i == args.length) {
          usage();
        }
        fileName = args[i];
      }
    }
    if (fileName == null) {
      fileName = "UserFile-" + domain + ".xml";
    }

    // The UID is not important. Make a fake one.
    UID uid = UID.toUID("AgentA/1094690973044");
    UserEntries userCache = new UserEntries(uid);
    userCache.setDomain(domain);
    init(userCache);

    UserFileParser ufp = new UserFileParser(userCache);
    System.out.println("Reading user XML file...");
    ufp.readUsers("UserFile.xml");
    OutputStream os = null;
    try {
      os = new FileOutputStream(fileName);
    } catch (FileNotFoundException e) {
      usage();
    }
    System.out.println("Generating user XML file: " + fileName + " for domain: " + domain);
    ufp.saveUsersAndRoles(os);
    try {
      os.close();
    } catch (IOException e) {
      System.out.println("Unable to close file");
    }
  }
 private void init(UserEntries userCache) {
   _userCache = userCache;
   _domain = _userCache.getDomain();
   if (_domain == null) {
     String s = "User domain should not be null";
     if (_log.isWarnEnabled()) {
       _log.warn(s);
     }
     throw new IllegalArgumentException(s);
   }
 }
 /**
  * Loads a database of users from an XML file.
  *
  * @param in - the InputStream containing the XML user file.
  */
 public void readUsers(InputStream in) {
   try {
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(in);
     Element rootElement = doc.getDocumentElement();
     NodeList nodes = rootElement.getChildNodes();
     HashMap userAssigns = new HashMap();
     HashMap roleAssigns = new HashMap();
     boolean isClearTextPassword = true;
     for (int i = 0; i < nodes.getLength(); i++) {
       Node item = nodes.item(i);
       if (item instanceof Element) {
         Element l = (Element) item;
         Map map = readMap(l);
         Set groups = (Set) map.remove(ROLE_ASSIGNMENT);
         if ("user".equals(l.getNodeName())) {
           String pwd = (String) map.get(UserEntries.FIELD_PASSWORD);
           String user = (String) map.get(UserEntries.FIELD_UID);
           if (user == null) {
             _log.warn("No id for user entry");
             continue;
           }
           if (_log.isDebugEnabled()) {
             _log.debug("Adding user " + user + " with password:"******"\\" + user, pwd);
           }
           map.put(UserEntries.FIELD_PASSWORD, pwd);
           _userCache.addUser(user, map);
           if (groups != null) {
             userAssigns.put(user, groups);
           }
         } else if ("role".equals(l.getNodeName())) {
           String role = (String) map.get(UserEntries.FIELD_RID);
           if (role == null) {
             if (_log.isWarnEnabled()) {
               _log.warn("No id for role entry");
             }
             continue;
           }
           _userCache.addRole(role, map);
           if (groups != null) {
             roleAssigns.put(role, groups);
           }
         } else if ("password".equals(l.getNodeName())) {
           String isClearText = (String) map.get(UserEntries.FIELD_IS_CLEAR_TEXT_PASSWORD);
           if (isClearText != null) {
             isClearTextPassword = Boolean.getBoolean(isClearText);
           }
         }
       }
     }
     Iterator assigns = userAssigns.entrySet().iterator();
     while (assigns.hasNext()) {
       Map.Entry entry = (Map.Entry) assigns.next();
       String user = (String) entry.getKey();
       Iterator iter = ((Set) entry.getValue()).iterator();
       while (iter.hasNext()) {
         String group = (String) iter.next();
         _userCache.assign(user, group);
       }
     }
     assigns = roleAssigns.entrySet().iterator();
     while (assigns.hasNext()) {
       Map.Entry entry = (Map.Entry) assigns.next();
       String role = (String) entry.getKey();
       Iterator iter = ((Set) entry.getValue()).iterator();
       while (iter.hasNext()) {
         String group = (String) iter.next();
         _userCache.addRoleToRole(role, group);
       }
     }
   } catch (ParserConfigurationException e) {
     _log.warn("Cannot parse user file: ", e);
   } catch (UserServiceException e) {
     _log.warn("Problem adding user or role from file: ", e);
   } catch (SAXException e) {
     _log.warn("Could not parse user file: ", e);
   } catch (IOException e) {
     _log.warn("Could not parse user file: ", e);
   }
 }