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