KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreFileMover


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

16 package org.apache.catalina.storeconfig;
17
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.sql.Timestamp JavaDoc;
24
25 /**
26  * Move server.xml or context.xml as backup
27  *
28  * @author Peter Rossbach
29  *
30  * TODO Get Encoding from Registry
31  */

32 public class StoreFileMover {
33
34     private String JavaDoc filename = "conf/server.xml";
35
36     private String JavaDoc encoding = "UTF-8";
37
38     private String JavaDoc basename = System.getProperty("catalina.base");
39
40     private File JavaDoc configOld;
41
42     private File JavaDoc configNew;
43
44     private File JavaDoc configSave;
45
46     /**
47      * @return Returns the configNew.
48      */

49     public File JavaDoc getConfigNew() {
50         return configNew;
51     }
52
53     /**
54      * @return Returns the configOld.
55      */

56     public File JavaDoc getConfigOld() {
57         return configOld;
58     }
59
60     /**
61      * @return Returns the configSave.
62      */

63     public File JavaDoc getConfigSave() {
64         return configSave;
65     }
66
67     /**
68      * @return Returns the basename.
69      */

70     public String JavaDoc getBasename() {
71         return basename;
72     }
73
74     /**
75      * @param basename
76      * The basename to set.
77      */

78     public void setBasename(String JavaDoc basename) {
79         this.basename = basename;
80     }
81
82     /**
83      * @return
84      */

85     public String JavaDoc getFilename() {
86         return filename;
87     }
88
89     /**
90      * @param string
91      */

92     public void setFilename(String JavaDoc string) {
93         filename = string;
94     }
95
96     /**
97      * @return
98      */

99     public String JavaDoc getEncoding() {
100         return encoding;
101     }
102
103     /**
104      * @param string
105      */

106     public void setEncoding(String JavaDoc string) {
107         encoding = string;
108     }
109
110     /**
111      * Calculate file objects for the old and new configuration files.
112      */

113     public StoreFileMover(String JavaDoc basename, String JavaDoc filename, String JavaDoc encoding) {
114         setBasename(basename);
115         setEncoding(encoding);
116         setFilename(filename);
117         init();
118     }
119
120     /**
121      * Calculate file objects for the old and new configuration files.
122      */

123     public StoreFileMover() {
124         init();
125     }
126
127     /**
128      * generate the Filename to new with TimeStamp
129      */

130     public void init() {
131         String JavaDoc configFile = getFilename();
132         configOld = new File JavaDoc(configFile);
133         if (!configOld.isAbsolute()) {
134             configOld = new File JavaDoc(getBasename(), configFile);
135         }
136         configNew = new File JavaDoc(configFile + ".new");
137         if (!configNew.isAbsolute()) {
138             configNew = new File JavaDoc(getBasename(), configFile + ".new");
139         }
140         if (!configNew.getParentFile().exists()) {
141             configNew.getParentFile().mkdirs();
142         }
143         String JavaDoc sb = getTimeTag();
144         configSave = new File JavaDoc(configFile + sb);
145         if (!configSave.isAbsolute()) {
146             configSave = new File JavaDoc(getBasename(), configFile + sb);
147         }
148     }
149
150     /**
151      * Shuffle old->save and new->old
152      *
153      * @throws IOException
154      */

155     public void move() throws IOException JavaDoc {
156         if (configOld.renameTo(configSave)) {
157             if (!configNew.renameTo(configOld)) {
158                 configSave.renameTo(configOld);
159                 throw new IOException JavaDoc("Cannot rename "
160                         + configNew.getAbsolutePath() + " to "
161                         + configOld.getAbsolutePath());
162             }
163         } else {
164             if (!configOld.exists()) {
165                 if (!configNew.renameTo(configOld)) {
166                     throw new IOException JavaDoc("Cannot move "
167                             + configNew.getAbsolutePath() + " to "
168                             + configOld.getAbsolutePath());
169                 }
170             } else {
171                 throw new IOException JavaDoc("Cannot rename "
172                     + configOld.getAbsolutePath() + " to "
173                     + configSave.getAbsolutePath());
174             }
175         }
176     }
177
178     /**
179      * Open an output writer for the new configuration file
180      *
181      * @param configNew
182      * @return
183      * @throws IOException
184      */

185     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
186         PrintWriter JavaDoc writer = null;
187         try {
188             writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(
189                     new FileOutputStream JavaDoc(configNew), getEncoding()));
190         } catch (IOException JavaDoc e) {
191             if (writer != null) {
192                 try {
193                     writer.close();
194                 } catch (Throwable JavaDoc t) {
195                     ;
196                 }
197             }
198             throw (e);
199         }
200         return writer;
201     }
202
203     /**
204      * Time value for backup yyyy-mm-dd.hh-mm-ss
205      *
206      * @return
207      */

208     protected String JavaDoc getTimeTag() {
209         String JavaDoc ts = (new Timestamp JavaDoc(System.currentTimeMillis())).toString();
210         // yyyy-mm-dd hh:mm:ss
211
// 0123456789012345678
212
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(".");
213         sb.append(ts.substring(0, 10));
214         sb.append('.');
215         sb.append(ts.substring(11, 13));
216         sb.append('-');
217         sb.append(ts.substring(14, 16));
218         sb.append('-');
219         sb.append(ts.substring(17, 19));
220         return sb.toString();
221     }
222
223 }
Popular Tags