Apache Configuration

浏览Apache的项目,突然发现 Apache Configuration   这个好东东,试用了一番,赞不绝口,于是就在这里推荐给各位朋友。 http://jakarta.apache.org/commons/configuration/  
      我们写程序的时候经常需要对一些参数进行 动态配置,比如动态开辟内存的大小,要打开的文件名,可视化程序的背景颜色、窗体大小等等。通常我们会把这些变量写到一个配置文件中,程序每次启动的时候加载它并初始化各种参数,根据需要还可以把发生变化的参数写回配置文件。
      每到这个时候,读写配置文件是一个很繁琐的事情。需要先自己 定义配置文件的格式,然后写代码打开文件,对关键字进行匹配,然后提取子串,再进行String到某类型的转换。总之这个过程是痛苦异常。
      现在有了 Apache Configuration 以后这个过程变得异常的轻松。下面我简单介绍一下 Apache Configuration 的用法。
      Apache Configuration 可以处理多种格式的配置文件,这里我以最常用的XML配置文件为例。假设有一个配置文件名字为table.xml,其内容如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
<rowsPerPage>15</rowsPerPage>
<buttons>
<name>OK,Cancel,Help</name>
</buttons>
<numberFormat pattern="###\,###.##"/>
</gui-definition>
Apache Configuration 读取这个配置文件的代码非常非常的简单。首先生成一个Configuration的实例,同时用xml文件名进行初始化。
try {
XMLConfiguration config = new XMLConfiguration("tables.xml");
}
catch(ConfigurationException cex) {
// something went wrong, e.g. the file was not found
}
初始化完毕后,就可以进行参数的读取了
 
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
String linkNormal = config.getString("colors.link[@normal]");
String defColor = config.getString("colors.default");
int rowsPerPage = config.getInt("rowsPerPage");
List buttons = config.getList("buttons.name");
怎么样,是不是非常的简单:) 除了读取参数之外还可以把修改过的参数存储到配置文件中。
config.setProperty("background", "#999999");
config.setProperty("rowsPerPage", 82);
config.save();
 
简直是太方便了!以后用JAVA写程序再也不用怕繁琐的配置文件操作了!
转载地址:http://www.cnblogs.com/ljkeke/archive/2009/02/02/1382265.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我规整以下代码:package main import ( "fmt" "log" "os" "os/exec" "strings" ) const ( apacheConfPath = "/usr/local/lighthouse/softwares/apache/conf/httpd.conf" // 修改Apache配置文件路径 localPort = "8080" ) func main() { // 修改Apache配置文件中的端口号 err := updateApacheConfig() if err != nil { log.Fatalf("Failed to update Apache configuration: %s", err) } // 重新加载Apache配置 err = reloadApache() if err != nil { log.Fatalf("Failed to reload Apache: %s", err) } // 配置防火墙规则 err = configureFirewall() if err != nil { log.Fatalf("Failed to configure firewall: %s", err) } fmt.Printf("Apache端口已修改为本地可见的端口 %s\n", localPort) } func updateApacheConfig() error { // 打开Apache配置文件 apacheConfFile, err := os.OpenFile(apacheConfPath, os.O_RDWR, 0644) if err != nil { return fmt.Errorf("failed to open Apache configuration file: %w", err) } defer apacheConfFile.Close() // 读取Apache配置文件内容 apacheConfBytes, err := os.ReadFile(apacheConfPath) if err != nil { return fmt.Errorf("failed to read Apache configuration file: %w", err) } // 替换端口号 newConfContent := strings.ReplaceAll(string(apacheConfBytes), "Listen 80", "Listen "+localPort) // 回到文件开头 _, err = apacheConfFile.Seek(0, 0) if err != nil { return fmt.Errorf("failed to seek to the beginning of Apache configuration file: %w", err) } // 清空文件内容 err = apacheConfFile.Truncate(0) if err != nil { return fmt.Errorf("failed to truncate Apache configuration file: %w", err) } // 写入新的配置内容 _, err = apacheConfFile.WriteString(newConfContent) if err != nil { return fmt.Errorf("failed to write updated Apache configuration: %w", err) } return nil } func reloadApache() error { cmd := exec.Command("systemctl", "restart", "httpd") // 使用systemctl命令重新启动Apache err := cmd.Run() if err != nil { return fmt.Errorf("failed to reload Apache: %w", err) } return nil } func configureFirewall() error { // 添加防火墙规则允许从本地访问新端口 allowCmd := exec.Command("iptables", "-A", "INPUT", "-p", "tcp", "--dport", localPort, "-j", "ACCEPT") err := allowCmd.Run() if err != nil { return fmt.Errorf("failed to configure firewall to allow local access: %w", err) } // 阻止从外部访问新端口 dropCmd := exec.Command("iptables", "-A", "INPUT", "-p", "tcp", "--dport", localPort, "-j", "DROP") err = dropCmd.Run() if err != nil { return fmt.Errorf("failed to configure firewall to block external access: %w", err) } // 保存防火墙规则 saveCmd := exec.Command("bash", "-c", "iptables-save > /etc/sysconfig/iptables") err = saveCmd.Run() if err != nil { return fmt.Errorf("failed to save firewall rules: %w", err) } return nil }
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值