Loads the Static UI content files from the classpath JAR to the configured static root directory @param the name of the content directory to write the content to
(String contentDirectory)
| 708 | * @param the name of the content directory to write the content to |
| 709 | */ |
| 710 | private static void loadContent(String contentDirectory) { |
| 711 | File gpDir = new File(contentDirectory); |
| 712 | final long startTime = System.currentTimeMillis(); |
| 713 | int filesLoaded = 0; |
| 714 | int fileFailures = 0; |
| 715 | int fileNewer = 0; |
| 716 | long bytesLoaded = 0; |
| 717 | String codeSourcePath = TSDMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
| 718 | File file = new File(codeSourcePath); |
| 719 | if( codeSourcePath.endsWith(".jar") && file.exists() && file.canRead() ) { |
| 720 | JarFile jar = null; |
| 721 | ChannelBuffer contentBuffer = ChannelBuffers.dynamicBuffer(300000); |
| 722 | try { |
| 723 | jar = new JarFile(file); |
| 724 | final Enumeration<JarEntry> entries = jar.entries(); |
| 725 | while(entries.hasMoreElements()) { |
| 726 | JarEntry entry = entries.nextElement(); |
| 727 | final String name = entry.getName(); |
| 728 | if (name.startsWith(CONTENT_PREFIX + "/")) { |
| 729 | final int contentSize = (int)entry.getSize(); |
| 730 | final long contentTime = entry.getTime(); |
| 731 | if(entry.isDirectory()) { |
| 732 | new File(gpDir, name).mkdirs(); |
| 733 | continue; |
| 734 | } |
| 735 | File contentFile = new File(gpDir, name.replace(CONTENT_PREFIX + "/", "")); |
| 736 | if( !contentFile.getParentFile().exists() ) { |
| 737 | contentFile.getParentFile().mkdirs(); |
| 738 | } |
| 739 | if( contentFile.exists() ) { |
| 740 | if( contentFile.lastModified() >= contentTime ) { |
| 741 | log.debug("File in directory was newer [{}]", name); |
| 742 | fileNewer++; |
| 743 | continue; |
| 744 | } |
| 745 | contentFile.delete(); |
| 746 | } |
| 747 | log.debug("Writing content file [{}]", contentFile ); |
| 748 | contentFile.createNewFile(); |
| 749 | if( !contentFile.canWrite() ) { |
| 750 | log.warn("Content file [{}] not writable", contentFile); |
| 751 | fileFailures++; |
| 752 | continue; |
| 753 | } |
| 754 | FileOutputStream fos = null; |
| 755 | InputStream jis = null; |
| 756 | try { |
| 757 | fos = new FileOutputStream(contentFile); |
| 758 | jis = jar.getInputStream(entry); |
| 759 | contentBuffer.writeBytes(jis, contentSize); |
| 760 | contentBuffer.readBytes(fos, contentSize); |
| 761 | fos.flush(); |
| 762 | jis.close(); jis = null; |
| 763 | fos.close(); fos = null; |
| 764 | filesLoaded++; |
| 765 | bytesLoaded += contentSize; |
| 766 | log.debug("Wrote content file [{}] + with size [{}]", contentFile, contentSize ); |
| 767 | } finally { |