クラス org.objectweb.cjdbc.controller.connection.DriverClassLoader

すべてのメンバ一覧

説明

This class defines a DriverClassLoader used to load drivers with their own classloder to be able to handle different implementations of drivers sharing the same class name. For example if you want to connect to two backends of the same vendor, but running with different releases and requiring a driver compatible with the respective database release

作者:
Emmanuel Cecchet
バージョン:
1.0

DriverClassLoader.java57 行で定義されています。

Protected メソッド

Class findClass (String className) throws ClassNotFoundException
URL findResource (String name)

関数

 DriverClassLoader (ClassLoader parent, File pPath)

Private メソッド

byte[] findClassInJarFile (File dir, String className) throws IOException
String convertClassNameToResourceName (String className)
void loadJarFile (String jarFileName) throws IOException

Private 変数

File path = null
Hashtable htJarContents = new Hashtable()


コンストラクタとデストラクタ

org.objectweb.cjdbc.controller.connection.DriverClassLoader.DriverClassLoader ClassLoader  parent,
File  pPath
[package]
 

Creates a new DriverClassLoader.java object

引数:
parent classloader, null if no parent classloader should be used
pPath path where the driver classfiles of jar files are located
DriverClassLoader.java69 行で定義されています。

参照先 org.objectweb.cjdbc.controller.connection.DriverClassLoader.path.

00070 { 00071 super(parent); 00072 path = pPath; 00073 if (path == null) 00074 path = new File(""); 00075 00076 }


メソッド

String org.objectweb.cjdbc.controller.connection.DriverClassLoader.convertClassNameToResourceName String  className  )  [private]
 

convert the class name into the rescource name. This method is just replacing the '.' in the name with a '/' and adding the suffix '.class'

引数:
className 
戻り値:
resource name
DriverClassLoader.java280 行で定義されています。

参照元 org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClassInJarFile().

00281 { 00282 String resourceName = className; 00283 resourceName = resourceName.replace('.', '/'); 00284 resourceName = resourceName + ".class"; 00285 return resourceName; 00286 }

Class org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClass String  className  )  throws ClassNotFoundException [protected]
 

finds the specified class

参照:
java.lang.ClassLoader#findClass(java.lang.String)
DriverClassLoader.java83 行で定義されています。

参照先 org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClassInJarFile(), と org.objectweb.cjdbc.controller.connection.DriverClassLoader.path.

