File: VersionHelper.java

package info (click to toggle)
libbase 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: java: 8,709; xml: 1,522; makefile: 18
file content (327 lines) | stat: -rw-r--r-- 8,632 bytes parent folder | download | duplicates (4)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.base.versioning;

import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.pentaho.reporting.libraries.base.util.ObjectUtilities;

/**
 * A utility class for reading versioning information from a Manifest file.
 *
 * @author Thomas Morgner
 */
public class VersionHelper
{
  private String version;
  private String title;
  private String productId;
  private String releaseMilestone;
  private String releaseMinor;
  private String releaseMajor;
  private String releaseCandidateToken;
  private String releaseNumber;
  private String releaseBuildNumber;
  private ProjectInformation projectInformation;

  /**
   * Loads the versioning information for the given project-information structure using the project information's
   * internal name as lookup key.
   *
   * @param projectInformation the project we load information for.
   */
  public VersionHelper(final ProjectInformation projectInformation)
  {
    if (projectInformation == null)
    {
      throw new NullPointerException();
    }

    this.projectInformation = projectInformation;

    final ClassLoader loader = projectInformation.getClass().getClassLoader();
    try
    {
      final Enumeration resources = loader.getResources("META-INF/MANIFEST.MF");
      while (resources.hasMoreElements())
      {
        final URL url = (URL) resources.nextElement();
        final InputStream inputStream = url.openStream();
        try
        {
          if (init(inputStream))
          {
            return;
          }
        }
        finally
        {
          inputStream.close();
        }
      }

    }
    catch (Exception e)
    {
      // Ignore; Maybe log.
    }
  }

  /**
   * Initializes the instance, reaading the properties from the input stream.
   *
   * @param input the input stream.
   * @return true, if the manifest contains version information about this library, false otherwise. 
   */
  private boolean init(final InputStream input)
  {
    if (input == null)
    {
      return false;
    }

    try
    {
      final Manifest props = new Manifest(input);
      final Attributes attr = getAttributes(props, projectInformation.getInternalName());
      final String maybeTitle = getValue(attr, "Implementation-ProductID", null);
      if (ObjectUtilities.equal(projectInformation.getInternalName(), maybeTitle) == false)
      {
        return false;
      }

      title = getValue(attr, "Implementation-Title", maybeTitle);
      releaseMajor = getValue(attr, "Release-Major-Number", "0");
      releaseMinor = getValue(attr, "Release-Minor-Number", "0");
      releaseMilestone = getValue(attr, "Release-Milestone-Number", "0");
      releaseCandidateToken = getValue(attr, "Release-Candidate-Token", "");
      releaseBuildNumber = getValue(attr, "Release-Build-Number", "");
      releaseNumber = getValue(attr, "Release-Number", "");
      if (releaseNumber.length() == 0)
      {
        releaseNumber = createReleaseVersion();
      }
      version = getValue(attr, "Implementation-Version", "");
      if (version.length() == 0)
      {
        version = createVersion();
      }

      productId = maybeTitle;
      if (productId.length() == 0)
      {
        productId = createProductId();
      }

      return true;
    }
    catch (final Exception e)
    {
      return false;
    }
  }

  /**
   * Looks up the attributes for the given module specified by <code>name</code> in the given Manifest.
   *
   * @param props the manifest where to search for the attributes.
   * @param name the name of the module.
   * @return the attributes for the module or the main attributes if the jar contains no such module.
   */
  private Attributes getAttributes(final Manifest props, final String name)
  {
    final Attributes attributes = props.getAttributes(name);
    if (attributes == null)
    {
      return props.getMainAttributes();
    }
    return attributes;
  }

  /**
   * Looks up a single value in the given attribute collection using the given key. If the key is not contained in
   * the attributes, this method returns the default value specified as parameter.
   *
   * @param attrs the attributes where to lookup the key.
   * @param name the name of the key to use for the lookup.
   * @param defaultValue the default value to return in case the attributes contain no such key.
   * @return the value from the attributes or the default values.
   */
  private String getValue(final Attributes attrs, final String name, final String defaultValue)
  {
    final String value = attrs.getValue(name);
    if (value == null)
    {
      return defaultValue;
    }
    return value.trim();
  }

  /**
   * Creates a product-id string, which is the implementation title plus the optional version information.
   *
   * @return the product id string.
   */
  private String createProductId()
  {
    if (version.trim().length() == 0)
    {
      return title;
    }
    return title + '-' + version;
  }

  /**
   * Creates a version string using only the major, minor and milestone version information.
   *
   * @return the version.
   */
  private String createVersion()
  {
    final StringBuffer buffer = new StringBuffer(50);
    buffer.append(releaseMajor);
    buffer.append('.');
    buffer.append(releaseMinor);
    buffer.append('.');
    buffer.append(releaseMilestone);
    return buffer.toString();
  }

  /**
   * Creates a version string using the major, minor and milestone version information and the build number.
   *
   * @return the release version.
   */
  private String createReleaseVersion()
  {
    final StringBuffer buffer = new StringBuffer(50);
    buffer.append(releaseMajor);
    buffer.append('.');
    buffer.append(releaseMinor);
    buffer.append('.');
    buffer.append(releaseMilestone);
    if (releaseCandidateToken.length() > 0)
    {
      buffer.append('-');
      buffer.append(releaseCandidateToken);
    }
    if (releaseBuildNumber.length() > 0)
    {
      buffer.append(" (Build ");
      buffer.append(releaseBuildNumber);
      buffer.append(')');
    }
    return buffer.toString();
  }

  /**
   * Returns the full version string as computed by createVersion().
   *
   * @return the version string.
   * @see #createVersion() 
   */
  public String getVersion()
  {
    return version;
  }

  /**
   * Returns the implementation title as specified in the manifest.
   *
   * @return the implementation title.
   */
  public String getTitle()
  {
    return title;
  }

  /**
   * Returns the product id as computed by createProductId().
   *
   * @return the product id.
   * @see #createProductId() 
   */
  public String getProductId()
  {
    return productId;
  }

  /**
   * Returns the release milestone number.
   *
   * @return the milestone number.
   */
  public String getReleaseMilestone()
  {
    return releaseMilestone;
  }

  /**
   * Returns the release minor number.
   *
   * @return the minor version number.
   */
  public String getReleaseMinor()
  {
    return releaseMinor;
  }

  /**
   * Returns the release major number.
   *
   * @return the major version number.
   */
  public String getReleaseMajor()
  {
    return releaseMajor;
  }

  /**
   * Returns the release candidate token.
   *
   * @return the candidate token.
   */
  public String getReleaseCandidateToken()
  {
    return releaseCandidateToken;
  }

  /**
   * Returns the release number.
   *
   * @return the release number.
   */
  public String getReleaseNumber()
  {
    return releaseNumber;
  }

  /**
   * Returns the release build number.
   *
   * @return the build-number).
   */
  public String getReleaseBuildNumber()
  {
    return releaseBuildNumber;
  }
}