001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.lang.management;
019:
020: import java.lang.management.ManagementPermission;
021: import java.lang.management.RuntimeMXBean;
022: import java.security.AccessController;
023: import java.security.PrivilegedAction;
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: /**
032: * Runtime type for {@link java.lang.management.RuntimeMXBean}
033: *
034: * @since 1.5
035: */
036: public final class RuntimeMXBeanImpl extends DynamicMXBeanImpl
037: implements RuntimeMXBean {
038:
039: private static RuntimeMXBeanImpl instance = new RuntimeMXBeanImpl();
040:
041: /**
042: * Constructor intentionally private to prevent instantiation by others.
043: * Sets the metadata for this bean.
044: */
045: private RuntimeMXBeanImpl() {
046: setMBeanInfo(ManagementUtils.getMBeanInfo(RuntimeMXBean.class
047: .getName()));
048: }
049:
050: /**
051: * Singleton accessor method.
052: *
053: * @return the <code>RuntimeMXBeanImpl</code> singleton.
054: */
055: static RuntimeMXBeanImpl getInstance() {
056: return instance;
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see java.lang.management.RuntimeMXBean#getBootClassPath()
063: */
064: public String getBootClassPath() {
065: if (!isBootClassPathSupported()) {
066: throw new UnsupportedOperationException(
067: "VM does not support boot classpath.");
068: }
069:
070: SecurityManager security = System.getSecurityManager();
071: if (security != null) {
072: security
073: .checkPermission(new ManagementPermission("monitor"));
074: }
075:
076: return AccessController
077: .doPrivileged(new PrivilegedAction<String>() {
078: public String run() {
079: return System
080: .getProperty("sun.boot.class.path");
081: }// end method run
082: });
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see java.lang.management.RuntimeMXBean#getClassPath()
089: */
090: public String getClassPath() {
091: return System.getProperty("java.class.path");
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see java.lang.management.RuntimeMXBean#getLibraryPath()
098: */
099: public String getLibraryPath() {
100: return System.getProperty("java.library.path");
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see java.lang.management.RuntimeMXBean#getManagementSpecVersion()
107: */
108: public String getManagementSpecVersion() {
109: return "1.0";
110: }
111:
112: /**
113: * @return the name of this running virtual machine.
114: * @see #getName()
115: */
116: private native String getNameImpl();
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see java.lang.management.RuntimeMXBean#getName()
122: */
123: public String getName() {
124: return this .getNameImpl();
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see java.lang.management.RuntimeMXBean#getSpecName()
131: */
132: public String getSpecName() {
133: return System.getProperty("java.vm.specification.name");
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see java.lang.management.RuntimeMXBean#getSpecVendor()
140: */
141: public String getSpecVendor() {
142: return System.getProperty("java.vm.specification.vendor");
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see java.lang.management.RuntimeMXBean#getSpecVersion()
149: */
150: public String getSpecVersion() {
151: return System.getProperty("java.vm.specification.version");
152: }
153:
154: /**
155: * @return the virtual machine start time in milliseconds.
156: * @see #getStartTime()
157: */
158: private native long getStartTimeImpl();
159:
160: /*
161: * (non-Javadoc)
162: *
163: * @see java.lang.management.RuntimeMXBean#getStartTime()
164: */
165: public long getStartTime() {
166: return this .getStartTimeImpl();
167: }
168:
169: /**
170: * @return the number of milliseconds the virtual machine has been running.
171: * @see #getUptime()
172: */
173: private native long getUptimeImpl();
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see java.lang.management.RuntimeMXBean#getUptime()
179: */
180: public long getUptime() {
181: return this .getUptimeImpl();
182: }
183:
184: /*
185: * (non-Javadoc)
186: *
187: * @see java.lang.management.RuntimeMXBean#getVmName()
188: */
189: public String getVmName() {
190: return System.getProperty("java.vm.name");
191: }
192:
193: /*
194: * (non-Javadoc)
195: *
196: * @see java.lang.management.RuntimeMXBean#getVmVendor()
197: */
198: public String getVmVendor() {
199: return System.getProperty("java.vm.vendor");
200: }
201:
202: /*
203: * (non-Javadoc)
204: *
205: * @see java.lang.management.RuntimeMXBean#getVmVersion()
206: */
207: public String getVmVersion() {
208: return System.getProperty("java.vm.version");
209: }
210:
211: /**
212: * @return <code>true</code> if supported, <code>false</code> otherwise.
213: * @see #isBootClassPathSupported()
214: */
215: private native boolean isBootClassPathSupportedImpl();
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see java.lang.management.RuntimeMXBean#isBootClassPathSupported()
221: */
222: public boolean isBootClassPathSupported() {
223: return this .isBootClassPathSupportedImpl();
224: }
225:
226: /*
227: * (non-Javadoc)
228: *
229: * @see java.lang.management.RuntimeMXBean#getInputArguments()
230: */
231: public List<String> getInputArguments() {
232: SecurityManager security = System.getSecurityManager();
233: if (security != null) {
234: security
235: .checkPermission(new ManagementPermission("monitor"));
236: }
237:
238: // TODO : Retrieve the input args from the VM
239: return new ArrayList<String>();
240: }
241:
242: /*
243: * (non-Javadoc)
244: *
245: * @see java.lang.management.RuntimeMXBean#getSystemProperties()
246: */
247: public Map<String, String> getSystemProperties() {
248: Map<String, String> result = new HashMap<String, String>();
249: Properties props = System.getProperties();
250: Enumeration<?> propNames = props.propertyNames();
251: while (propNames.hasMoreElements()) {
252: String propName = (String) propNames.nextElement();
253: String propValue = props.getProperty(propName);
254: result.put(propName, propValue);
255: }// end while
256: return result;
257: }
258: }
|