001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt.lib;
009    
010    import java.io.InputStream;
011    import java.util.Properties;
012    
013    import org.slf4j.Logger;
014    import org.slf4j.LoggerFactory;
015    
016    /**
017     * Publish library version information
018     */
019    public class VersionUDT {
020    
021            protected static final Logger log = LoggerFactory
022                            .getLogger(VersionUDT.class);
023    
024            //
025    
026            protected static final String PROP_JAVA_VENDOR = "java.vendor";
027            protected static final String PROP_JAVA_VERSION = "java.version";
028            protected static final String PROP_JAVA_VM_NAME = "java.vm.name";
029    
030            public static final String JAVA_VENDOR = System
031                            .getProperty(PROP_JAVA_VENDOR);
032            public static final String JAVA_VERSION = System
033                            .getProperty(PROP_JAVA_VERSION);
034            public static final String JAVA_VM_NAME = System
035                            .getProperty(PROP_JAVA_VM_NAME);
036    
037            //
038    
039            protected static final String PROP_OS_NAME = "os.name";
040            protected static final String PROP_OS_ARCH = "os.arch";
041            protected static final String PROP_OS_VERSION = "os.version";
042    
043            public static final String OS_NAME = System.getProperty(PROP_OS_NAME);
044            public static final String OS_ARCH = System.getProperty(PROP_OS_ARCH);
045            public static final String OS_VERSION = System.getProperty(PROP_OS_VERSION);
046    
047            //
048    
049            protected static final String PROP_FILE = "version.properties";
050    
051            protected static final String PROP_UDT_VERSION = "udt.version";
052    
053            protected static final String PROP_BARCHART_NAME = "barchart.name";
054            protected static final String PROP_BARCHART_GROUP = "barchart.groupId";
055            protected static final String PROP_BARCHART_ARTIFACT = "barchart.artifactId";
056            protected static final String PROP_BARCHART_VERSION = "barchart.version";
057            protected static final String PROP_BARCHART_TIMESTAMP = "barchart.timestamp";
058    
059            public static final String UDT_VERSION;
060    
061            public static final String BARCHART_NAME;
062    
063            public static final String BARCHART_GROUP;
064            public static final String BARCHART_ARTIFACT;
065            public static final String BARCHART_VERSION;
066    
067            public static final String BARCHART_TIMESTAMP;
068    
069            static final String UNKNOWN = "UNKNOWN";
070    
071            static {
072    
073                    String udtVersion = UNKNOWN;
074                    String name = UNKNOWN;
075                    String group = UNKNOWN;
076                    String artifact = UNKNOWN;
077                    String version = UNKNOWN;
078                    String timestamp = UNKNOWN;
079    
080                    try {
081    
082                            final Properties props = new Properties();
083    
084                            final InputStream stream = VersionUDT.class.getClassLoader()
085                                            .getResourceAsStream(PROP_FILE);
086    
087                            props.load(stream);
088    
089                            udtVersion = props.getProperty(PROP_UDT_VERSION);
090    
091                            name = props.getProperty(PROP_BARCHART_NAME);
092                            group = props.getProperty(PROP_BARCHART_GROUP);
093                            artifact = props.getProperty(PROP_BARCHART_ARTIFACT);
094                            version = props.getProperty(PROP_BARCHART_VERSION);
095                            timestamp = props.getProperty(PROP_BARCHART_TIMESTAMP);
096    
097                    } catch (final Exception e) {
098                            log.error("failed to load version properties", e);
099                    }
100    
101                    UDT_VERSION = udtVersion;
102    
103                    BARCHART_NAME = barchartName(name);
104    
105                    BARCHART_GROUP = group;
106                    BARCHART_ARTIFACT = artifact;
107                    BARCHART_VERSION = version;
108                    BARCHART_TIMESTAMP = timestamp;
109    
110            }
111    
112            private static final String SNAPSHOT = "-SNAPSHOT";
113    
114            /**
115             * FIXME needs build system change
116             * <p>
117             * current contract is to depend on NAR snapshot
118             */
119            protected static String barchartName(final String name) {
120                    if (name.contains(SNAPSHOT)) {
121                            return name;
122                    } else {
123                            return name + SNAPSHOT;
124                    }
125            }
126    
127            protected static void append(final StringBuilder text, final String EOL) {
128    
129                    text.append(PROP_BARCHART_NAME);
130                    text.append(" = ");
131                    text.append(BARCHART_NAME);
132                    text.append(EOL);
133    
134                    text.append(PROP_BARCHART_GROUP);
135                    text.append(" = ");
136                    text.append(BARCHART_GROUP);
137                    text.append(EOL);
138    
139                    text.append(PROP_BARCHART_ARTIFACT);
140                    text.append(" = ");
141                    text.append(BARCHART_ARTIFACT);
142                    text.append(EOL);
143    
144                    text.append(PROP_BARCHART_VERSION);
145                    text.append(" = ");
146                    text.append(BARCHART_VERSION);
147                    text.append(EOL);
148    
149                    text.append(PROP_BARCHART_TIMESTAMP);
150                    text.append(" = ");
151                    text.append(BARCHART_TIMESTAMP);
152                    text.append(EOL);
153    
154                    text.append(PROP_JAVA_VENDOR);
155                    text.append(" = ");
156                    text.append(JAVA_VENDOR);
157                    text.append(EOL);
158    
159                    text.append(PROP_JAVA_VERSION);
160                    text.append(" = ");
161                    text.append(JAVA_VERSION);
162                    text.append(EOL);
163    
164                    text.append(PROP_JAVA_VM_NAME);
165                    text.append(" = ");
166                    text.append(JAVA_VM_NAME);
167                    text.append(EOL);
168    
169                    text.append(PROP_OS_NAME);
170                    text.append(" = ");
171                    text.append(OS_NAME);
172                    text.append(EOL);
173    
174                    text.append(PROP_OS_ARCH);
175                    text.append(" = ");
176                    text.append(OS_ARCH);
177                    text.append(EOL);
178    
179                    text.append(PROP_OS_VERSION);
180                    text.append(" = ");
181                    text.append(OS_VERSION);
182                    text.append(EOL);
183    
184            }
185    
186            public static final void log() {
187                    log.info("\n{}", asText());
188            }
189    
190            public static final String asText() {
191    
192                    final StringBuilder text = new StringBuilder(128);
193    
194                    text.append("\n");
195                    text.append("#######################################");
196                    text.append("\n");
197    
198                    append(text, "\n");
199    
200                    text.append("#######################################");
201                    text.append("\n");
202                    text.append("\n");
203    
204                    return text.toString();
205    
206            }
207    
208            public static final String asHtml() {
209    
210                    final StringBuilder text = new StringBuilder(128);
211    
212                    text.append("<html><pre>");
213    
214                    append(text, "<br>");
215    
216                    text.append("</pre></html>");
217    
218                    return text.toString();
219    
220            }
221    
222    }