1 26 package org.snipsnap.net.admin; 27 28 import org.snipsnap.app.Application; 29 import org.snipsnap.config.Configuration; 30 import org.snipsnap.jdbc.IntHolder; 31 import org.snipsnap.net.filter.MultipartWrapper; 32 import org.snipsnap.snip.XMLSnipImport; 33 import org.snipsnap.user.User; 34 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 38 import java.io.BufferedInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.util.HashMap ; 42 import java.util.Map ; 43 44 public class DatabaseImport implements SetupHandler { 45 private HashMap workerThreads = new HashMap (); 46 47 public String getName() { 48 return "import"; 49 } 50 51 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 52 String appOid = (String ) Application.get().getObject(Application.OID); 53 ImportThread workerThread = (ImportThread) workerThreads.get(appOid); 54 if (workerThread != null && workerThread.isAlive()) { 55 setRunning(workerThread, request.getSession()); 56 return errors; 57 } else if (workerThread != null) { 58 workerThreads.remove(appOid); 59 request.getSession().removeAttribute("running"); 60 if(workerThread.getErrors() != null) { 61 errors.putAll(workerThread.getErrors()); 62 } else { 63 errors.put("message", "import.okay"); 64 } 65 return errors; 66 } 67 68 if(request instanceof MultipartWrapper) { 69 MultipartWrapper req = (MultipartWrapper) request; 70 71 boolean overwrite = request.getParameter("import.overwrite") != null; 72 String importTypes[] = request.getParameterValues("import.types"); 73 req.setAttribute("importOverwrite", overwrite ? "true" : "false"); 74 req.setAttribute("importTypes", importTypes); 75 76 int importMask = overwrite ? XMLSnipImport.OVERWRITE : 0; 77 for (int i = 0; importTypes != null && i < importTypes.length; i++) { 78 if ("users".equals(importTypes[i])) { 79 importMask = importMask | XMLSnipImport.IMPORT_USERS; 80 req.setAttribute("importTypeUsers", "true"); 81 } 82 if ("snips".equals(importTypes[i])) { 83 importMask = importMask | XMLSnipImport.IMPORT_SNIPS; 84 req.setAttribute("importTypeSnips", "true"); 85 } 86 } 87 88 try { 89 InputStream is = new BufferedInputStream (req.getFileInputStream("import.file")); 90 if (importMask == XMLSnipImport.OVERWRITE) { 91 errors.put("import.types", "import.types"); 92 } else if (is != null && is.available() > 0) { 93 ImportThread importThread = new ImportThread(is, importMask); 94 importThread.start(); 95 workerThreads.put(appOid, importThread); 96 setRunning(importThread, req.getSession()); 97 } else { 98 errors.put("import.file", "import.file"); 99 } 100 } catch (IOException e) { 101 errors.put("message", "import.failed"); 102 } 103 } 104 return errors; 105 } 106 107 private void setRunning(ImportThread workerThread, HttpSession session) { 108 Map statusMap = (Map ) session.getAttribute("running"); 109 if (null == statusMap) { 110 statusMap = new HashMap (); 111 } 112 statusMap.put("current", new Long (workerThread.getCurrent())); 113 statusMap.put("import", "true"); 114 session.setAttribute("running", statusMap); 115 } 116 117 class ImportThread extends Thread { 118 private User user; 119 private String appOid; 120 private InputStream file; 121 private int mask; 122 123 private IntHolder status; 124 125 private Map errors = null; 126 127 public Map getErrors() { 128 return errors; 129 } 130 131 public int getCurrent() { 132 if (null == status) { 133 return 0; 134 } 135 return status.getValue(); 136 } 137 138 public ImportThread(InputStream file, int mask) { 139 this.file = file; 140 this.mask = mask; 141 this.appOid = (String )Application.get().getObject(Application.OID); 142 this.user = Application.get().getUser(); 143 } 144 145 public void run() { 146 Application.get().storeObject(Application.OID, appOid); 147 Application.get().setUser(user); 148 149 Configuration config = Application.get().getConfiguration(); 150 151 System.err.println("ConfigureServlet: Disabling weblogs ping and jabber notification ..."); 152 String ping = config.get(Configuration.APP_PERM_WEBLOGSPING); 153 String noty = config.get(Configuration.APP_PERM_NOTIFICATION); 154 config.set(Configuration.APP_PERM_WEBLOGSPING, "deny"); 155 config.set(Configuration.APP_PERM_NOTIFICATION, "deny"); 156 157 status = XMLSnipImport.getStatus(); 158 159 try { 160 XMLSnipImport.load(file, mask); 161 } catch (Exception e) { 162 System.err.println("ConfigurServlet.importDatabase: unable to import snips: " + e); 163 e.printStackTrace(); 164 errors = new HashMap (); 165 errors.put("message", "import.failed"); 166 } 167 168 System.err.println("ConfigureServlet: Resetting weblogs ping and jabber notification to config settings ..."); 169 config.set(Configuration.APP_PERM_WEBLOGSPING, ping); 170 config.set(Configuration.APP_PERM_NOTIFICATION, noty); 171 } 172 } 173 } 174 175 | Popular Tags |