ShellBasedIdMapping implements IdMappingServiceProvider { synchronized private void updateMapIncr(final String name, final boolean isGrp) throws IOException { if (!checkSupportedPlatform()) { return; } if (isInteger(name) && isGrp) { loadFullGroupMap(); return; } boolean updated = false; updateStaticMapping(); if (OS.startsWith("Linux")) { if (isGrp) { updated = updateMapInternal(gidNameMap, "group", getName2IdCmdLinux(name, true), ":", staticMapping.gidMapping); } else { updated = updateMapInternal(uidNameMap, "user", getName2IdCmdLinux(name, false), ":", staticMapping.uidMapping); } } else { if (isGrp) { updated = updateMapInternal(gidNameMap, "group", getName2IdCmdMac(name, true), "\\s+", staticMapping.gidMapping); } else { updated = updateMapInternal(uidNameMap, "user", getName2IdCmdMac(name, false), "\\s+", staticMapping.uidMapping); } } if (updated) { lastUpdateTime = Time.monotonicNow(); } } @VisibleForTesting  ShellBasedIdMapping(Configuration conf,
      boolean constructFullMapAtInit);  ShellBasedIdMapping(Configuration conf); @VisibleForTesting long getTimeout(); @VisibleForTesting BiMap<Integer, String> getUidNameMap(); @VisibleForTesting BiMap<Integer, String> getGidNameMap(); @VisibleForTesting synchronized void clearNameMaps(); @VisibleForTesting static boolean updateMapInternal(BiMap<Integer, String> map,
      String mapName, String command, String regex,
      Map<Integer, Integer> staticMapping); synchronized void updateMaps(); synchronized int getUid(String user); synchronized int getGid(String group); synchronized String getUserName(int uid, String unknown); synchronized String getGroupName(int gid, String unknown); int getUidAllowingUnknown(String user); int getGidAllowingUnknown(String group);  }
@Test public void testUpdateMapIncr() throws IOException { Configuration conf = new Configuration(); conf.setLong(IdMappingConstant.USERGROUPID_UPDATE_MILLIS_KEY, 600000); ShellBasedIdMapping refIdMapping = new ShellBasedIdMapping(conf, true); ShellBasedIdMapping incrIdMapping = new ShellBasedIdMapping(conf); BiMap<Integer, String> uidNameMap = refIdMapping.getUidNameMap(); BiMap<Integer, String> gidNameMap = refIdMapping.getGidNameMap(); incrIdMapping.clearNameMaps(); uidNameMap = refIdMapping.getUidNameMap(); for (BiMap.Entry<Integer, String> me : uidNameMap.entrySet()) { Integer id = me.getKey(); String name = me.getValue(); String tname = incrIdMapping.getUserName(id, null); assertEquals(name, tname); } assertEquals(uidNameMap.size(), incrIdMapping.getUidNameMap().size()); incrIdMapping.clearNameMaps(); for (BiMap.Entry<Integer, String> me : uidNameMap.entrySet()) { Integer id = me.getKey(); String name = me.getValue(); Integer tid = incrIdMapping.getUid(name); assertEquals(id, tid); } assertEquals(uidNameMap.size(), incrIdMapping.getUidNameMap().size()); incrIdMapping.clearNameMaps(); gidNameMap = refIdMapping.getGidNameMap(); for (BiMap.Entry<Integer, String> me : gidNameMap.entrySet()) { Integer id = me.getKey(); String name = me.getValue(); String tname = incrIdMapping.getGroupName(id, null); assertEquals(name, tname); } assertEquals(gidNameMap.size(), incrIdMapping.getGidNameMap().size()); incrIdMapping.clearNameMaps(); gidNameMap = refIdMapping.getGidNameMap(); for (BiMap.Entry<Integer, String> me : gidNameMap.entrySet()) { Integer id = me.getKey(); String name = me.getValue(); Integer tid = incrIdMapping.getGid(name); assertEquals(id, tid); } assertEquals(gidNameMap.size(), incrIdMapping.getGidNameMap().size()); }