/* * (c) Copyright Rosella Software, 2005-. All rights reserved. * * Rosella Software * http://www.roselladb.com * Sydney, Australia */ // package your.package.name; import java.io.*; import java.lang.*; public class SwtJdbcInstaller { /** * Checks if a JAR file with the name of the 1st argument * is already installed into the Java Runtime running the * application. For example, SWT package has a "swt.jar" file. * You can check whether SWT package is installed or not as * follows; * * if (!isJarInstalled("swt.jar")) { * // install SWT here! * } */ static public boolean isJarInstalled(String jarfilename) { SwtJdbcInstaller sji = new SwtJdbcInstaller(); ClassLoader cl = sji.getClass().getClassLoader(); String extdirs = System.getProperty("java.ext.dirs"); File f = new File(extdirs, jarfilename); return f.exists(); } /** * This install a Jar file into the JVM's jar directory. * Jar files can be JDBC drivers and SWT package or your own * packages! This assumes that your main JAR file contains * the JAR file to install to JVM directory! * The 1st argument is the path that the jar to be installed * is stored in the main JAR file. If stored at root level, * just use "". The 2nd argument is the name of the file. * For example, to install SWT package file located at "windows"; * * installJarFile("windows", "swt.jar"); */ static public void installJarFile(String pathname, String jarfilename) { SwtJdbcInstaller sji = new SwtJdbcInstaller(); ClassLoader cl = sji.getClass().getClassLoader(); String extdirs = System.getProperty("java.ext.dirs"); String st = null; if (pathname==null || pathname.equals("")) { st = jarfilename; } else { if (pathname.endsWith("/")) { st = pathname+jarfilename; } else { st = pathname+"/"+jarfilename; } } InputStream stream = null; if (cl!=null) { stream = cl.getResourceAsStream(st); } else { stream = ClassLoader.getSystemResourceAsStream(st); } copyFile(stream, new File(extdirs, jarfilename)); } /** * Same as the previous one except that this accept input * file instead. */ static public void installJarFile(File in, String jarfilename) { String extdirs = System.getProperty("java.ext.dirs"); try { InputStream stream = new FileInputStream(in);; copyFile(stream, new File(extdirs, jarfilename)); } catch (Throwable t) { t.printStackTrace(); } } /** * This install a DLL file into the JVM's DLL directory. * DLL files can be JDBC drivers and SWT package or your own * native packages! This assumes that your main JAR file contains * the DLL file to install to JVM directory! * The 1st argument is the path that the DLL to be installed * is stored in the main JAR file. If stored at root level, * just use "". The 2nd argument is the name of the file. * For example, to install SWT package file located at "windows"; * * installDllFile("windows", "swt-aswt-win32-3123.DLL"); * installDllFile("windows", "swt-gdip-win32-3123.DLL"); * installDllFile("windows", "swt-win32-3123.DLL); */ static public void installDllFile(String pathname, String dllfilename) { SwtJdbcInstaller sji = new SwtJdbcInstaller(); ClassLoader cl = sji.getClass().getClassLoader(); String javahome = System.getProperty("java.home"); String dllhome = javahome+File.separator+"bin"; String st = null; if (pathname==null || pathname.equals("")) { st = dllfilename; } else { if (pathname.endsWith("/")) { st = pathname+dllfilename; } else { st = pathname+"/"+dllfilename; } } InputStream stream = null; if (cl!=null) { stream = cl.getResourceAsStream(st); } else { stream = ClassLoader.getSystemResourceAsStream(st); } copyFile(stream, new File(dllhome, dllfilename)); } /** * Same as the previous one except that this accept input * file instead. */ static public void installDllFile(File in, String dllfilename) { String javahome = System.getProperty("java.home"); String dllhome = javahome+File.separator+"bin"; try { InputStream stream = new FileInputStream(in);; copyFile(stream, new File(dllhome, dllfilename)); } catch (Throwable t) { t.printStackTrace(); } } /** * Copy files from input stream to output file. */ private static void copyFile(InputStream input, File output0) { int length = 512; // buffer block size int read_length; long sleeping = 1; byte[] buffer = new byte[length]; // make sure that existing files deleted on Windows! while (true) { if (!output0.exists()) { break; } try {Thread.sleep(sleeping);} catch (Exception e) {}; sleeping *= 2; output0.delete(); } FileOutputStream output = null; try { output = new FileOutputStream(output0); while (true) { read_length = input.read(buffer, 0, length); if (read_length <= 0) { break; } output.write(buffer, 0, read_length); } } catch (Throwable e) { e.printStackTrace(); } try {input.close();} catch (Throwable t) {}; try {output.close();} catch (Throwable t) {}; } }