1 16 package org.apache.catalina.storeconfig; 17 18 import java.io.File ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PrintWriter ; 23 import java.sql.Timestamp ; 24 25 32 public class StoreFileMover { 33 34 private String filename = "conf/server.xml"; 35 36 private String encoding = "UTF-8"; 37 38 private String basename = System.getProperty("catalina.base"); 39 40 private File configOld; 41 42 private File configNew; 43 44 private File configSave; 45 46 49 public File getConfigNew() { 50 return configNew; 51 } 52 53 56 public File getConfigOld() { 57 return configOld; 58 } 59 60 63 public File getConfigSave() { 64 return configSave; 65 } 66 67 70 public String getBasename() { 71 return basename; 72 } 73 74 78 public void setBasename(String basename) { 79 this.basename = basename; 80 } 81 82 85 public String getFilename() { 86 return filename; 87 } 88 89 92 public void setFilename(String string) { 93 filename = string; 94 } 95 96 99 public String getEncoding() { 100 return encoding; 101 } 102 103 106 public void setEncoding(String string) { 107 encoding = string; 108 } 109 110 113 public StoreFileMover(String basename, String filename, String encoding) { 114 setBasename(basename); 115 setEncoding(encoding); 116 setFilename(filename); 117 init(); 118 } 119 120 123 public StoreFileMover() { 124 init(); 125 } 126 127 130 public void init() { 131 String configFile = getFilename(); 132 configOld = new File (configFile); 133 if (!configOld.isAbsolute()) { 134 configOld = new File (getBasename(), configFile); 135 } 136 configNew = new File (configFile + ".new"); 137 if (!configNew.isAbsolute()) { 138 configNew = new File (getBasename(), configFile + ".new"); 139 } 140 if (!configNew.getParentFile().exists()) { 141 configNew.getParentFile().mkdirs(); 142 } 143 String sb = getTimeTag(); 144 configSave = new File (configFile + sb); 145 if (!configSave.isAbsolute()) { 146 configSave = new File (getBasename(), configFile + sb); 147 } 148 } 149 150 155 public void move() throws IOException { 156 if (configOld.renameTo(configSave)) { 157 if (!configNew.renameTo(configOld)) { 158 configSave.renameTo(configOld); 159 throw new IOException ("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 ("Cannot move " 167 + configNew.getAbsolutePath() + " to " 168 + configOld.getAbsolutePath()); 169 } 170 } else { 171 throw new IOException ("Cannot rename " 172 + configOld.getAbsolutePath() + " to " 173 + configSave.getAbsolutePath()); 174 } 175 } 176 } 177 178 185 public PrintWriter getWriter() throws IOException { 186 PrintWriter writer = null; 187 try { 188 writer = new PrintWriter (new OutputStreamWriter ( 189 new FileOutputStream (configNew), getEncoding())); 190 } catch (IOException e) { 191 if (writer != null) { 192 try { 193 writer.close(); 194 } catch (Throwable t) { 195 ; 196 } 197 } 198 throw (e); 199 } 200 return writer; 201 } 202 203 208 protected String getTimeTag() { 209 String ts = (new Timestamp (System.currentTimeMillis())).toString(); 210 StringBuffer sb = new StringBuffer ("."); 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 |