1 package net.sourceforge.javalogging.installer; 2 3 import java.awt.*; 4 import java.io.*; 5 import java.net.*; 6 import java.util.Enumeration ; 7 import java.util.StringTokenizer ; 8 import java.util.zip.*; 9 import javax.swing.*; 10 11 public class Installer { 12 13 private static ProgressDialog progress = new ProgressDialog(); 14 15 public static void main( String [] args ) { 16 17 progress.setVisible( true ); 18 19 String step = null; 20 try { 21 22 if ( checkJavaVersion() ) { 24 25 if ( lumberjackAlreadyInstalled() ) { 27 int answer = JOptionPane.showConfirmDialog( progress, 28 "Lumberjack is already installed. Overwrite?", 29 "Overwrite current installation?", 30 JOptionPane.OK_CANCEL_OPTION ); 31 if ( answer != JOptionPane.OK_OPTION ) { 32 JOptionPane.showMessageDialog( progress, "Installation cancelled..." ); 33 System.exit( 1 ); 34 } 35 progress.addMessage( "Overwriting existing installation..." ); 36 progress.mark( progress.CHECK_EXISTING ); 37 try { 38 removeExistingLumberjack(); 39 } 40 catch ( Exception e ) { 41 step = "Problems removing existing Lumberjack"; 42 throw e; 43 } 44 } 45 else { 46 progress.mark( progress.CHECK_EXISTING ); 47 } 48 49 try { 50 installClassFiles(); 51 progress.mark( progress.INSTALL_CLASSES ); 52 } 53 catch ( Exception e ) { 54 step = "Problems installing class files."; 55 throw e; 56 } 57 58 boolean installProperties = true; 59 if ( loggingPropertiesAlreadyInstalled() ) { 60 int answer = JOptionPane.showConfirmDialog( progress, 61 "'logging.properties' exists. Overwrite?", 62 "Overwrite current logging.properties?", 63 JOptionPane.OK_CANCEL_OPTION ); 64 if ( answer != JOptionPane.OK_OPTION ) { 65 progress.addMessage( "Not installing 'logging.properties'..." ); 66 installProperties = false; 67 } 68 else { 69 progress.addMessage( "Overwriting existing 'logging.properties'..." ); 70 } 71 } 72 if ( installProperties ) { 73 try { 74 installPropertiesFile(); 75 progress.mark( progress.INSTALL_PROPERTIES ); 76 } 77 catch ( Exception e ) { 78 step = "Problems installing logging.properties."; 79 throw e; 80 } 81 } 82 progress.enableClose( "All done!", 0 ); 83 } 84 } 85 catch ( Exception e ) { 86 progress.addMessage( step ); 87 StringWriter sw = new StringWriter(); 88 PrintWriter pw = new PrintWriter( sw ); 89 e.printStackTrace( pw ); 90 progress.enableClose( sw.getBuffer().toString(), 1 ); 91 } 92 } 93 94 private static boolean checkJavaVersion() { 95 String version = System.getProperty( "java.version" ); 96 if ( ! version.startsWith( "1.2" ) 98 && ! version.startsWith( "1.3" ) ) { 99 if ( version.charAt( 0 ) == '1' 100 && version.charAt( 1 ) == '.' 101 && version.charAt( 2 ) >= '4' ) { 102 progress.enableClose( "The Java Logging API is in JDK 1.4 and beyond...", 1 ); 103 return false; 104 } 105 else if ( version.startsWith( "1.1" ) ) { 106 progress.enableClose( "Lumberjack does not work with JDK 1.1.x...", 1 ); 107 return false; 108 } 109 else { 110 progress.enableClose( "Unable to parse version info. Sorry!", 1 ); 111 return false; 112 } 113 } 114 return true; 115 } 116 117 private static boolean lumberjackAlreadyInstalled() { 118 String javaHome = System.getProperty( "java.home" ); 119 File classes = new File( javaHome + File.separator + "classes" ); 120 if ( classes.exists() ) { 121 File logging = new File( classes.getAbsolutePath() 122 + File.separator + "java" 123 + File.separator + "util" 124 + File.separator + "logging" ); 125 if ( logging.exists() ) { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 private static boolean loggingPropertiesAlreadyInstalled() { 133 String javaHome = System.getProperty( "java.home" ); 134 File loggingDotProperties = new File( javaHome + File.separator + "lib" + File.separator + "logging.properties" ); 135 if ( loggingDotProperties.exists() ) { 136 return true; 137 } 138 return false; 139 } 140 141 private static void removeExistingLumberjack() throws Exception { 142 String javaHome = System.getProperty( "java.home" ); 143 File logging = new File( javaHome 144 + File.separator + "classes" 145 + File.separator + "java" 146 + File.separator + "util" 147 + File.separator + "logging" ); 148 File[] allFiles = logging.listFiles(); 149 for ( int index = 0 ; index < allFiles.length ; index++ ) { 150 File current = allFiles[ index ]; 151 if ( ! current.delete() ) { 152 throw new IllegalArgumentException ( "Unable to delete '" 153 + current.getAbsolutePath() + "'!" ); 154 } 155 } 156 } 157 158 private static void installClassFiles() throws Exception { 159 163 String javaHome = System.getProperty( "java.home" ); 166 StringBuffer loggingDirName = new StringBuffer ( javaHome 167 + File.separator + "classes" 168 + File.separator + "java" 169 + File.separator + "util" 170 + File.separator + "logging" ); 171 File loggingDir = new File( loggingDirName.toString() ); 172 if ( ! loggingDir.exists() && ! loggingDir.mkdirs() ) { 173 String msg = "Unable to assure output directory '" + loggingDir.getAbsolutePath() + "'!'"; 174 progress.addMessage( msg ); 175 throw new FileNotFoundException( msg ); 176 } 177 178 ZipFile z = new ZipFile( System.getProperty( "java.class.path" ) ); 179 Enumeration e = z.entries(); 180 while ( e.hasMoreElements() ) { 181 ZipEntry entry = (ZipEntry) e.nextElement(); 182 String name = entry.getName(); 183 if ( name.startsWith( "java/util/logging" ) 184 && name.length() > "java/util/logging/".length() ) { 185 186 int entrySize = (int) entry.getSize(); 188 byte[] contents = new byte[ entrySize ]; 189 InputStream in = z.getInputStream( entry ); 190 int bytesRead = 0; 191 while ( bytesRead < entrySize ) { 192 bytesRead += in.read( contents, bytesRead, entrySize - bytesRead ); 193 } 194 in.close(); 195 196 String fileName = name.substring( name.lastIndexOf( '/' ) + 1 ); 197 File outputFile = new File( loggingDirName.toString() + File.separator + fileName ); 198 OutputStream out = new FileOutputStream( outputFile ); 199 out.write( contents, 0, bytesRead ); 200 out.close(); 201 202 progress.addMessage( "Installed '" + name + "' (" + entry.getSize() + " bytes)..." ); 203 } 204 } 205 } 206 207 private static void installPropertiesFile() throws Exception { 208 ZipFile z = new ZipFile( System.getProperty( "java.class.path" ) ); 209 ZipEntry entry = (ZipEntry) z.getEntry( "net/sourceforge/javalogging/logging.properties" ); 210 211 int entrySize = (int) entry.getSize(); 213 byte[] contents = new byte[ entrySize ]; 214 InputStream in = z.getInputStream( entry ); 215 int bytesRead = 0; 216 while ( bytesRead < entrySize ) { 217 bytesRead += in.read( contents, bytesRead, entrySize - bytesRead ); 218 } 219 in.close(); 220 221 String javaHome = System.getProperty( "java.home" ); 222 StringBuffer outputFileName = new StringBuffer ( javaHome 223 + File.separator + "lib" 224 + File.separator + "logging.properties" ); 225 226 File outputFile = new File( outputFileName.toString() ); 227 OutputStream out = new FileOutputStream( outputFile ); 228 out.write( contents ); 229 out.close(); 230 231 progress.addMessage( "Installed '" + outputFile.getAbsolutePath() + "' (" + entry.getSize() + " bytes)..." ); 232 233 } 234 235 } | Popular Tags |