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    package org.apache.geronimo.console.jsr77;
018    
019    import javax.servlet.http.HttpSession;
020    import javax.management.j2ee.statistics.JVMStats;
021    import javax.management.j2ee.statistics.BoundedRangeStatistic;
022    import uk.ltd.getahead.dwr.WebContextFactory;
023    import org.apache.geronimo.console.util.PortletManager;
024    import org.apache.geronimo.console.util.ManagementHelper;
025    import org.apache.geronimo.management.geronimo.J2EEDomain;
026    import org.apache.geronimo.management.StatisticsProvider;
027    import org.apache.geronimo.management.geronimo.J2EEServer;
028    import org.apache.geronimo.management.geronimo.JVM;
029    
030    /**
031     * Looks up JSR-77 statistics in response to AJAX calls from portlets.
032     *
033     * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
034     */
035    public class Jsr77Lookup {
036        public DynamicServerInfo getJavaVMStatistics() {
037            HttpSession session = WebContextFactory.get().getSession(false);
038            ManagementHelper helper = PortletManager.getManagementHelper(session);
039            J2EEDomain[] domains = helper.getDomains();
040            J2EEServer[] servers = domains[0].getServerInstances();
041            JVM[] jvms = helper.getJavaVMs(servers[0]);
042            long elapsed = System.currentTimeMillis() - jvms[0].getKernelBootTime().getTime();
043            if(jvms[0].isStatisticsProvider()) {
044                JVMStats stats = (JVMStats) ((StatisticsProvider)jvms[0]).getStats();
045                BoundedRangeStatistic heap = stats.getHeapSize();
046                return new DynamicServerInfo(heap.getCurrent(), heap.getHighWaterMark(), heap.getUpperBound(), elapsed);
047            } else {
048                return new DynamicServerInfo(elapsed);
049            }
050        }
051    }