Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

783 lines
17 KiB

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Web;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Xml.Serialization;
  10. using UDDI.Diagnostics;
  11. namespace UDDI
  12. {
  13. public class ConnectionManager
  14. {
  15. private static string readerConnectionString;
  16. private static string writerConnectionString;
  17. private SqlConnection conn;
  18. private SqlTransaction txn;
  19. [ThreadStatic]
  20. static private ConnectionManager cm;
  21. private ConnectionManager( bool writeAccess, bool transactional )
  22. {
  23. try
  24. {
  25. Debug.VerifySetting( "Database.ReaderConnectionString" );
  26. Debug.VerifySetting( "Database.WriterConnectionString" );
  27. //
  28. // Get the database connection strings. We do this each time a
  29. // connection is opened so that an operator can change database
  30. // connection strings without having to restart the server.
  31. //
  32. readerConnectionString = Config.GetString( "Database.ReaderConnectionString" );
  33. writerConnectionString = Config.GetString( "Database.WriterConnectionString" );
  34. }
  35. catch( UDDIException )
  36. {
  37. //
  38. // Treat these specially; we want to give the user a better error message since this
  39. // means that this web server is not associated with a UDDI site.
  40. //
  41. //throw new UDDIException( ErrorType.E_fatalError, "This web server cannot process UDDI requests because it is not part of a UDDI site or the UDDI site being used is not valid. Ensure that values in the ReaderConnectionString and WriterConnectionString registry keys specify a valid UDDI Site." );
  42. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_INVALID_UDDI_SITE" );
  43. }
  44. if( writeAccess )
  45. conn = new SqlConnection( writerConnectionString );
  46. else
  47. conn = new SqlConnection( readerConnectionString );
  48. conn.Open();
  49. if( transactional )
  50. {
  51. txn = conn.BeginTransaction();
  52. Debug.Write( SeverityType.Info, CategoryType.Data, "Initiating a Transaction" );
  53. }
  54. }
  55. public static void Open( bool writeAccess, bool transactional )
  56. {
  57. Debug.Enter();
  58. ConnectionManager cm = new ConnectionManager( writeAccess, transactional );
  59. HttpContext ctx = HttpContext.Current;
  60. if( null != (object) ctx )
  61. {
  62. ctx.Items[ "Connection" ] = cm;
  63. }
  64. else
  65. {
  66. ConnectionManager.cm = cm;
  67. }
  68. Debug.Leave();
  69. }
  70. public static ConnectionManager Get()
  71. {
  72. HttpContext ctx = HttpContext.Current;
  73. if( null != (object) ctx )
  74. {
  75. return (ConnectionManager) ctx.Items[ "Connection" ];
  76. }
  77. else
  78. {
  79. return ConnectionManager.cm;
  80. }
  81. }
  82. public static SqlConnection GetConnection()
  83. {
  84. ConnectionManager cm = Get();
  85. if( null == (object) cm )
  86. return null;
  87. return cm.conn;
  88. }
  89. public static SqlTransaction GetTransaction()
  90. {
  91. ConnectionManager cm = Get();
  92. if( null == (object) cm )
  93. return null;
  94. return cm.txn;
  95. }
  96. public static void BeginTransaction()
  97. {
  98. Debug.Enter();
  99. ConnectionManager cm = Get();
  100. #if never
  101. Debug.Verify( null != (object)cm, "Static connection manager was null in attempt to begin transaction." );
  102. Debug.Verify( null != (object)cm.conn, "Database connection was null in attempt to begin transaction." );
  103. Debug.Verify( null == (object)cm.txn, "Database was already in a transaction in attempt to begin a new transaction." );
  104. #endif
  105. Debug.Verify( null != (object)cm, "UDDI_ERROR_TRANSACTION_BEGIN_CONNECTION_MANAGER" );
  106. Debug.Verify( null != (object)cm.conn, "UDDI_ERROR_TRANSACTION_BEGIN_CONNECTION" );
  107. Debug.Verify( null == (object)cm.txn, "UDDI_ERROR_ALREADY_IN_TRANSACTION" );
  108. cm.txn = cm.conn.BeginTransaction();
  109. Debug.Leave();
  110. }
  111. public static void Commit()
  112. {
  113. Debug.Enter();
  114. ConnectionManager cm = Get();
  115. #if never
  116. Debug.Verify( null != (object)cm.conn, "Database connection was null in attempt to commit transaction." );
  117. Debug.Verify( null != (object)cm.txn, "Database transaction was null in attempt to commit transaction." );
  118. #endif
  119. Debug.Verify( null != (object)cm.conn, "UDDI_ERROR_TRANSACTION_COMMIT_CONNECTION" );
  120. Debug.Verify( null != (object)cm.txn, "UDDI_ERROR_TRANSACTION_COMMIT_TRANSACTION" );
  121. cm.txn.Commit();
  122. cm.txn = null;
  123. Debug.Leave();
  124. }
  125. public static void Abort()
  126. {
  127. Debug.Enter();
  128. ConnectionManager cm = Get();
  129. #if never
  130. Debug.Verify( null != (object)cm.conn, "Database connection was null in attempt to abort transaction." );
  131. Debug.Verify( null != (object)cm.txn, "Database transaction was null in attempt to abort transaction." );
  132. #endif
  133. Debug.Verify( null != (object)cm.conn, "UDDI_ERROR_TRANSACTION_ABORT_CONNECTION" );
  134. Debug.Verify( null != (object)cm.txn, "UDDI_ERROR_TRANSACTION_ABORT_TRANSACTION" );
  135. cm.txn.Rollback();
  136. cm.txn = null;
  137. Debug.Leave();
  138. }
  139. public static void Close()
  140. {
  141. Debug.Enter();
  142. SqlConnection cn = GetConnection();
  143. SqlTransaction txn = GetTransaction();
  144. //
  145. // This function can be safely called repeatedly
  146. if( null == (object) cn )
  147. return;
  148. cn.Close();
  149. HttpContext ctx = HttpContext.Current;
  150. if( null != (object) ctx )
  151. {
  152. ctx.Items.Remove( "Connection" );
  153. }
  154. else
  155. {
  156. ConnectionManager.cm = null;
  157. }
  158. Debug.Leave();
  159. }
  160. }
  161. public class SqlStoredProcedureAccessor
  162. {
  163. private SqlCommand cmd;
  164. private SqlParameterAccessor paramAcc;
  165. public SqlStoredProcedureAccessor()
  166. {
  167. cmd = new SqlCommand();
  168. cmd.Connection = ConnectionManager.GetConnection();
  169. cmd.Transaction = ConnectionManager.GetTransaction();
  170. cmd.CommandType = CommandType.StoredProcedure;
  171. paramAcc = new SqlParameterAccessor( cmd.Parameters );
  172. }
  173. public SqlStoredProcedureAccessor( string procedureName )
  174. : this()
  175. {
  176. ProcedureName = procedureName;
  177. }
  178. public void Close()
  179. {
  180. cmd.Dispose();
  181. }
  182. public int Fill( DataSet dataSet, string srcTable )
  183. {
  184. SqlDataAdapter adapter = new SqlDataAdapter( cmd );
  185. return adapter.Fill( dataSet, srcTable );
  186. }
  187. public string ProcedureName
  188. {
  189. get { return cmd.CommandText; }
  190. set { cmd.CommandText = value; }
  191. }
  192. public SqlParameterAccessor Parameters
  193. {
  194. get { return paramAcc; }
  195. }
  196. public int ExecuteNonQuery()
  197. {
  198. return cmd.ExecuteNonQuery();
  199. }
  200. public SqlDataReaderAccessor ExecuteReader()
  201. {
  202. return new SqlDataReaderAccessor( cmd.ExecuteReader() );
  203. }
  204. public object ExecuteScalar()
  205. {
  206. return cmd.ExecuteScalar();
  207. }
  208. }
  209. public class SqlParameterAccessor
  210. {
  211. private SqlParameterCollection parameters;
  212. public SqlParameterAccessor( SqlParameterCollection parameters )
  213. {
  214. this.parameters = parameters;
  215. }
  216. public void Clear()
  217. {
  218. parameters.Clear();
  219. }
  220. public void Add( string name, SqlDbType dbType )
  221. {
  222. Add( name, dbType, ParameterDirection.Input );
  223. }
  224. public void Add( string name, SqlDbType dbType, ParameterDirection direction )
  225. {
  226. parameters.Add( new SqlParameter( name, dbType ) ).Direction = direction;
  227. }
  228. public void Add( string name, SqlDbType dbType, int size )
  229. {
  230. Add( name, dbType, size, ParameterDirection.Input );
  231. }
  232. public void Add( string name, SqlDbType dbType, int size, ParameterDirection direction )
  233. {
  234. parameters.Add( new SqlParameter( name, dbType, size ) ).Direction = direction;
  235. }
  236. public void SetNull( string index )
  237. {
  238. parameters[ index ].Value = DBNull.Value;
  239. }
  240. public void SetNull( int index )
  241. {
  242. parameters[ index ].Value = DBNull.Value;
  243. }
  244. public void SetBinary( string index, byte[] data )
  245. {
  246. if( null == data )
  247. parameters[ index ].Value = DBNull.Value;
  248. else
  249. parameters[ index ].Value = data;
  250. }
  251. public void SetString( string index, string data )
  252. {
  253. if( null == data )
  254. parameters[ index ].Value = DBNull.Value;
  255. else
  256. parameters[ index ].Value = data;
  257. }
  258. public void SetString( int index, string data )
  259. {
  260. if( null == data )
  261. parameters[ index ].Value = DBNull.Value;
  262. else
  263. parameters[ index ].Value = data;
  264. }
  265. public void SetShort( string index, short data )
  266. {
  267. parameters[ index ].Value = data;
  268. }
  269. public void SetShort( int index, short data )
  270. {
  271. parameters[ index ].Value = data;
  272. }
  273. public void SetInt( string index, int data )
  274. {
  275. parameters[ index ].Value = data;
  276. }
  277. public void SetInt( int index, int data )
  278. {
  279. parameters[ index ].Value = data;
  280. }
  281. public void SetLong( string index, long data )
  282. {
  283. parameters[ index ].Value = data;
  284. }
  285. public void SetLong( int index, long data )
  286. {
  287. parameters[ index ].Value = data;
  288. }
  289. public void SetGuid( string index, Guid guid )
  290. {
  291. if( null == (object)guid )
  292. parameters[ index ].Value = DBNull.Value;
  293. else
  294. parameters[ index ].Value = guid;
  295. }
  296. public void SetGuid( int index, Guid guid )
  297. {
  298. if( null == (object)guid )
  299. parameters[ index ].Value = DBNull.Value;
  300. else
  301. parameters[ index ].Value = guid;
  302. }
  303. public void SetGuidFromString( string index, string guid )
  304. {
  305. try
  306. {
  307. if( Utility.StringEmpty( guid ) )
  308. parameters[ index ].Value = DBNull.Value;
  309. else
  310. parameters[ index ].Value = new Guid( guid );
  311. }
  312. catch
  313. {
  314. throw new UDDIException( ErrorType.E_invalidKeyPassed, "UDDI_ERROR_INVALID_KEY" );
  315. }
  316. }
  317. public void SetGuidFromString( int index, string guid )
  318. {
  319. try
  320. {
  321. if( Utility.StringEmpty( guid ) )
  322. parameters[ index ].Value = DBNull.Value;
  323. else
  324. parameters[ index ].Value = new Guid( guid );
  325. }
  326. catch
  327. {
  328. throw new UDDIException( ErrorType.E_invalidKeyPassed, "UDDI_ERROR_INVALID_KEY" );
  329. }
  330. }
  331. public void SetGuidFromKey( string index, string key )
  332. {
  333. try
  334. {
  335. if( Utility.StringEmpty( key ) )
  336. parameters[ index ].Value = DBNull.Value;
  337. else
  338. parameters[ index ].Value = Conversions.GuidFromKey( key );
  339. }
  340. catch
  341. {
  342. throw new UDDIException( ErrorType.E_invalidKeyPassed, "UDDI_ERROR_INVALID_KEY" );
  343. }
  344. }
  345. public void SetGuidFromKey( int index, string key )
  346. {
  347. try
  348. {
  349. if( Utility.StringEmpty( key ) )
  350. parameters[ index ].Value = DBNull.Value;
  351. else
  352. parameters[ index ].Value = Conversions.GuidFromKey( key );
  353. }
  354. catch
  355. {
  356. throw new UDDIException( ErrorType.E_invalidKeyPassed, "UDDI_ERROR_INVALID_KEY" );
  357. }
  358. }
  359. public void SetBool( string index, bool flag )
  360. {
  361. parameters[ index ].Value = flag;
  362. }
  363. public void SetBool( int index, bool flag )
  364. {
  365. parameters[ index ].Value = flag;
  366. }
  367. public void SetDateTime( string index, DateTime datetime )
  368. {
  369. parameters[ index ].Value = datetime;
  370. }
  371. public void SetDateTime( int index, DateTime datetime )
  372. {
  373. parameters[ index ].Value = datetime;
  374. }
  375. public byte[] GetBinary( string index )
  376. {
  377. object data = parameters[ index ].Value;
  378. if( DBNull.Value == data )
  379. return null;
  380. return (byte[])data;
  381. }
  382. public string GetString( string index )
  383. {
  384. object data = parameters[ index ].Value;
  385. if( DBNull.Value == data )
  386. return null;
  387. return Convert.ToString( data );
  388. }
  389. public string GetString( int index )
  390. {
  391. object data = parameters[ index ].Value;
  392. if( DBNull.Value == data )
  393. return null;
  394. return Convert.ToString( data );
  395. }
  396. public int GetInt( string index )
  397. {
  398. object data = parameters[ index ].Value;
  399. if( DBNull.Value == data )
  400. return 0;
  401. return Convert.ToInt32( data );
  402. }
  403. public int GetInt( int index )
  404. {
  405. object data = parameters[ index ].Value;
  406. if( DBNull.Value == data )
  407. return 0;
  408. return Convert.ToInt32( data );
  409. }
  410. public short GetShort( string index )
  411. {
  412. object data = parameters[ index ].Value;
  413. if( DBNull.Value == data )
  414. return 0;
  415. return Convert.ToInt16( data );
  416. }
  417. public short GetShort( int index )
  418. {
  419. object data = parameters[ index ].Value;
  420. if( DBNull.Value == data )
  421. return 0;
  422. return Convert.ToInt16( data );
  423. }
  424. public long GetLong( string index )
  425. {
  426. object data = parameters[ index ].Value;
  427. if( DBNull.Value == data )
  428. return 0;
  429. return Convert.ToInt64( data );
  430. }
  431. public long GetLong( int index )
  432. {
  433. object data = parameters[ index ].Value;
  434. if( DBNull.Value == data )
  435. return 0;
  436. return Convert.ToInt64( data );
  437. }
  438. public string GetGuidString( string index )
  439. {
  440. object data = parameters[ index ].Value;
  441. if( DBNull.Value == data )
  442. return null;
  443. System.Guid guid = (System.Guid)data;
  444. return Convert.ToString( guid );
  445. }
  446. public string GetGuidString( int index )
  447. {
  448. object data = parameters[ index ].Value;
  449. if( DBNull.Value == data )
  450. return null;
  451. System.Guid guid = (System.Guid)data;
  452. return Convert.ToString( guid );
  453. }
  454. public bool GetBool( string index )
  455. {
  456. object data = parameters[ index ].Value;
  457. if( DBNull.Value == data )
  458. return false;
  459. return Convert.ToBoolean( parameters[ index ].Value );
  460. }
  461. public bool GetBool( int index )
  462. {
  463. object data = parameters[ index ].Value;
  464. if( DBNull.Value == data )
  465. return false;
  466. return Convert.ToBoolean( parameters[ index ].Value );
  467. }
  468. public object GetDateTime( string index )
  469. {
  470. object data = parameters[ index ].Value;
  471. if( DBNull.Value == data )
  472. return null;
  473. return parameters[ index ].Value ;
  474. }
  475. public object GetDateTime( int index )
  476. {
  477. object data = parameters[ index ].Value;
  478. if( DBNull.Value == data )
  479. return null;
  480. return parameters[ index ].Value ;
  481. }
  482. public bool IsNull( string index )
  483. {
  484. return DBNull.Value == parameters[ index ].Value;
  485. }
  486. public bool IsNull( int index )
  487. {
  488. return DBNull.Value == parameters[ index ].Value;
  489. }
  490. }
  491. public class SqlDataReaderAccessor
  492. {
  493. private SqlDataReader reader;
  494. public SqlDataReaderAccessor( SqlDataReader reader )
  495. {
  496. this.reader = reader;
  497. }
  498. ~SqlDataReaderAccessor()
  499. {
  500. }
  501. public void Close()
  502. {
  503. reader.Close();
  504. }
  505. public bool Read()
  506. {
  507. return reader.Read();
  508. }
  509. public bool NextResult()
  510. {
  511. return reader.NextResult();
  512. }
  513. public bool IsDBNull( int index )
  514. {
  515. return reader.IsDBNull( index );
  516. }
  517. public byte[] GetBinary( string index )
  518. {
  519. object data = reader[ index ];
  520. if( DBNull.Value == data )
  521. return null;
  522. return (byte[])data;
  523. }
  524. public string GetString( string index )
  525. {
  526. object data = reader[ index ];
  527. if( DBNull.Value == data )
  528. return null;
  529. return Convert.ToString( data );
  530. }
  531. public string GetString( int index )
  532. {
  533. object data = reader[ index ];
  534. if( DBNull.Value == data )
  535. return null;
  536. return Convert.ToString( data );
  537. }
  538. public int GetInt( string index )
  539. {
  540. object data = reader[ index ];
  541. if( DBNull.Value == data )
  542. return 0;
  543. return Convert.ToInt32( data );
  544. }
  545. public int GetInt( int index )
  546. {
  547. object data = reader[ index ];
  548. if( DBNull.Value == data )
  549. return 0;
  550. return Convert.ToInt32( data );
  551. }
  552. public short GetShort( string index )
  553. {
  554. object data = reader[ index ];
  555. if( DBNull.Value == data )
  556. return 0;
  557. return Convert.ToInt16( data );
  558. }
  559. public short GetShort( int index )
  560. {
  561. object data = reader[ index ];
  562. if( DBNull.Value == data )
  563. return 0;
  564. return Convert.ToInt16( data );
  565. }
  566. public long GetLong( string index )
  567. {
  568. object data = reader[ index ];
  569. if( DBNull.Value == data )
  570. return 0;
  571. return Convert.ToInt64( data );
  572. }
  573. public long GetLong( int index )
  574. {
  575. object data = reader[ index ];
  576. if( DBNull.Value == data )
  577. return 0;
  578. return Convert.ToInt64( data );
  579. }
  580. public string GetGuidString( string index )
  581. {
  582. object data = reader[ index ];
  583. if( DBNull.Value == data )
  584. return null;
  585. return Convert.ToString( data );
  586. }
  587. public string GetGuidString( int index )
  588. {
  589. object data = reader[ index ];
  590. if( DBNull.Value == data )
  591. return null;
  592. return Convert.ToString( data );
  593. }
  594. public string GetKeyFromGuid( string index )
  595. {
  596. object data = reader[ index ];
  597. if( DBNull.Value == data )
  598. return null;
  599. return "uuid:" + Convert.ToString( data );
  600. }
  601. public string GetKeyFromGuid( int index )
  602. {
  603. object data = reader[ index ];
  604. if( DBNull.Value == data )
  605. return null;
  606. return "uuid:" + Convert.ToString( data );
  607. }
  608. }
  609. }