📄 managedatabase.java
字号:
* * @param request Servlet request. * @param response Servlet response. * @param session Servlet session for the current user. * @param transfer <code>true</code> if the user has requested a transfer. * <code>false</code> if the user want just to change * in the database settings. */ private void storeSettings( HttpServletRequest request, HttpServletResponse response, HttpSession session, boolean transfer ) throws IOException, ServletException { // set new values in the properties manager... properties.setProperty("db_script", (String) session.getAttribute(CLASS_NAME + "jahiaDBScript")); properties.setProperty("db_driver", (String) session.getAttribute(CLASS_NAME + "jahiaDBDriver")); properties.setProperty("db_url", (String) session.getAttribute(CLASS_NAME + "jahiaDBUrl")); properties.setProperty("db_username", (String) session.getAttribute(CLASS_NAME + "jahiaDBUsername")); properties.setProperty("db_password", (String) session.getAttribute(CLASS_NAME + "jahiaDBPassword")); if(!transfer) { properties.setProperty("db_min_connections", (String) session.getAttribute(CLASS_NAME + "jahiaDBMinConnections")); properties.setProperty("db_max_connections", (String) session.getAttribute(CLASS_NAME + "jahiaDBMaxConnections")); properties.setProperty("db_waitIfBusy", (String) session.getAttribute(CLASS_NAME + "jahiaDBWaitIfBusy")); properties.setProperty("db_verbose", (String) session.getAttribute(CLASS_NAME + "jahiaDBVerbose")); } // write in the jahia properties file... properties.storeProperties(); request.setAttribute( "processMessage", "Please restart Jahia for the changes to take effect." ); session.setAttribute( CLASS_NAME + "jahiaDisplayMessage", Jahia.COPYRIGHT ); JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "finish.jsp" ); } // end storeSettings /** * Grab all the content of a database and create a real SQL dump string. * This method automatically open and close the database connection. * @author Alexandre Kraft * * @param driver The database driver. * @param url The database url. * @param username The database username. * @param password The database password. * @return Returns a big string containing a SQL dump. */ private String grabDatabaseData( String driver, String url, String username, String password ) { String sqlOutput = ""; try { dbConnect.databaseOpen( driver, url, username, password ); ResultSet tablesResultSet = dbConnect.getConnection().getMetaData().getTables(null, null, null, new String[]{"TABLE"}); StringBuffer sqlInsertCurrentValues = new StringBuffer(); Vector tablesResultSetVector = new Vector(); while(tablesResultSet.next()) { tablesResultSetVector.add( tablesResultSet.getString("TABLE_NAME").toLowerCase() ); } tablesResultSet.close(); Enumeration tablesResultSetEnum = tablesResultSetVector.elements(); String pattern = "\\" + "n"; while(tablesResultSetEnum.hasMoreElements()) { String table_name = (String) tablesResultSetEnum.nextElement(); try { if(table_name.substring(0,6).equals("jahia_")) { StringBuffer sqlColumnsList = new StringBuffer(); ResultSet columnsResultSet = dbConnect.getStatement().executeQuery("SELECT * FROM " + table_name); ResultSetMetaData columnsMetaData = columnsResultSet.getMetaData(); int columnCount = columnsMetaData.getColumnCount(); for(int column=1; column<=columnCount; column++) { if(column > 1) { sqlColumnsList.append(", "); } sqlColumnsList.append(columnsMetaData.getColumnLabel(column).toLowerCase()); } while(columnsResultSet.next()) { StringBuffer sqlValuesList = new StringBuffer(); for(int column=1; column<=columnCount; column++) { String column_name = columnsResultSet.getString(column); int column_type = columnsMetaData.getColumnType(column); boolean isText = false; if(column > 1) { sqlValuesList.append(", "); } if((column_type == Types.VARCHAR) || (column_type == Types.CHAR) || (column_type == Types.BLOB) || (column_type == Types.LONGVARCHAR)) { isText = true; } else if(column_type == Types.OTHER) { try { int testConversion = Integer.parseInt(column_name); } catch (NumberFormatException nfe) { isText = true; } } if(isText) { sqlValuesList.append("'"); } sqlValuesList.append(column_name); if(isText) { sqlValuesList.append("'"); } } String parsedString = JahiaTools.replacePattern(sqlValuesList.toString(),"\n",pattern); parsedString = JahiaTools.replacePattern(parsedString,"\r",""); sqlInsertCurrentValues.append("INSERT INTO " + table_name + "(" + sqlColumnsList.toString().toLowerCase() + ") VALUES(" + parsedString + ")\n"); } columnsResultSet.close(); } } catch (IndexOutOfBoundsException ioobe) { } catch (SQLException sqle) { } } dbConnect.databaseClose(); sqlOutput = sqlInsertCurrentValues.toString(); } catch (Exception e) { } return sqlOutput; } // end grabDatabaseData /** * Insert data into a database via the previously grabbed SQL dump. * @author Alexandre Kraft * * @param allGrabbedData The SQL dump (get it with grabDatabaseData method). * @param driver The database driver. * @param url The database url. * @param username The database username. * @param password The database password. */ private void insertDatabaseData( Enumeration allGrabbedData, String driver, String url, String username, String password ) { String lastTableName = ""; // try to execute the SQL query... try { dbConnect.databaseOpen( driver, url, username, password ); while(allGrabbedData.hasMoreElements()) { try { String runtimeSQL = (String) allGrabbedData.nextElement(); String runtimeTableName = runtimeSQL.substring(runtimeSQL.indexOf("jahia_"), runtimeSQL.indexOf("(")).trim(); try { if(!runtimeTableName.equals(lastTableName)) { ServicesRegistry.getInstance().getDBPoolService().executeUpdate(dbConnect.getStatement(), "DELETE FROM " + runtimeTableName ); } } catch (SQLException sqle) { } try { Statement stm = dbConnect.getStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stm,JahiaTools.replacePattern(runtimeSQL,"\\n", "\n")); } catch (SQLException sqle) { } lastTableName = runtimeTableName; } catch (IndexOutOfBoundsException ioobe) { } } dbConnect.databaseClose(); } catch (NullPointerException npe) { } } // end insertDatabaseData /** * Create tables into a database for the previously grabbed SQL dump. * This option run via the database script. * @author Alexandre Kraft * * @param scriptFileName The filename of the database script. * @param driver The database driver. * @param url The database url. * @param username The database username. * @param password The database password. */ private void insertDatabaseTables( String scriptFileName, String driver, String url, String username, String password ) { String lastTableName = ""; // try to execute the SQL query... try { dbConnect.databaseOpen( driver, url, username, password ); // get script runtime... File scriptsFileObject = new File( Jahia.jahiaDatabaseScriptsPath + File.separator + scriptFileName ); Enumeration scriptsListRuntime = scriptsManager.getDatabaseScriptsRuntime( scriptsFileObject ); while(scriptsListRuntime.hasMoreElements()) { try { String runtimeSQL = (String) scriptsListRuntime.nextElement(); String runtimeTableName = runtimeSQL.substring(runtimeSQL.indexOf("jahia_"), runtimeSQL.indexOf("(")).trim(); try { dbConnect.getStatement().execute( "DROP TABLE " + runtimeTableName ); } catch (SQLException sqle) { } try { dbConnect.getStatement().execute( runtimeSQL ); } catch (SQLException sqle) { } } catch (IndexOutOfBoundsException ioobe) { } } dbConnect.databaseClose(); } catch (NullPointerException npe) { } catch (IOException ioe) { } } // end insertDatabaseTables /** * This method check if the database url setted in the formular by * the user is the same or not as the current database. If it's the same * return an error (i don't want to transfer a database on herself :o) * @author Alexandre Kraft * * @param url The database url to check. * @return <code>true</code> if the user has indicate the same database, * <code>false</code> if the user has indicate one another database. */ private boolean checkSameDatabase( String db_url ) throws IOException, ServletException { boolean theSameDatabase = false; if(db_url.equals(properties.getProperty("db_url"))) { theSameDatabase = true; } return theSameDatabase; } // end checkSameDatabase} // end ManageServer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -