/** * Servlet initialisation: creates the connection to the erlang node and imports site information. */ @Override public void init2(ServletConfig config) throws ServletException { super.init2(config); Properties properties = new Properties(); try { InputStream fis = config.getServletContext().getResourceAsStream("/WEB-INF/scalaris.properties"); if (fis != null) { properties.load(fis); properties.setProperty("PropertyLoader.loadedfile", "/WEB-INF/scalaris.properties"); fis.close(); } else { properties = null; } } catch (IOException e) { // e.printStackTrace(); properties = null; } ConnectionFactory cFactory; if (properties != null) { cFactory = new ConnectionFactory(properties); } else { cFactory = new ConnectionFactory(); cFactory.setClientName("wiki"); } Random random = new Random(); String clientName = new BigInteger(128, random).toString(16); cFactory.setClientName(cFactory.getClientName() + '_' + clientName); cFactory.setClientNameAppendUUID(true); // cFactory.setConnectionPolicy(new RoundRobinConnectionPolicy(cFactory.getNodes())); cPool = new ConnectionPool(cFactory, CONNECTION_POOL_SIZE); if (Options.getInstance().SCALARIS_NODE_DISCOVERY > 0) { nodeDiscovery = new NodeDiscovery(cPool); nodeDiscovery.startWithFixedDelay(Options.getInstance().SCALARIS_NODE_DISCOVERY); } }
/** * Shows a page for importing a DB dump. * * @param request the request of the current operation * @param response the response of the current operation * @throws IOException * @throws ServletException */ @Override protected synchronized void showImportPage( HttpServletRequest request, HttpServletResponse response, Connection connection, WikiPageBean page) throws ServletException, IOException { page.setNotAvailable(true); StringBuilder content = new StringBuilder(); String dumpsPath = getServletContext().getRealPath("/WEB-INF/dumps"); final String serviceUser = page.getServiceUser().isEmpty() ? "" : "&service_user="******"import"); if (req_import == null || !availableDumps.contains(req_import)) { content.append("<h2>Please select a wiki dump to import</h2>\n"); content.append("<form method=\"get\" action=\"wiki\">\n"); if (!page.getServiceUser().isEmpty()) { content.append( "<input type=\"hidden\" value=\"" + page.getServiceUser() + "\" name=\"service_user\"/>"); } content.append("<p>\n"); content.append(" <select name=\"import\" size=\"10\" style=\"width:500px;\">\n"); for (String dump : availableDumps) { content.append(" <option>" + dump + "</option>\n"); } content.append(" </select>\n"); content.append(" </p>\n"); content.append( " <p>Maximum number of revisions per page: <input name=\"max_revisions\" size=\"2\" value=\"2\" /></br><span style=\"font-size:80%\">(<tt>-1</tt> to import everything)</span></p>\n"); content.append( " <p>No entry newer than: <input name=\"max_time\" size=\"20\" value=\"\" /></br><span style=\"font-size:80%\">(ISO8601 format, e.g. <tt>2004-01-07T08:09:29Z</tt> - leave empty to import everything)</span></p>\n"); content.append(" <input type=\"submit\" value=\"Import\" />\n"); content.append("</form>\n"); content.append( "<p>Note: You will be re-directed to the main page when the import finishes.</p>"); } else { content.append("<h2>Importing \"" + req_import + "\"...</h2>\n"); try { int maxRevisions = parseInt(request.getParameter("max_revisions"), 2); Calendar maxTime = parseDate(request.getParameter("max_time"), null); startImport(dumpsPath, req_import, maxRevisions, maxTime); response.setHeader( "Refresh", "2; url = wiki?import=" + currentImport + serviceUser + "#refresh"); content.append( "<p>Current log file (refreshed automatically every " + IMPORT_REDIRECT_EVERY + " seconds):</p>\n"); content.append("<pre>"); content.append("starting import...\n"); content.append("</pre>"); content.append( "<p><a name=\"refresh\" href=\"wiki?import=" + currentImport + serviceUser + "#refresh\">refresh</a></p>"); if (importHandler.hasStopSupport()) { content.append( "<p><a href=\"wiki?stop_import=" + currentImport + serviceUser + "\">stop</a> (WARNING: pages may be incomplete due to missing templates)</p>"); } } catch (Exception e) { setParam_error(request, "ERROR: import failed"); addToParam_notice(request, "error: <pre>" + e.getMessage() + "</pre>"); currentImport = ""; } } } else if (!currentImport.isEmpty() && importHandler != null) { content.append("<h2>Importing \"" + currentImport + "\"...</h2>\n"); String req_stop_import = request.getParameter("stop_import"); boolean stopImport = false; if (importHandler.hasStopSupport() && req_stop_import != null && !req_stop_import.isEmpty()) { stopImport = true; importHandler.stopParsing(); content.append("<p>Current log file:</p>\n"); } else { response.setHeader( "Refresh", IMPORT_REDIRECT_EVERY + "; url = wiki?import=" + currentImport + serviceUser + "#refresh"); content.append( "<p>Current log file (refreshed automatically every " + IMPORT_REDIRECT_EVERY + " seconds):</p>\n"); } content.append("<pre>"); String log = importLog.toString(); int start = log.indexOf("\n"); if (start != -1) { content.append(log.substring(start)); } content.append("</pre>"); if (!stopImport) { content.append( "<p><a name=\"refresh\" href=\"wiki?import=" + currentImport + serviceUser + "#refresh\">refresh</a></p>"); if (importHandler.hasStopSupport()) { content.append( "<p><a href=\"wiki?stop_import=" + currentImport + serviceUser + "\">stop</a> (WARNING: pages may be incomplete due to missing templates)</p>"); } } else { content.append( "<p>Import has been stopped by the user. Return to <a href=\"wiki?title=" + MAIN_PAGE + serviceUser + "\">" + MAIN_PAGE + "</a>.</p>"); } } else if (!currentImport.isEmpty() && importHandler == null) { content.append("<h2>Import of \"" + currentImport + "\" finished</h2>\n"); content.append("<p>Current log file:</p>\n"); content.append("<pre>"); String log = importLog.toString(); int start = log.indexOf("\n"); if (start != -1) { content.append(log.substring(start)); } content.append("</pre>"); String req_stop_import = request.getParameter("stop_import"); if (req_stop_import != null && !req_stop_import.isEmpty()) { synchronized (WikiServletScalaris.this) { importLog.close(); WikiServletScalaris.this.currentImport = ""; } response.setHeader("Refresh", "1; url = wiki?title=" + MAIN_PAGE + serviceUser + ""); content.append( "<p>If not re-directed automatically: Return to <a href=\"wiki?title=" + MAIN_PAGE + serviceUser + "\">" + MAIN_PAGE + "</a></p>\n"); } else { content.append( "<p><a href=\"wiki?stop_import=" + currentImport + serviceUser + "\">clear log and return to Main Page</a></p>"); } } page.setNotice(WikiServlet.getParam_notice(request)); page.setError(getParam_error(request)); page.setTitle("Import Wiki dump"); page.setPage(content.toString()); forwardToPageJsp(request, response, connection, page, "page.jsp"); }
/** * Shows a page for importing a DB dump. * * @param request the request of the current operation * @param response the response of the current operation * @throws IOException * @throws ServletException */ @Override protected synchronized void showImportPage( HttpServletRequest request, HttpServletResponse response, Connection connection) throws ServletException, IOException { WikiPageBean page = new WikiPageBean(); page.setNotAvailable(true); request.setAttribute("pageBean", page); StringBuilder content = new StringBuilder(); String dumpsPath = getServletContext().getRealPath("/WEB-INF/dumps"); if (currentImport.isEmpty() && importHandler == null) { TreeSet<String> availableDumps = new TreeSet<String>(); File dumpsDir = new File(dumpsPath); if (dumpsDir.isDirectory()) { availableDumps.addAll( Arrays.asList( dumpsDir.list( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return MATCH_WIKI_IMPORT_FILE.matcher(name).matches(); } }))); } // get parameters: String req_import = request.getParameter("import"); if (req_import == null || !availableDumps.contains(req_import)) { content.append("<h2>Please select a wiki dump to import</h2>\n"); content.append("<form method=\"get\" action=\"wiki\">\n"); content.append("<p>\n"); content.append(" <select name=\"import\" size=\"10\" style=\"width:500px;\">\n"); for (String dump : availableDumps) { content.append(" <option>" + dump + "</option>\n"); } content.append(" </select>\n"); content.append(" </p>\n"); content.append( " <p>Maximum number of revisions per page: <input name=\"max_revisions\" size=\"2\" value=\"2\" /></br><span style=\"font-size:80%\">(<tt>-1</tt> to import everything)</span></p>\n"); content.append( " <p>No entry newer than: <input name=\"max_time\" size=\"20\" value=\"\" /></br><span style=\"font-size:80%\">(ISO8601 format, e.g. <tt>2004-01-07T08:09:29Z</tt> - leave empty to import everything)</span></p>\n"); content.append(" <input type=\"submit\" value=\"Import\" />\n"); content.append("</form>\n"); content.append( "<p>Note: You will be re-directed to the main page when the import finishes.</p>"); } else { content.append("<h2>Importing \"" + req_import + "\"...</h2>\n"); try { currentImport = req_import; int maxRevisions = parseInt(request.getParameter("max_revisions"), 2); Calendar maxTime = parseDate(request.getParameter("max_time"), null); importLog = new CircularByteArrayOutputStream(1024 * 1024); PrintStream ps = new PrintStream(importLog); ps.println("starting import..."); String fileName = dumpsPath + File.separator + req_import; if (fileName.endsWith(".db")) { importHandler = new WikiDumpPreparedSQLiteToScalaris(fileName, cPool.getConnectionFactory()); } else { importHandler = new WikiDumpToScalarisHandler( de.zib.scalaris.examples.wikipedia.data.xml.Main.blacklist, null, maxRevisions, null, maxTime, cPool.getConnectionFactory()); } importHandler.setMsgOut(ps); this.new ImportThread(importHandler, fileName, ps).start(); response.setHeader("Refresh", "2; url = wiki?import=" + currentImport); content.append( "<p>Current log file (refreshed automatically every " + IMPORT_REDIRECT_EVERY + " seconds):</p>\n"); content.append("<pre>"); content.append("starting import...\n"); content.append("</pre>"); content.append("<p><a href=\"wiki?import=" + currentImport + "\">refresh</a></p>"); content.append( "<p><a href=\"wiki?stop_import=" + currentImport + "\">stop</a> (WARNING: pages may be incomplete due to missing templates)</p>"); } catch (Exception e) { setParam_error(request, "ERROR: import failed"); addToParam_notice(request, "error: <pre>" + e.getMessage() + "</pre>"); currentImport = ""; } } } else { content.append("<h2>Importing \"" + currentImport + "\"...</h2>\n"); String req_stop_import = request.getParameter("stop_import"); boolean stopImport; if (req_stop_import == null || req_stop_import.isEmpty()) { stopImport = false; response.setHeader( "Refresh", IMPORT_REDIRECT_EVERY + "; url = wiki?import=" + currentImport); content.append( "<p>Current log file (refreshed automatically every " + IMPORT_REDIRECT_EVERY + " seconds):</p>\n"); } else { stopImport = true; importHandler.stopParsing(); content.append("<p>Current log file:</p>\n"); } content.append("<pre>"); String log = importLog.toString(); int start = log.indexOf("\n"); if (start != -1) { content.append(log.substring(start)); } content.append("</pre>"); if (!stopImport) { content.append("<p><a href=\"wiki?import=" + currentImport + "\">refresh</a></p>"); content.append( "<p><a href=\"wiki?stop_import=" + currentImport + "\">stop</a> (WARNING: pages may be incomplete due to missing templates)</p>"); } else { content.append( "<p>Import has been stopped by the user. Return to <a href=\"wiki?title=" + MAIN_PAGE + "\">" + MAIN_PAGE + "</a>.</p>"); } } page.setNotice(WikiServlet.getParam_notice(request)); page.setError(getParam_error(request)); page.setTitle("Import Wiki dump"); page.setPage(content.toString()); RequestDispatcher dispatcher = request.getRequestDispatcher("page.jsp"); dispatcher.forward(request, response); }