KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > BackupFilenameManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * BackupFilenameManager.java
26  *
27  * Created on August 13, 2004, 12:10 PM
28  */

29
30 package com.sun.enterprise.config.backup;
31
32 import java.io.*;
33
34 /**
35  * Manage filenames to select for backups.
36  * @author bnevins
37  */

38 class BackupFilenameManager
39 {
40     BackupFilenameManager(File backupDir) throws BackupException
41     {
42         this.dir = backupDir;
43         findZips();
44         findLatest();
45     }
46     
47     ///////////////////////////////////////////////////////////////////////////
48

49     File next() throws BackupException
50     {
51         int newVersionNum = 1;
52         
53         if(latestVersion != null)
54             newVersionNum = latestVersion.num + 1;
55     
56         String JavaDoc suffix = padWithLeadingZeroes(newVersionNum);
57         return new File(dir, Constants.BACKUP_FILENAME_ROOT + suffix + ".zip");
58     }
59     
60     ///////////////////////////////////////////////////////////////////////////
61

62     File latest() throws BackupWarningException
63     {
64         if(latestVersion == null)
65             throw new BackupWarningException("backup-res.NoBackupFiles", dir);
66         
67         return latestVersion.zip;
68     }
69     
70     ///////////////////////////////////////////////////////////////////////////////
71

72     /** Looks through the backups directory and assembles
73      * a list of all backup files found.
74      * @throws BackupWarningException if there are no backup zip files
75      */

76     private void findZips() throws BackupWarningException
77     {
78         File[] zips = dir.listFiles(new ZipFilenameFilter());
79         int len = 0;
80         
81         if(zips != null)
82             len = zips.length;
83         
84         zipsAndNumbers = new ZipFileAndNumber[len];
85         
86         for(int i = 0; i < len; i++)
87         {
88             zipsAndNumbers[i] = new ZipFileAndNumber(zips[i]);
89         }
90     }
91     
92     ///////////////////////////////////////////////////////////////////////////////
93

94     /** Looks through the list of zips and sets the one with the biggest version number
95      */

96     private void findLatest() throws BackupWarningException
97     {
98         int biggest = 0;
99         
100         for(int i = 0; i < zipsAndNumbers.length; i++)
101         {
102             int curr = zipsAndNumbers[i].num;
103             
104             if(curr > biggest)
105             {
106                 biggest = curr;
107                 latestVersion = zipsAndNumbers[i];
108                 LoggerHelper.fine(zipsAndNumbers[i].zip.toString() + " newest backup so far...");
109             }
110         }
111     }
112     
113     ///////////////////////////////////////////////////////////////////////////////
114

115     /** Convert the array of zip filenames into an array of the number suffixes.
116      */

117     private String JavaDoc padWithLeadingZeroes(int num) throws BackupException
118     {
119         if(num < 10)
120             return "0000" + num;
121
122         if(num < 100)
123             return "000" + num;
124         
125         if(num < 1000)
126             return "00" + num;
127         
128         if(num < 10000)
129             return "0" + num;
130
131         if(num < 100000)
132             return "" + num;
133         
134         throw new BackupException("Latest version >= 100,000. Delete some backup files.");
135     }
136     
137     ///////////////////////////////////////////////////////////////////////////
138

139     private static class ZipFileAndNumber
140     {
141         private ZipFileAndNumber(File zip)
142         {
143             this.zip = zip;
144             String JavaDoc fname = zip.getName();
145             
146             if(isValid())
147             {
148                 
149                 fname = fname.substring(Constants.BACKUP_FILENAME_ROOT.length(), fname.length() - 4);
150                 try
151                 {
152                     num = Integer.parseInt(fname);
153                 }
154                 catch(Exception JavaDoc e)
155                 {
156                     // nothing to do -- num is already set to -1
157
}
158             }
159         }
160         
161         /**
162          * make sure that:
163          * (1) the filename is the right format
164          * (2) that it is internally correct (has a status file with a timestamp
165          **/

166         
167         private boolean isValid()
168         {
169             Status status = new Status();
170             long time = status.getInternalTimestamp(zip);
171             
172             return time > 0 && zip.getName().startsWith(Constants.BACKUP_FILENAME_ROOT);
173         }
174         
175         private File zip;
176         private int num = -1;
177     }
178
179     ///////////////////////////////////////////////////////////////////////////
180

181     private File dir;
182     private ZipFileAndNumber[] zipsAndNumbers;
183     private int[] numbers;
184     private ZipFileAndNumber latestVersion;
185     
186     ///////////////////////////////////////////////////////////////////////////////
187

188
189     public static void main(String JavaDoc[] args)
190     {
191         try
192         {
193             File f = new File("c:/tmp/test");
194             BackupFilenameManager mgr = new BackupFilenameManager(f);
195             File fnew = mgr.next();
196             System.out.println("Next backup file: " + fnew);
197             File fold = mgr.latest();
198             System.out.println("Latest backup file: " + fold);
199         }
200         catch(Exception JavaDoc e)
201         {
202             e.printStackTrace();
203         }
204     }
205 }
206
Popular Tags