1.概述
Ice使用了一种配置机制,允许我们控制自己的Ice应用在运行时的许多行为,比如最大消息尺寸、线程数,是否产生网络跟踪消息。这种机制不仅能用于配置Ice,还可以用它来给自己的应用提供配置参数。
2.属性
Ice及其各子系统是通过属性(property)来配置的。一个属性就是一个名-值对(name-value)。例如: Ice.UDP.SendSize = 65535;
保留的前缀:Ice , IceBox , IcePack , IcePatch , IceSSL , IceStorm , Freeze及Glacier。我们不能用这些前缀起头的属性来配置自己的应用。
3.配置文件
属性通常在配置文件中设置。配置文件含有一些名-值对,每一对都在单独的行上。空行及完全有空白字符组成的行会被忽略。#字符后面的字符串是注释,直到当前行行尾。
配置文件示例:
# Example config file for Ice
Ice.MessageSizeMax = 2048 # Larger message size is 2MB
Ice.Trace.NetWork = 3# Highest level of tracing for network
Ice.Trace.Protocol =# Disable protocol tracing
如果把空置赋给属性,就会清除该属性。
对于C++,Ice会在我们创建通信器时读取配置文件的内容,配置文件名由ICE_CONFIG环境变量的内容来决定。例如:
$ ICE_CONFIG = /opt/Ice/default_config
$ export ICE_CONFIG
$ ./server
这使得服务器从配置文件/opt/Ice/default_config中读取它的属性设置。
4.在命令行中设置属性
除了在配置文件中设置属性。我们还可以在命令行上设置属性,例如:
$ ./server –Ice.UDP.SendSize=65535 –IceSSL.Trace.Security=2
任何以—起头,并且后跟某个保留前缀的命令行选项,都会在创建通信器时被读取,并转换成属性设置。命令行中的属性设置会覆盖配置文件中的设置。
为方便起见,任何没有明确设置的属性都会被设置为值1.
例如:./server –Ice.Trace.Protocol等价于 ./server –Ice.Trace.protocol=1(只适合命令行设置)
5.Ice.Config属性
对于Ice run time而言,Ice.Config属性有着特殊含义:它确定用于读取属性设置的配置文件的路径名。例如:
$ ./server –Ice.Config=/usr/local/filesystem/config
这使得配置设置从/usr/local/filesystem/comfig配置文件中被读出。
6.命令行解析与初始化
如果我们使用了Ice::Application助手类,run方法收到的参数也是清理过的参数向量。
7.Ice.ProgramName属性
在C++中,initialize把Ice.ProgramName属性设成当前程序的名字(argv[0])。Ice把程序名用于日志信息。
8.在程序中使用属性
Ice属性机制不仅可用于配置Ice,还可以用于自己的应用程序的配置机制。例如我们引入一个属性,控制我们的文件系统应用的最大文件尺寸:
# Configuration file for file system application
Filesystem.MaxFileSize = 1024 #Max file size in kB
Ice run time像存储其他任何属性一样存储FileSystem.MaxFileSize属性,并且让我们通过Properties接口访问它。
要在自己的程序里访问属性值,需要调用getProperties,获取通信器的各个属性:
module Ice {
localinterface Properties; // Forward declaration
localinterface Communicator {
PropertiesgetProperties();
// ...
};
};
Properties接口提供了用于读写属性设置的方法:
module Ice {
local dictionary<string,string> PropertyDict;
local interface Properties {
stringgetProperty(string key);
stringgetPropertyWithDefault(string key, string value);
intgetPropertyAsInt(string key);
intgetPropertyAsIntWithDefault(string key, int value);
PropertyDictgetPropertiesForPrefix(string prefix);
voidsetProperty(string key, string value);
StringSeqgetCommandLineOptions();
StringSeqparseCommandLineOptions(string prefix,
StringSeqoptions);
StringSeqparseIceCommandLineOptions(StringSeq options);
voidload(string file);
Propertiesclone();
};
};
8.1)读取属性
getProperty:这个操作返回指定属性的值。
getPropertyWithDefault:如果该属性没有设置,操作返回自己提供的缺省值。
getPropertyAsInt:把指定属性的值作为整数返回。
getPropertyAsIntWithDefault:若属性没有设置,或包含的是不能解析成整数的串,返回自己所提供的缺省值。
getPropertyForPrefix:把以指定前缀起头的所有属性,作为PropertyDict类型的词典返回。
例如:
// ...
Ice::CommunicatorPtric;
// ...
ic =Ice::initialize(argc, argv);
// Getthe maximum file size.
//
Ice::PropertiesPtr props =ic->getProperties();
Ice::IntmaxSize
=props->getPropertyAsIntWithDefault("Filesystem.MaxFileSize",
1024);
// ...
假定我们创建了一个配置文件,用于设置Filesystem.MaxFileSize属性(并相应地设置了ICE_CONFIG变量或—Ice.Config选项),我们的应用将会获得所配置的属性值。
8.2)设置属性
setProperty 操作把某个属性设成指定的值(只要把属性设成空串,你就可以清除它)。只有在你调用initialize 之前,这个操作才有用。如果不管配置文件中的设置是什么,你都想要确保某个属性被设成特定的值, getDefaultProperties 会很有用。例如:
// Get the initialized property set.
//
Ice::PropertiesPtr props = Ice::getDefaultProperties(argc, argv);
// Make sure that network and protocol tracing are off.
//
props->setProperty("Ice.Trace.Network", "0");
props->setProperty("Ice.Trace.Protocol", "0");
// Initialize a communicator with these properties.
//
Ice::CommunicatorPtr ic = Ice::initialize(argc, argv);
// ...
8.3)解析属性
Properties接口提供了三个用于转换和解析属性的操作:getCommandLineOptions,parseCommandLineOptions,parseIceCommandLineOptions。
8.4)实用操作
properties接口提供了两个实用操作:clone--这个操作制作一个现有属性集的副本。
load-- 这个操作的参数是一个配置文件的路径名,他会根据这个文件初始化属性集。
8.5)处理多个属性集
有时在程序中可能使用了多个通信器,因而需要使用多个属性集。为了能都使用独立的属性集,Ice提供了用于创建新属性集的实用函数:
// Create a property set for the first communcator.
//
Ice::PropertiesPtr props1 = createProperties();
// Make sure that network tracing is off.
//
props1->setProperty("Ice.Trace.Network", "0");
// Initialize a communicator with this property set.
//
Ice::CommunicatorPtr ic1
= Ice::initializeWithProperties(argc, argv, props1);
9.总结
Ice属性机制提供了一种简单的配置Ice的路径,我们可以再配置文件中或在命令行上设置属性。用于访问属性的API小而简单,所以要在运行时用它获取属性集很容易。这是Ice的灵活性的表现。
--------------------------------------------------------------------------------------
以上笔记的参考资料来自《Ice1.3.0(冯威达 译)》与《Ice3.4.2(原版)》