1 40 package com.mvnforum.admin; 41 42 import java.io.*; 43 import java.sql.*; 44 import java.util.*; 45 import java.util.zip.ZipEntry ; 46 import java.util.zip.ZipOutputStream ; 47 48 import javax.servlet.http.HttpServletRequest ; 49 50 import com.mvnforum.*; 51 import com.mvnforum.admin.importexport.XMLUtil; 52 import com.mvnforum.admin.importexport.XMLWriter; 53 import net.myvietnam.mvncore.db.DBUtils; 54 import net.myvietnam.mvncore.exception.*; 55 import net.myvietnam.mvncore.util.I18nUtil; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 66 public class ExportWebHelper { 67 68 69 private static Log log = LogFactory.getLog(ExportWebHelper.class); 70 71 72 private ExportWebHelper() { 73 super(); 74 } 75 76 77 public static void exportXml(String filename, HttpServletRequest request, 81 int logonMemberID, String logonMemberName, 82 Calendar backupTime, String backupIP) 83 throws ExportException { 84 85 log.debug("Making backup xml = \""+filename+"\"."); 86 String filepath = MVNForumConfig.getBackupDir() + File.separatorChar + filename; 87 File resultFile = new File(filepath); 88 XMLWriter xmlWriter = new XMLWriter(" ", resultFile); 89 try { 90 createBackupXml(xmlWriter, backupTime, backupIP); 91 } catch (ExportException e) { 92 98 try { 99 xmlWriter.close(); 100 } catch (IOException ee) { 101 log.error("I/O error while closing created XML. Ignoring..."); 102 } finally { 104 xmlWriter=null; 105 } 106 if (resultFile!=null) { resultFile.delete(); resultFile=null; } 107 throw e; } finally { 109 try { 111 xmlWriter.close(); 112 } catch (IOException e) { 113 log.error("I/O error while closing created XML. Ignoring..."); 114 } finally { 116 xmlWriter=null; 117 } 118 } 119 resultFile=null; 120 } 121 122 public static void exportZip(String filename, HttpServletRequest request, 123 int logonMemberID, String logonMemberName, 124 Calendar backupTime, String backupIP) 125 throws ExportException { 126 127 log.debug("Making backup zip = \""+filename+"\"."); 128 File mainXmlFile = null; 130 131 Locale locale = I18nUtil.getLocaleInRequest(request); 132 133 try { 134 mainXmlFile = File.createTempFile("IMPORT", ".xml", new File(MVNForumConfig.getTempDir())); 135 XMLWriter xmlWriter = new XMLWriter(" ", mainXmlFile); 136 try { 137 createBackupXml(xmlWriter, backupTime, backupIP); 138 } finally { 139 xmlWriter.close(); xmlWriter=null; 140 } 141 } catch (IOException e) { 142 log.error("I/O error while creating XML."); 143 if (mainXmlFile!=null) { mainXmlFile.delete(); mainXmlFile=null; } 144 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ExportException.io_error.create_mail_xml_file"); 145 throw new ExportException(localizedMessage); 146 } catch (ExportException e) { 148 if (mainXmlFile!=null) { mainXmlFile.delete(); mainXmlFile=null; } 149 throw e; } 151 152 159 String zipFilePath = MVNForumConfig.getBackupDir() + File.separatorChar + filename; 160 String avatarsDir = request.getSession().getServletContext().getRealPath(MVNForumGlobal.UPLOADED_AVATAR_DIR); 161 String attachsDir = MVNForumConfig.getAttachmentDir(); 162 163 ZipOutputStream out = null; 164 try { 165 out = new ZipOutputStream (new FileOutputStream(zipFilePath)); 167 out.setMethod(ZipOutputStream.DEFLATED); 169 170 addDirectory(out, new File(avatarsDir), MVNForumConfig.BACKUP_FILE_AvatarsDirNameInZip); 172 addDirectory(out, new File(attachsDir), MVNForumConfig.BACKUP_FILE_AttachsDirNameInZip); 173 addFile(out, mainXmlFile, MVNForumConfig.BACKUP_FILE_MainXmlFileNameInZip); 175 } catch (FileNotFoundException e) { 176 log.error("Can't find a file that has been created."); 177 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ExportException.cannot_file_created"); 178 throw new ExportException(localizedMessage); 179 } catch (IOException e) { 181 log.error("I/O error while making ZIP file."); 182 try { out.close(); } catch (Exception ee) {} 183 out = null; 184 try { new File(zipFilePath).delete(); } catch (Exception ee) {} 185 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ExportException.io_error.make_zip_file"); 186 throw new ExportException(localizedMessage); 187 } finally { 189 if (out!=null) try { out.flush(); out.close(); } catch (IOException e) {} 190 out = null; 191 if (mainXmlFile!=null) { mainXmlFile.delete(); mainXmlFile=null; } 193 } 194 195 } 196 197 private static void addFile(ZipOutputStream out, File file, String relativePath) 198 throws IOException { 199 byte[] buf = new byte[1024]; 200 FileInputStream in = new FileInputStream(file); 201 202 out.putNextEntry(new ZipEntry (relativePath)); 204 209 int len; 211 while ((len = in.read(buf)) > 0) { 212 out.write(buf, 0, len); 213 } 214 215 in.close(); in=null; 217 out.closeEntry(); 218 } 219 220 private static void addDirectory(ZipOutputStream out, File dir, String relativePath) 221 throws IOException { 222 if (dir.isFile()) { 223 log.error("Called addDirectory on a file \""+dir.getAbsolutePath()+"\"."); 224 throw new IOException("I/O error while adding directory to ZIP."); 225 } 226 if (relativePath.length()>0) { if (!relativePath.endsWith("/")) relativePath+="/"; 228 out.putNextEntry(new ZipEntry (relativePath)); 230 out.closeEntry(); 231 } 232 File[] files = dir.listFiles(); 233 if (files != null) { 234 for (int i = 0; i < files.length; i++) { 235 File file = files[i]; 236 if (file.isFile()) { 237 addFile(out, file, relativePath+file.getName()); 238 } else { 239 addDirectory(out, file, relativePath+file.getName()+"/"); 240 } 241 } 242 } 243 } 244 245 246 private static void createBackupXml(XMLWriter xmlWriter, 250 Calendar backupTime, String backupIP) 251 throws ExportException { 252 253 try { 254 xmlWriter.startDocument(MVNForumConstant.dtdschemaDecl); 255 String strExportDate = XMLUtil.sqlTimestampToStringDefNow( 256 new Timestamp(backupTime.getTime().getTime())); 257 xmlWriter.startElement("mvnforum", 258 new String []{"version", "1.0", 259 "exportDate", strExportDate}); 260 261 MemberXML.exportMemberList(xmlWriter); 262 GroupXML.exportGroupList(xmlWriter); 263 CategoryXML.exportCategoryList(xmlWriter); 264 RankXML.exportRankList(xmlWriter); 265 266 xmlWriter.endElement("mvnforum"); 267 xmlWriter.endDocument(); 268 } catch (IOException e) { 269 log.error("I/O error while trying to create backup XML file on server.", e); 270 throw new ExportException("", e); 271 } catch (ObjectNotFoundException e) { 272 log.error("Object not found error while trying to createBackupXml()", e); 273 throw new ExportException("Database object not found error while trying to create backup XML file on server.", e); 274 } catch (DatabaseException e) { 275 log.error("Database error while trying to createBackupXml()", e); 276 throw new ExportException("Database error while trying to create backup XML file on server.", e); 277 } 278 } 279 280 281 public static Collection execSqlQuery(String query) 285 throws DatabaseException { 286 Connection connection = null; 287 PreparedStatement statement = null; 288 ResultSet resultSet = null; 289 Collection retValue = new ArrayList(); 290 try { 291 connection = DBUtils.getConnection(); 292 statement = connection.prepareStatement(query); 293 resultSet = statement.executeQuery(); 294 int numCols = resultSet.getMetaData().getColumnCount(); 295 while (resultSet.next()) { 296 String [] thisResult = new String [numCols]; 297 for (int i=0; i<numCols; i++) { 298 thisResult[i]=resultSet.getString(i+1); 299 } 300 retValue.add(thisResult); 301 } 302 return retValue; 303 } catch (SQLException sqle) { 304 log.error("Sql Execution Error!", sqle); 305 throw new DatabaseException("Error executing SQL in ExportWebHelper.execSqlQuery."); 306 } finally { 307 DBUtils.closeResultSet(resultSet); 308 DBUtils.closeStatement(statement); 309 DBUtils.closeConnection(connection); 310 } 311 } 312 313 314 } 315 316 317
| Popular Tags
|