All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stackify.api.common.EnvironmentDetails Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright 2013 Stackify
 *
 * 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 com.stackify.api.common;

import java.net.InetAddress;

import com.stackify.api.EnvironmentDetail;

/**
 * Utility class for retrieving environment details and building an EnvironmentDetail object
 * 
 * @author Eric Martin
 */
public class EnvironmentDetails {

	/**
	 * Creates an environment details object with system information
	 * @param application The configured application name
	 * @param environment The configured application environment
	 * @return The EnvironmentDetail object
	 */
	public static EnvironmentDetail getEnvironmentDetail(final String application, final String environment) {
		
		// lookup the host name

		String hostName = getHostName();
		
		// lookup the current path
		
		String currentPath = System.getProperty("user.dir");
		
		// build the environment details
		
		EnvironmentDetail.Builder environmentBuilder = EnvironmentDetail.newBuilder();
		environmentBuilder.deviceName(hostName);
		environmentBuilder.appLocation(currentPath);
		environmentBuilder.configuredAppName(application);
		environmentBuilder.configuredEnvironmentName(environment);
		
		return environmentBuilder.build();
	}
	
	/**
	 * @return The host name 
	 */
	private static String getHostName() {
		
		// HOSTNAME environment variable
		
		try {
			String hostName = System.getenv("HOSTNAME");
			
			if ((hostName != null) && (0 < hostName.length())) {
				return hostName;
			}
		} catch (Throwable t) {
			// Do nothing
		}
		
		// InetAddress.getLocalHost().getHostName()
		
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String hostName = addr.getHostName();
			
			if ((hostName != null) && (0 < hostName.length())) {
				return hostName;
			}
		} catch (Throwable t) {
			// Do nothing
		}

		return null;
	}
	
	/**
	 * Hidden to prevent construction
	 */
	private EnvironmentDetails() {
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy