001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------
028: * BasicProjectInfo.java
029: * ---------------------
030: * (C)opyright 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: BasicProjectInfo.java,v 1.7 2006/04/14 13:00:56 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 07-Jun-2004 : Added source headers (DG);
040: *
041: */
042:
043: package org.jfree.base;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.lang.reflect.Method;
048:
049: import org.jfree.util.ObjectUtilities;
050: import org.jfree.util.Log;
051:
052: /**
053: * Basic project info.
054: *
055: * @author Thomas Morgner
056: */
057: public class BasicProjectInfo extends Library {
058: /**
059: * A helper class, which simplifies the loading of optional library
060: * implementations.
061: */
062: private static class OptionalLibraryHolder {
063: private String libraryClass;
064: private transient Library library;
065:
066: public OptionalLibraryHolder(final String libraryClass) {
067: if (libraryClass == null) {
068: throw new NullPointerException(
069: "LibraryClass must not be null.");
070: }
071: this .libraryClass = libraryClass;
072: }
073:
074: public OptionalLibraryHolder(final Library library) {
075: if (library == null) {
076: throw new NullPointerException(
077: "Library must not be null.");
078: }
079: this .library = library;
080: this .libraryClass = library.getClass().getName();
081: }
082:
083: public String getLibraryClass() {
084: return libraryClass;
085: }
086:
087: public Library getLibrary() {
088: if (library == null) {
089: library = loadLibrary(libraryClass);
090: }
091: return library;
092: }
093:
094: protected Library loadLibrary(final String classname) {
095: if (classname == null) {
096: return null;
097: }
098: try {
099: final Class c = ObjectUtilities.getClassLoader(
100: getClass()).loadClass(classname);
101: try {
102: final Method m = c.getMethod("getInstance",
103: (Class[]) null);
104: return (Library) m.invoke(null, (Object[]) null);
105: } catch (Exception e) {
106: // ok, fall back ...
107: }
108: return (Library) c.newInstance();
109: } catch (Exception e) {
110: // ok, this library has no 'getInstance()' method. Check the
111: // default constructor ...
112: return null;
113: }
114: }
115:
116: }
117:
118: /** The project copyright statement. */
119: private String copyright;
120:
121: /** A list of libraries used by the project. */
122: private List libraries;
123:
124: private List optionalLibraries;
125:
126: /**
127: * Default constructor.
128: */
129: public BasicProjectInfo() {
130: this .libraries = new ArrayList();
131: this .optionalLibraries = new ArrayList();
132: }
133:
134: /**
135: * Creates a new library reference.
136: *
137: * @param name the name.
138: * @param version the version.
139: * @param licence the licence.
140: * @param info the web address or other info.
141: */
142: public BasicProjectInfo(final String name, final String version,
143: final String licence, final String info) {
144: this ();
145: setName(name);
146: setVersion(version);
147: setLicenceName(licence);
148: setInfo(info);
149: }
150:
151: /**
152: * Creates a new project info instance.
153: *
154: * @param name the project name.
155: * @param version the project version.
156: * @param info the project info (web site for example).
157: * @param copyright the copyright statement.
158: * @param licenceName the license name.
159: */
160: public BasicProjectInfo(final String name, final String version,
161: final String info, final String copyright,
162: final String licenceName) {
163: this (name, version, licenceName, info);
164: setCopyright(copyright);
165: }
166:
167: /**
168: * Returns the copyright statement.
169: *
170: * @return The copyright statement.
171: */
172: public String getCopyright() {
173: return this .copyright;
174: }
175:
176: /**
177: * Sets the project copyright statement.
178: *
179: * @param copyright the project copyright statement.
180: */
181: public void setCopyright(final String copyright) {
182: this .copyright = copyright;
183: }
184:
185: /**
186: * Sets the project info string (for example, this could be the project URL).
187: *
188: * @param info the info string.
189: */
190: public void setInfo(final String info) {
191: super .setInfo(info);
192: }
193:
194: /**
195: * Sets the license name.
196: *
197: * @param licence the license name.
198: */
199: public void setLicenceName(final String licence) {
200: super .setLicenceName(licence);
201: }
202:
203: /**
204: * Sets the project name.
205: *
206: * @param name the project name.
207: */
208: public void setName(final String name) {
209: super .setName(name);
210: }
211:
212: /**
213: * Sets the project version number.
214: *
215: * @param version the version number.
216: */
217: public void setVersion(final String version) {
218: super .setVersion(version);
219: }
220:
221: /**
222: * Returns a list of libraries used by the project.
223: *
224: * @return the list of libraries.
225: */
226: public Library[] getLibraries() {
227: return (Library[]) this .libraries
228: .toArray(new Library[this .libraries.size()]);
229: }
230:
231: /**
232: * Adds a library.
233: *
234: * @param library the library.
235: */
236: public void addLibrary(final Library library) {
237: if (library == null) {
238: throw new NullPointerException();
239: }
240: this .libraries.add(library);
241: }
242:
243: /**
244: * Returns a list of optional libraries used by the project.
245: *
246: * @return the list of libraries.
247: */
248: public Library[] getOptionalLibraries() {
249: final ArrayList libraries = new ArrayList();
250: for (int i = 0; i < optionalLibraries.size(); i++) {
251: OptionalLibraryHolder holder = (OptionalLibraryHolder) optionalLibraries
252: .get(i);
253: Library l = holder.getLibrary();
254: if (l != null) {
255: libraries.add(l);
256: }
257: }
258: return (Library[]) libraries.toArray(new Library[libraries
259: .size()]);
260: }
261:
262: /**
263: * Adds an optional library. These libraries will be booted, if they define
264: * a boot class. A missing class is considered non-fatal and it is assumed
265: * that the programm knows how to handle that.
266: *
267: * @param library the library.
268: */
269: public void addOptionalLibrary(final String libraryClass) {
270: if (libraryClass == null) {
271: throw new NullPointerException(
272: "Library classname must be given.");
273: }
274: this .optionalLibraries.add(new OptionalLibraryHolder(
275: libraryClass));
276: }
277:
278: /**
279: * Adds an optional library. These libraries will be booted, if they define
280: * a boot class. A missing class is considered non-fatal and it is assumed
281: * that the programm knows how to handle that.
282: *
283: * @param library the library.
284: */
285: public void addOptionalLibrary(final Library library) {
286: if (library == null) {
287: throw new NullPointerException("Library must be given.");
288: }
289: this .optionalLibraries.add(new OptionalLibraryHolder(library));
290: }
291: }
|