1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
//
// This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
// project.
//
// Copyright (C) 1998-2024 OpenLink Software
//
// This project is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; only version 2 of the License, dated June 1991.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
//
//
// $Id$
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
#if ODBC_CLIENT
namespace OpenLink.Data.VirtuosoOdbcClient
#elif CLIENT
namespace OpenLink.Data.VirtuosoClient
#else
namespace OpenLink.Data.Virtuoso
#endif
{
/// <summary>
/// Summary description for ConnectionOptions.
/// </summary>
internal sealed class ConnectionOptions
{
internal abstract class Option
{
internal abstract void Set (string settingString);
internal abstract string Get ();
internal virtual ArrayList GetList () { return null;}
};
private sealed class ListOption : Option
{
internal override void Set (string settingString)
{
setting = settingString;
items = new ArrayList();
StringBuilder buff = new StringBuilder();
string val;
for(int i = 0; i < settingString.Length; i++)
{
char c = settingString[i];
if (c == ',')
{
val = buff.ToString().Trim();
if (val != String.Empty)
items.Add(val);
buff.Length = 0;
}
else if (!Char.IsWhiteSpace(c))
{
buff.Append(c);
}
}
val = buff.ToString().Trim();
if (val != String.Empty)
items.Add(val);
}
internal override string Get ()
{
return setting;
}
internal override ArrayList GetList ()
{
return items;
}
internal ArrayList items;
internal string setting;
public override string ToString()
{
return setting;
}
}
private sealed class StringOption : Option
{
internal override void Set (string settingString)
{
setting = settingString;
}
internal override string Get ()
{
return setting;
}
internal string setting;
public override string ToString()
{
return setting;
}
}
private sealed class IntegerOption : Option
{
internal override void Set (string settingString)
{
setting = int.Parse (settingString);
}
internal override string Get ()
{
return setting.ToString();
}
internal int setting;
public override string ToString()
{
return setting.ToString ();
}
}
private sealed class BooleanOption : Option
{
internal override void Set (string settingString)
{
setting = bool.Parse (settingString);
}
internal override string Get ()
{
return setting.ToString();
}
internal bool setting;
public override string ToString()
{
return setting.ToString ();
}
}
internal const int DEFAULT_CONN_TIMEOUT = 15;
internal const int DEFAULT_CONN_LIFETIME = 0;
internal const int DEFAULT_MIN_POOL_SIZE = 0;
internal const int DEFAULT_MAX_POOL_SIZE = 100;
#if ODBC_CLIENT || CLIENT
internal const bool DEFAULT_USE_ODBC = true;
#else
internal const bool DEFAULT_USE_ODBC = false;
#endif
internal const bool DEFAULT_PERSIST_SECURITY_INFO = false;
internal const bool DEFAULT_POOLING = true;
internal const bool DEFAULT_ENLIST = true;
internal const bool DEFAULT_ROUND_ROBIN = false;
internal const string ODBC = "ODBC";
internal const string HOST = "HOST";
internal const string DATASOURCE = "Data Source";
internal const string SERVER = "Server";
internal const string ADDRESS = "Address";
internal const string NETWORKADDRESS = "Network Address";
internal const string UID = "UID";
internal const string USER_ID = "User ID";
internal const string USERID = "UserId";
internal const string PWD = "PWD";
internal const string PASSWORD = "Password";
internal const string DATABASE = "Database";
internal const string INITIALCATALOG = "Initial Catalog";
internal const string CHARSET = "Charset";
internal const string ENCRYPT = "Encrypt";
internal const string PERSISTSECURITYINFO = "PersistSecurityInfo";
internal const string PERSIST_SECURITY_INFO = "Persist Security Info";
internal const string CONNECTTIMEOUT = "ConnectTimeout";
internal const string CONNECT_TIMEOUT = "Connect Timeout";
internal const string CONNECTIONTIMEOUT = "ConnectionTimeout";
internal const string CONNECTION_TIMEOUT = "Connection Timeout";
internal const string CONNECTIONLIFETIME = "ConnectionLifetime";
internal const string CONNECTION_LIFETIME = "Connection Lifetime";
internal const string MINPOOLSIZE = "MinPoolSize";
internal const string MIN_POOL_SIZE = "Min Pool Size";
internal const string MAXPOOLSIZE = "MaxPoolSize";
internal const string MAX_POOL_SIZE = "Max Pool Size";
internal const string POOLING = "Pooling";
internal const string ENLIST = "Enlist";
internal const string ROUNDROBIN = "RoundRobin";
internal const string ROUND_ROBIN = "Round Robin";
internal const string LOG_ENABLE = "Log_enable";
private BooleanOption odbc;
private ListOption host;
private StringOption uid;
private StringOption pwd;
private StringOption database;
private StringOption charset;
private StringOption encrypt;
private BooleanOption persistSecurityInfo;
private IntegerOption connectionTimeout;
private IntegerOption connectionLifetime;
private IntegerOption minPoolSize;
private IntegerOption maxPoolSize;
private BooleanOption pooling;
private BooleanOption enlist;
private BooleanOption roundrobin;
private IntegerOption log_enable;
/// <summary>
/// Contains the original connection string.
/// </summary>
private string connectionString;
/// <summary>
/// Contains the original connection string excluding the security-sensitive information.
/// </summary>
private string secureConnectionString;
private Hashtable options;
internal ConnectionOptions ()
{
odbc = new BooleanOption ();
host = new ListOption ();
uid = new StringOption ();
pwd = new StringOption ();
database = new StringOption ();
charset = new StringOption ();
encrypt = new StringOption ();
persistSecurityInfo = new BooleanOption ();
connectionTimeout = new IntegerOption ();
connectionLifetime = new IntegerOption ();
minPoolSize = new IntegerOption ();
maxPoolSize = new IntegerOption ();
pooling = new BooleanOption ();
enlist = new BooleanOption ();
roundrobin = new BooleanOption ();
log_enable = new IntegerOption();
Reset ();
options = CollectionsUtil.CreateCaseInsensitiveHashtable (23);
options.Add (ODBC, odbc);
options.Add (HOST, host);
options.Add (DATASOURCE, host);
options.Add (SERVER, host);
options.Add (ADDRESS, host);
options.Add (NETWORKADDRESS, host);
options.Add (UID, uid);
options.Add (USER_ID, uid);
options.Add (USERID, uid);
options.Add (PWD, pwd);
options.Add (PASSWORD, pwd);
options.Add (DATABASE, database);
options.Add (INITIALCATALOG, database);
options.Add (CHARSET, charset);
options.Add (ENCRYPT, encrypt);
options.Add (PERSISTSECURITYINFO, persistSecurityInfo);
options.Add (PERSIST_SECURITY_INFO, persistSecurityInfo);
options.Add (CONNECTTIMEOUT, connectionTimeout);
options.Add (CONNECT_TIMEOUT, connectionTimeout);
options.Add (CONNECTIONTIMEOUT, connectionTimeout);
options.Add (CONNECTION_TIMEOUT, connectionTimeout);
options.Add (CONNECTIONLIFETIME, connectionLifetime);
options.Add (CONNECTION_LIFETIME, connectionLifetime);
options.Add (MINPOOLSIZE, minPoolSize);
options.Add (MIN_POOL_SIZE, minPoolSize);
options.Add (MAXPOOLSIZE, maxPoolSize);
options.Add (MAX_POOL_SIZE, maxPoolSize);
options.Add (POOLING, pooling);
options.Add (ENLIST, enlist);
options.Add (ROUNDROBIN, roundrobin);
options.Add (ROUND_ROBIN, roundrobin);
options.Add(LOG_ENABLE, log_enable);
}
internal void Reset ()
{
odbc.setting = DEFAULT_USE_ODBC;
host.setting = null;
uid.setting = null;
pwd.setting = null;
database.setting = null;
charset.setting = null;
encrypt.setting = null;
persistSecurityInfo.setting = false;
connectionTimeout.setting = DEFAULT_CONN_TIMEOUT;
connectionLifetime.setting = DEFAULT_CONN_LIFETIME;
minPoolSize.setting = DEFAULT_MIN_POOL_SIZE;
maxPoolSize.setting = DEFAULT_MAX_POOL_SIZE;
pooling.setting = true;
enlist.setting = Platform.HasDtc ();
roundrobin.setting = false;
log_enable.setting = -1;
}
internal void Verify ()
{
if (MinPoolSize < 0)
throw new ArgumentException ("Invalid Min Pool Size value.");
if (MaxPoolSize <= 0)
throw new ArgumentException ("Invalid Max Pool Size value.");
if (MinPoolSize > MaxPoolSize)
throw new ArgumentException ("Min Pool Size is greater than Max Pool Size.");
if (Enlist && !Platform.HasDtc ())
throw new ArgumentException ("Transaction enlistment is not supported on this platform.");
if (Log_enable !=-1 && (Log_enable < 0 || Log_enable > 3))
throw new ArgumentException ("Log_enable must >=0 and <= 3.");
}
internal void Secure ()
{
if (PersistSecurityInfo == false)
connectionString = secureConnectionString;
}
internal Option GetOption (string name)
{
return (Option) options[name];
}
internal bool UseOdbc
{
get { return odbc.setting; }
}
internal bool PersistSecurityInfo
{
get { return persistSecurityInfo.setting; }
}
internal string UserId
{
get { return uid.setting; }
}
internal string Password
{
get { return pwd.setting; }
}
internal string DataSource
{
get { return host.setting; }
}
internal ArrayList DataSourceList
{
get { return host.GetList(); }
}
internal string Database
{
get { return database.setting; }
}
internal string Charset
{
get { return charset.setting; }
}
internal string Encrypt
{
get { return encrypt.setting; }
}
internal int ConnectionTimeout
{
get { return connectionTimeout.setting; }
}
internal int ConnectionLifetime
{
get { return connectionLifetime.setting; }
}
internal int MinPoolSize
{
get { return minPoolSize.setting; }
}
internal int MaxPoolSize
{
get { return maxPoolSize.setting; }
}
internal bool Pooling
{
get
{
if (pooling.setting == false)
return false;
if (maxPoolSize.setting <= 0)
return false;
if (host.setting == null || uid.setting == null || pwd.setting == null)
return false;
if (host.setting.StartsWith (":in-process:"))
return false;
return true;
}
}
internal bool Enlist
{
get { return enlist.setting; }
}
internal bool RoundRobin
{
get { return roundrobin.setting; }
}
internal int Log_enable
{
get { return log_enable.setting; }
}
internal string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}
internal string SecureConnectionString
{
get { return secureConnectionString; }
set { secureConnectionString = value; }
}
/// <summary>
/// Checks if a connection option is security-sensitive.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
internal static bool IsSecuritySensitive (string name)
{
name = name.ToUpper ();
return name == "PWD" || name == "PASSWORD";
}
}
}
|