KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > util > FileVersionUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.util;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 /**
24  * Supports a simple versioning scheme using the file system
25  *
26  * @version $Revision: 426415 $
27  */

28 public class FileVersionUtil{
29     private static final Log log=LogFactory.getLog(FileVersionUtil.class);
30     private static final String JavaDoc VERSION_PREFIX="version_";
31     private static final String JavaDoc[] RESERVED= { VERSION_PREFIX };
32
33     /**
34      * Get the latest version number for a directory
35      *
36      * @param rootDirectory
37      * @return the version number
38      */

39     public static int getLatestVersionNumber(File JavaDoc rootDirectory) {
40         int result=-1;
41         if(isVersioned(rootDirectory)){
42             File JavaDoc[] 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     /**
54      * Get the latest versioned directory
55      *
56      * @param rootDirectory
57      * @return the directory
58      * @throws IOException
59      */

60     public static File JavaDoc getLatestVersionDirectory(File JavaDoc rootDirectory) {
61         File JavaDoc result=null;
62         int highestVersion=-1;
63         if(rootDirectory != null && isVersioned(rootDirectory)){
64             File JavaDoc[] 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     /**
77      * Create a new version directory
78      * @param rootDirectory
79      * @return the created version directory
80      * @throws IOException
81      */

82     public static File JavaDoc createNewVersionDirectory(File JavaDoc rootDirectory) throws IOException JavaDoc{
83         File JavaDoc result= getNewVersionDirectory(rootDirectory);
84         if(!FileUtil.buildDirectory(result)){
85             throw new IOException JavaDoc("Failed to build version directory: "+result);
86         }
87         return result;
88     }
89
90     /**
91      * get's the new version file - without creating the directory
92      *
93      * @param rootDirectory
94      * @return the version directory
95      * @throws IOException
96      */

97     public static File JavaDoc getNewVersionDirectory(File JavaDoc rootDirectory) throws IOException JavaDoc{
98         File JavaDoc result=null;
99         if(FileUtil.buildDirectory(rootDirectory)){
100             String JavaDoc 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 JavaDoc("Cannot build parent directory: "+rootDirectory);
111         }
112         return result;
113     }
114
115     /**
116      * Used to move non-version files/directories to versioned
117      *
118      * @param rootDirectory
119      * @throws IOException
120      */

121     public static void initializeVersionDirectory(File JavaDoc rootDirectory) throws IOException JavaDoc{
122         if(!isVersioned(rootDirectory)){
123             File JavaDoc newRoot=createNewVersionDirectory(rootDirectory);
124             File JavaDoc[] 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 JavaDoc moveTo = FileUtil.getDirectoryPath(newRoot, files[i].getName());
130                     FileUtil.moveFile(files[i],moveTo);
131                 }
132             }
133         }
134     }
135
136     private static boolean isVersioned(File JavaDoc rootDirectory) {
137         boolean result=false;
138         if(rootDirectory.exists()&&rootDirectory.isDirectory()){
139             File JavaDoc[] 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 JavaDoc 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 JavaDoc name){
167         int result=-1;
168         if(name!=null&&name.startsWith(VERSION_PREFIX)){
169             String JavaDoc number=name.substring(VERSION_PREFIX.length());
170             result=Integer.parseInt(number);
171         }
172         return result;
173     }
174 }
Popular Tags