⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 installaction.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			this.dropPostgresqlTables(conn);
		}
		else if (dbType.startsWith("mysql")) {
			dbType = "mysql";
		}
		
		boolean status = true;
		List statements = this.readFromDat(SystemGlobals.getApplicationPath() + "/install/" + dbType + "_structure.dat");
		for (Iterator iter = statements.iterator(); iter.hasNext(); ) {
			String query = (String)iter.next();
			if (query == null || "".equals(query.trim())) {
				continue;
			}
			
			Statement s = conn.createStatement();
			
			try {
				s.executeUpdate(query);
			}
			catch (SQLException ex) {
				status = false;

				logger.error("Error executing query: " + query + ": " + ex);
				this.context.put("exceptionMessage", ex.getMessage() + "\n" + query);
				
				break;
			}
			finally {
				s.close();
			}
		}
		
		return status;
	}
	
	private boolean checkForWritableDir()
	{
		boolean canWriteToWebInf = this.canWriteToWebInf();
		boolean canWriteToIndex = this.canWriteToIndex();
		
		if (!canWriteToWebInf || !canWriteToIndex) {
			this.context.put("message", I18n.getMessage("Install.noWritePermission"));
			this.context.put("tryAgain", true);
			this.error();
			return false;
		}

		return true;
	}
	
	private boolean canWriteToWebInf()
	{
		return new File(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) + "/modulesMapping.properties").canWrite();
	}
	
	
	private boolean canWriteToIndex()
	{
		return new File(SystemGlobals.getApplicationPath() + "/index.htm").canWrite();
	}
	
	private void configureNativeConnection() throws Exception
	{
		String username = this.getFromSession("dbUser");
		String password = this.getFromSession("dbPassword");
		String dbName = this.getFromSession("dbName");
		String host = this.getFromSession("dbHost");
		String type = this.getFromSession("database");
		String encoding = this.getFromSession("dbEncoding");
		
		boolean isMysql = type.startsWith("mysql");
		boolean isMysql41 = "mysql41".equals(type);
		
		if (isMysql) {
			type = "mysql";
		}
		
		Properties p = new Properties();
		p.load(new FileInputStream(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) 
				+ "/database/" + type + "/" + type + ".properties"));
		
		// Write database information to the respective file
		p.setProperty(ConfigKeys.DATABASE_CONNECTION_HOST, host);
		p.setProperty(ConfigKeys.DATABASE_CONNECTION_USERNAME, username);
		p.setProperty(ConfigKeys.DATABASE_CONNECTION_PASSWORD, password);
		p.setProperty(ConfigKeys.DATABASE_CONNECTION_DBNAME, dbName);
		p.setProperty(ConfigKeys.DATABASE_CONNECTION_ENCODING, encoding);
		
		if (isMysql41) {
			p.setProperty(ConfigKeys.DATABASE_MYSQL_ENCODING, "");
			p.setProperty(ConfigKeys.DATABASE_MYSQL_UNICODE, "");
		}
		
		try {
			p.store(new FileOutputStream(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) 
					+ "/database/" + type + "/" + type + ".properties"), null);
		}
		catch (Exception e) {
			logger.warn("Error while trying to write to " + type + ".properties: " + e);
		}
		
		// Proceed to SystemGlobals / jforum-custom.conf configuration
		for (Enumeration e = p.keys(); e.hasMoreElements(); ) {
			String key = (String)e.nextElement();
			SystemGlobals.setValue(key, p.getProperty(key));
		}
		
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_HOST, host);
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_USERNAME, username);
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_PASSWORD, password);
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_DBNAME, dbName);
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_ENCODING, encoding);
	}
	
	private void copyFile(String from, String to) throws Exception
	{
		FileChannel source = new FileInputStream(new File(from)).getChannel();
		FileChannel dest =  new FileOutputStream(new File(to)).getChannel();
		
		source.transferTo(0, source.size(), dest);
		source.close();
		dest.close();
	}
	
	private Connection configureDatabase() throws Exception
	{
		String database = this.getFromSession("database");
		String connectionType = this.getFromSession("db_connection_type");
		String implementation;
		boolean isDs = false;
		
		if ("native".equals(connectionType)) {
			implementation = "yes".equals(this.getFromSession("usePool")) 
				? "net.jforum.PooledConnection"
				: "net.jforum.SimpleConnection";
			
			this.configureNativeConnection();
		}
		else {
			isDs = true;
			implementation = "net.jforum.DataSourceConnection";
			SystemGlobals.setValue(ConfigKeys.DATABASE_DATASOURCE_NAME, this.getFromSession("dbdatasource"));
		}
		
		if (database.startsWith("mysql")) {
			if ("mysql41".equals(database)) {
				String path = SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) + "/database/mysql";
				
				this.copyFile(path + "/mysql_41.sql", path + "/mysql_41-bkp.sql");
				
				new File(path + "/mysql.sql").delete();
				new File(path + "/mysql_41.sql").renameTo(new File(path + "/mysql.sql"));
				
				this.copyFile(path + "/mysql_41-bkp.sql", path + "/mysql_41.sql");
			}
			
			database = "mysql";
		}
		
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_IMPLEMENTATION, implementation);
		SystemGlobals.setValue(ConfigKeys.DATABASE_DRIVER_NAME, database);
		
		SystemGlobals.saveInstallation();
		this.restartSystemGlobals();
		
		int fileChangesDelay = SystemGlobals.getIntValue(ConfigKeys.FILECHANGES_DELAY);
		if (fileChangesDelay > 0) {
			FileMonitor.getInstance().addFileChangeListener(new SystemGlobalsListener(),
					SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG), fileChangesDelay);
		}
		
		Connection conn = null;
		
		try {
			DBConnection s;
			
			if (!isDs) { 
				s = new SimpleConnection();
			}
			else {
				s =  new DataSourceConnection();
			}
			
			s.init();
			
			conn = s.getConnection();
		}
		catch (Exception e) {
			logger.warn("Error while trying to get a connection: " + e);
			this.context.put("exceptionMessage", e.getMessage());
			return null;
		}
		
		return conn;
	}
	
	private void restartSystemGlobals() throws Exception
	{
		String appPath = SystemGlobals.getApplicationPath();
		SystemGlobals.initGlobals(appPath, appPath + "/WEB-INF/config/SystemGlobals.properties");
        SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG));
        
        if (new File(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
            SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG));
        }
	}
	
	private boolean updateAdminPassword(Connection conn) throws Exception
	{
		logger.info("Going to update the administrator's password");
		
		boolean status = false;
		
		try {
			PreparedStatement p = conn.prepareStatement("UPDATE jforum_users SET user_password = ? WHERE username = 'Admin'");
			p.setString(1, MD5.crypt(this.getFromSession("adminPassword")));
			p.executeUpdate();
			p.close();
			
			status = true;
		}
		catch (Exception e) {
			logger.warn("Error while trying to update the administrator's password: " + e);
			this.context.put("exceptionMessage", e.getMessage());
		}
		
		return status;
	}
	
	public void checkInformation() throws Exception
	{
		String language = this.request.getParameter("language");
		String database = this.request.getParameter("database");
		String dbHost = this.request.getParameter("dbhost");
		String dbUser = this.request.getParameter("dbuser");
		String dbName = this.request.getParameter("dbname");
		String dbPassword = this.request.getParameter("dbpasswd");
		String dbEncoding = this.request.getParameter("dbencoding");
		String dbEncodingOther = this.request.getParameter("dbencoding_other");
		String usePool = this.request.getParameter("use_pool");
		String forumLink = this.request.getParameter("forum_link");
		String adminPassword = this.request.getParameter("admin_pass1");
		
		dbHost = this.notNullDefault(dbHost, "localhost");
		dbEncodingOther = this.notNullDefault(dbEncodingOther, "utf-8");
		dbEncoding = this.notNullDefault(dbEncoding, dbEncodingOther);
		forumLink = this.notNullDefault(forumLink, "http://localhost");
		dbName = this.notNullDefault(dbName, "jforum");
		
		if ("hsqldb".equals(database)) {
			dbUser = this.notNullDefault(dbUser, "sa");
		}
		
		this.addToSessionAndContext("language", language);
		this.addToSessionAndContext("database", database);
		this.addToSessionAndContext("dbHost", dbHost);
		this.addToSessionAndContext("dbUser", dbUser);
		this.addToSessionAndContext("dbName", dbName);
		this.addToSessionAndContext("dbPassword", dbPassword);
		this.addToSessionAndContext("dbEncoding", dbEncoding);
		this.addToSessionAndContext("usePool", usePool);
		this.addToSessionAndContext("forumLink", forumLink);
		this.addToSessionAndContext("siteLink", this.request.getParameter("site_link"));
		this.addToSessionAndContext("adminPassword", adminPassword);
		this.addToSessionAndContext("dbdatasource", this.request.getParameter("dbdatasource"));
		this.addToSessionAndContext("db_connection_type", this.request.getParameter("db_connection_type"));
		
		this.addToSessionAndContext("configureDatabase", null);
		this.addToSessionAndContext("createTables", null);
		this.addToSessionAndContext("importTablesData", null);
		
		this.context.put("canWriteToWebInf", this.canWriteToWebInf());
		this.context.put("canWriteToIndex", this.canWriteToIndex());
		
		this.context.put("moduleAction", "install_check_info.htm");
	}
	
	private List readFromDat(String filename) throws Exception
	{
		List l = new ArrayList();
		
		FileInputStream fis = new FileInputStream(filename);
		ObjectInputStream in = new ObjectInputStream(fis);
		l = (ArrayList)in.readObject();
		in.close();
		
		return l;
	}
	
	private void dropPostgresqlTables(Connection conn) throws Exception
	{
		String[] tables = { "jforum_banlist", "jforum_banlist_seq", "jforum_categories", 
				"jforum_categories_order_seq", "jforum_categories_seq", "jforum_config",
				"jforum_config_seq", "jforum_forums", "jforum_forums_seq", "jforum_groups",
				"jforum_groups_seq", "jforum_posts", "jforum_posts_seq", "jforum_posts_text",
				"jforum_privmsgs", "jforum_privmsgs_seq", "jforum_privmsgs_text",
				"jforum_ranks", "jforum_ranks_seq", "jforum_role_values", "jforum_roles",
				"jforum_roles_seq", "jforum_search_results", "jforum_search_topics",
				"jforum_search_wordmatch", "jforum_search_words", "jforum_search_words_seq", "jforum_sessions",
				"jforum_smilies", "jforum_smilies_seq", "jforum_themes", "jforum_themes_seq",
				"jforum_topics", "jforum_topics_seq", "jforum_topics_watch", "jforum_user_groups",
				"jforum_users", "jforum_users_seq", "jforum_vote_desc", "jforum_vote_desc_seq",
				"jforum_vote_results", "jforum_vote_voters", "jforum_words", "jforum_words_seq",
				"jforum_karma_seq", "jforum_karma", "jforum_bookmarks_seq", "jforum_bookmarks", 
				"jforum_quota_limit", "jforum_quota_limit_seq", "jforum_extension_groups_seq", 
				"jforum_extension_groups", "jforum_extensions_seq", "jforum_extensions", 
				"jforum_attach_seq", "jforum_attach", "jforum_attach_desc_seq", "jforum_attach_desc",
				"jforum_attach_quota_seq", "jforum_attach_quota" };

		for (int i = 0; i < tables.length; i++) {
			Statement s = conn.createStatement();
			String query = tables[i].endsWith("_seq") ? "DROP SEQUENCE " : "DROP TABLE ";
			query += tables[i];
			
			try {
				s.executeUpdate(query);
			}
			catch (SQLException e) {
				logger.info("IGNORE: " + e.getMessage());
			}
			
			s.close();
		}
	}
	
	private void addToSessionAndContext(String key, String value)
	{
		this.request.getSession().setAttribute(key, value);
		this.context.put(key, value);
	}
	
	private String notNullDefault(String value, String useDefault)
	{
		if (value == null || value.trim().equals("")) {
			return useDefault;
		}
		
		return value;
	}
	
	/** 
	 * @see net.jforum.Command#list()
	 */
	public void list() throws Exception
	{
		this.welcome();
	}
	
	/** 
	 * @see net.jforum.Command#process()
	 */
	public Template process(ActionServletRequest request, 
			HttpServletResponse response, 
			Connection conn, SimpleHash context) throws Exception 
	{
		this.setTemplateName("default/empty.htm");
		return super.process(request, response, conn, context);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -