File: ProjectInformation.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 (330 lines) | stat: -rw-r--r-- 9,123 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
328
329
330
/*
 * 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.lang.reflect.Method;
import java.util.ArrayList;

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

/**
 * The project information structure contains information about the current project. This is an extended version
 * of the dependency information enriched with information about the boot-process and a list of dependencies.
 * <p/>
 * This class needs to be subclassed by each project that wants to participate in the global boot process.
 *
 * @author Thomas Morgner
 */
public abstract class ProjectInformation extends DependencyInformation
{
  private String copyright;
  private String bootClass;
  private ArrayList libraries;
  private ArrayList optionalLibraries;
  private VersionHelper versionHelper;
  private String internalName;

  /**
   * Creates a new project information object with the given name.
   *
   * @param name the name of the project, when internal and public names are equal.
   */
  protected ProjectInformation(final String name)
  {
    this(name, name);
  }

  /**
   * Creates a new project information object with the given name. The internal name is used to lookup the
   * version information in the manifest file, while the public name is presented to the humans.
   *
   * @param internalName the internal name of the project.
   * @param publicName   the public name of the project.
   */
  protected ProjectInformation(final String internalName, final String publicName)
  {
    super(publicName);
    if (internalName == null)
    {
      this.internalName = publicName;
    }
    else
    {
      this.internalName = internalName;
    }
    this.libraries = new ArrayList();
    optionalLibraries = new ArrayList();
  }

  /**
   * Returs a version helper for this project. The version helper is used to extract the versioning information
   * from the manifest file of the project's JAR.
   *
   * @return the version helper, never null.
   */
  private synchronized VersionHelper getVersionHelper()
  {
    if (versionHelper == null)
    {
      versionHelper = new VersionHelper(this);
    }
    return versionHelper;
  }

  /**
   * Returns the copyright string for thie project.
   *
   * @return the copyright string (might be null).
   */
  public String getCopyright()
  {
    return copyright;
  }

  /**
   * Updates the copyright string for thie project.
   *
   * @param copyright the copyright string.
   */
  protected void setCopyright(final String copyright)
  {
    this.copyright = copyright;
  }

  /**
   * Returns the internal name of the project.
   *
   * @return the internal name, never null.
   */
  public String getInternalName()
  {
    return internalName;
  }

  /**
   * Returns the boot class.
   *
   * @return the bootclass (might be null).
   */
  public String getBootClass()
  {
    return bootClass;
  }

  /**
   * Redefines the boot class.
   *
   * @param bootClass the bootclass (might be null).
   */
  protected void setBootClass(final String bootClass)
  {
    this.bootClass = bootClass;
  }

  /**
   * Returns a list of libraries used by the project.
   *
   * @return the list of libraries.
   */
  public DependencyInformation[] getLibraries()
  {
    return (DependencyInformation[]) this.libraries.toArray
        (new DependencyInformation[this.libraries.size()]);
  }

  /**
   * Adds a library.
   *
   * @param library the library.
   */
  public void addLibrary(final DependencyInformation library)
  {
    if (library == null)
    {
      throw new NullPointerException();
    }
    if (this.libraries.contains(library))
    {
      throw new NullPointerException();
    }

    this.libraries.add(library);
  }

  /**
   * Returns a list of optional libraries used by the project.
   *
   * @return the list of libraries.
   */
  public DependencyInformation[] getOptionalLibraries()
  {
    return (DependencyInformation[]) optionalLibraries.toArray(new DependencyInformation[optionalLibraries.size()]);
  }

  /**
   * Adds an optional library. These libraries will be booted, if they define a boot class. A missing class is
   * considered non-fatal and it is assumed that the programm knows how to handle that.
   *
   * @param libraryClass the library.
   */
  protected void addOptionalLibrary(final String libraryClass)
  {
    if (libraryClass == null)
    {
      throw new NullPointerException("Library classname must be given.");
    }
    final DependencyInformation depInfo = loadLibrary(libraryClass);
    if (depInfo != null)
    {
      this.optionalLibraries.add(depInfo);
    }
  }

  /**
   * Tries to load the dependency information for the given class.
   *
   * @param classname the classname of the class that contains the <code>public static
   * DependencyInformation getInstance()</code> method.
   * @return the dependency information for the library or null, if the library's project-info class could not be loaded.
   */
  private DependencyInformation loadLibrary(final String classname)
  {
    if (classname == null)
    {
      return null;
    }
    try
    {
      final ClassLoader loader = ObjectUtilities.getClassLoader(getClass());
      final Class c = Class.forName(classname, false, loader);
      try
      {
        // This cast is necessary for JDK 1.5 or later
        //noinspection RedundantCast
        final Method m = c.getMethod("getInstance", (Class[]) null);
        return (DependencyInformation) m.invoke(null, (Object[]) null);
      }
      catch (Exception e)
      {
        // ok, fall back ...
      }
      return (DependencyInformation) c.newInstance();
    }
    catch (Exception e)
    {
      // ok, this library has no 'getInstance()' method. Check the
      // default constructor ...
      return null;
    }
  }

  /**
   * Adds an optional library. These libraries will be booted, if they define a boot class. A missing class is
   * considered non-fatal and it is assumed that the programm knows how to handle that.
   *
   * @param library the library.
   */
  protected void addOptionalLibrary(final DependencyInformation library)
  {
    if (library == null)
    {
      throw new NullPointerException("Library must be given.");
    }
    this.optionalLibraries.add(library);
  }

  /**
   * Returns the version number from the Manifest.
   *
   * @return the version, or null if no version information is known.
   */
  public String getVersion()
  {
    return getVersionHelper().getVersion();
  }

  /**
   * Returns the product ID from the Manifest.
   *
   * @return the product ID, or null if none is specified in the manifest.
   */
  public String getProductId()
  {
    return getVersionHelper().getProductId();
  }

  /**
   * Returns the release milestone number from the Manifest.
   *
   * @return the release milestone number, or null if none is specified in the manifest.
   */
  public String getReleaseMilestone()
  {
    return getVersionHelper().getReleaseMilestone();
  }

  /**
   * Returns the release minor version number from the Manifest.
   *
   * @return the release minor version number, or null if none is specified in the manifest.
   */
  public String getReleaseMinor()
  {
    return getVersionHelper().getReleaseMinor();
  }

  /**
   * Returns the release major version number from the Manifest.
   *
   * @return the release major version number, or null if none is specified in the manifest.
   */
  public String getReleaseMajor()
  {
    return getVersionHelper().getReleaseMajor();
  }

  /**
   * Returns the release candidate token from the Manifest.
   *
   * @return the release candidate token, or null if none is specified in the manifest.
   */
  public String getReleaseCandidateToken()
  {
    return getVersionHelper().getReleaseCandidateToken();
  }

  /**
   * Returns the release number from the Manifest.
   *
   * @return the release number, or null if none is specified in the manifest.
   */
  public String getReleaseNumber()
  {
    return getVersionHelper().getReleaseNumber();
  }

  /**
   * Returns the release build number from the Manifest.
   *
   * @return the release build number, or null if none is specified in the manifest.
   */
  public String getReleaseBuildNumber()
  {
    return versionHelper.getReleaseBuildNumber();
  }
}