org.deltafi.actionkit.service.HostnameService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deltafi-action-kit Show documentation
Show all versions of deltafi-action-kit Show documentation
DeltaFi Action Kit - Action implementation SDK for DeltaFi actions
/*
* DeltaFi - Data transformation and enrichment platform
*
* Copyright 2021-2023 DeltaFi Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.deltafi.actionkit.service;
import lombok.extern.slf4j.Slf4j;
import org.deltafi.actionkit.properties.ActionsProperties;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
/**
* Service object that provides access to the local hostname
*/
@Slf4j
public class HostnameService {
private static final String DEFAULT_HOSTNAME = "UNKNOWN";
private static final String HOSTNAME_ENV_VAR = "HOSTNAME";
private static final String COMPUTERNAME_ENV_VAR = "COMPUTERNAME";
private final String hostname;
public HostnameService(ActionsProperties actionsProperties) {
if (actionsProperties.getHostname() != null) {
this.hostname = actionsProperties.getHostname();
} else {
this.hostname = detectHostname();
}
}
/**
* @return the local hostname where an action is running
*/
public String getHostname() {
return hostname;
}
private String detectHostname() {
Map env = System.getenv();
if (env.containsKey(HOSTNAME_ENV_VAR)) {
return env.get(HOSTNAME_ENV_VAR);
}
if (env.containsKey(COMPUTERNAME_ENV_VAR)) {
return env.get(COMPUTERNAME_ENV_VAR);
}
return detectHostnameFromInet();
}
private String detectHostnameFromInet() {
try {
InetAddress localhost = InetAddress.getLocalHost();
return localhost.getHostName();
} catch (UnknownHostException e) {
log.error("Failed to determine hostname", e);
return DEFAULT_HOSTNAME;
}
}
}