BasicProjectInfo.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ---------------------
  28. * BasicProjectInfo.java
  29. * ---------------------
  30. * (C)opyright 2004, by Thomas Morgner and Contributors.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: BasicProjectInfo.java,v 1.10 2008/09/10 09:23:34 mungady Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 07-Jun-2004 : Added source headers (DG);
  40. *
  41. */
  42. package org.jfree.base;
  43. import java.lang.reflect.Method;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import org.jfree.util.ObjectUtilities;
  47. /**
  48. * Basic project info.
  49. *
  50. * @author Thomas Morgner
  51. */
  52. public class BasicProjectInfo extends Library {
  53. /**
  54. * A helper class, which simplifies the loading of optional library
  55. * implementations.
  56. */
  57. private static class OptionalLibraryHolder {
  58. private String libraryClass;
  59. private transient Library library;
  60. public OptionalLibraryHolder(final String libraryClass) {
  61. if (libraryClass == null) {
  62. throw new NullPointerException("LibraryClass must not be null.");
  63. }
  64. this.libraryClass = libraryClass;
  65. }
  66. public OptionalLibraryHolder(final Library library) {
  67. if (library == null) {
  68. throw new NullPointerException("Library must not be null.");
  69. }
  70. this.library = library;
  71. this.libraryClass = library.getClass().getName();
  72. }
  73. public String getLibraryClass() {
  74. return this.libraryClass;
  75. }
  76. public Library getLibrary() {
  77. if (this.library == null) {
  78. this.library = loadLibrary(this.libraryClass);
  79. }
  80. return this.library;
  81. }
  82. protected Library loadLibrary(final String classname) {
  83. if (classname == null) {
  84. return null;
  85. }
  86. try {
  87. final Class c = ObjectUtilities.getClassLoader(
  88. getClass()).loadClass(classname);
  89. try {
  90. final Method m = c.getMethod("getInstance", (Class[]) null);
  91. return (Library) m.invoke(null, (Object[]) null);
  92. }
  93. catch(Exception e) {
  94. // ok, fall back ...
  95. }
  96. return (Library) c.newInstance();
  97. }
  98. catch (Exception e) {
  99. // ok, this library has no 'getInstance()' method. Check the
  100. // default constructor ...
  101. return null;
  102. }
  103. }
  104. }
  105. /** The project copyright statement. */
  106. private String copyright;
  107. /** A list of libraries used by the project. */
  108. private List libraries;
  109. private List optionalLibraries;
  110. /**
  111. * Default constructor.
  112. */
  113. public BasicProjectInfo() {
  114. this.libraries = new ArrayList();
  115. this.optionalLibraries = new ArrayList();
  116. }
  117. /**
  118. * Creates a new library reference.
  119. *
  120. * @param name the name.
  121. * @param version the version.
  122. * @param licence the licence.
  123. * @param info the web address or other info.
  124. */
  125. public BasicProjectInfo(final String name, final String version,
  126. final String licence, final String info) {
  127. this();
  128. setName(name);
  129. setVersion(version);
  130. setLicenceName(licence);
  131. setInfo(info);
  132. }
  133. /**
  134. * Creates a new project info instance.
  135. *
  136. * @param name the project name.
  137. * @param version the project version.
  138. * @param info the project info (web site for example).
  139. * @param copyright the copyright statement.
  140. * @param licenceName the license name.
  141. */
  142. public BasicProjectInfo(final String name, final String version,
  143. final String info, final String copyright,
  144. final String licenceName) {
  145. this(name, version, licenceName, info);
  146. setCopyright(copyright);
  147. }
  148. /**
  149. * Returns the copyright statement.
  150. *
  151. * @return The copyright statement.
  152. */
  153. public String getCopyright() {
  154. return this.copyright;
  155. }
  156. /**
  157. * Sets the project copyright statement.
  158. *
  159. * @param copyright the project copyright statement.
  160. */
  161. public void setCopyright(final String copyright) {
  162. this.copyright = copyright;
  163. }
  164. /**
  165. * Sets the project info string (for example, this could be the project URL).
  166. *
  167. * @param info the info string.
  168. */
  169. public void setInfo(final String info) {
  170. super.setInfo(info);
  171. }
  172. /**
  173. * Sets the license name.
  174. *
  175. * @param licence the license name.
  176. */
  177. public void setLicenceName(final String licence) {
  178. super.setLicenceName(licence);
  179. }
  180. /**
  181. * Sets the project name.
  182. *
  183. * @param name the project name.
  184. */
  185. public void setName(final String name) {
  186. super.setName(name);
  187. }
  188. /**
  189. * Sets the project version number.
  190. *
  191. * @param version the version number.
  192. */
  193. public void setVersion(final String version) {
  194. super.setVersion(version);
  195. }
  196. /**
  197. * Returns a list of libraries used by the project.
  198. *
  199. * @return the list of libraries.
  200. */
  201. public Library[] getLibraries() {
  202. return (Library[]) this.libraries.toArray
  203. (new Library[this.libraries.size()]);
  204. }
  205. /**
  206. * Adds a library.
  207. *
  208. * @param library the library.
  209. */
  210. public void addLibrary (final Library library) {
  211. if (library == null) {
  212. throw new NullPointerException();
  213. }
  214. this.libraries.add(library);
  215. }
  216. /**
  217. * Returns a list of optional libraries used by the project.
  218. *
  219. * @return the list of libraries.
  220. */
  221. public Library[] getOptionalLibraries() {
  222. final ArrayList libraries = new ArrayList();
  223. for (int i = 0; i < this.optionalLibraries.size(); i++) {
  224. OptionalLibraryHolder holder =
  225. (OptionalLibraryHolder) this.optionalLibraries.get(i);
  226. Library l = holder.getLibrary();
  227. if (l != null) {
  228. libraries.add(l);
  229. }
  230. }
  231. return (Library[]) libraries.toArray(new Library[libraries.size()]);
  232. }
  233. /**
  234. * Adds an optional library. These libraries will be booted, if they define
  235. * a boot class. A missing class is considered non-fatal and it is assumed
  236. * that the programm knows how to handle that.
  237. *
  238. * @param libraryClass the library.
  239. */
  240. public void addOptionalLibrary (final String libraryClass) {
  241. if (libraryClass == null) {
  242. throw new NullPointerException("Library classname must be given.");
  243. }
  244. this.optionalLibraries.add
  245. (new OptionalLibraryHolder(libraryClass));
  246. }
  247. /**
  248. * Adds an optional library. These libraries will be booted, if they define
  249. * a boot class. A missing class is considered non-fatal and it is assumed
  250. * that the programm knows how to handle that.
  251. *
  252. * @param library the library.
  253. */
  254. public void addOptionalLibrary (final Library library) {
  255. if (library == null) {
  256. throw new NullPointerException("Library must be given.");
  257. }
  258. this.optionalLibraries.add(new OptionalLibraryHolder(library));
  259. }
  260. }