00084 { 00085 00086 FileInputStream fis = null; 00087 00088 try 00089 { 00090 byte[] classBytes = null; 00091 00092 // first we try to locate a class file. 00093 String pathName = className.replace('.', File.separatorChar); 00094 File file = new File(path.getAbsolutePath(), pathName + ".class"); 00095 if (file.exists()) 00096 { 00097 // we have found a class file and read it 00098 fis = new FileInputStream(file); 00099 classBytes = new byte[fis.available()]; 00100 fis.read(classBytes); 00101 } 00102 else 00103 { 00104 // no class file exists we have to check jar files 00105 classBytes = findClassInJarFile(path, className); 00106 } 00107 00108 // we convert the bytes into the specified class 00109 Class clazz = defineClass(null, classBytes, 0, classBytes.length); 00110 return clazz; 00111 } 00112 catch (Exception e) 00113 { 00114 // We could not find the class, so indicate the problem with an exception 00115 throw new ClassNotFoundException(className, e); 00116 } 00117 finally 00118 { 00119 if (null != fis) 00120 { 00121 try 00122 { 00123 fis.close(); 00124 } 00125 catch (Exception e) 00126 { 00127 } 00128 } 00129 } 00130 }

byte [] org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClassInJarFile File  dir,
String  className
throws IOException [private]
 

Find the first jar file containing the className and load it

引数:
dir directory where we are looking for jar files
className name of the class we are looking for
戻り値:
the class as byte[]
例外:
IOException if an error occurs
DriverClassLoader.java146 行で定義されています。

参照先 org.objectweb.cjdbc.controller.connection.DriverClassLoader.convertClassNameToResourceName(), org.objectweb.cjdbc.controller.connection.DriverClassLoader.htJarContents, と org.objectweb.cjdbc.controller.connection.DriverClassLoader.loadJarFile().

参照元 org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClass().

00148 { 00149 // is the class already cached ? 00150 String resourceName = convertClassNameToResourceName(className); 00151 byte[] classBytes = (byte[]) htJarContents.get(resourceName); 00152 if (classBytes != null) 00153 { 00154 // it has been cached, we return 00155 return classBytes; 00156 } 00157 00158 if (!dir.canRead()) 00159 throw new IOException(dir + " is not readable."); 00160 00161 if (dir.isFile()) 00162 { 00163 // driverPath specified a jar file 00164 loadJarFile(dir.getAbsolutePath()); 00165 // after loading the jar file the class bytes are in the cache 00166 return (byte[]) htJarContents.get(resourceName); 00167 } 00168 00169 // the class is not yet cached we have to find the right jar file 00170 00171 // find all jar files in the directory 00172 String[] jarFiles = dir.list(new FilenameFilter() 00173 { 00174 public boolean accept(File dir, String name) 00175 { 00176 return name.endsWith(".jar"); 00177 } 00178 }); 00179 00180 if (jarFiles == null) 00181 throw new IOException("Invalid path " + dir); 00182 00183 // loop over jar files 00184 for (int i = 0; i < jarFiles.length; i++) 00185 { 00186 File file = new File(dir, jarFiles[i]); 00187 JarFile jarFile = new JarFile(file); 00188 00189 // we see whether the jar file contains the class we are looking for 00190 // no need in loading jar files as long as we don't really need the 00191 // content. 00192 if (jarFile.getEntry(resourceName) != null) 00193 { 00194 // we have found the right jar file and are loading it now 00195 loadJarFile(jarFile.getName()); 00196 00197 // after loading the jar file the class bytes are in the cache 00198 classBytes = (byte[]) htJarContents.get(resourceName); 00199 } 00200 } 00201 return classBytes; 00202 }

URL org.objectweb.cjdbc.controller.connection.DriverClassLoader.findResource String  name  )  [protected]
 

参照:
java.lang.ClassLoader#findResource(java.lang.String)
DriverClassLoader.java207 行で定義されています。
00208 { 00209 00210 // we try to locate the resource unjarred 00211 if (path.isDirectory()) 00212 { 00213 File searchResource = new File(path, name); 00214 if (searchResource.exists()) 00215 { 00216 try 00217 { 00218 return searchResource.toURL(); 00219 } 00220 catch (MalformedURLException mfe) 00221 { 00222 } 00223 } 00224 } 00225 else if (path.isFile()) 00226 { 00227 // try getting the resource from the file 00228 try 00229 { 00230 new JarFile(path); 00231 // convert the jar entry into URL format 00232 return new URL("jar:" + path.toURL() + "!/" + name); 00233 } 00234 catch (Exception e) 00235 { 00236 // we couldn't find resource in file 00237 return null; 00238 } 00239 } 00240 00241 //now we are checking the jar files 00242 try 00243 { 00244 // find all jar files in the directory 00245 String[] jarFiles = path.list(new FilenameFilter() 00246 { 00247 public boolean accept(File dir, String name) 00248 { 00249 return name.endsWith(".jar"); 00250 } 00251 }); 00252 // loop over jar files 00253 for (int i = 0; i < jarFiles.length; i++) 00254 { 00255 File file = new File(path, jarFiles[i]); 00256 JarFile jarFile = new JarFile(file); 00257 00258 // we see whether the jar file contains the resource we are looking for 00259 if (jarFile.getJarEntry(name) != null) 00260 { 00261 // convert the jar entry into URL format 00262 return new URL("jar:" + file.toURL() + "!/" + name); 00263 } 00264 } 00265 } 00266 catch (Exception e) 00267 { 00268 e.printStackTrace(); 00269 } 00270 return null; 00271 }

void org.objectweb.cjdbc.controller.connection.DriverClassLoader.loadJarFile String  jarFileName  )  throws IOException [private]
 

Load the contents of jar file in the cache

引数:
jarFileName name of the jar file we want to load
例外:
IOException 
DriverClassLoader.java294 行で定義されています。

参照元 org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClassInJarFile().

00295 { 00296 Hashtable htSizes = new Hashtable(); 00297 // extracts just sizes only. 00298 // For a reason I dont' understand not all files return the size in the loop 00299 // below (using ZipInputStream). So we cache the sizes here in case the loop 00300 // below does not give us the file size 00301 ZipFile zf = new ZipFile(jarFileName); 00302 Enumeration e = zf.entries(); 00303 while (e.hasMoreElements()) 00304 { 00305 ZipEntry ze = (ZipEntry) e.nextElement(); 00306 00307 htSizes.put(ze.getName(), new Integer((int) ze.getSize())); 00308 } 00309 zf.close(); 00310 00311 // extract resources and put them into the hashtable. 00312 FileInputStream fis = new FileInputStream(jarFileName); 00313 BufferedInputStream bis = new BufferedInputStream(fis); 00314 ZipInputStream zis = new ZipInputStream(bis); 00315 ZipEntry ze = null; 00316 while ((ze = zis.getNextEntry()) != null) 00317 { 00318 if (ze.isDirectory()) 00319 { 00320 continue; 00321 } 00322 00323 int size = (int) ze.getSize(); 00324 // -1 means unknown size. 00325 if (size == -1) 00326 { 00327 // that is the reason we have cached the file size above. 00328 size = ((Integer) htSizes.get(ze.getName())).intValue(); 00329 } 00330 00331 byte[] b = new byte[size]; 00332 int rb = 0; 00333 int chunk = 0; 00334 while ((size - rb) > 0) 00335 { 00336 chunk = zis.read(b, rb, size - rb); 00337 if (chunk == -1) 00338 { 00339 break; 00340 } 00341 rb += chunk; 00342 } 00343 00344 // add to internal resource hashtable 00345 htJarContents.put(ze.getName(), b); 00346 } 00347 00348 }


変数

Hashtable org.objectweb.cjdbc.controller.connection.DriverClassLoader.htJarContents = new Hashtable() [private]
 

we cache the contents of the jar files, as we don't want to have to read the file for every single class we are going to need DriverClassLoader.java136 行で定義されています。

参照元 org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClassInJarFile().

File org.objectweb.cjdbc.controller.connection.DriverClassLoader.path = null [private]
 

path on filesystem where the driver is located DriverClassLoader.java61 行で定義されています。

参照元 org.objectweb.cjdbc.controller.connection.DriverClassLoader.DriverClassLoader(), と org.objectweb.cjdbc.controller.connection.DriverClassLoader.findClass().


このクラスの説明は次のファイルから生成されました:
CJDBCversion1.0.4に対してTue Oct 12 15:16:39 2004に生成されました。 doxygen 1.3.8