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

📄 installaction.java

📁 一个论坛程序的简单实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				InstallServlet.getContext().put("exceptionMessage", ex.getMessage() + "\n" + query);
				break;
			}
			finally {
				s.close();
			}
		}
		
		conn.setAutoCommit(autoCommit);
		return status;
	}
	
	private Properties loadProperties(String filename) throws IOException
	{
		Properties p = new Properties();
		FileInputStream inputStream = new FileInputStream(filename);
		p.load(inputStream);
		inputStream.close();
		
		return p;
	}
	
	private boolean createTables(Connection conn) throws Exception
	{
		logger.info("Going to create tables...");
		String dbType = this.getFromSession("database");
		
		if ("postgresql".equals(dbType)) {
			this.dropPostgresqlTables(conn);
		}
		
		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();
			
			Statement s = conn.createStatement();
			
			try {
				s.executeUpdate(query);
			}
			catch (SQLException ex) {
				status = false;
				conn.rollback();

				logger.error("Error executing query: " + query + ": " + ex);
				InstallServlet.getContext().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) {
			InstallServlet.getContext().put("message", I18n.getMessage("Install.noWritePermission"));
			InstallServlet.getContext().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 Connection configureDatabase() 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");

		String implementation = "yes".equals(this.getFromSession("usePool")) 
			? "net.jforum.PooledConnection"
			: "net.jforum.SimpleConnection";
		
		Properties p = new Properties();
		p.load(new FileInputStream(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR) 
				+ "/database/" + type + "/" + type + ".properties"));
		
		for (Enumeration e = p.keys(); e.hasMoreElements(); ) {
			String key = (String)e.nextElement();
			SystemGlobals.setValue(key, p.getProperty(key));
		}
		
		SystemGlobals.setValue(ConfigKeys.DATABASE_CONNECTION_IMPLEMENTATION, implementation);
		SystemGlobals.setValue(ConfigKeys.DATABASE_DRIVER_NAME, type);
		
		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);
		
		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 = new SimpleConnection();
			s.init();
			
			conn = s.getConnection();
		}
		catch (Exception e) {
			logger.warn("Error while trying to get a connection: " + e);
			InstallServlet.getContext().put("exceptionMessage", e.getMessage());
			return null;
		}
		
		return conn;
	}
	
	private void restartSystemGlobals() throws Exception
	{
		if (new File(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
            SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG));
        }
		
		String appPath = SystemGlobals.getApplicationPath();
		SystemGlobals.initGlobals(appPath, appPath + "/WEB-INF/config/SystemGlobals.properties", null);
        SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_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);
			InstallServlet.getContext().put("exceptionMessage", e.getMessage());
		}
		
		return status;
	}
	
	public void checkInformation() throws Exception
	{
		String language = InstallServlet.getRequest().getParameter("language");
		String database = InstallServlet.getRequest().getParameter("database");
		String dbHost = InstallServlet.getRequest().getParameter("dbhost");
		String dbUser = InstallServlet.getRequest().getParameter("dbuser");
		String dbName = InstallServlet.getRequest().getParameter("dbname");
		String dbPassword = InstallServlet.getRequest().getParameter("dbpasswd");
		String dbEncoding = InstallServlet.getRequest().getParameter("dbencoding");
		String dbEncodingOther = InstallServlet.getRequest().getParameter("dbencoding_other");
		String usePool = InstallServlet.getRequest().getParameter("use_pool");
		String forumLink = InstallServlet.getRequest().getParameter("forum_link");
		String adminPassword = InstallServlet.getRequest().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("adminPassword", adminPassword);
		
		this.addToSessionAndContext("configureDatabase", null);
		this.addToSessionAndContext("createTables", null);
		this.addToSessionAndContext("importTablesData", null);
		
		InstallServlet.getContext().put("canWriteToWebInf", this.canWriteToWebInf());
		InstallServlet.getContext().put("canWriteToIndex", this.canWriteToIndex());
		
		InstallServlet.getContext().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" };

		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)
	{
		InstallServlet.getRequest().getSession().setAttribute(key, value);
		InstallServlet.getContext().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() throws Exception 
	{
		this.setTemplateName("default/empty.htm");
		return super.process();
	}
}

⌨️ 快捷键说明

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