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    import org.directwebremoting.annotations.RemoteProxy;
030    
031    /**
032     * Looks up JSR-77 statistics in response to AJAX calls from portlets.
033     *
034     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
035     */
036    @RemoteProxy(name="Jsr77Stats")
037    public class Jsr77Lookup {
038        public DynamicServerInfo getJavaVMStatistics() {
039            HttpSession session = WebContextFactory.get().getSession(true);
040            ManagementHelper helper = PortletManager.getManagementHelper(session);
041            J2EEDomain[] domains = helper.getDomains();
042            J2EEServer[] servers = domains[0].getServerInstances();
043            JVM[] jvms = helper.getJavaVMs(servers[0]);
044            long elapsed = System.currentTimeMillis() - jvms[0].getKernelBootTime().getTime();
045            if(jvms[0].isStatisticsProvider()) {
046                JVMStats stats = (JVMStats) ((StatisticsProvider)jvms[0]).getStats();
047                BoundedRangeStatistic heap = stats.getHeapSize();
048                return new DynamicServerInfo(heap.getCurrent(), heap.getHighWaterMark(), heap.getUpperBound(), elapsed);
049            } else {
050                return new DynamicServerInfo(elapsed);
051            }
052        }
053    }