001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *       Copyright 2014 ForgeRock AS.
025 */
026
027package org.forgerock.opendj.server.core;
028
029import static org.forgerock.util.Utils.closeSilently;
030
031import java.io.BufferedInputStream;
032import java.io.IOException;
033import java.io.InputStream;
034import java.util.MissingResourceException;
035import java.util.Properties;
036
037/**
038 * OpenDJ product information, including version number, build information, and
039 * references to documentation.
040 */
041public final class ProductInformation {
042    private static final ProductInformation DEFAULT = new ProductInformation("opendj");
043
044    /**
045     * Returns the singleton product information instance.
046     *
047     * @return The singleton product information instance.
048     */
049    public static ProductInformation getInstance() {
050        return DEFAULT;
051    }
052
053    private final Properties properties;
054    private final String versionFull;
055    private final String versionPrintable;
056
057    private ProductInformation(final String productName) {
058        final String resourceName = "/META-INF/product/" + productName + ".properties";
059        final InputStream stream = getClass().getResourceAsStream(resourceName);
060
061        if (stream == null) {
062            throw new MissingResourceException("Can't find product information " + resourceName,
063                    productName, "");
064        }
065
066        properties = new Properties();
067        final InputStream is = new BufferedInputStream(stream);
068        try {
069            properties.load(is);
070        } catch (final IOException e) {
071            throw new MissingResourceException("Can't load product information " + resourceName
072                    + " due to IO exception: " + e.getMessage(), productName, "");
073        } finally {
074            closeSilently(is);
075        }
076
077        versionFull =
078                productName() + " " + version()
079                        + (patchFixIds().length() > 0 ? "+" + patchFixIds() : "");
080        versionPrintable =
081                versionFull + System.getProperty("line.separator") + "Build " + buildId()
082                        + System.getProperty("line.separator");
083    }
084
085    /**
086     * Returns the build ID for the generated build of the Directory Server.
087     *
088     * @return The build ID for the generated build of the Directory Server.
089     */
090    public String buildId() {
091        return properties.getProperty("build.id");
092    }
093
094    /**
095     * Returns {@code true} if this is a debug build of the Directory Server
096     * that may include additional debugging facilities not available in
097     * standard release versions.
098     *
099     * @return {@code true} if this is a debug build of the Directory Server
100     *         that may include additional debugging facilities not available in
101     *         standard release versions.
102     */
103    public boolean buildIsDebug() {
104        return Boolean.valueOf(properties.getProperty("build.isdebug"));
105    }
106
107    /**
108     * Returns the vendor for the Java version used to generate this build.
109     *
110     * @return The vendor for the Java version used to generate this build.
111     */
112    public String buildJavaVendor() {
113        return properties.getProperty("build.java.vendor");
114    }
115
116    /**
117     * Returns the Java version used to generate this build.
118     *
119     * @return The Java version used to generate this build.
120     */
121    public String buildJavaVersion() {
122        return properties.getProperty("build.java.version");
123    }
124
125    /**
126     * Returns the vendor for the JVM used to generate this build.
127     *
128     * @return The vendor for the JVM used to generate this build.
129     */
130    public String buildJvmVendor() {
131        return properties.getProperty("build.jvm.vendor");
132    }
133
134    /**
135     * Returns the JVM version used to generate this build.
136     *
137     * @return The JVM version used to generate this build.
138     */
139    public String buildJvmVersion() {
140        return properties.getProperty("build.jvm.version");
141    }
142
143    /**
144     * Returns the operating system on which this build was generated.
145     *
146     * @return The operating system on which this build was generated.
147     */
148    public String buildOs() {
149        return properties.getProperty("build.os");
150    }
151
152    /**
153     * Returns the username of the user that created this build.
154     *
155     * @return The username of the user that created this build.
156     */
157    public String buildUser() {
158        return properties.getProperty("build.user");
159    }
160
161    /**
162     * Returns the URL of the product WIKI page.
163     *
164     * @return The URL of the product WIKI page.
165     */
166    public String documentationAdminGuideUrl() {
167        return properties.getProperty("doc.guide.admin.url");
168    }
169
170    /**
171     * Returns the URL of the product home page.
172     *
173     * @return The URL of the product home page.
174     */
175    public String documentationHomePageUrl() {
176        return properties.getProperty("doc.homepage.url");
177    }
178
179    /**
180     * Returns the URL of the product WIKI page.
181     *
182     * @return The URL of the product WIKI page.
183     */
184    public String documentationReferenceGuideUrl() {
185        return properties.getProperty("doc.guide.ref.url");
186    }
187
188    /**
189     * Returns the URL of the product WIKI page.
190     *
191     * @return The URL of the product WIKI page.
192     */
193    public String documentationWikiUrl() {
194        return properties.getProperty("doc.wiki.url");
195    }
196
197    /**
198     * Returns the set of bug IDs for fixes included in this build of the
199     * Directory Server.
200     *
201     * @return The set of bug IDs for fixes included in this build of the
202     *         Directory Server.
203     */
204    public String patchFixIds() {
205        return properties.getProperty("patch.fix.ids");
206    }
207
208    /**
209     * Returns the full product name for the Directory Server, which may contain
210     * white space.
211     *
212     * @return The full product name for the Directory Server.
213     */
214    public String productName() {
215        return properties.getProperty("product.name");
216    }
217
218    /**
219     * Returns the product publication date.
220     *
221     * @return The product publication date.
222     */
223    public String productPublicationDate() {
224        return properties.getProperty("product.publication.date");
225    }
226
227    /**
228     * Returns the product release date.
229     *
230     * @return The product release date.
231     */
232    public String productReleaseDate() {
233        return properties.getProperty("product.release.date");
234    }
235
236    /**
237     * Returns the short product name for the Directory Server, suitable for use
238     * in file names.
239     *
240     * @return The short product name for the Directory Server.
241     */
242    public String productShortName() {
243        return properties.getProperty("product.name.short");
244    }
245
246    /**
247     * Returns the revision number of the source repository on which this build
248     * is based.
249     *
250     * @return The revision number of the source repository on which this build
251     *         is based.
252     */
253    public String scmRevision() {
254        return properties.getProperty("scm.revision");
255    }
256
257    /**
258     * Returns the URL of the source repository location on which this build is
259     * based.
260     *
261     * @return The URL of the source repository location on which this build is
262     *         based.
263     */
264    public String scmUrl() {
265        return properties.getProperty("scm.url");
266    }
267
268    /**
269     * Returns the version number for the Directory Server. The return string
270     * will have the format {@code major.minor.point[-qualifier]}.
271     *
272     * @return The version number for the Directory Server.
273     */
274    public String version() {
275        return properties.getProperty("version");
276    }
277
278    /**
279     * Returns the build number for the Directory Server.
280     *
281     * @return The build number for the Directory Server.
282     */
283    public int versionBuildNumber() {
284        return Integer.valueOf(properties.getProperty("version.build"));
285    }
286
287    /**
288     * Returns the compact version string for this product, suitable for use in
289     * path names and similar cases.
290     *
291     * @return The compact version string for this product, suitable for use in
292     *         path names and similar cases.
293     */
294    public String versionCompact() {
295        return properties.getProperty("version.compact");
296    }
297
298    /**
299     * Returns the full version string for this product.
300     *
301     * @return The full version string for this product.
302     */
303    public String versionFull() {
304        return versionFull;
305    }
306
307    /**
308     * Returns the major version number for the Directory Server.
309     *
310     * @return The major version number for the Directory Server.
311     */
312    public int versionMajorNumber() {
313        return Integer.valueOf(properties.getProperty("version.major"));
314    }
315
316    /**
317     * Returns the minor version number for the Directory Server.
318     *
319     * @return The minor version number for the Directory Server.
320     */
321    public int versionMinorNumber() {
322        return Integer.valueOf(properties.getProperty("version.minor"));
323    }
324
325    /**
326     * Returns the point version number for the Directory Server.
327     *
328     * @return The point version number for the Directory Server.
329     */
330    public int versionPointNumber() {
331        return Integer.valueOf(properties.getProperty("version.point"));
332    }
333
334    /**
335     * Returns the printable version string for this product.
336     *
337     * @return The printable version string for this product.
338     */
339    public String versionPrintable() {
340        return versionPrintable;
341    }
342
343    /**
344     * Returns the version qualifier string for the Directory Server.
345     *
346     * @return The version qualifier string for the Directory Server.
347     */
348    public String versionQualifier() {
349        return properties.getProperty("version.qualifier");
350    }
351
352    /**
353     * Returns the revision number of the source repository on which this build
354     * is based.
355     *
356     * @return The revision number of the source repository on which this build
357     *         is based.
358     */
359    public String versionRevision() {
360        return properties.getProperty("scm.revision");
361    }
362}