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

📄 cmssetup.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/boot/CmsSetup.java,v $
* Date   : $Date: 2003/04/10 07:33:04 $
* Version: $Revision: 1.22 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.boot;

import com.opencms.flex.util.CmsUUID;

import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import source.org.apache.java.util.ExtendedProperties;

/**
 * Bean with get / set methods for all properties stored in the
 * 'opencms.properties' file. The path to the opencms home folder and
 * its config folder can also be stored an retrieved as well as a vector
 * containing possible error messages thrown by the setup.
 *
 * @author Magnus Meurer
 * @author Thomas Weckert (t.weckert@alkacon.com)
 */
public class CmsSetup {

	/** 
	 * Contains error messages, displayed by the setup wizard 
	 */
	private static Vector errors = new Vector();

	/** 
	 * Contains the properties from the opencms.properties file 
	 */
	private ExtendedProperties m_ExtProperties;

	/** 
	 * properties from dbsetup.properties 
	 */
	private Properties m_DbProperties;

	/** Contains the absolute path to the opencms home directory */
	private String m_basePath;

	/** 
	 * Indicates if the user has chosen standard (false) or advanced (true) setup
	 */
	private boolean m_setupType;

	/**
	 * name of the database (mysql)
	 */
	private String m_database;

	/**
	 * database password used to drop and create database
	 */
	private String m_dbCreatePwd;

	/**
	 * replacer string
	 */
	private Hashtable m_replacer;

	/** 
	 * This method reads the properties from the opencms.property file
	 * and sets the CmsSetup properties with the matching values.
	 * This method should be called when the first page of the OpenCms
	 * Setup Wizard is called, so the input fields of the wizard are pre-defined
	 */
	public void initProperties(String props) {
		String path = getConfigFolder() + props;
		try {
			FileInputStream fis = new FileInputStream(new File(path));
			m_ExtProperties = new ExtendedProperties();
			m_ExtProperties.load(fis);
			fis.close();
			m_DbProperties = new Properties();
			m_DbProperties.load(getClass().getClassLoader().getResourceAsStream("com/opencms/boot/dbsetup.properties"));
		}
		catch (Exception e) {
			e.printStackTrace();
			errors.add(e.toString());
		}
	}
    
    /**
     * This method checks the validity of the given properties
     * and adds unset properties if possible
     * @return boolean true if all properties are set correctly
     */
    public boolean checkProperties() {

        // check if properties available
        if (getProperties() == null) {
            return false;
        }

        // check the ethernet address
        // in order to generate a random address, if not available                   
        if ("".equals(getEthernetAddress())) {
            setEthernetAddress(CmsUUID.getDummyEthernetAddress());
        }
        // check the maximum file size, set it to unlimited, if not valid 
        String size = getFileMaxUploadSize();
        if (size == null || "".equals(size)) {
            setFileMaxUploadSize("-1");
        }
        else {
            try {
                Integer.parseInt(size);
            } catch (Exception e) {
                setFileMaxUploadSize("-1");
            }
        }
 
        return true;
    }

	/** 
	 * This method sets the value for a given key in the extended properties.
	 * @param key The key of the property
	 * @param value The value of the property
	 */
	public void setExtProperty(String key, String value) {
		m_ExtProperties.put(key, value);
	}

	/**
	 * Returns the value for a given key from the extended properties.
	 * @return the string value for a given key
	 */
	public String getExtProperty(String key) {
		Object value = null;
            
		return ((value = m_ExtProperties.get(key)) != null) ? value.toString() : "";
	}

	/** 
	 * This method sets the value for a given key in the database properties.
	 * @param key The key of the property
	 * @param value The value of the property
	 */
	public void setDbProperty(String key, String value) {
		m_DbProperties.put(key, value);
	}

	/**
	 * Returns the value for a given key from the database properties.
	 * @return the string value for a given key
	 */
	public String getDbProperty(String key) {
        Object value = null;            
        return ((value = m_DbProperties.get(key)) != null) ? value.toString() : "";        
	}

	/**
	 * Safely inserts a backslash before each comma in a given string.
	 */
    /*
	private String escapeComma(String str) {
		StringBuffer dummy = new StringBuffer("");
		char[] chars = str.toCharArray();
		int len = chars.length;

		try {
			for (int i = 0; i < len; i++) {
				if (chars[i] == ',') {
					dummy.append('\\');
				}
				dummy.append(chars[i]);
			}
		}
		catch (Exception e) {
            System.out.println( e.toString() );
		}

		return dummy.toString();
	}
    */

	/** Sets the path to the OpenCms home directory */
	public void setBasePath(String basePath) {
		m_basePath = basePath;
		if (!m_basePath.endsWith(File.separator)) {
			// Make sure that Path always ends with a separator, not always the case in different environments
			// since getServletContext().getRealPath("/") does not end with a "/" in all servlet runtimes
			m_basePath += File.separator;
		}
	}

