KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > DatabaseImport


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

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 JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38 import java.io.BufferedInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 public class DatabaseImport implements SetupHandler {
45   private HashMap JavaDoc workerThreads = new HashMap JavaDoc();
46
47   public String JavaDoc getName() {
48     return "import";
49   }
50
51   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
52     String JavaDoc appOid = (String JavaDoc) 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 JavaDoc 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 JavaDoc is = new BufferedInputStream JavaDoc(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 JavaDoc e) {
101         errors.put("message", "import.failed");
102       }
103     }
104     return errors;
105   }
106
107   private void setRunning(ImportThread workerThread, HttpSession JavaDoc session) {
108     Map JavaDoc statusMap = (Map JavaDoc) session.getAttribute("running");
109     if (null == statusMap) {
110       statusMap = new HashMap JavaDoc();
111     }
112     statusMap.put("current", new Long JavaDoc(workerThread.getCurrent()));
113     statusMap.put("import", "true");
114     session.setAttribute("running", statusMap);
115   }
116
117   class ImportThread extends Thread JavaDoc {
118     private User user;
119     private String JavaDoc appOid;
120     private InputStream JavaDoc file;
121     private int mask;
122
123     private IntHolder status;
124
125     private Map JavaDoc errors = null;
126
127     public Map JavaDoc 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 JavaDoc file, int mask) {
139       this.file = file;
140       this.mask = mask;
141       this.appOid = (String JavaDoc)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 JavaDoc ping = config.get(Configuration.APP_PERM_WEBLOGSPING);
153       String JavaDoc 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 JavaDoc e) {
162         System.err.println("ConfigurServlet.importDatabase: unable to import snips: " + e);
163         e.printStackTrace();
164         errors = new HashMap JavaDoc();
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