KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Status.java
26  *
27  * Created on March 27, 2004, 10:40 PM
28  */

29
30 package com.sun.enterprise.config.backup;
31
32 import com.sun.enterprise.config.backup.util.FileUtils;
33 import java.io.*;
34 import java.util.*;
35 import java.util.zip.*;
36
37 /**
38  *
39  * @author Byron Nevins
40  */

41 class Status
42 {
43     String JavaDoc write(BackupRequest req)
44     {
45         props = new Properties();
46         request = req;
47         statusFile = new File(request.domainDir, Constants.PROPS_FILENAME);
48
49         try
50         {
51             setProps();
52             FileOutputStream out = new FileOutputStream(statusFile);
53             props.store(out, Constants.PROPS_HEADER);
54             return propsToString(false);
55         }
56         catch(Exception JavaDoc e)
57         {
58             return StringHelper.get("backup-res.CantWriteStatus", statusFile);
59         }
60     }
61     
62     ///////////////////////////////////////////////////////////////////////////////
63

64     /**
65      * @param file Either a zip file that contains backup.properties -- or backup.properties
66      * itself. terse is automatically set to true.
67      * @return a String summary of the backup
68      */

69     String JavaDoc read(File file)
70     {
71         return read(file, true);
72     }
73     
74     ///////////////////////////////////////////////////////////////////////////////
75

76     /**
77      * @param file Either a zip file that contains backup.properties -- or backup.properties
78      * itself.
79      * @param terse if true, give a short summary
80      * @return a String summary of the backup
81      */

82     String JavaDoc read(File file, boolean terse)
83     {
84         props = null;
85         
86         setPropsFromFile(file);
87         if(props == null)
88         {
89             return badStatusFileMessage(file);
90         }
91
92         return propsToString(terse);
93     }
94
95     /**
96      * open the zip file, parse the status file and return the timestamp
97      * of when it was created.
98     */

99
100     long getInternalTimestamp(File f)
101     {
102         props = null;
103         setPropsFromFile(f);
104
105         try
106         {
107             String JavaDoc s = props.getProperty(Constants.PROPS_TIMESTAMP_MSEC);
108             return Long.parseLong(s);
109         }
110         catch(Exception JavaDoc e)
111         {
112             LoggerHelper.warning(badStatusFileMessage(f));
113             return 0;
114         }
115     }
116     
117     ///////////////////////////////////////////////////////////////////////////////
118

119     void delete()
120     {
121         if(!statusFile.delete())
122         {
123             // TBD warning message
124
statusFile.deleteOnExit();
125         }
126     }
127     
128     ///////////////////////////////////////////////////////////////////////////////
129

130     String JavaDoc getDomainName()
131     {
132         if(props == null)
133             return null;
134         
135         return props.getProperty(Constants.PROPS_DOMAIN_NAME);
136     }
137
138     ///////////////////////////////////////////////////////////////////////////////
139
////// PRIVATE METHODS AND DATA ///////////////////////////////////////////
140
///////////////////////////////////////////////////////////////////////////////
141

142     /**
143      * @param file Either a zip file that contains backup.properties -- or backup.properties
144      * itself.
145      * @param terse if true, give a short summary
146      * @return a String summary of the backup
147      */

148     private void setPropsFromFile(File file)
149     {
150         props = null;
151         ZipInputStream zis = null;
152
153
154         if(file.getName().toLowerCase().endsWith(".properties"))
155         {
156             readPropertiesFile(file);
157             // props is now set...
158
return;
159         }
160
161         try
162         {
163             zis = new ZipInputStream(new FileInputStream(file));
164             ZipEntry ze;
165
166             while( (ze = zis.getNextEntry()) != null )
167             {
168                 if(ze.getName().equals(Constants.PROPS_FILENAME))
169                 {
170                     props = new Properties();
171                     props.load(zis);
172                     break;
173                 }
174             }
175             // props may be null
176
}
177         catch(Exception JavaDoc e)
178         {
179             // overkill...
180
props = null;
181         }
182         finally
183         {
184             if(zis != null)
185             {
186                 try
187                 {
188                     zis.close();
189                 }
190                 catch(Exception JavaDoc e)
191                 {
192                 }
193             }
194         }
195     }
196     
197     ///////////////////////////////////////////////////////////////////////////////
198

199     private void readPropertiesFile(File propsFile)
200     {
201         try
202         {
203             BufferedInputStream in = new BufferedInputStream(new FileInputStream(propsFile));
204             props = new Properties();
205             props.load(in);
206             in.close();
207         }
208         catch(IOException ioe)
209         {
210             props = null;
211         }
212     }
213
214     ///////////////////////////////////////////////////////////////////////////////
215

216     private void setProps()
217     {
218         props.setProperty(Constants.PROPS_USER_NAME, System.getProperty(Constants.PROPS_USER_NAME));
219         props.setProperty(Constants.PROPS_TIMESTAMP_MSEC, "" + request.timestamp);
220         props.setProperty(Constants.PROPS_DOMAINS_DIR, FileUtils.safeGetCanonicalPath(request.domainsDir));
221         props.setProperty(Constants.PROPS_DOMAIN_DIR, FileUtils.safeGetCanonicalPath(request.domainDir));
222         props.setProperty(Constants.PROPS_BACKUP_FILE, FileUtils.safeGetCanonicalPath(request.backupFile));
223         props.setProperty(Constants.PROPS_DOMAIN_NAME, request.domainName);
224         props.setProperty(Constants.PROPS_DESCRIPTION, request.description);
225         props.setProperty(Constants.PROPS_TIMESTAMP_HUMAN, new Date(request.timestamp).toString());
226     }
227
228     ///////////////////////////////////////////////////////////////////////////////
229

230     private String JavaDoc propsToString(boolean terse)
231     {
232         final String JavaDoc pre = "backup-res.Props.";
233         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
234         
235         
236         if(terse)
237         {
238             sb.append(props.getProperty(Constants.PROPS_BACKUP_FILE));
239         }
240
241         else
242         {
243             sb.append(StringHelper.get(pre + Constants.PROPS_DESCRIPTION, props.getProperty(Constants.PROPS_DESCRIPTION)));
244             sb.append("\n");
245             sb.append(StringHelper.get(pre + Constants.PROPS_BACKUP_FILE, props.getProperty(Constants.PROPS_BACKUP_FILE)));
246             sb.append("\n");
247             sb.append(StringHelper.get(pre + Constants.PROPS_TIMESTAMP_HUMAN, props.getProperty(Constants.PROPS_TIMESTAMP_HUMAN)));
248             sb.append("\n");
249             sb.append(StringHelper.get(pre + Constants.PROPS_DOMAINS_DIR, props.getProperty(Constants.PROPS_DOMAINS_DIR)));
250             sb.append("\n");
251             sb.append(StringHelper.get(pre + Constants.PROPS_DOMAIN_DIR, props.getProperty(Constants.PROPS_DOMAIN_DIR)));
252             sb.append("\n");
253             sb.append(StringHelper.get(pre + Constants.PROPS_DOMAIN_NAME, props.getProperty(Constants.PROPS_DOMAIN_NAME)));
254             sb.append("\n");
255             sb.append(StringHelper.get(pre + Constants.PROPS_USER_NAME, props.getProperty(Constants.PROPS_USER_NAME)));
256         }
257
258         return sb.toString();
259     }
260     
261     ///////////////////////////////////////////////////////////////////////////////
262

263     private String JavaDoc badStatusFileMessage(File file)
264     {
265         String JavaDoc msg = StringHelper.get("backup-res.Props.backup.file", file);
266         msg += "\n";
267         msg += StringHelper.get("backup-res.CorruptBackupFile.NoStatusFile");
268         return msg;
269     }
270     
271     ///////////////////////////////////////////////////////////////////////////////
272

273     private BackupRequest request;
274     private File statusFile;
275     private Properties props;
276 }
277
278
279
Popular Tags