Source for org.jfree.base.BasicProjectInfo

   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: 
  43: package org.jfree.base;
  44: 
  45: import java.lang.reflect.Method;
  46: import java.util.ArrayList;
  47: import java.util.List;
  48: 
  49: import org.jfree.util.ObjectUtilities;
  50: 
  51: /**
  52:  * Basic project info.
  53:  *
  54:  * @author Thomas Morgner
  55:  */
  56: public class BasicProjectInfo extends Library {
  57:     /**
  58:      * A helper class, which simplifies the loading of optional library
  59:      * implementations.
  60:      */
  61:     private static class OptionalLibraryHolder {
  62:         private String libraryClass;
  63:         private transient Library library;
  64: 
  65:         public OptionalLibraryHolder(final String libraryClass) {
  66:             if (libraryClass == null) {
  67:                 throw new NullPointerException("LibraryClass must not be null.");
  68:             }
  69:             this.libraryClass = libraryClass;
  70:         }
  71: 
  72:         public OptionalLibraryHolder(final Library library) {
  73:           if (library == null) {
  74:               throw new NullPointerException("Library must not be null.");
  75:           }
  76:           this.library = library;
  77:           this.libraryClass = library.getClass().getName();
  78:         }
  79: 
  80:         public String getLibraryClass() {
  81:             return this.libraryClass;
  82:         }
  83: 
  84:         public Library getLibrary() {
  85:             if (this.library == null) {
  86:                 this.library = loadLibrary(this.libraryClass);
  87:             }
  88:             return this.library;
  89:         }
  90: 
  91:         protected Library loadLibrary(final String classname) {
  92:             if (classname == null) {
  93:                 return null;
  94:             }
  95:             try {
  96:                 final Class c = ObjectUtilities.getClassLoader(
  97:                         getClass()).loadClass(classname);
  98:                 try {
  99:                     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: }