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