1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 using System;
6 using System.Net;
7 using System.Text;
8 
9 namespace IceInternal
10 {
11     public sealed class DefaultsAndOverrides
12     {
DefaultsAndOverrides(Ice.Properties properties, Ice.Logger logger)13         internal DefaultsAndOverrides(Ice.Properties properties, Ice.Logger logger)
14         {
15             string val;
16 
17             defaultProtocol = properties.getPropertyWithDefault("Ice.Default.Protocol", "tcp");
18 
19             val = properties.getProperty("Ice.Default.Host");
20             if(val.Length != 0)
21             {
22                 defaultHost = val;
23             }
24             else
25             {
26                 defaultHost = null;
27             }
28 
29             val = properties.getProperty("Ice.Default.SourceAddress");
30             if(val.Length > 0)
31             {
32                 defaultSourceAddress = Network.getNumericAddress(val);
33                 if(defaultSourceAddress == null)
34                 {
35                     throw new Ice.InitializationException("invalid IP address set for Ice.Default.SourceAddress: `" +
36                                                           val + "'");
37                 }
38             }
39             else
40             {
41                 defaultSourceAddress = null;
42             }
43 
44             val = properties.getProperty("Ice.Override.Timeout");
45             if(val.Length > 0)
46             {
47                 overrideTimeout = true;
48                 overrideTimeoutValue = properties.getPropertyAsInt("Ice.Override.Timeout");
49                 if(overrideTimeoutValue < 1 && overrideTimeoutValue != -1)
50                 {
51                     overrideTimeoutValue = -1;
52                     StringBuilder msg = new StringBuilder("invalid value for Ice.Override.Timeout `");
53                     msg.Append(properties.getProperty("Ice.Override.Timeout"));
54                     msg.Append("': defaulting to -1");
55                     logger.warning(msg.ToString());
56                 }
57             }
58             else
59             {
60                 overrideTimeout = false;
61                 overrideTimeoutValue = -1;
62             }
63 
64             val = properties.getProperty("Ice.Override.ConnectTimeout");
65             if(val.Length > 0)
66             {
67                 overrideConnectTimeout = true;
68                 overrideConnectTimeoutValue = properties.getPropertyAsInt("Ice.Override.ConnectTimeout");
69                 if(overrideConnectTimeoutValue < 1 && overrideConnectTimeoutValue != -1)
70                 {
71                     overrideConnectTimeoutValue = -1;
72                     StringBuilder msg = new StringBuilder("invalid value for Ice.Override.ConnectTimeout `");
73                     msg.Append(properties.getProperty("Ice.Override.ConnectTimeout"));
74                     msg.Append("': defaulting to -1");
75                     logger.warning(msg.ToString());
76                 }
77             }
78             else
79             {
80                 overrideConnectTimeout = false;
81                 overrideConnectTimeoutValue = -1;
82             }
83 
84             val = properties.getProperty("Ice.Override.CloseTimeout");
85             if(val.Length > 0)
86             {
87                 overrideCloseTimeout = true;
88                 overrideCloseTimeoutValue = properties.getPropertyAsInt("Ice.Override.CloseTimeout");
89                 if(overrideCloseTimeoutValue < 1 && overrideCloseTimeoutValue != -1)
90                 {
91                     overrideCloseTimeoutValue = -1;
92                     StringBuilder msg = new StringBuilder("invalid value for Ice.Override.CloseTimeout `");
93                     msg.Append(properties.getProperty("Ice.Override.CloseTimeout"));
94                     msg.Append("': defaulting to -1");
95                     logger.warning(msg.ToString());
96                 }
97             }
98             else
99             {
100                 overrideCloseTimeout = false;
101                 overrideCloseTimeoutValue = -1;
102             }
103 
104             val = properties.getProperty("Ice.Override.Compress");
105             if(val.Length > 0)
106             {
107                 overrideCompress = true;
108                 overrideCompressValue = properties.getPropertyAsInt("Ice.Override.Compress") > 0;
109                 if(!BZip2.supported() && overrideCompressValue)
110                 {
111                     string lib = AssemblyUtil.isWindows ? "bzip2.dll" : "libbz2.so.1";
112                     Console.Error.WriteLine("warning: " + lib + " not found, Ice.Override.Compress ignored.");
113                     overrideCompressValue = false;
114                 }
115             }
116             else
117             {
118                 overrideCompress = !BZip2.supported();
119                 overrideCompressValue = false;
120             }
121 
122             val = properties.getProperty("Ice.Override.Secure");
123             if(val.Length > 0)
124             {
125                 overrideSecure = true;
126                 overrideSecureValue = properties.getPropertyAsInt("Ice.Override.Secure") > 0;
127             }
128             else
129             {
130                 overrideSecure = false;
131                 overrideSecureValue = false;
132             }
133 
134             defaultCollocationOptimization =
135                 properties.getPropertyAsIntWithDefault("Ice.Default.CollocationOptimized", 1) > 0;
136 
137             val = properties.getPropertyWithDefault("Ice.Default.EndpointSelection", "Random");
138             if(val.Equals("Random"))
139             {
140                 defaultEndpointSelection = Ice.EndpointSelectionType.Random;
141             }
142             else if(val.Equals("Ordered"))
143             {
144                 defaultEndpointSelection = Ice.EndpointSelectionType.Ordered;
145             }
146             else
147             {
148                 Ice.EndpointSelectionTypeParseException ex = new Ice.EndpointSelectionTypeParseException();
149                 ex.str = "illegal value `" + val + "'; expected `Random' or `Ordered'";
150                 throw ex;
151             }
152 
153             defaultTimeout = properties.getPropertyAsIntWithDefault("Ice.Default.Timeout", 60000);
154             if(defaultTimeout < 1 && defaultTimeout != -1)
155             {
156                 defaultTimeout = 60000;
157                 StringBuilder msg = new StringBuilder("invalid value for Ice.Default.Timeout `");
158                 msg.Append(properties.getProperty("Ice.Default.Timeout"));
159                 msg.Append("': defaulting to 60000");
160                 logger.warning(msg.ToString());
161             }
162 
163             defaultLocatorCacheTimeout = properties.getPropertyAsIntWithDefault("Ice.Default.LocatorCacheTimeout", -1);
164             if(defaultLocatorCacheTimeout < -1)
165             {
166                 defaultLocatorCacheTimeout = -1;
167                 StringBuilder msg = new StringBuilder("invalid value for Ice.Default.LocatorCacheTimeout `");
168                 msg.Append(properties.getProperty("Ice.Default.LocatorCacheTimeout"));
169                 msg.Append("': defaulting to -1");
170                 logger.warning(msg.ToString());
171             }
172 
173             defaultInvocationTimeout = properties.getPropertyAsIntWithDefault("Ice.Default.InvocationTimeout", -1);
174             if(defaultInvocationTimeout < 1 && defaultInvocationTimeout != -1 && defaultInvocationTimeout != -2)
175             {
176                 defaultInvocationTimeout = -1;
177                 StringBuilder msg = new StringBuilder("invalid value for Ice.Default.InvocationTimeout `");
178                 msg.Append(properties.getProperty("Ice.Default.InvocationTimeout"));
179                 msg.Append("': defaulting to -1");
180                 logger.warning(msg.ToString());
181             }
182 
183             defaultPreferSecure = properties.getPropertyAsIntWithDefault("Ice.Default.PreferSecure", 0) > 0;
184 
185             val = properties.getPropertyWithDefault("Ice.Default.EncodingVersion",
186                                                     Ice.Util.encodingVersionToString(Ice.Util.currentEncoding));
187             defaultEncoding = Ice.Util.stringToEncodingVersion(val);
188             Protocol.checkSupportedEncoding(defaultEncoding);
189 
190             bool slicedFormat = properties.getPropertyAsIntWithDefault("Ice.Default.SlicedFormat", 0) > 0;
191             defaultFormat = slicedFormat ? Ice.FormatType.SlicedFormat : Ice.FormatType.CompactFormat;
192         }
193 
194         public string defaultHost;
195         public EndPoint defaultSourceAddress;
196         public string defaultProtocol;
197         public bool defaultCollocationOptimization;
198         public Ice.EndpointSelectionType defaultEndpointSelection;
199         public int defaultTimeout;
200         public int defaultLocatorCacheTimeout;
201         public int defaultInvocationTimeout;
202         public bool defaultPreferSecure;
203         public Ice.EncodingVersion defaultEncoding;
204         public Ice.FormatType defaultFormat;
205 
206         public bool overrideTimeout;
207         public int overrideTimeoutValue;
208         public bool overrideConnectTimeout;
209         public int overrideConnectTimeoutValue;
210         public bool overrideCloseTimeout;
211         public int overrideCloseTimeoutValue;
212         public bool overrideCompress;
213         public bool overrideCompressValue;
214         public bool overrideSecure;
215         public bool overrideSecureValue;
216     }
217 
218 }
219