1 20 package org.jdesktop.jdic.packager.impl; 21 22 import java.io.File ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.DataOutputStream ; 26 import java.io.DataInputStream ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.util.StringTokenizer ; 31 import java.net.URL ; 32 33 import com.sun.deploy.net.proxy.DeployProxySelector; 34 import com.sun.deploy.net.proxy.StaticProxyManager; 35 import com.sun.deploy.services.ServiceManager; 36 import com.sun.deploy.services.PlatformType; 37 import com.sun.javaws.jnl.ExtensionDesc; 38 import com.sun.javaws.jnl.IconDesc; 39 import com.sun.javaws.jnl.InformationDesc; 40 import com.sun.javaws.jnl.JARDesc; 41 import com.sun.javaws.jnl.JREDesc; 42 import com.sun.javaws.jnl.LaunchDesc; 43 import com.sun.javaws.jnl.LaunchDescFactory; 44 import com.sun.javaws.jnl.PackageDesc; 45 import com.sun.javaws.jnl.PropertyDesc; 46 import com.sun.javaws.jnl.ResourceVisitor; 47 import com.sun.javaws.jnl.ResourcesDesc; 48 49 52 public class FileOperUtility { 53 static { 54 if (System.getProperty("os.name").indexOf("Windows") != -1) { 55 ServiceManager.setService(PlatformType.STANDALONE_TIGER_WIN32); 56 } else { 57 ServiceManager.setService(PlatformType.STANDALONE_TIGER_UNIX); 58 } 59 60 try { 61 DeployProxySelector.reset(); 62 } catch (Throwable t) { 63 StaticProxyManager.reset(); 64 } 65 } 66 67 75 public static void urlFile2LocalFile(URL url, URL codebase, 76 String localbase) throws IOException { 77 if (url == null || url.getFile() == null || url.getFile().length() <= 0) 78 return; 79 String relPath = getRelativePath(url.toString(), codebase.toString()); 80 File localFile = new File (localbase + File.separator + relPath); 81 copyRemoteFile(url, localFile); 82 } 83 84 private static void createLocalFile(File localFile) throws IOException { 85 if (localFile == null) 86 return; 87 88 if (!localFile.getParentFile().exists()) { 89 try { 90 if (!localFile.getParentFile().mkdirs()) { 91 throw new IOException ("Cannot make parent directory " + 92 "when trying to create local file"); 93 } 94 } catch (Exception e) { 95 throw new IOException ("Cannot make parent directory when " + 96 "trying to create local file: " + e.getMessage()); 97 } 98 } 99 100 if (localFile.exists()) { 101 try { 102 if (!localFile.delete()) { 103 throw new IOException ("Cannot delete original file when " + 104 "trying to create local file"); 105 } 106 } catch (Exception e) { 107 throw new IOException ("Cannot delete original file when " + 108 "trying to create local file: " + e.getMessage()); 109 } 110 } 111 112 try { 113 if (!localFile.createNewFile()) { 114 throw new IOException ("Cannot create new local file"); 115 } 116 } catch (Exception e) { 117 throw new IOException ("Cannot create new local file: " + 118 e.getMessage()); 119 } 120 } 121 122 private static void copyRemoteFile(URL url, File localFile) 123 throws IOException { 124 if (url == null || localFile == null) 125 return; 126 127 if (url.getFile() == "" || url.getFile() == null) 128 return; 129 130 if (localFile.isDirectory()) { 131 localFile = new File (localFile.getPath() + File.separator + 132 url.getFile()); 133 } 134 135 if (!localFile.exists()) { 136 createLocalFile(localFile); 137 } 138 139 DataInputStream inStream = new DataInputStream (url.openStream()); 140 DataOutputStream outStream = new DataOutputStream ( 141 new FileOutputStream (localFile)); 142 143 copyStream(inStream, outStream); 144 } 145 146 private static void copyStream(InputStream inStream, 147 OutputStream outStream) throws IOException { 148 int readbytes = 0; 149 150 try { 151 do { 152 byte[] buffer = new byte[512]; 153 readbytes = inStream.read(buffer, 0, 512); 154 if (readbytes <= 0) { 155 break; 156 } 157 158 outStream.write(buffer, 0, readbytes); 159 outStream.flush(); 160 } while (true); 161 } catch (IOException ioE) { 162 ioE.printStackTrace(); 163 } finally { 164 if (inStream != null) { 165 inStream.close(); 166 } 167 if (outStream != null) { 168 outStream.close(); 169 } 170 } 171 } 172 173 180 public static String getRelativePath(String path, String base) { 181 if (path == null || base == null) 182 return null; 183 if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { 185 path = path.toLowerCase(); 186 base = base.toLowerCase(); 187 } 188 if (path.lastIndexOf(base) < 0) { 189 int index = path.lastIndexOf("/"); 190 if (index < 0) { 191 return path; 192 } else { 193 return path.substring(index + 1); 194 } 195 } 196 String relPath = path.substring(path.lastIndexOf(base) + base.length()); 197 StringTokenizer st = new StringTokenizer (relPath, "/", false); 198 String nativeRelPath = ""; 199 while (st.hasMoreTokens()) { 200 if (nativeRelPath.length() == 0) { 201 nativeRelPath = st.nextToken(); 202 } else { 203 nativeRelPath += File.separator + st.nextToken(); 204 } 205 } 206 207 return nativeRelPath; 208 } 209 210 217 public static void copyLocalFile(String sourceFileName, String destFileName) 218 throws IOException { 219 File sourceFile = new File (sourceFileName); 220 File destFile = new File (destFileName); 221 222 if (!sourceFile.exists()) 223 return; 224 225 226 if (destFile.exists() && sourceFile.getPath(). 227 equals(destFile.getPath())) { 228 return; 229 } 230 231 if (sourceFile.isFile()) { 232 if (destFile.isDirectory()) { 233 destFile = new File (destFile.getPath() + File.separator + 234 sourceFile.getName()); 235 } 236 createLocalFile(destFile); 237 238 DataInputStream inStream = new DataInputStream ( 239 new FileInputStream (sourceFile)); 240 DataOutputStream outStream = new DataOutputStream ( 241 new FileOutputStream (destFile)); 242 243 copyStream(inStream, outStream); 244 } else if (sourceFile.isDirectory()) { 245 if (destFile.exists() && !destFile.isDirectory()) { 246 throw new IOException ("cannot copy directory because there " + 247 "is a file with the same name in destination: " + 248 destFile.getPath()); 249 } 250 251 try { 252 if (!destFile.exists() && !destFile.mkdirs()) { 253 throw new IOException ("cannot create local directory: " + 254 destFile.getPath()); 255 } 256 257 File [] filelist = sourceFile.listFiles(); 258 if (filelist == null) 259 return; 260 for (int i = 0; i < filelist.length; i++) { 261 if (filelist[i].getPath().equals(destFile.getPath())) { 262 return; 263 } 264 String destName = destFileName + File.separator + 265 getRelativePath(filelist[i].getCanonicalPath(), 266 sourceFile.getCanonicalPath()); 267 copyLocalFile(filelist[i].getCanonicalPath(), destName); 268 } 269 } catch (Exception e) { 270 throw new IOException ("Cannot copy local directory: " + 271 e.getMessage()); 272 } 273 } 274 } 275 276 282 public static void deleteDirTree(File dir) throws IOException { 283 if (dir == null || !dir.isDirectory()) 284 return; 285 File [] filelist = dir.listFiles(); 286 try { 287 if (filelist == null) { 288 if (!dir.delete()) { 289 throw new IOException ("Cannot delete directory: " + 290 dir.getPath()); 291 } 292 return; 293 } 294 for (int i = 0; i < filelist.length; i++) { 295 if (filelist[i].isFile()) { 296 if (!filelist[i].delete()) { 297 throw new IOException ("Cannot delete file: " + 298 filelist[i].getPath()); 299 } 300 } else if (filelist[i].isDirectory()) { 301 deleteDirTree(filelist[i]); 302 } 303 } 304 } catch (Exception e) { 305 throw new IOException ("Failed to delete directory: " + 306 e.getMessage()); 307 } 308 } 309 310 315 public static String createUniqueTmpDir() 316 throws IOException { 317 String tempDirPath = null; 319 try { 320 File sysTempDir = new File (System.getProperty("java.io.tmpdir")); 321 322 File tempFile = File.createTempFile("jnlp", "jnlp", sysTempDir); 323 324 tempDirPath = tempFile.getPath(); 325 tempFile.delete(); 326 tempFile = new File (tempDirPath); 327 tempFile.mkdirs(); 328 tempFile.deleteOnExit(); 329 } catch (IOException e) { 330 throw new IOException ( 331 "Failed to create a local temp directory: " 332 + tempDirPath); 333 } 334 return tempDirPath + File.separator; 335 } 336 337 345 protected static URL getRemoteResource(URL jnlp, String localBase) 346 throws IOException { 347 URL localJnlpUrl = null; 348 URL codebase = null; 349 try { 350 LaunchDesc laDesc = LaunchDescFactory.buildDescriptor(jnlp); 351 codebase = laDesc.getCodebase(); 352 String jnlpName = jnlp.getFile().substring( 353 jnlp.getFile().lastIndexOf("/") + 1); 354 File localJnlpFile = new File (localBase + 355 File.separator + jnlpName); 356 copyRemoteFile(jnlp, localJnlpFile); 357 358 if (localJnlpFile == null || !localJnlpFile.exists()) { 359 throw new IOException ("Cannot copy remote jnlp file to " + 360 "local"); 361 } 362 localJnlpUrl = localJnlpFile.toURL(); 363 364 InformationDesc infoDesc = laDesc.getInformation(); 365 IconDesc[] iconArray = infoDesc.getIcons(); 366 for (int i = 0; i < iconArray.length; i++) { 367 URL iconURL = iconArray[i].getLocation(); 368 urlFile2LocalFile(iconURL, codebase, localBase); 369 } 370 ResourcesDesc reDesc = laDesc.getResources(); 371 reDesc.visit( 372 new JDICPackagerResourceCopyVisitor(codebase, localBase)); 373 } catch (Exception e) { 374 throw new IOException ("Exception when geting remote resource: " + 375 e.getMessage()); 376 } 377 378 return localJnlpUrl; 379 } 380 381 386 public static boolean isDirectoryReadable(File dirPath) { 387 if (dirPath.isDirectory()) { 388 if (dirPath.canRead()) { 389 return true; 390 } 391 } 392 return false; 393 } 394 395 400 public static boolean isDirectoryWritable(File dirPath) { 401 if (dirPath.isDirectory()) { 402 if (dirPath.canWrite()) { 403 return true; 404 } 405 } 406 return false; 407 } 408 409 415 public static boolean isFileReadable(File filePath) { 416 if (filePath.isFile()) { 417 if (filePath.canRead()) { 418 return true; 419 } 420 } 421 return false; 422 } 423 424 430 public static String getFileNameWithoutExt(File filePath) { 431 if (filePath == null) 432 return null; 433 434 String fileName = filePath.getName(); 435 if (fileName == null) 436 return null; 437 438 return (fileName.substring(0, fileName.lastIndexOf("."))); 439 } 440 } 441 442 445 446 class JDICPackagerResourceCopyVisitor implements ResourceVisitor { 447 private URL codebase = null; 448 private String localBase = null; 449 450 public JDICPackagerResourceCopyVisitor(URL incodebase, String inlocalbase) { 451 codebase = incodebase; 452 localBase = inlocalbase; 453 } 454 public void visitJARDesc(JARDesc jad) { 455 try { 456 FileOperUtility.urlFile2LocalFile(jad.getLocation(), 457 codebase, localBase); 458 } catch (IOException ioE) { 459 ioE.printStackTrace(); 460 } 461 } 462 463 public void visitExtensionDesc(ExtensionDesc ed) { 464 try { 465 469 FileOperUtility.getRemoteResource(ed.getLocation(), 470 localBase); 471 } catch (IOException ioE) { 472 ioE.printStackTrace(); 473 } 474 } 475 476 public void visitPropertyDesc(PropertyDesc prd) {} 477 478 public void visitJREDesc(JREDesc jrd) {} 479 480 public void visitPackageDesc(PackageDesc pad) {} 481 } 482 | Popular Tags |