	/** Returns the absolute path to the OpenCms home directory */
	public String getBasePath() {
		return m_basePath.replace('\\', '/').replace('/', File.separatorChar);
	}

	/** Sets the setup type to the given value: standard (false), advanced (true) */
	public void setSetupType(boolean setupType) {
		m_setupType = setupType;
	}

	/** Returns the value of the setup type: standard (false), advanced (true) */
	public boolean getSetupType() {
		return m_setupType;
	}

	/** Sets the resource broker to the given value */
	public void setResourceBroker(String resourceBroker) {
		setExtProperty("resourcebroker", resourceBroker);
	}

	/** Gets the resource broker */
	public String getResourceBroker() {
		return this.getExtProperty("resourcebroker");
	}

	/** Returns all resource Brokers found in 'dbsetup.properties' */
	public Vector getResourceBrokers() {
		Vector values = new Vector();

		String value = this.getDbProperty("resourceBrokers");
		StringTokenizer tokenizer = new StringTokenizer(value, ",");
		while (tokenizer.hasMoreTokens()) {
			values.add(tokenizer.nextToken().trim());
		}
		return values;
	}
    
    /** Returns "nice display names" for all resource Brokers found in 'dbsetup.properties' */
    public Vector getResourceBrokerNames() {
        Vector values = new Vector();

        String value = this.getDbProperty("resourceBrokerNames");
        StringTokenizer tokenizer = new StringTokenizer(value, ",");
        while (tokenizer.hasMoreTokens()) {
            values.add(tokenizer.nextToken().trim());
        }
        return values;
    }    

	/** Sets the connection string to the database to the given value */
	public void setDbWorkConStr(String dbWorkConStr) {
		setExtProperty("pool." + getResourceBroker() + ".url", dbWorkConStr);
		setExtProperty("pool." + getResourceBroker() + "backup.url", dbWorkConStr);
		setExtProperty("pool." + getResourceBroker() + "online.url", dbWorkConStr);
	}

	/** Sets the user of the database to the given value */
	public void setDbWorkUser(String dbWorkUser) {
		setExtProperty("pool." + getResourceBroker() + ".user", dbWorkUser);
		setExtProperty("pool." + getResourceBroker() + "backup.user", dbWorkUser);
		setExtProperty("pool." + getResourceBroker() + "online.user", dbWorkUser);
	}

	/** Returns the user of the database from the properties */
	public String getDbWorkUser() {
		return this.getExtProperty("pool." + getResourceBroker() + ".user");
	}

	/** Sets the password of the database to the given value */
	public void setDbWorkPwd(String dbWorkPwd) {
		setExtProperty("pool." + getResourceBroker() + ".password", dbWorkPwd);
		setExtProperty("pool." + getResourceBroker() + "backup.password", dbWorkPwd);
		setExtProperty("pool." + getResourceBroker() + "online.password", dbWorkPwd);
	}

	/** Returns a conenction string */
	public String getDbWorkConStr() {
		return this.getExtProperty("pool." + getResourceBroker() + ".url");
	}

	/** Returns the password of the database from the properties */
	public String getDbWorkPwd() {
		return this.getExtProperty("pool." + getResourceBroker() + ".password");
	}
 
	/** Returns the extended properties */
	public ExtendedProperties getProperties() {
		return m_ExtProperties;
	}

	/** Adds a new error message to the vector */
	public static void setErrors(String error) {
		errors.add(error);
	}

	/** Returns the error messages */
	public Vector getErrors() {
		return errors;
	}

	/** Returns the path to the opencms config folder */
	public String getConfigFolder() {
		return (m_basePath + "WEB-INF/config/").replace('\\', '/').replace('/', File.separatorChar);
	}

	/** Returns the database driver belonging to the resource broker */
	public String getDbDriver() {
		return m_ExtProperties.get("pool." + getResourceBroker() + ".driver").toString();
	}

	/** Sets the minimum connections to the given value */
	public void setMinConn(String minConn) {
		setExtProperty("pool." + getResourceBroker() + ".minConn", minConn);
	}

	/** Returns the min. connections */
	public String getMinConn() {
		return this.getExtProperty("pool." + getResourceBroker() + ".minConn");
	}

	/** Sets the maximum connections to the given value */
	public void setMaxConn(String maxConn) {
		setExtProperty("pool." + getResourceBroker() + ".maxConn", maxConn);
	}

	/** Returns the max. connections */
	public String getMaxConn() {
		return this.getExtProperty("pool." + getResourceBroker() + ".maxConn");
	}

	/** Sets the increase rate to the given value */
	public void setIncreaseRate(String increaseRate) {
		setExtProperty("pool." + getResourceBroker() + ".increaseRate", increaseRate);
	}

	/** Returns the increase rate */
	public String getIncreaseRate() {
		return this.getExtProperty("pool." + getResourceBroker() + ".increaseRate");
	}

⌨️ 快捷键说明

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