001: /**
002: *
003: */package clime.messadmin.model;
004:
005: import java.io.File;
006: import java.io.Serializable;
007: import java.lang.reflect.Method;
008: import java.util.ArrayList;
009: import java.util.Collections;
010: import java.util.Date;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014:
015: import clime.messadmin.providers.ProviderUtils;
016: import clime.messadmin.providers.spi.ServerDataProvider;
017: import clime.messadmin.utils.SimpleEntry;
018:
019: /**
020: * @author Cédrik LIME
021: */
022: public class ServerInfo implements Serializable, IServerInfo {
023: private static transient Method maxMemory = null;
024: private static transient Method availableProcessors = null;
025: private static transient Method getenv = null;
026: private static transient Method getUsableSpace = null;
027:
028: protected final long startupTime = System.currentTimeMillis();
029:
030: static {
031: // @since 1.4
032: try {
033: maxMemory = Runtime.getRuntime().getClass().getMethod(
034: "maxMemory", null);//$NON-NLS-1$
035: availableProcessors = Runtime.getRuntime().getClass()
036: .getMethod("availableProcessors", null);//$NON-NLS-1$
037: } catch (SecurityException e) {
038: } catch (NoSuchMethodException e) {
039: }
040: // @since 1.5
041: try {
042: getenv = System.class.getMethod("getenv", null);//$NON-NLS-1$
043: } catch (SecurityException e) {
044: } catch (NoSuchMethodException e) {
045: }
046: // @since 1.6
047: try {
048: getUsableSpace = File.class.getMethod(
049: "getUsableSpace", null);//$NON-NLS-1$
050: } catch (SecurityException e) {
051: } catch (NoSuchMethodException e) {
052: }
053: }
054:
055: /**
056: *
057: */
058: public ServerInfo() {
059: super ();
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: public List getServerSpecificData() {
066: Iterator iter = ProviderUtils.getProviders(
067: ServerDataProvider.class).iterator();
068: List result = new ArrayList();
069: while (iter.hasNext()) {
070: ServerDataProvider sd = (ServerDataProvider) iter.next();
071: try {
072: String title = sd.getServerDataTitle();
073: String xhtml = sd.getXHTMLServerData();
074: if (title != null && xhtml != null) {
075: result.add(new SimpleEntry(title, xhtml));
076: }
077: } catch (RuntimeException rte) {
078: result
079: .add(new SimpleEntry(sd.getClass().getName(),
080: rte));
081: }
082: }
083: return result;
084: }
085:
086: /**
087: * {@inheritDoc}
088: */
089: public Date getStartupTime() {
090: return new Date(startupTime);
091: }
092:
093: /**
094: * {@inheritDoc}
095: */
096: public long getMaxMemory() {
097: //return Runtime.getRuntime().maxMemory();
098: if (maxMemory != null) {
099: try {
100: Object maxMem = maxMemory.invoke(Runtime.getRuntime(),
101: null);
102: return ((Long) maxMem).longValue();
103: } catch (Exception e) {
104: return -1;
105: }
106: } else {
107: return -1;
108: }
109: }
110:
111: /**
112: * {@inheritDoc}
113: */
114: public long getFreeMemory() {
115: return Runtime.getRuntime().freeMemory();
116: }
117:
118: /**
119: * {@inheritDoc}
120: */
121: public long getTotalMemory() {
122: return Runtime.getRuntime().totalMemory();
123: }
124:
125: /**
126: * {@inheritDoc}
127: */
128: public int getCpuCount() {
129: //return Runtime.getRuntime().availableProcessors();
130: if (availableProcessors != null) {
131: try {
132: Object cpuCount = availableProcessors.invoke(Runtime
133: .getRuntime(), null);
134: return ((Integer) cpuCount).intValue();
135: } catch (Exception e) {
136: return -1;
137: }
138: } else {
139: return -1;
140: }
141: }
142:
143: public Map/*<String,String>*/getSystemProperties() {
144: return System.getProperties();
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: public Map/*<String,String>*/getSystemEnv() {
151: //return System.getenv();
152: if (getenv != null) {
153: try {
154: Object systemEnv = getenv.invoke(null, null);
155: return (Map) systemEnv;
156: } catch (Exception e) {
157: return Collections.EMPTY_MAP;
158: }
159: } else {
160: return Collections.EMPTY_MAP;
161: }
162: }
163:
164: /**
165: * {@inheritDoc}
166: */
167: public long getUsableSpaceForFile(String fileName) {
168: File file = new File(fileName);
169: return getUsableSpaceForFile(file);
170: }
171:
172: /**
173: * {@inheritDoc}
174: */
175: public long getUsableSpaceForFile(File file) {
176: if (getUsableSpace == null || file == null || !file.exists()) {
177: return -1;
178: }
179: //return file.getUsableSpace();
180: try {
181: Object usableSpace = getUsableSpace.invoke(file, null);
182: return ((Long) usableSpace).longValue();
183: } catch (Exception e) {
184: return -1;
185: }
186: }
187:
188: /*
189: public String getServerInfo() {
190: }
191: */
192:
193: /**
194: * {@inheritDoc}
195: */
196: public void gc() {
197: System.gc();
198: }
199: }
|