📄 configuretask.java
字号:
package crms.ant;import java.util.*;import java.io.*;import org.apache.tools.ant.*;import org.apache.tools.ant.input.*;public class ConfigureTask extends Task { static final String OUTPUT_FILES[] = { "conf/crms.conf", "conf/daemon.init", "conf/web.xml", "conf/log4j.config", "conf/ant.properties", "src/crms/servlet/JSPConfig.java" }; static final String CACHE_FILE = "config.cache"; boolean use_existing = false; protected void prompt(String prompt, String variable, HashMap settings) { prompt(prompt, variable, settings, null); } protected void prompt(String prompt, String variable, HashMap settings, String help) { InputRequest request = null; String pstr = new String(prompt); String initial = (String)settings.get(variable); if (use_existing) { if (initial != null) { return; } else { pstr += " (New)"; } } if (initial != null) { pstr += " [" + initial + "]"; } if (help != null) { pstr += "(?)"; } pstr += ": "; request = new InputRequest(pstr); getProject().getInputHandler().handleInput(request); String result = request.getInput(); if (result.trim().compareTo("?") == 0) { if (help != null) { System.out.println("Help: " + help); } else { System.out.println("Help not available for this option."); } prompt(prompt, variable, settings, help); return; } if (result.trim().length() != 0 || initial == null) { settings.put(variable, result); } } public void execute() throws BuildException { HashMap settings; // load setting cache settings = readSettingCache(); if (settings == null) { settings = defaultSettings(); } else { prompt("Use previously cached settings? (Y/N)", "use_cache", settings, "The answer Y here will use the saved settings instead of prompting again."); use_existing = ((String)settings.get("use_cache")).trim().regionMatches(true, 0, "Y", 0, 1); } prompt("Tomcat Host", "tc_host", settings); prompt("Tomcat port", "tc_port", settings); prompt("Webmail URL", "webmail", settings); prompt("LDAP Server", "ldap_host", settings); prompt("LDAP Port", "ldap_port", settings); prompt("LDAP Base DN", "ldap_base", settings); prompt("LDAP User DN", "ldap_userdn", settings); prompt("Database Server", "db_host", settings); prompt("Database User", "db_user", settings); prompt("Database Password", "db_pass", settings); prompt("Daemon Install Path", "daemon_home", settings); prompt("Daemon Log File", "daemon_log", settings); prompt("Daemon PID File", "daemon_pid", settings); prompt("CRMS Data Path", "crms_home", settings, "Report and other information is saved here as well as file upload data. Default: /home/crms"); // save cache storeSettingCache(settings); // produce files for (int i = 0; i < OUTPUT_FILES.length; i++) { processFile(OUTPUT_FILES[i], settings); } System.out.println("done."); } protected HashMap readSettingCache() { HashMap settings = null; try { BufferedReader in = new BufferedReader(new FileReader(CACHE_FILE)); String line; settings = new HashMap(); while ( (line = in.readLine()) != null ) { String tokens[] = line.split("=", 2); settings.put(tokens[0], tokens[1]); } } catch (Exception e) { } return settings; } protected void storeSettingCache(HashMap settings) { Set keys = settings.keySet(); Iterator i = keys.iterator(); try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(CACHE_FILE))); while (i.hasNext()) { String key = (String)i.next(); out.println(key + "=" + (String)settings.get(key)); } out.close(); } catch (Exception e) { e.printStackTrace(); } } protected void processFile(String file, HashMap settings) { System.out.println("Writing " + file + "..."); try { BufferedReader in = new BufferedReader(new FileReader(file + ".in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file))); String line = null; while ( (line = in.readLine()) != null ) { Set keys = settings.keySet(); Iterator i = keys.iterator(); while (i.hasNext()) { String key = (String)i.next(); String search = "@" + key.toUpperCase() + "@"; line = line.replaceAll(search, (String)settings.get(key)); // System.out.println("s/" + search + "/" + settings.get(key)); } //System.out.println("line="+line); out.println(line); } out.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } protected HashMap defaultSettings() { HashMap settings = new HashMap(); settings.put("use_cache", "Y"); settings.put("webmail", "http://localhost/src/compose.php"); settings.put("tc_host", "localhost"); settings.put("tc_port", "8080"); settings.put("ldap_host", "localhost"); settings.put("db_host", "localhost"); settings.put("db_name", "crms"); settings.put("db_user", "postgres"); settings.put("db_pass", "postgres"); settings.put("ldap_port", "389"); settings.put("ldap_base", "o=UNKNOWN,c=AU"); settings.put("ldap_userdn", "ou=Staff"); settings.put("daemon_home", "/usr/local/sbin"); settings.put("daemon_log", "/var/log/crmsdaemon.log"); settings.put("daemon_pid", "/var/run/crmsdaemon.pid"); settings.put("crms_home", "/home/crms"); return settings; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -