1 17 package org.apache.servicemix.jbi.util; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 28 public class FileVersionUtil{ 29 private static final Log log=LogFactory.getLog(FileVersionUtil.class); 30 private static final String VERSION_PREFIX="version_"; 31 private static final String [] RESERVED= { VERSION_PREFIX }; 32 33 39 public static int getLatestVersionNumber(File rootDirectory) { 40 int result=-1; 41 if(isVersioned(rootDirectory)){ 42 File [] files=rootDirectory.listFiles(); 43 for(int i=0;i<files.length;i++){ 44 int version=getVersionNumber(files[i].getName()); 45 if(version>result){ 46 result=version; 47 } 48 } 49 } 50 return result; 51 } 52 53 60 public static File getLatestVersionDirectory(File rootDirectory) { 61 File result=null; 62 int highestVersion=-1; 63 if(rootDirectory != null && isVersioned(rootDirectory)){ 64 File [] files=rootDirectory.listFiles(); 65 for(int i=0;i<files.length;i++){ 66 int version=getVersionNumber(files[i].getName()); 67 if(version>highestVersion){ 68 highestVersion=version; 69 result=files[i]; 70 } 71 } 72 } 73 return result; 74 } 75 76 82 public static File createNewVersionDirectory(File rootDirectory) throws IOException { 83 File result= getNewVersionDirectory(rootDirectory); 84 if(!FileUtil.buildDirectory(result)){ 85 throw new IOException ("Failed to build version directory: "+result); 86 } 87 return result; 88 } 89 90 97 public static File getNewVersionDirectory(File rootDirectory) throws IOException { 98 File result=null; 99 if(FileUtil.buildDirectory(rootDirectory)){ 100 String versionDirectoryName=VERSION_PREFIX; 101 if(isVersioned(rootDirectory)){ 102 int versionNumber=getLatestVersionNumber(rootDirectory); 103 versionNumber=versionNumber>0?versionNumber+1:1; 104 versionDirectoryName+=versionNumber; 105 }else{ 106 versionDirectoryName+=1; 107 } 108 result=FileUtil.getDirectoryPath(rootDirectory,versionDirectoryName); 109 }else{ 110 throw new IOException ("Cannot build parent directory: "+rootDirectory); 111 } 112 return result; 113 } 114 115 121 public static void initializeVersionDirectory(File rootDirectory) throws IOException { 122 if(!isVersioned(rootDirectory)){ 123 File newRoot=createNewVersionDirectory(rootDirectory); 124 File [] files=rootDirectory.listFiles(); 125 for(int i=0;i<files.length;i++){ 126 if(!isReserved(files[i].getName())){ 127 log.info(rootDirectory.getPath()+": moving non-versioned file "+files[i].getName()+" to " 128 +newRoot.getName()); 129 File moveTo = FileUtil.getDirectoryPath(newRoot, files[i].getName()); 130 FileUtil.moveFile(files[i],moveTo); 131 } 132 } 133 } 134 } 135 136 private static boolean isVersioned(File rootDirectory) { 137 boolean result=false; 138 if(rootDirectory.exists()&&rootDirectory.isDirectory()){ 139 File [] files=rootDirectory.listFiles(); 140 result=files==null||files.length==0; 141 if(!result){ 142 for(int i=0;i<files.length;i++){ 143 if(isReserved(files[i].getName())){ 144 result=true; 145 break; 146 } 147 } 148 } 149 } 150 return result; 151 } 152 153 private static boolean isReserved(String name){ 154 boolean result=false; 155 if(name!=null){ 156 for(int i=0;i<RESERVED.length;i++){ 157 if(name.startsWith(RESERVED[i])){ 158 result=true; 159 break; 160 } 161 } 162 } 163 return result; 164 } 165 166 private static int getVersionNumber(String name){ 167 int result=-1; 168 if(name!=null&&name.startsWith(VERSION_PREFIX)){ 169 String number=name.substring(VERSION_PREFIX.length()); 170 result=Integer.parseInt(number); 171 } 172 return result; 173 } 174 } | Popular Tags |