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.CompilationMXBean;
021: import java.security.AccessController;
022: import java.security.PrivilegedAction;
023:
024: /**
025: * Runtime type for {@link CompilationMXBean}
026: *
027: * @since 1.5
028: */
029: public final class CompilationMXBeanImpl extends DynamicMXBeanImpl
030: implements CompilationMXBean {
031:
032: private static CompilationMXBeanImpl instance = createInstance();
033:
034: /**
035: * Conditionally returns the singleton instance of this type of MXBean.
036: *
037: * @return if the virtual machine has a compilation system, returns a new
038: * instance of <code>CompilationMXBean</code>, otherwise returns
039: * <code>null</code>.
040: */
041: private static CompilationMXBeanImpl createInstance() {
042: CompilationMXBeanImpl result = null;
043:
044: if (isJITEnabled()) {
045: result = new CompilationMXBeanImpl();
046: }
047: return result;
048: }
049:
050: /**
051: * Query whether the VM is running with a JIT compiler enabled.
052: *
053: * @return true if a JIT is enabled, false otherwise
054: */
055: private static native boolean isJITEnabled();
056:
057: /**
058: * Constructor intentionally private to prevent instantiation by others.
059: * Sets the metadata for this bean.
060: */
061: private CompilationMXBeanImpl() {
062: setMBeanInfo(ManagementUtils
063: .getMBeanInfo(CompilationMXBean.class.getName()));
064: }
065:
066: /**
067: * Singleton accessor method.
068: *
069: * @return the <code>ClassLoadingMXBeanImpl</code> singleton.
070: */
071: static CompilationMXBeanImpl getInstance() {
072: return instance;
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see java.lang.management.CompilationMXBean#getName()
079: */
080: public String getName() {
081: return AccessController
082: .doPrivileged(new PrivilegedAction<String>() {
083: public String run() {
084: return System.getProperty("java.compiler");
085: }// end method run
086: });
087: }
088:
089: /**
090: * @return the compilation time in milliseconds
091: * @see #getTotalCompilationTime()
092: */
093: private native long getTotalCompilationTimeImpl();
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see java.lang.management.CompilationMXBean#getTotalCompilationTime()
099: */
100: public long getTotalCompilationTime() {
101: if (!isCompilationTimeMonitoringSupported()) {
102: throw new UnsupportedOperationException(
103: "VM does not support monitoring of compilation time.");
104: }
105: return this .getTotalCompilationTimeImpl();
106: }
107:
108: /**
109: * @return <code>true</code> if compilation timing is supported, otherwise
110: * <code>false</code>.
111: * @see #isCompilationTimeMonitoringSupported()
112: */
113: private native boolean isCompilationTimeMonitoringSupportedImpl();
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see java.lang.management.CompilationMXBean#isCompilationTimeMonitoringSupported()
119: */
120: public boolean isCompilationTimeMonitoringSupported() {
121: return this.isCompilationTimeMonitoringSupportedImpl();
122: }
123: }
|