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.impl.version;
017
018import java.io.InputStream;
019import java.text.SimpleDateFormat;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Date;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.xml.parsers.SAXParserFactory;
027
028import org.apache.avalon.framework.logger.AbstractLogEnabled;
029import org.apache.avalon.framework.thread.ThreadSafe;
030
031import org.ametys.core.version.Version;
032import org.ametys.core.version.VersionsHandler;
033import org.ametys.runtime.servlet.RuntimeConfig;
034import org.ametys.runtime.util.MapHandler;
035
036
037/**
038 * Default implementation of a VersionHandler returning exactly two versions : that of the Runtime kernel and that of the running application.<br>
039 * Applications may subclass this default implementation 
040 */
041public class DefaultVersionsHandler extends AbstractLogEnabled implements VersionsHandler, ThreadSafe
042{
043    private Version _ametysVersion;
044    private Version _applicationVersion;
045    
046    @Override
047    public final Collection<Version> getVersions()
048    {
049        ArrayList<Version> versions = new ArrayList<>();
050        
051        if (_applicationVersion == null)
052        {
053            _applicationVersion = _getApplicationVersion();
054        }
055        
056        if (_ametysVersion == null)
057        {
058            _ametysVersion = _getVersionFromClasspath("/org/ametys/runtime/kernel/version.xml", "Ametys");
059        }
060        
061        versions.add(_applicationVersion);
062        
063        Collection<Version> additionalVersions = getAdditionalVersions();
064        
065        if (additionalVersions != null)
066        {
067            versions.addAll(additionalVersions);
068        }
069        
070        versions.add(_ametysVersion);
071
072        return versions;
073    }
074    
075    /**
076     * Returns any additional versions informations displayable in the administrator area.<br>
077     * <i>Note: This implementation returns null.</i>
078     * @return any additional versions informations displayable in the administrator area.<br>
079     */
080    protected Collection<Version> getAdditionalVersions()
081    {
082        return null;
083    }
084    
085    private Version _getApplicationVersion()
086    {
087        RuntimeConfig config = RuntimeConfig.getInstance();
088        return new Version("Application", config.getApplicationVersion(), config.getApplicationBuildDate());
089    }
090    
091    /**
092     * Helper for getting Version from an XML file in the classpath.<br>
093     * The file must be formed like this :<br>
094     * &lt;version&gt;<br>
095     * &nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;<i>Version name</i>&lt;/version&gt;<br>
096     * &nbsp;&nbsp;&nbsp;&nbsp;&lt;date&gt;<i>Version date in format "yyyyMMdd'T'HHmmz"</i>&lt;/date&gt;<br>
097     * &lt;/version&gt;<br>
098     * @param path a path in the classpath poiting to an XML file
099     * @param versionName the name of the Version to create
100     * @return the created Version
101     */
102    protected final Version _getVersionFromClasspath(String path, String versionName)
103    {
104        Map<String, String> config = new HashMap<>();
105        Date date = null;
106        
107        try (InputStream is = getClass().getResourceAsStream(path))
108        {
109            if (is == null)
110            {
111                getLogger().warn(versionName + " version is unavailable");
112                return new Version(versionName, null, null);
113            }
114            
115            SAXParserFactory.newInstance().newSAXParser().parse(is, new MapHandler(config));
116            
117            String strDate = config.get("date");
118            date = new SimpleDateFormat("yyyyMMdd'T'HHmmz").parse(strDate);
119        }
120        catch (Exception ex)
121        {
122            getLogger().warn("Unable to get version number for " + versionName, ex);
123        }
124        
125        return new Version(versionName, config.get("version"), date);
126    }
127}