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

clime.messadmin.model.ServerInfo Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
package clime.messadmin.model;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import clime.messadmin.providers.ProviderUtils;
import clime.messadmin.providers.spi.ServerDataProvider;
import clime.messadmin.utils.SimpleEntry;

/**
 * @author Cédrik LIME
 */
public class ServerInfo implements Serializable, IServerInfo {
	private static transient Method maxMemory = null;
	private static transient Method availableProcessors = null;
	private static transient Method getenv = null;

	protected final long startupTime = System.currentTimeMillis();

	static {
		// @since 1.4
		try {
			maxMemory = Runtime.getRuntime().getClass().getMethod("maxMemory", null);
			availableProcessors = Runtime.getRuntime().getClass().getMethod("availableProcessors", null);
		} catch (SecurityException e) {
		} catch (NoSuchMethodException e) {
		}
		// @since 1.5
		try {
			getenv = System.class.getMethod("getenv", null);
		} catch (SecurityException e) {
		} catch (NoSuchMethodException e) {
		}
	}

	/**
	 * 
	 */
	public ServerInfo() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	public List getServerSpecificData() {
		Iterator iter = ProviderUtils.getProviders(ServerDataProvider.class).iterator();
		List result = new ArrayList();
		while (iter.hasNext()) {
			ServerDataProvider sd = (ServerDataProvider) iter.next();
			try {
				String title = sd.getServerDataTitle();
				String xhtml = sd.getXHTMLServerData();
				if (title != null && xhtml != null) {
					result.add(new SimpleEntry(title, xhtml));
				}
			} catch (RuntimeException rte) {
				result.add(new SimpleEntry(sd.getClass().getName(), rte));
			}
		}
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	public Date getStartupTime() {
		return new Date(startupTime);
	}

	/**
	 * {@inheritDoc}
	 */
	public long getMaxMemory() {
		//return Runtime.getRuntime().maxMemory();
		if (maxMemory != null) {
			try {
				Object maxMem = maxMemory.invoke(Runtime.getRuntime(), null);
				return ((Long) maxMem).longValue();
			} catch (Exception e) {
				return -1;
			}
		} else {
			return -1;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public long getFreeMemory() {
		return Runtime.getRuntime().freeMemory();
	}

	/**
	 * {@inheritDoc}
	 */
	public long getTotalMemory() {
		return Runtime.getRuntime().totalMemory();
	}

	/**
	 * {@inheritDoc}
	 */
	public int getCpuCount() {
		//return Runtime.getRuntime().availableProcessors();
		if (maxMemory != null) {
			try {
				Object cpuCount = availableProcessors.invoke(Runtime.getRuntime(), null);
				return ((Integer) cpuCount).intValue();
			} catch (Exception e) {
				return -1;
			}
		} else {
			return -1;
		}
	}

	public Map/**/ getSystemProperties() {
		return System.getProperties();
	}

	/**
	 * {@inheritDoc}
	 */
	public Map/**/ getSystemEnv() {
		//return System.getenv();
		if (getenv != null) {
			try {
				Object systemEnv = getenv.invoke(null, null);
				return (Map) systemEnv;
			} catch (Exception e) {
				return Collections.EMPTY_MAP;
			}
		} else {
			return Collections.EMPTY_MAP;
		}
	}

	/*
	public String getServerInfo() {
	}
	*/

	/**
	 * {@inheritDoc}
	 */
	public void gc() {
		System.gc();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy