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