1 26 package org.snipsnap.net.admin; 27 28 import org.snipsnap.config.Configuration; 29 import org.snipsnap.container.Components; 30 import org.snipsnap.snip.SnipSpace; 31 import org.snipsnap.snip.XMLSnipExport; 32 import org.snipsnap.user.UserManager; 33 import org.snipsnap.app.Application; 34 import org.snipsnap.jdbc.IntHolder; 35 import org.radeox.util.logging.Logger; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.http.HttpSession ; 40 import java.io.File ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.io.BufferedOutputStream ; 45 import java.text.SimpleDateFormat ; 46 import java.util.Arrays ; 47 import java.util.Date ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.HashMap ; 51 52 public class DatabaseExport implements SetupHandler { 53 private HashMap workerThreads = new HashMap (); 54 55 public String getName() { 56 return "export"; 57 } 58 59 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 60 String appOid = (String ) Application.get().getObject(Application.OID); 61 ExportThread workerThread = (ExportThread) workerThreads.get(appOid); 62 if (workerThread != null && workerThread.isAlive()) { 63 setRunning(workerThread, request.getSession()); 64 return errors; 65 } else if(workerThread != null) { 66 workerThreads.remove(appOid); 67 request.getSession().removeAttribute("running"); 68 errors.put("message", "export.okay"); 69 return errors; 70 } 71 72 String output = request.getParameter("export.file"); 73 request.setAttribute("exportFile", output); 74 String exportTypes[] = request.getParameterValues("export.types"); 75 request.setAttribute("exportTypes", exportTypes); 76 77 UserManager um = (UserManager) Components.getComponent(UserManager.class); 78 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 79 80 List users = null; 81 List snips = null; 82 83 String exportMatch = request.getParameter("export.match"); 84 if (null == exportMatch) { 85 exportMatch = ""; 86 } 87 request.setAttribute("exportMatch", exportMatch); 88 89 String exportIgnore = request.getParameter("export.ignore"); 90 request.setAttribute("exportIgnore", exportIgnore == null ? "" : exportIgnore); 91 if ("".equals(exportIgnore)) { 92 exportIgnore = null; 93 } 94 95 for (int i = 0; i < exportTypes.length; i++) { 96 if ("users".equals(exportTypes[i])) { 97 users = um.getAll(); 98 request.setAttribute("exportTypeUsers", "true"); 99 } 100 if ("snips".equals(exportTypes[i])) { 101 if (null != exportMatch && !"".equals(exportMatch)) { 102 snips = Arrays.asList(space.match(exportMatch)); 103 } else { 104 snips = space.getAll(); 105 } 106 request.setAttribute("exportTypeSnips", "true"); 107 } 108 } 109 110 if (users == null && snips == null) { 111 errors.put("export.types", "export.types"); 112 return errors; 113 } 114 115 OutputStream out = null; 116 try { 117 if ("webinf".equals(output)) { 118 SimpleDateFormat df = new SimpleDateFormat ("yyyyMMdd"); 119 File outFile = new File (config.getWebInfDir(), 120 config.getName() + "-" + df.format(new Date ()) + ".snip"); 121 workerThread = new ExportThread(new BufferedOutputStream (new FileOutputStream (outFile)), snips, users, exportIgnore, config.getFilePath()); 122 workerThread.start(); 123 workerThreads.put(appOid, workerThread); 124 setRunning(workerThread, request.getSession()); 125 return errors; 126 127 } else if ("download".equals(output)) { 128 response.setContentType("text/xml"); 129 out = response.getOutputStream(); 130 } else { 131 errors.put("message", "export.failed"); 132 return errors; 133 } 134 135 XMLSnipExport.store(new BufferedOutputStream (out), snips, users, exportIgnore, null, config.getFilePath()); 136 } catch (IOException e) { 137 errors.put("message", "export.failed"); 138 } 139 140 if(errors.size() == 0) { 141 return null; 142 } 143 return errors; 144 } 145 146 private void setRunning(ExportThread workerThread, HttpSession session) { 147 Map statusMap = (Map ) session.getAttribute("running"); 148 if (null == statusMap) { 149 statusMap = new HashMap (); 150 } 151 statusMap.put("max", new Integer (workerThread.getMax())); 152 statusMap.put("current", new Integer (workerThread.getCurrent())); 153 statusMap.put("export", "true"); 154 session.setAttribute("running", statusMap); 155 156 } 157 158 class ExportThread extends Thread { 159 private OutputStream out; 160 private List snips, users; 161 private String exportIgnore; 162 private File filePath; 163 164 private int maxValue = 0; 165 private IntHolder status; 166 167 public int getMax() { 168 return maxValue; 169 } 170 171 public int getCurrent() { 172 if(null == status) { 173 return 0; 174 } 175 return status.getValue(); 176 } 177 178 public ExportThread(OutputStream out, List snips, List users, String ignore, File filePath) { 179 this.out = out; 180 this.snips = snips; 181 this.users = users; 182 this.exportIgnore = ignore; 183 this.filePath = filePath; 184 maxValue = (snips != null ? snips.size() : 0) + (users != null ? users.size() : 0); 185 } 186 187 public void run() { 188 status = XMLSnipExport.getStatus(); 189 XMLSnipExport.store(out, snips, users, exportIgnore, null, filePath); 190 try { 191 out.close(); 192 } catch (IOException e) { 193 Logger.warn("DatabaseExport: unable to close document", e); 194 } 195 } 196 } 197 } | Popular Tags |