Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/util/plugin/PluginLoader.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.util.plugin;
22  import java.util.*;
23  
24  /** Wow loads a list of plugins!
25   * @author Chris Bitmead (original) Antonio Petrelli (modified)
26   * @version 0.2.1
27   * @commentedby Antonio Petrelli
28   */
29  public class PluginLoader {
30      /** The properties to use.
31       */    
32    Properties props;
33          /** Loads plugins from a properties object.
34           * @param props The properties object to use.
35           * Each property must contains a list of classes, separated by a comma, a space
36           * or a TAB.
37           */        
38    public PluginLoader(Properties props) {
39      this.props = props;
40    }
41          /** Loads the classes and returns them as a list.
42           * @param propertyName The property key to use.
43           * @throws ClassNotFoundException If a class cannot be found.
44           * @return The list of classes.
45           */        
46    public List loadClasses(String propertyName) throws ClassNotFoundException {
47      String classNames = props.getProperty(propertyName);
48      List rtn = new ArrayList();
49      StringTokenizer st = new StringTokenizer(classNames, ", \t");
50      while (st.hasMoreTokens()) {
51        String className = st.nextToken();
52        Class cls = Class.forName(className);
53        rtn.add(cls);
54      }
55      return rtn;
56    }
57          /** Returns instances of classes contained in a property object.
58           * @param propertyName The property to use to get classes from.
59           * @throws ClassNotFoundException If a class cannot be found.
60           * @throws IllegalAccessException If an illegal access has been made.
61           * @throws InstantiationException If the class cannot be instantiated.
62           * @return The list of instance of these classes.
63           */        
64    public List loadInstances(String propertyName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
65      List classList = loadClasses(propertyName);
66      Iterator it = classList.iterator();
67      List rtn = new ArrayList();
68      while (it.hasNext()) {
69        Class cls = (Class)it.next();
70        Object o = cls.newInstance();
71        rtn.add(o);
72      }
73      return rtn;
74    }
75  
76          public static List loadClasses(Collection col) throws ClassNotFoundException {
77                  String className;
78                  Class cls;
79                  List rtn = new ArrayList();
80                  Iterator colIt;
81                  
82                  colIt = col.iterator();
83      while (colIt.hasNext()) {
84        className = (String) colIt.next();
85        cls = Class.forName(className);
86        rtn.add(cls);
87      }
88      return rtn;
89    }
90  
91          public static List loadInstances(Collection col) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
92      List classList = loadClasses(col);
93      Iterator it = classList.iterator();
94      List rtn = new ArrayList();
95      while (it.hasNext()) {
96        Class cls = (Class)it.next();
97        Object o = cls.newInstance();
98        rtn.add(o);
99      }
100     return rtn;
101   }
102 
103         public static List loadClasses(Collection col, ClassLoader loader) throws ClassNotFoundException {
104                 String className;
105                 Class cls;
106                 List rtn = new ArrayList();
107                 Iterator colIt;
108                 
109                 colIt = col.iterator();
110     while (colIt.hasNext()) {
111       className = (String) colIt.next();
112       cls = loader.loadClass(className);
113       rtn.add(cls);
114     }
115     return rtn;
116   }
117 
118         public static List loadInstances(Collection col, ClassLoader loader) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
119     List classList = loadClasses(col, loader);
120     Iterator it = classList.iterator();
121     List rtn = new ArrayList();
122     while (it.hasNext()) {
123       Class cls = (Class)it.next();
124       Object o = cls.newInstance();
125       rtn.add(o);
126     }
127     return rtn;
128   }
129 }