summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/common/QpidProperties.java
blob: f4f764db1b4d500ae953ab56221e7a3bb6473331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.    
 *
 * 
 */
package org.apache.qpid.common;

import org.apache.log4j.Logger;

import java.util.Properties;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;

public class QpidProperties
{
    private static final Logger _logger = Logger.getLogger(QpidProperties.class);
    
    public static final String VERSION_RESOURCE = "qpidversion.properties";

    public static final String PRODUCT_NAME_PROPERTY = "qpid.name";
    public static final String RELEASE_VERSION_PROPERTY = "qpid.version";
    public static final String BUILD_VERSION_PROPERTY = "qpid.svnversion";

    private static final String DEFAULT = "unknown";

    private static String productName = DEFAULT;
    private static String releaseVersion = DEFAULT;
    private static String buildVersion = DEFAULT;

    /** Loads the values from the version properties file. */
    static
    {
        Properties props = new Properties();

        try
        {
            InputStream propertyStream = QpidProperties.class.getClassLoader().getResourceAsStream(VERSION_RESOURCE);
            if (propertyStream == null)
            {
                _logger.warn("Unable to find resource " + VERSION_RESOURCE + " from classloader");
            }
            else
            {
                props.load(propertyStream);

                if (_logger.isDebugEnabled())
                {
                    _logger.debug("Dumping QpidProperties");
                    for (Map.Entry<Object,Object> entry : props.entrySet())
                    {
                        _logger.debug("Property: " + entry.getKey() + " Value: "+ entry.getValue());
                    }
                    _logger.debug("End of property dump");
                }

                productName = readPropertyValue(props, PRODUCT_NAME_PROPERTY);
                releaseVersion = readPropertyValue(props, RELEASE_VERSION_PROPERTY);
                buildVersion = readPropertyValue(props, BUILD_VERSION_PROPERTY);                
            }
        }
        catch (IOException e)
        {
            // Log a warning about this and leave the values initialized to unknown.
            _logger.error("Could not load version.properties resource: " + e, e);
        }
    }

    public static String getProductName()
    {
        return productName;
    }

    public static String getReleaseVersion()
    {
        return releaseVersion;
    }

    public static String getBuildVersion()
    {
        return buildVersion;
    }

    public static String getVersionString()
    {
        return getProductName() + " - " + getReleaseVersion() + " build: " + getBuildVersion();
    }

    private static String readPropertyValue(Properties props, String propertyName)
    {
        String retVal = (String) props.get(propertyName);
        if (retVal == null)
        {
            retVal = DEFAULT;
        }
        return retVal;
    }

    public static void main(String[] args)
    {
        System.out.println(getVersionString());
    }
}