Source:
<code>public KerberosAuthenticator(KerberosConfig config) { System.setProperty("java.security.krb5.conf", config.getKerberosConfig().getAbsolutePath()); try { String canonicalLocalHostName = InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(Locale.US); <START> String hostname = config.getServiceHostName() == null ? canonicalLocalHostName : config.getServiceHostName(); <END> String servicePrincipal = config.getServiceName() + "/" + hostname; loginContext = new LoginContext("", null, null, new Configuration() { @Override public AppConfigurationEntry[] getAppConfigurationEntry(String name) { Map<String, String> options = new HashMap<>(); options.put("refreshKrb5Config", "true"); options.put("doNotPrompt", "true"); if (LOG.isDebugEnabled()) { options.put("debug", "true"); } if (config.getKeytab() != null) { options.put("keyTab", config.getKeytab().getAbsolutePath()); } options.put("isInitiator", "false"); options.put("useKeyTab", "true"); options.put("principal", servicePrincipal); options.put("storeKey", "true"); return new AppConfigurationEntry[] {new AppConfigurationEntry(Krb5LoginModule.class.getName(), REQUIRED, options)}; } }); loginContext.login(); serverCredential = doAs(loginContext.getSubject(), () -> gssManager.createCredential( gssManager.createName(config.getServiceName() + "@" + hostname, GSSName.NT_HOSTBASED_SERVICE), INDEFINITE_LIFETIME, new Oid[] { new Oid("1.2.840.113554.1.2.2"), new Oid("1.3.6.1.5.5.2") }, ACCEPT_ONLY)); } catch (LoginException | UnknownHostException e) { throw new RuntimeException(e); } }</code><technical_language>This simplified (using com.google.common.base.MoreObjects) java String hostname = firstNonNull(config.getServiceHostName(), hostname);</technical_language>

Target:
public KerberosAuthenticator(KerberosConfig config) { System.setProperty("java.security.krb5.conf", config.getKerberosConfig().getAbsolutePath()); try { String hostname = Optional.ofNullable(config.getServiceHostName()) .orElseGet(() -> getLocalHost().getCanonicalHostName()) .toLowerCase(Locale.US); String servicePrincipal = config.getServiceName() + "/" + hostname; loginContext = new LoginContext("", null, null, new Configuration() { @Override public AppConfigurationEntry[] getAppConfigurationEntry(String name) { Map<String, String> options = new HashMap<>(); options.put("refreshKrb5Config", "true"); options.put("doNotPrompt", "true"); if (LOG.isDebugEnabled()) { options.put("debug", "true"); } if (config.getKeytab() != null) { options.put("keyTab", config.getKeytab().getAbsolutePath()); } options.put("isInitiator", "false"); options.put("useKeyTab", "true"); options.put("principal", servicePrincipal); options.put("storeKey", "true"); return new AppConfigurationEntry[] {new AppConfigurationEntry(Krb5LoginModule.class.getName(), REQUIRED, options)}; } }); loginContext.login(); serverCredential = doAs(loginContext.getSubject(), () -> gssManager.createCredential( gssManager.createName(config.getServiceName() + "@" + hostname, GSSName.NT_HOSTBASED_SERVICE), INDEFINITE_LIFETIME, new Oid[] { new Oid("1.2.840.113554.1.2.2"), new Oid("1.3.6.1.5.5.2") }, ACCEPT_ONLY)); } catch (LoginException e) { throw new RuntimeException(e); } }