001/*
002 *  Copyright 2012 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.plugins.core.ui.system;
017
018import java.io.IOException;
019import java.lang.management.ManagementFactory;
020import java.util.Collection;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.commons.lang3.StringUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.util.SystemStatus;
032import org.ametys.core.version.Version;
033import org.ametys.core.version.VersionsHandler;
034
035/**
036 * Generate the startuptime of the server 
037 */
038public class StartupGenerator extends ServiceableGenerator
039{
040    private static String __version;
041    
042    /** System status provider */
043    private SystemStatus _systemStatus;
044    
045    @Override
046    public void service(ServiceManager sm) throws ServiceException
047    {
048        super.service(sm);
049        _systemStatus = (SystemStatus) sm.lookup(SystemStatus.ROLE);
050    }
051    
052    @Override
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        if (__version == null)
056        {
057            VersionsHandler handler;
058            
059            try
060            {
061                handler = (VersionsHandler) manager.lookup(VersionsHandler.ROLE);
062            }
063            catch (ServiceException e)
064            {
065                String errorMessage = "Unable to get the VersionsHandler";
066                getLogger().error(errorMessage, e);
067                throw new ProcessingException(errorMessage, e);
068            }
069    
070            StringBuffer value = new StringBuffer();
071            
072            Collection<Version> versions = handler.getVersions();
073            for (Version version : versions)
074            {
075                if (value.length() > 0)
076                {
077                    value.append(" / ");
078                }
079                
080                value.append(version.getName());
081                
082                if (StringUtils.isNotBlank(version.getVersion()))
083                {
084                    value.append(" - ");
085                    value.append(version.getVersion());
086                }
087                
088                if (version.getDate() != null)
089                {
090                    value.append(" - ");
091                    value.append(Long.toString(version.getDate().getTime()));
092                }
093            }
094            
095            __version = Integer.toString(value.toString().hashCode());
096        }
097        
098        AttributesImpl attrs = new AttributesImpl();
099        attrs.addCDATAAttribute("version", __version);
100        attrs.addCDATAAttribute("status", StringUtils.join(_systemStatus.getStatus(), ','));
101        
102        contentHandler.startDocument();
103        XMLUtils.createElement(contentHandler, "startup-time", attrs, Long.toString(ManagementFactory.getRuntimeMXBean().getStartTime()));
104        contentHandler.endDocument();
105    }
106}