Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Znav
nextParsing Properties
prevReading Properties

The setProperty operation on the Properties interface sets a property to the specified value:

Wiki Markup
{zcode:slice}
module Ice {
    local interface Properties {

        void setProperty(string key, string value);

        // ...
    };
};
{zcode}

You can clear a property by setting it to the empty string.

For properties that control the Ice run time and its services (that is, properties that start with one of the reserved prefixes), this operation is useful only if you call it before you call initialize. This is because property values are usually read by the Ice run time only once, when you call initialize, so the Ice run time does not pay attention to a property value that is changed after you have initialized a communicator. Of course, this begs the question of how you can set a property value and have it also recognized by a communicator.

To permit you to set properties before initializing a communicator, the Ice run time provides an overloaded helper function called createProperties that creates a property set. In C++, the function is in the Ice namespace:

Wiki Markup
{zcode:cpp}
namespace Ice {

PropertiesPtr createProperties(const StringConverterPtr& = 0);
PropertiesPtr createProperties(StringSeq&,
                               const PropertiesPtr& = 0,
                               const StringConverterPtr& = 0);
PropertiesPtr createProperties(int&, char*[],
                               const PropertiesPtr& = 0,
                               const StringConverterPtr& = 0);

}
{zcode}

The StringConverter parameter allows you to parse properties whose values contain non-ASCII characters and to correctly convert these characters into the native codeset. The converter that is passed to createProperties remains attached to the returned property set for the life time of the property set.

The function is overloaded to accept either an argc/argv pair or a StringSeq, to aid in parsing properties.

In Java, the functions are static methods of the Util class inside the Ice package:

Wiki Markup
{zcode:java}
package Ice;

public final class Util
{
    public static Properties
    createProperties();

    public static Properties
    createProperties(StringSeqHolder args);

    public static Properties
    createProperties(StringSeqHolder args, Properties defaults);

    public static Properties
    createProperties(String[] args);

    public static Properties
    createProperties(String[] args, Properties defaults);

    // ...
}
{zcode}

In C#, the Util class in the Ice namespace supplies equivalent methods:

Wiki Markup
{zcode:cs}
namespace Ice {
    public sealed class Util {
        public static Properties createProperties();
        public static Properties createProperties(ref string[] args);
        public static Properties createProperties(ref string[] args, Properties defaults);
    }
}
{zcode}

The Python and Ruby methods reside in the Ice module:

Wiki Markup
{zcode}
def createProperties(args=[], defaults=None)
{zcode}

In PHP, use the Ice_createProperties method:

Wiki Markup
{zcode:php}
function Ice_createProperties(args=array(), defaults=null)
{zcode}

As for initialize, createProperties strips Ice-related command-line options from the passed argument vector. (For Java, only the versions that accept a StringSeqHolder do this.)

The functions behave as follows:

  • The parameter-less version of createProperties simply creates an empty property set. It does not check ICE_CONFIG for a configuration file to parse.
  • The other overloads of createProperties accept an argument vector and a default property set. The returned property set contains all the property settings that are passed as the default, plus any property settings in the argument vector. If the argument vector sets a property that is also set in the passed default property set, the setting in the argument vector overrides the default. The overloads that accept an argument vector also look for the --Ice.Config option; if the argument vector specifies a configuration file, the configuration file is parsed. The order of precedence of property settings, from lowest to highest, is:
    • Property settings passed in the default parameter
    • Property settings set in the configuration file
    • Property settings in the argument vector.
      The overloads that accept an argument vector also look for the setting of the ICE_CONFIG environment variable and, if that variable specifies a configuration file, parse that file. (However, an explicit --Ice.Config option in the argument vector or the defaults parameter overrides any setting of the ICE_CONFIG environment variable.)

createProperties is useful if you want to ensure that a property is set to a particular value, regardless of any setting of that property in a configuration file or in the argument vector. Here is an example:

Wiki Markup
{zcode:cpp}
// Get the initialized property set.
//
Ice::PropertiesPtr props = Ice::createProperties(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::InitializationData id;
id.properties = props;
Ice::CommunicatorPtr ic = Ice::initialize(id);

// ...
{zcode}

The equivalent Java code looks as follows:

Wiki Markup
{zcode:java}
Ice.StringSeqHolder argsH = new Ice.StringSeqHolder(args);
Ice.Properties properties = Ice.Util.createProperties(argsH);
properties.setProperty("Ice.Warn.Connections", "0");
properties.setProperty("Ice.Trace.Protocol", "0");
Ice.InitializationData id = new Ice.InitializationData();
id.properties = properties;
communicator = Ice.Util.initialize(id);
{zcode}

We first convert the argument array to an initialized StringSeqHolder. This is necessary so createProperties can strip Ice-specific settings. In that way, we first obtain an initialized property set, then override the settings for the two tracing properties, and then set the properties in the InitializationData structure.

The equivalent Python code is shown next:

Wiki Markup
{zcode:py}
props = Ice.createProperties(sys.argv)
props.setProperty("Ice.Trace.Network", "0")
props.setProperty("Ice.Trace.Protocol", "0")
id = Ice.InitializationData()
id.properties = props
ic = Ice.initialize(id)
{zcode}

This is the equivalent code in Ruby:

Wiki Markup
{zcode:rb}
props = Ice::createProperties(ARGV)
props.setProperty("Ice.Trace.Network", "0")
props.setProperty("Ice.Trace.Protocol", "0")
id = Ice::InitializationData.new
id.properties = props
ic = Ice::initialize(id)
{zcode}

Finally, we present the code in PHP:

Wiki Markup
{zcode:php}
$props = Ice_createProperties($args);
$props->setProperty("Ice.Trace.Network", "0");
$props->setProperty("Ice.Trace.Protocol", "0");
$id = new Ice_InitializationData();
$id->properties = $props;
$ic = Ice_initialize($id);
{zcode}
Ztop
See Also
Zret
Znav
nextParsing Properties
prevReading Properties