001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.util;
018:
019: import java.io.File;
020: import java.io.IOException;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Supports a simple versioning scheme using the file system
027: *
028: * @version $Revision: 564607 $
029: */
030: public final class FileVersionUtil {
031:
032: private static final Log LOG = LogFactory
033: .getLog(FileVersionUtil.class);
034:
035: private static final String VERSION_PREFIX = "version_";
036:
037: private static final String[] RESERVED = { VERSION_PREFIX };
038:
039: private FileVersionUtil() {
040: }
041:
042: /**
043: * Get the latest version number for a directory
044: *
045: * @param rootDirectory
046: * @return the version number
047: */
048: public static int getLatestVersionNumber(File rootDirectory) {
049: int result = -1;
050: if (isVersioned(rootDirectory)) {
051: File[] files = rootDirectory.listFiles();
052: for (int i = 0; i < files.length; i++) {
053: int version = getVersionNumber(files[i].getName());
054: if (version > result) {
055: result = version;
056: }
057: }
058: }
059: return result;
060: }
061:
062: /**
063: * Get the latest versioned directory
064: *
065: * @param rootDirectory
066: * @return the directory
067: * @throws IOException
068: */
069: public static File getLatestVersionDirectory(File rootDirectory) {
070: File result = null;
071: int highestVersion = -1;
072: if (rootDirectory != null && isVersioned(rootDirectory)) {
073: File[] files = rootDirectory.listFiles();
074: for (int i = 0; i < files.length; i++) {
075: int version = getVersionNumber(files[i].getName());
076: if (version > highestVersion) {
077: highestVersion = version;
078: result = files[i];
079: }
080: }
081: }
082: return result;
083: }
084:
085: /**
086: * Create a new version directory
087: *
088: * @param rootDirectory
089: * @return the created version directory
090: * @throws IOException
091: */
092: public static File createNewVersionDirectory(File rootDirectory)
093: throws IOException {
094: File result = getNewVersionDirectory(rootDirectory);
095: if (!FileUtil.buildDirectory(result)) {
096: throw new IOException("Failed to build version directory: "
097: + result);
098: }
099: return result;
100: }
101:
102: /**
103: * get's the new version file - without creating the directory
104: *
105: * @param rootDirectory
106: * @return the version directory
107: * @throws IOException
108: */
109: public static File getNewVersionDirectory(File rootDirectory)
110: throws IOException {
111: File result = null;
112: if (FileUtil.buildDirectory(rootDirectory)) {
113: String versionDirectoryName = VERSION_PREFIX;
114: if (isVersioned(rootDirectory)) {
115: int versionNumber = getLatestVersionNumber(rootDirectory);
116: versionNumber = versionNumber > 0 ? versionNumber + 1
117: : 1;
118: versionDirectoryName += versionNumber;
119: } else {
120: versionDirectoryName += 1;
121: }
122: result = FileUtil.getDirectoryPath(rootDirectory,
123: versionDirectoryName);
124: } else {
125: throw new IOException("Cannot build parent directory: "
126: + rootDirectory);
127: }
128: return result;
129: }
130:
131: /**
132: * Used to move non-version files/directories to versioned
133: *
134: * @param rootDirectory
135: * @throws IOException
136: */
137: public static void initializeVersionDirectory(File rootDirectory)
138: throws IOException {
139: if (!isVersioned(rootDirectory)) {
140: File newRoot = createNewVersionDirectory(rootDirectory);
141: File[] files = rootDirectory.listFiles();
142: for (int i = 0; i < files.length; i++) {
143: if (!isReserved(files[i].getName())) {
144: LOG.info(rootDirectory.getPath()
145: + ": moving non-versioned file "
146: + files[i].getName() + " to "
147: + newRoot.getName());
148: File moveTo = FileUtil.getDirectoryPath(newRoot,
149: files[i].getName());
150: FileUtil.moveFile(files[i], moveTo);
151: }
152: }
153: }
154: }
155:
156: private static boolean isVersioned(File rootDirectory) {
157: boolean result = false;
158: if (rootDirectory.exists() && rootDirectory.isDirectory()) {
159: File[] files = rootDirectory.listFiles();
160: result = files == null || files.length == 0;
161: if (!result) {
162: for (int i = 0; i < files.length; i++) {
163: if (isReserved(files[i].getName())) {
164: result = true;
165: break;
166: }
167: }
168: }
169: }
170: return result;
171: }
172:
173: private static boolean isReserved(String name) {
174: boolean result = false;
175: if (name != null) {
176: for (int i = 0; i < RESERVED.length; i++) {
177: if (name.startsWith(RESERVED[i])) {
178: result = true;
179: break;
180: }
181: }
182: }
183: return result;
184: }
185:
186: private static int getVersionNumber(String name) {
187: int result = -1;
188: if (name != null && name.startsWith(VERSION_PREFIX)) {
189: String number = name.substring(VERSION_PREFIX.length());
190: result = Integer.parseInt(number);
191: }
192: return result;
193: }
194: }
|