001package org.andromda.core.common;
002
003import java.io.InputStream;
004import java.net.URL;
005import java.util.Properties;
006
007/**
008 * This class provides statistics on how the build was performed.
009 *
010 * @author Martin West
011 * @author Chad Brandon
012 */
013public final class BuildInformation
014{
015    /**
016     * The shared instance.
017     */
018    private static final BuildInformation INSTANCE = new BuildInformation();
019
020    /**
021     * Gets the shared instance of the BuildInformation.
022     *
023     * @return the shared BuildInformation instance.
024     */
025    public static BuildInformation instance()
026    {
027        return INSTANCE;
028    }
029
030    /**
031     * Private default constructor of BuildInformation. This class is not intended to be instantiated.
032     */
033    private BuildInformation()
034    {
035        this.initialize();
036    }
037
038    /**
039     * The build timestamp.
040     */
041    private String buildDate;
042
043    /**
044     * The build operating system and version.
045     */
046    private String buildSystem;
047
048    /**
049     * The JDK details used to build the system.
050     */
051    private String buildJdk;
052
053    /**
054     * The name of the user that built the system.
055     */
056    private String buildBuilder;
057
058    /**
059     * The version of the AndroMDA build.
060     */
061    private String buildVersion;
062
063    private void initialize()
064    {
065        final String buildPropertiesUri = "META-INF/andromda-build.properties";
066        final String versionPropertyName = "andromda.build.version";
067        final String datePropertyName = "andromda.build.date";
068        final String systemPropertyName = "andromda.build.system";
069        final String jdkPropertyName = "andromda.build.jdk";
070        final String builderPropertyName = "andromda.build.builder";
071        final URL versionUri = ResourceUtils.getResource(buildPropertiesUri);
072        try
073        {
074            if (versionUri == null)
075            {
076                throw new IllegalStateException(
077                    "BuildInformation: could not load file --> '" + buildPropertiesUri + '\'');
078            }
079            final Properties properties = new Properties();
080            InputStream stream = versionUri.openStream();
081            properties.load(stream);
082            stream.close();
083
084            this.buildDate = properties.getProperty(datePropertyName);
085            this.buildSystem = properties.getProperty(systemPropertyName);
086            this.buildJdk = properties.getProperty(jdkPropertyName);
087            this.buildBuilder = properties.getProperty(builderPropertyName);
088            this.buildVersion = properties.getProperty(versionPropertyName);
089        }
090        catch (final Throwable throwable)
091        {
092            ExceptionRecorder.instance().record(throwable);
093            throw new IllegalStateException(throwable.getMessage());
094        }
095    }
096
097    /**
098     * Return the name of the operating system and version.
099     *
100     * @return Returns the build version.
101     */
102    public String getBuildVersion()
103    {
104        return this.buildVersion;
105    }
106
107    /**
108     * Return the user name of the id which built the system.
109     *
110     * @return Returns the build builder.
111     */
112    public String getBuildBuilder()
113    {
114        return this.buildBuilder;
115    }
116
117    /**
118     * Return the timestamp of the build.
119     *
120     * @return Returns the build date.
121     */
122    public String getBuildDate()
123    {
124        return this.buildDate;
125    }
126
127    /**
128     * Return the vendor and jdk version.
129     *
130     * @return Returns the build jdk.
131     */
132    public String getBuildJdk()
133    {
134        return this.buildJdk;
135    }
136
137    /**
138     * Return the name of the operating system and version.
139     *
140     * @return Returns the build system.
141     */
142    public String getBuildSystem()
143    {
144        return this.buildSystem;
145    }
146}