View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
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  
20  package org.xnap.loader;
21  
22  // this class is only allowed to access XNap classes in its own package
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URL;
27  import java.net.URLClassLoader;
28  import java.util.LinkedList;
29  import java.util.List;
30  import java.util.Set;
31  import java.util.*;
32  
33  /***
34   * This class provides support to add additional jar files to the classpath.
35   */
36  public class XNapClassLoader extends URLClassLoader
37  {
38  
39      //--- Constant(s) ---
40  
41      //--- Data field(s) ---
42  
43      private static XNapClassLoader singleton = new XNapClassLoader();
44      private static File[] searchPathes;
45  	private static Set urls = new HashSet();
46  
47      //--- Constructor(s) ---
48  
49      private XNapClassLoader()
50      {
51  		super(new URL[] {});
52      }
53  
54      //--- Method(s) ---
55  
56      public static XNapClassLoader getInstance()
57      {
58  		return singleton;
59      }
60  
61      /***
62       * Adds <code>url</code> to the classpath.
63       */
64      public void add(URL url) throws IOException
65      {
66  		if (!urls.contains(url)) {
67  			addURL(url);
68  			urls.add(url);
69  		}
70      }
71  
72      /***
73       * Locates <code>files</code> in jar search path and adds them to the 
74       * classpath.
75       *
76       * @exception IOException if a file can not be found or is not a valid
77       *            jar file
78       * @see #locate(String)
79       */
80      public void add(String[] files) throws IOException
81      {
82  		for (int i = 0; i < files.length; i++) {
83  			File jar = new File(files[i]);
84  			add(jar.toURL());	
85  		}
86      }
87  
88      /***
89       * Returns true, if all <code>depends</code> are located in the jar
90       * search path.
91       */
92      public static boolean areDependenciesSatisfied(String[] depends)
93      {
94  		for (int i = 0; i < depends.length; i++) {
95  			File jar = locate(depends[i]);
96  			if (jar == null) {
97  				return false;
98  			}
99  		}
100 		return true;
101     }
102 
103     /***
104      * Returns the XNap preferences directory with a trailing file 
105      * separator.
106      */
107     public static String getHomeDir()
108     {
109 		StringBuffer sb = new StringBuffer();
110 		sb.append(System.getProperty("user.home"));
111 		sb.append(File.separatorChar);
112 		sb.append(".xnap");
113 		sb.append(File.separatorChar);
114 		return sb.toString();
115     }
116 
117 	public static String getURLsAsString()
118 	{
119 		StringBuffer sb = new StringBuffer();
120 		ArrayList list = new ArrayList(urls);
121 		Collections.sort(list);
122 		for (Iterator it = list.iterator(); it.hasNext();) {
123 			//FIX: sb.append(((URL)it.next()).getFilename());
124 			if (it.hasNext()) {
125 				sb.append(", ");
126 			}
127 		}
128 		return sb.toString();
129 	}
130 
131     /***
132      * Returns the jar search path.
133      */
134     public static File[] getSearchPath()
135     {
136 		if (searchPathes == null) {
137 			List list = new LinkedList();
138 			File f = XNapLoader.getJarPath();
139 			if (f != null) {
140 				list.add(f);
141 			}
142 			list.add(new File(getHomeDir() + "packages"));
143 			list.add(new File("/usr/share/xnap"));
144 			searchPathes = (File[])list.toArray(new File[0]);
145 		}
146 		return searchPathes;
147     }
148 
149     /***
150      * Searches the jar search path for filename and returns a
151      * <code>File</code> object.
152      *
153      * @return null, if <code>filename</code> was not found; the file,
154      *         otherwise 
155      * @see #locate(String)
156      */
157     public static File locate(String filename)
158     {
159 		File pathes[] = getSearchPath();
160 		for (int i = 0; i < pathes.length; i++) {
161 			File f = new File(pathes[i], filename);
162 			if (f.exists() && f.canRead()) {
163 				return f;
164 			}
165 		}
166 
167 		return null;
168     }
169 
170 	/***
171 	 * Returns true if url has been already loaded */
172 	public static boolean isLoaded(URL url) {
173 		return urls.contains(url);
174 	}
175 }