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.runtime.parameter.ParameterHelper;
031import org.ametys.runtime.servlet.RuntimeConfig;
032
033
034/**
035 * SAXes general information about the system 
036 */
037public class GeneralStatusGenerator extends AbstractGenerator
038{
039    @Override
040    public void generate() throws IOException, SAXException, ProcessingException
041    {
042        contentHandler.startDocument();
043        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
044        RuntimeMXBean rBean = ManagementFactory.getRuntimeMXBean();
045        
046        XMLUtils.startElement(contentHandler, "characteristics");
047        XMLUtils.createElement(contentHandler, "osTime", ParameterHelper.valueToString(new Date()));
048        
049        XMLUtils.createElement(contentHandler, "osName", osBean.getName());
050        XMLUtils.createElement(contentHandler, "osVersion", osBean.getVersion());
051        
052        String osPatch = System.getProperty("sun.os.patch.level");
053        if (StringUtils.isNotEmpty(osPatch))
054        {
055            XMLUtils.createElement(contentHandler, "osPatch", osPatch);
056        }
057        
058        XMLUtils.createElement(contentHandler, "availableProc", String.valueOf(osBean.getAvailableProcessors()));
059        XMLUtils.createElement(contentHandler, "architecture", osBean.getArch());
060        
061        XMLUtils.createElement(contentHandler, "javaVendor", System.getProperty("java.vendor"));
062        XMLUtils.createElement(contentHandler, "javaVersion", System.getProperty("java.version"));
063        XMLUtils.createElement(contentHandler, "jvmName", rBean.getVmName());
064        XMLUtils.createElement(contentHandler, "startTime", ParameterHelper.valueToString(new Date(rBean.getStartTime())));
065        
066        XMLUtils.createElement(contentHandler, "ametysHome", RuntimeConfig.getInstance().getAmetysHome().getPath());
067        
068        XMLUtils.endElement(contentHandler, "characteristics");
069        contentHandler.endDocument();
070    }
071}