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