KerberosUtils { static int parseLifetime(String lifetime) { Matcher dhmsMatcher = LIFETIME_DHMS.matcher(lifetime); Matcher hmsMatcher = LIFETIME_HMS.matcher(lifetime); Matcher secondsMatcher = LIFETIME_SECS.matcher(lifetime); if (dhmsMatcher.matches()) { int days = 0, hours = 0, minutes = 0, seconds = 0; for (int i = 1; i <= dhmsMatcher.groupCount(); i++) { String match = dhmsMatcher.group(i); if (match != null) { int value = Integer.parseInt(match.substring(0, match.length() - 1)); switch (match.charAt(match.length() - 1)) { case 'd': days = value; break; case 'h': hours = value; break; case 'm': minutes = value; break; case 's': seconds = value; break; } } } return (86400 * days) + (3600 * hours) + (60 * minutes) + seconds; } else if (hmsMatcher.matches()) { int hours = Integer.parseInt(hmsMatcher.group(1)); int minutes = Integer.parseInt(hmsMatcher.group(2)); int seconds = hmsMatcher.group(3) == null ? 0 : Integer.parseInt(hmsMatcher.group(4)); return (3600 * hours) + (60 * minutes) + seconds; } else if (secondsMatcher.matches()) { return Integer.parseInt(secondsMatcher.group(1)); } else { LOG.warn( "Could not determine ticket lifetime from [{}]. Falling back to default [{} seconds]", lifetime, DEFAULT_LIFETIME_SECS); return DEFAULT_LIFETIME_SECS; } }  static KerberosLoginContext createKerberosLoginContext(String contextName, Config config); static boolean shouldLoginFromKeytab(Config config); static String getKerberosRealm(Config config); static void loginIfRequired(KerberosLoginContext loginContext, Config config);  }
@Test public void testParseLifetime() { String dhms1 = " 2d 4h 30m 10s "; String dhms2 = "3d"; String hms1 = "10:30"; String hms2 = "12:45:30"; String sec1 = "2750"; String sec2 = " 3450 "; assertEquals(189010, KerberosUtils.parseLifetime(dhms1)); assertEquals(86400 * 3, KerberosUtils.parseLifetime(dhms2)); assertEquals(10 * 3600 + 30 * 60, KerberosUtils.parseLifetime(hms1)); assertEquals(12 * 3600 + 45 * 60 + 30, KerberosUtils.parseLifetime(hms2)); assertEquals(2750, KerberosUtils.parseLifetime(sec1)); assertEquals(3450, KerberosUtils.parseLifetime(sec2)); }