View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library 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 GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import java.io.File;
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.Iterator;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import net.sf.jameleon.util.Configurator;
29  
30  public class Utils{
31  
32      public static ClassLoader createClassLoader(){
33          Configurator.clearInstance();
34          Utils utils = new Utils();
35          URL[] urls = utils.getURLS();
36          Configurator.clearInstance();
37          return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
38      }
39  
40      private URL[] getURLS(){
41          Set urlsS = getEntriesForClasspath();
42          URL[] urls = new URL[urlsS.size()];
43          Iterator it = urlsS.iterator();
44          for (int i = 0; it.hasNext(); i++) {
45              urls[i] = (URL) it.next();
46          }
47          return urls;
48      }
49  
50      private Set getEntriesForClasspath(){
51          Set fileNames = Configurator.getInstance().getKeysStartingWith("classpath.entry");
52          return getURLS(fileNames);
53      }
54  
55      private Set getURLS(Set fileNames){
56          Set urls = new HashSet();
57          Iterator it = fileNames.iterator();
58          String value;
59          File f;
60          while (it.hasNext()) {
61              value = Configurator.getInstance().getValue((String)it.next());
62              f = new File(value);
63              if (f.exists()) {
64                  try{
65                      urls.add(f.toURL());
66                  }catch(Exception e){
67                      System.err.println("Couldn't add "+value);
68                  }
69              }
70          }
71          return urls;
72      }
73  
74  }