Source Code Cross Referenced for FileVersionUtil.java in  » ESB » servicemix » org » apache » servicemix » jbi » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » servicemix » org.apache.servicemix.jbi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.