001/*
002 *  Copyright 2015 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.plugins.admin.jvmstatus;
017
018import java.io.IOException;
019import java.lang.management.ManagementFactory;
020import java.lang.management.OperatingSystemMXBean;
021import java.lang.management.RuntimeMXBean;
022import java.util.Date;
023
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.generation.AbstractGenerator;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang3.StringUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.core.util.DateUtils;
031import org.ametys.runtime.plugins.admin.statistics.KernelStatisticsProvider;
032import org.ametys.runtime.servlet.RuntimeConfig;
033import org.ametys.runtime.servlet.RuntimeServlet;
034
035import com.jsoftbiz.utils.OS;
036
037
038/**
039 * SAXes general information about the system
040 */
041public class GeneralStatusGenerator extends AbstractGenerator
042{
043    @Override
044    public void generate() throws IOException, SAXException, ProcessingException
045    {
046        contentHandler.startDocument();
047        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
048        RuntimeMXBean rBean = ManagementFactory.getRuntimeMXBean();
049        
050        XMLUtils.startElement(contentHandler, "characteristics");
051        XMLUtils.createElement(contentHandler, "osTime", DateUtils.dateToString(new Date()));
052        
053        XMLUtils.createElement(contentHandler, "osName", OS.OS.getPlatformName());
054        XMLUtils.createElement(contentHandler, "osVersion", OS.OS.getVersion());
055        
056        String osPatch = System.getProperty("sun.os.patch.level");
057        if (StringUtils.isNotEmpty(osPatch))
058        {
059            XMLUtils.createElement(contentHandler, "osPatch", osPatch);
060        }
061        
062        XMLUtils.createElement(contentHandler, "availableProc", String.valueOf(osBean.getAvailableProcessors()));
063        XMLUtils.createElement(contentHandler, "architecture", OS.OS.getArch());
064        
065        XMLUtils.createElement(contentHandler, "javaVendor", System.getProperty("java.vendor"));
066        XMLUtils.createElement(contentHandler, "javaVersion", System.getProperty("java.version"));
067        XMLUtils.createElement(contentHandler, "jvmName", rBean.getVmName());
068        XMLUtils.createElement(contentHandler, "startTime", DateUtils.dateToString(new Date(rBean.getStartTime())));
069        
070        XMLUtils.createElement(contentHandler, "tomcatVersion", KernelStatisticsProvider.getTomcatVersion());
071        
072        String instanceId = "<?>";
073        try
074        {
075            instanceId = RuntimeServlet.getInstanceId();
076        }
077        catch (IllegalStateException e)
078        {
079            // Ignore
080        }
081        XMLUtils.createElement(contentHandler, "ametysInstanceId", instanceId);
082        
083        XMLUtils.createElement(contentHandler, "ametysHome", RuntimeConfig.getInstance().getAmetysHome().getPath());
084        
085        XMLUtils.endElement(contentHandler, "characteristics");
086        contentHandler.endDocument();
087    }
088}