KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > LucaneClassLoader


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.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  
20 package org.lucane.common;
21
22 import java.net.*;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * Used to add urls for services and plugins to the classpath
28  */

29 public class LucaneClassLoader extends URLClassLoader
30 {
31   //-- singleton
32
private static LucaneClassLoader instance = null;
33
34   /**
35    * Get ClassLoader instance
36    *
37    * @return the unique instance
38    */

39   public static LucaneClassLoader getInstance()
40   {
41     if(instance == null)
42       instance = new LucaneClassLoader();
43
44     return instance;
45   }
46   
47   private ArrayList JavaDoc urls;
48   
49   /**
50    * Constructor
51    */

52   private LucaneClassLoader()
53   {
54     super(new URL[0], LucaneClassLoader.class.getClassLoader());
55     this.urls = new ArrayList JavaDoc();
56   }
57
58   /**
59    * Add an url to use in class lookup
60    *
61    * @param url the url
62    */

63   public void addUrl(URL url)
64   {
65     this.urls.add(url);
66     this.addURL(url);
67   }
68   
69   /**
70    * Add an url to use in class lookup
71    *
72    * @param url the url
73    */

74   public void addUrl(String JavaDoc url)
75   throws MalformedURLException
76   {
77     this.addUrl(new URL(url));
78   }
79   
80   /**
81    * Get the classpath string corresponding to this classloader and its parent.
82    * (only jar files !)
83    *
84    * @return the classpath
85    */

86   public String JavaDoc getClassPath()
87   {
88     String JavaDoc sep = System.getProperty("path.separator");
89     StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
90     URL[] urls = ((URLClassLoader)this.getParent()).getURLs();
91     for(int i=0;i<urls.length;i++)
92     {
93         cp.append(urlToFile(urls[i]));
94         cp.append(sep);
95     }
96     
97     Iterator JavaDoc i = this.urls.iterator();
98     while(i.hasNext())
99     {
100         URL url = (URL)i.next();
101         cp.append(urlToFile(url));
102         cp.append(sep);
103     }
104     
105     return cp.toString();
106   }
107   
108   /**
109    * Convert a jar:file///xxx.jar!/ url to /xxx.jar
110    *
111    * @param url the base url
112    * @return the file path
113    */

114   private String JavaDoc urlToFile(URL url)
115   {
116     String JavaDoc file = url.toString();
117     file = file.substring("jar:file:///".length());
118     file = file.substring(0, file.length()-2);
119     return file;
120   }
121 }
122
Popular Tags