installer.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 2,278 行 · 第 1/5 页
JAVA
2,278 行
//// // This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2005 The OpenNMS Group, Inc. All rights// reserved.// OpenNMS(R) is a derivative work, containing both original code, included code// and modified// code that was published under the GNU General Public License. Copyrights for// modified// and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// The code in this file is Copyright (C) 2004 DJ Gregor.//// Based on install.pl which was Copyright (C) 1999-2001 Oculan Corp. All// rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program 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 General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact:// OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///package org.opennms.install;import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.PrintStream;import java.io.PrintWriter;import java.io.Reader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.ListIterator;import java.util.Map;import java.util.Properties;import java.util.TreeMap;import java.util.regex.Matcher;import java.util.regex.Pattern;/* * Big To-dos: - Fix all of the XXX items (some coding, some discussion) - * Change the Exceptions to something more reasonable - Do exception handling * where it makes sense (give users reasonable error messages for common * problems) - Add a friendly startup script? - Javadoc */public class Installer { static final float POSTGRES_MIN_VERSION = 7.2f; static final String s_version = "$Id: Installer.java 3056 2006-03-26 19:39:16Z djgregor $"; static final int s_fetch_size = 1024; String m_opennms_home = null; boolean m_update_database = false; boolean m_do_inserts = false; boolean m_update_iplike = false; boolean m_update_unicode = false; boolean m_install_webapp = false; boolean m_fix_constraint = false; boolean m_force = false; boolean m_debug = false; boolean m_ignore_notnull = false; boolean m_no_revert = false; String m_pg_driver = null; String m_pg_url = null; String m_pg_user = "postgres"; String m_pg_pass = ""; String m_pg_bindir = null; String m_user = null; String m_pass = null; String m_database = null; String m_sql_dir = null; String m_create_sql = null; String m_pg_iplike = null; String m_tomcat_conf = null; String m_webappdir = null; String m_tomcatserverlibdir = null; String m_install_webappsdir = null; String m_tomcat_serverlibs = null; String m_fix_constraint_name = null; boolean m_fix_constraint_remove_rows = false; HashMap m_seqmapping = null; LinkedList m_tables = null; LinkedList m_sequences = null; // LinkedList m_cfunctions = new LinkedList(); // Unused, not in create.sql // LinkedList m_functions = new LinkedList(); // Unused, not in create.sql // LinkedList m_languages = new LinkedList(); // Unused, not in create.sql LinkedList m_indexes = new LinkedList(); HashMap m_inserts = new HashMap(); HashSet m_drops = new HashSet(); HashSet m_changed = new HashSet(); float m_pg_version; String m_cascade = ""; String m_sql; PrintStream m_out = System.out; Properties m_properties = null; Connection m_dbconnection; Map m_dbtypes = null; String m_required_options = "At least one of -d, -i, -s, -U, -y, " + "-C, or -T is required."; public void install(String[] argv) throws Exception { printHeader(); loadProperties(); parseArguments(argv); if (!m_update_database && !m_do_inserts && !m_update_iplike && !m_update_unicode && m_tomcat_conf == null && !m_install_webapp && !m_fix_constraint) { throw new Exception("Nothing to do.\n" + m_required_options + "\nUse '-h' for help."); } // Don't bother checking the Java version. Leave it up to runjava. // checkJava(); // XXX Check Tomcat version? if (m_update_database || m_update_iplike || m_update_unicode || m_do_inserts || m_fix_constraint) { databaseConnect("template1"); databaseCheckVersion(); databaseCheckLanguage(); } printDiagnostics(); verifyFilesAndDirectories(); if (m_install_webapp) { checkWebappOldOpennmsDir(); checkServerXmlOldOpennmsContext(); } if (m_update_database || m_fix_constraint) { readTables(); } if (m_update_database) { // XXX Check and optionally modify pg_hba.conf if (!databaseUserExists()) { databaseAddUser(); } if (!databaseDBExists()) { databaseAddDB(); } } if (m_update_database || m_update_iplike || m_update_unicode || m_do_inserts || m_fix_constraint) { databaseDisconnect(); databaseConnect(m_database); } if (m_fix_constraint) { fixConstraint(); } if (m_update_database) { checkOldTables(); checkConstraints(); createTables(); createSequences(); createIndexes(); // createFunctions(m_cfunctions); // Unused, not in create.sql // createLanguages(); // Unused, not in create.sql // createFunctions(m_functions); // Unused, not in create.sql fixData(); } if (m_do_inserts) { insertData(); } if (m_update_unicode) { checkUnicode(); } if (m_install_webapp) { installWebApp(); } if (m_tomcat_conf != null) { updateTomcatConf(); } if (m_update_iplike) { updateIplike(); } if (m_update_database) { // XXX should we be using createFunctions and createLanguages // instead? updatePlPgsql(); // XXX should we be using createFunctions instead? addStoredProcedures(); } if (m_update_database || m_update_iplike || m_update_unicode || m_do_inserts) { databaseDisconnect(); } if (m_update_database) { createConfiguredFile(); } System.out.println(); System.out.println("Installer completed successfully!"); } public void createConfiguredFile() throws IOException { File f = new File(m_opennms_home + File.separator + "etc" + File.separator + "configured"); f.createNewFile(); } public void printHeader() { m_out.println("===============================================" + "==============================="); m_out.println("OpenNMS Installer Version " + s_version); m_out.println("===============================================" + "==============================="); m_out.println(""); m_out.println("Configures PostgreSQL tables, users, and other " + "miscellaneous settings."); m_out.println(""); } public void loadProperties() throws Exception { m_properties = new Properties(); m_properties.load(Installer.class .getResourceAsStream("installer.properties")); /* * Do this if we want to merge our properties with the system * properties... */ Properties sys = System.getProperties(); m_properties.putAll(sys); m_opennms_home = fetchProperty("install.dir"); m_database = fetchProperty("install.database.name"); m_user = fetchProperty("install.database.user"); m_pass = fetchProperty("install.database.password"); m_pg_driver = fetchProperty("install.database.driver"); m_pg_url = fetchProperty("install.database.url"); m_pg_bindir = fetchProperty("install.database.bindir"); m_sql_dir = fetchProperty("install.etc.dir"); m_install_webappsdir = fetchProperty("install.webapps.dir"); m_tomcat_serverlibs = fetchProperty("install.tomcat.serverlibs"); String soext = fetchProperty("build.soext"); String pg_iplike_dir = fetchProperty("install.postgresql.dir"); m_pg_iplike = pg_iplike_dir + File.separator + "iplike." + soext; m_create_sql = m_sql_dir + File.separator + "create.sql"; } public String fetchProperty(String property) throws Exception { String value; if ((value = m_properties.getProperty(property)) == null) { throw new Exception("property \"" + property + "\" not set " + "from bundled installer.properties file"); } return value; } public void parseArguments(String[] argv) throws Exception { LinkedList args = new LinkedList(); for (int i = 0; i < argv.length; i++) { StringBuffer b = new StringBuffer(argv[i]); boolean is_arg = false; while (b.length() > 0 && b.charAt(0) == '-') { is_arg = true; b.deleteCharAt(0); } if (is_arg) { while (b.length() > 0) { char c = b.charAt(0); b.deleteCharAt(0); switch (c) { case 'h': printHelp(); break; case 'c': m_force = true; break; case 'C': i++; m_fix_constraint = true; m_fix_constraint_name = getNextArg(argv, i, 'C'); break; case 'd': m_update_database = true; break; case 'i': m_do_inserts = true; break; case 'N': m_ignore_notnull = true; break; case 'p': i++; m_pg_pass = getNextArg(argv, i, 'p'); break; case 'R': m_no_revert = true; break; case 's': m_update_iplike = true; break; case 'T': i++; m_tomcat_conf = getNextArg(argv, i, 'T'); break; case 'u': i++; m_pg_user = getNextArg(argv, i, 'u'); break; case 'U': m_update_unicode = true; break; case 'w': i++; m_webappdir = getNextArg(argv, i, 'w'); break; case 'W': i++; m_tomcatserverlibdir = getNextArg(argv, i, 'W'); break; case 'x': m_debug = true; break; case 'X': m_fix_constraint_remove_rows = true; break; case 'y': m_install_webapp = true; break; default: throw new Exception("unknown option '" + c + "'" + ", use '-h' option for usage"); } } } else { args.add(argv[i]); } } if (args.size() != 0) { throw new Exception("too many command-line arguments specified"); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?