1 10 11 package org.mule.modules.boot; 12 13 import java.io.BufferedReader ; 14 import java.io.InputStreamReader ; 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.FileReader ; 19 import java.io.IOException ; 20 import java.util.jar.JarEntry ; 21 import java.util.jar.JarFile ; 22 import java.util.jar.JarOutputStream ; 23 import java.util.zip.ZipEntry ; 24 25 29 public class LicenseHandler 30 { 31 private File muleHome; 32 private File muleBase; 33 private static int maxRowsToDisplay = 80; 34 35 public static String defaultLicenseType = "MuleSource Public License"; 36 public static String defaultLicenseVersion = "UNKNOWN"; 37 public static String ackJarName = "mule-local-install.jar"; 38 public static String ackLicenseName = "META-INF/mule/LICENSE.txt"; 39 public static String ackFileName = "META-INF/mule/license.props"; 40 public static String licenseFileName = "LICENSE.txt"; 41 42 48 public LicenseHandler(File muleHome) 49 { 50 this.muleHome = muleHome; 51 this.muleBase = muleHome; 52 } 53 54 61 public LicenseHandler(File muleHome, File muleBase) 62 { 63 this.muleHome = muleHome; 64 this.muleBase = muleBase; 65 } 66 67 75 public boolean getAcceptance() 76 { 77 String licenseType = ""; 78 String licenseVersion = ""; 79 80 try { 81 File licenseFile = new File (muleHome, licenseFileName); 82 File muleLib = new File (muleHome, "lib/mule"); 83 84 89 if (!licenseFile.exists() || !muleLib.exists()) 90 { 91 System.out.println("\nYour Mule installation seems to be incomplete. Please try downloading it again from http://mule.mulesource.org/wiki/display/MULE/Download and start again."); 92 return false; 93 } 94 95 System.out.println("\n\nPlease read over the following license agreement carefully:\n\n"); 96 97 int row = 1; 98 String input = ""; 99 100 BufferedReader is = new BufferedReader ( 101 new InputStreamReader (System.in)); 102 BufferedReader br = new BufferedReader ( 103 new FileReader (licenseFile)); 104 105 while (br.ready()) 106 { 107 String line = br.readLine(); 108 109 if (row == 1) licenseType = line; 110 if (row == 2 && line.startsWith("Version ")) 111 licenseVersion = line.substring(8); 112 113 if ((row % maxRowsToDisplay ) == 0) 114 { 115 System.out.print("\nHit return to continue ... "); 116 input = is.readLine(); 117 } 118 119 System.out.println(line); 120 row++; 121 } 122 123 124 System.out.print("\n\nDo you accept the terms and conditions of this license agreement [y/n]?"); 125 input = is.readLine(); 126 127 if (!input.toLowerCase().startsWith("y")) 128 { 129 System.out.println("\nSorry, until you accept the terms and conditions of this EULA, you won't be able to start Mule"); 130 return false; 131 } 132 } 133 catch (Exception e) 134 { 135 System.out.println("\nSorry, we encountered an error in processing your license agreement - please try again"); 136 return false; 137 } 138 139 if (licenseType.equals("")) licenseType = defaultLicenseType; 140 if (licenseVersion.equals("")) licenseVersion = defaultLicenseVersion; 141 142 try 143 { 144 saveLicenseAck(licenseType, licenseVersion); 145 } 146 catch (Exception e) 147 { 148 System.out.println(e); 149 } 150 151 return true; 152 } 153 154 182 public void saveLicenseAck(String licenseType, String licenseVersion) throws Exception 183 { 184 File muleLib = new File (muleHome, "lib/mule"); 185 File tempJar = createAckJarFile(licenseType, licenseVersion); 186 187 if (licenseJarExists()) 191 { 192 return; 193 } 194 195 if (!muleLib.canWrite()) { 196 if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile())) { 198 muleLib = new File (muleBase, "lib/user"); 199 200 if (!muleLib.canWrite()) 201 throw new Exception ("No write permissions for " + ackJarName + " in either MULE_HOME or MULE_BASE"); 202 203 } else { 204 throw new Exception ("No write permission for " + ackJarName); 205 } 206 } 207 208 File newJarFile = new File (muleLib, ackJarName); 211 if (newJarFile.exists()) 212 throw new Exception ("Unable to rename temporary jar to " + newJarFile.getAbsolutePath() + " a file with this name already exists!"); 213 214 if (!tempJar.renameTo(newJarFile)) 215 throw new Exception ("Unable to rename temporary jar to " + newJarFile.getAbsolutePath()); 216 } 217 218 222 223 public boolean licenseJarExists() 224 { 225 try 226 { 227 File muleLib = new File (muleHome, "lib/mule"); 228 File testJarFile = new File (muleLib, ackJarName); 229 JarFile jar = null; 230 231 if (testJarFile.exists()) 232 { 233 jar = new JarFile (testJarFile); 234 } 235 else 236 { 237 if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile())) 240 { 241 muleLib = new File (muleBase, "lib/user"); 242 testJarFile = new File (muleLib, ackJarName); 243 244 if (testJarFile.exists()) 245 { 246 jar = new JarFile (testJarFile); 247 } 248 } 249 } 250 251 if (jar != null) 252 { 253 ZipEntry entry = jar.getEntry(ackFileName); 254 255 if (entry != null) 258 { 259 return true; 260 } 261 } 262 } 263 catch (Exception e) 264 { 265 System.out.println("Unknown error checking for license jar: " + 266 e.toString()); 267 } 268 269 return false; 270 } 271 272 279 private File createAckJarFile(String licenseType, String licenseVersion) throws Exception 280 { 281 File tempJar = File.createTempFile(ackJarName, null); 282 File licenseFile = new File (muleHome, licenseFileName); 283 JarOutputStream newJar = null; 284 285 String ackData = "LicenseType=" + licenseType + "\n"; 286 ackData += "LicenseVersion=" + licenseVersion + "\n"; 287 ackData += "LicenseDate=" + (new java.util.Date ()).toString() + "\n"; 288 289 try { 290 newJar = new JarOutputStream (new FileOutputStream (tempJar)); 291 } catch (IOException ioe) { 292 throw new Exception ("Unable to create temporary jar file"); 293 } 294 295 byte buffer[] = new byte[1024]; 296 int bytesRead; 297 FileInputStream fis = null; 298 299 try 300 { 301 fis = new FileInputStream (licenseFile); 302 303 JarEntry entry = new JarEntry (ackFileName); 304 newJar.putNextEntry(entry); 305 newJar.write(ackData.getBytes(), 0, ackData.getBytes().length); 306 307 entry = new JarEntry (ackLicenseName); 308 newJar.putNextEntry(entry); 309 310 while ((bytesRead = fis.read(buffer)) != -1) 311 { 312 newJar.write(buffer, 0, bytesRead); 313 } 314 315 } catch (IOException ioe) { 316 throw new Exception ("Unable to write " + ackFileName + 317 " to temporary jar file"); 318 } finally { 319 if (fis != null) { 320 try { fis.close(); } catch (Exception e) { } 321 } 322 } 323 324 try { 325 newJar.close(); 326 } catch (IOException ioe) { 327 throw new Exception ("Unable to close temporary jar file"); 328 } 329 330 return tempJar; 331 } 332 333 } 334 335 | Popular Tags |