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.10 2008/09/10 09:23:34 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 07-Jun-2004 : Added source headers (DG);
040 *
041 */
042
043package org.jfree.base;
044
045import java.lang.reflect.Method;
046import java.util.ArrayList;
047import java.util.List;
048
049import org.jfree.util.ObjectUtilities;
050
051/**
052 * Basic project info.
053 *
054 * @author Thomas Morgner
055 */
056public class BasicProjectInfo extends Library {
057    /**
058     * A helper class, which simplifies the loading of optional library
059     * implementations.
060     */
061    private static class OptionalLibraryHolder {
062        private String libraryClass;
063        private transient Library library;
064
065        public OptionalLibraryHolder(final String libraryClass) {
066            if (libraryClass == null) {
067                throw new NullPointerException("LibraryClass must not be null.");
068            }
069            this.libraryClass = libraryClass;
070        }
071
072        public OptionalLibraryHolder(final Library library) {
073          if (library == null) {
074              throw new NullPointerException("Library must not be null.");
075          }
076          this.library = library;
077          this.libraryClass = library.getClass().getName();
078        }
079
080        public String getLibraryClass() {
081            return this.libraryClass;
082        }
083
084        public Library getLibrary() {
085            if (this.library == null) {
086                this.library = loadLibrary(this.libraryClass);
087            }
088            return this.library;
089        }
090
091        protected Library loadLibrary(final String classname) {
092            if (classname == null) {
093                return null;
094            }
095            try {
096                final Class c = ObjectUtilities.getClassLoader(
097                        getClass()).loadClass(classname);
098                try {
099                    final Method m = c.getMethod("getInstance", (Class[]) null);
100                    return (Library) m.invoke(null, (Object[]) null);
101                }
102                catch(Exception e) {
103                    // ok, fall back ...
104                }
105                return (Library) c.newInstance();
106            }
107            catch (Exception e) {
108                // ok, this library has no 'getInstance()' method. Check the
109                // default constructor ...
110                return null;
111            }
112        }
113
114    }
115
116    /** The project copyright statement. */
117    private String copyright;
118
119    /** A list of libraries used by the project. */
120    private List libraries;
121
122    private List optionalLibraries;
123
124    /**
125     * Default constructor.
126     */
127    public BasicProjectInfo() {
128        this.libraries = new ArrayList();
129        this.optionalLibraries = new ArrayList();
130    }
131
132    /**
133     * Creates a new library reference.
134     *
135     * @param name    the name.
136     * @param version the version.
137     * @param licence the licence.
138     * @param info    the web address or other info.
139     */
140    public BasicProjectInfo(final String name, final String version,
141                            final String licence, final String info) {
142        this();
143        setName(name);
144        setVersion(version);
145        setLicenceName(licence);
146        setInfo(info);
147    }
148
149    /**
150     * Creates a new project info instance.
151     *
152     * @param name  the project name.
153     * @param version  the project version.
154     * @param info  the project info (web site for example).
155     * @param copyright  the copyright statement.
156     * @param licenceName  the license name.
157     */
158    public BasicProjectInfo(final String name, final String version,
159                            final String info, final String copyright,
160                            final String licenceName) {
161        this(name, version, licenceName, info);
162        setCopyright(copyright);
163    }
164
165    /**
166     * Returns the copyright statement.
167     *
168     * @return The copyright statement.
169     */
170    public String getCopyright() {
171        return this.copyright;
172    }
173
174    /**
175     * Sets the project copyright statement.
176     *
177     * @param copyright  the project copyright statement.
178     */
179    public void setCopyright(final String copyright) {
180        this.copyright = copyright;
181    }
182
183    /**
184     * Sets the project info string (for example, this could be the project URL).
185     *
186     * @param info  the info string.
187     */
188    public void setInfo(final String info) {
189        super.setInfo(info);
190    }
191
192    /**
193     * Sets the license name.
194     *
195     * @param licence  the license name.
196     */
197    public void setLicenceName(final String licence) {
198        super.setLicenceName(licence);
199    }
200
201    /**
202     * Sets the project name.
203     *
204     * @param name  the project name.
205     */
206    public void setName(final String name) {
207        super.setName(name);
208    }
209
210    /**
211     * Sets the project version number.
212     *
213     * @param version  the version number.
214     */
215    public void setVersion(final String version) {
216        super.setVersion(version);
217    }
218
219    /**
220     * Returns a list of libraries used by the project.
221     *
222     * @return the list of libraries.
223     */
224    public Library[] getLibraries() {
225        return (Library[]) this.libraries.toArray
226                (new Library[this.libraries.size()]);
227    }
228
229    /**
230     * Adds a library.
231     *
232     * @param library  the library.
233     */
234    public void addLibrary (final Library library) {
235        if (library == null) {
236            throw new NullPointerException();
237        }
238        this.libraries.add(library);
239    }
240
241    /**
242     * Returns a list of optional libraries used by the project.
243     *
244     * @return the list of libraries.
245     */
246    public Library[] getOptionalLibraries() {
247        final ArrayList libraries = new ArrayList();
248        for (int i = 0; i < this.optionalLibraries.size(); i++) {
249          OptionalLibraryHolder holder =
250                  (OptionalLibraryHolder) this.optionalLibraries.get(i);
251          Library l = holder.getLibrary();
252          if (l != null) {
253              libraries.add(l);
254          }
255        }
256        return (Library[]) libraries.toArray(new Library[libraries.size()]);
257    }
258
259    /**
260     * Adds an optional library. These libraries will be booted, if they define
261     * a boot class. A missing class is considered non-fatal and it is assumed
262     * that the programm knows how to handle that.
263     *
264     * @param libraryClass  the library.
265     */
266    public void addOptionalLibrary (final String libraryClass) {
267        if (libraryClass == null) {
268            throw new NullPointerException("Library classname must be given.");
269        }
270        this.optionalLibraries.add
271                (new OptionalLibraryHolder(libraryClass));
272    }
273
274
275    /**
276     * Adds an optional library. These libraries will be booted, if they define
277     * a boot class. A missing class is considered non-fatal and it is assumed
278     * that the programm knows how to handle that.
279     *
280     * @param library  the library.
281     */
282    public void addOptionalLibrary (final Library library) {
283      if (library == null) {
284          throw new NullPointerException("Library must be given.");
285      }
286      this.optionalLibraries.add(new OptionalLibraryHolder(library));
287  }
288}