//****************************************************************************************************** // AllAdaptersCollection.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 09/02/2010 - J. Ritchie Carroll // Generated original version of source code. // 12/20/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using GSF.Diagnostics; namespace GSF.TimeSeries.Adapters { /// /// Represents a collection of all implementations (i.e., a collection of 's). /// /// /// This collection allows all implementations to be managed as a group. /// public class AllAdaptersCollection : AdapterCollectionBase { #region [ Constructors ] /// /// Constructs a new instance of the . /// public AllAdaptersCollection() { base.Name = "All Adapters Collection"; } #endregion #region [ Properties ] /// /// Gets flag that determines if implementations are automatically initialized /// when they are added to the collection. /// /// /// We don't auto-initialize collections added to the since no data source /// will be available when the collections are being created. /// protected override bool AutoInitialize => false; /// /// Gets or sets based data source used to load each . /// Updates to this property will cascade to all items in this . /// /// /// Table name specified in from is expected /// to have the following table column names:
/// ID, AdapterName, AssemblyName, TypeName, ConnectionString
/// ID column type should be integer based, all other column types are expected to be string based. ///
public override DataSet DataSource { get { return base.DataSource; } set { // First remove old adapters that are no longer defined in the configuration // so that we do not update the data source of non-existent adapters if (Initialized) RemoveOldAdapters(value); // Update the data source of all existing adapters base.DataSource = value; // Add new adapters that weren't // previously defined in the configuration if (Initialized) AddNewAdapters(value); } } #endregion #region [ Methods ] /// /// Initializes each implementation in this . /// public override void Initialize() { Initialized = false; lock (this) { foreach (IAdapterCollection item in this) { try { item.Initialize(); } catch (Exception ex) { OnProcessException(MessageLevel.Warning, ex); } } } Initialized = true; } /// /// Wires events and initializes new implementation. /// /// New implementation. /// /// Derived classes should override if more events are defined. /// protected override void InitializeItem(IAdapterCollection item) { base.InitializeItem(item); item.InputMeasurementKeysUpdated += (sender, args) => OnInputMeasurementKeysUpdated(); item.OutputMeasurementsUpdated += (sender, args) => OnOutputMeasurementsUpdated(); } /// /// Create newly defined adapters in the adapter collection configurations. /// private void AddNewAdapters(DataSet dataSource) { List adapters; // Create a local synchronized copy of the items to iterate to avoid nested locks lock (this) { adapters = new List(this); } foreach (IAdapterCollection adapterCollection in adapters) { lock (adapterCollection) { string dataMember = adapterCollection.DataMember; if (dataSource.Tables.Contains(dataMember)) { // Create newly defined adapters foreach (DataRow adapterRow in dataSource.Tables[dataMember].Rows) { IAdapter adapter; if (!adapterCollection.TryGetAdapterByID(uint.Parse(adapterRow["ID"].ToNonNullString("0")), out adapter) && adapterCollection.TryCreateAdapter(adapterRow, out adapter)) adapterCollection.Add(adapter); } } } } } /// /// Remove adapters that are no longer present in the adapter collection configurations. /// private void RemoveOldAdapters(DataSet dataSource) { List adapters; // Create a local synchronized copy of the items to iterate to avoid nested locks lock (this) { adapters = new List(this); } foreach (IAdapterCollection adapterCollection in adapters) { lock (adapterCollection) { string dataMember = adapterCollection.DataMember; if (dataSource.Tables.Contains(dataMember)) { // Remove adapters that are no longer present in the configuration for (int i = adapterCollection.Count - 1; i >= 0; i--) { IAdapter adapter = adapterCollection[i]; DataRow[] adapterRows = dataSource.Tables[dataMember].Select($"ID = {adapter.ID}"); if (adapterRows.Length == 0 && adapter.ID != 0) { try { adapter.Stop(); } catch (Exception ex) { OnProcessException(MessageLevel.Info, new InvalidOperationException($"Exception while stopping adapter {adapter.Name}: {ex.Message}", ex)); } adapterCollection.Remove(adapter); } } } } } } /// /// Attempts to get any adapter in all collections with the specified . /// /// ID of adapter to get. /// Adapter reference if found; otherwise null. /// Adapter collection reference if is found; otherwise null. /// true if adapter with the specified was found; otherwise false. public bool TryGetAnyAdapterByID(uint id, out IAdapter adapter, out IAdapterCollection adapterCollection) { lock (this) { foreach (IAdapterCollection collection in this) { if (collection.TryGetAdapterByID(id, out adapter)) { adapterCollection = collection; return true; } } } adapter = null; adapterCollection = null; return false; } /// /// Attempts to get any adapter in all collections with the specified . /// /// Name of adapter to get. /// Adapter reference if found; otherwise null. /// Adapter collection reference if is found; otherwise null. /// true if adapter with the specified was found; otherwise false. public bool TryGetAnyAdapterByName(string name, out IAdapter adapter, out IAdapterCollection adapterCollection) { lock (this) { foreach (IAdapterCollection collection in this) { if (collection.TryGetAdapterByName(name, out adapter)) { adapterCollection = collection; return true; } } } adapter = null; adapterCollection = null; return false; } /// /// Attempts to initialize (or reinitialize) an individual based on its ID from any collection. /// /// The numeric ID associated with the to be initialized. /// true if item was successfully initialized; otherwise false. /// /// This method traverses all collections looking for an adapter with the specified ID. /// public override bool TryInitializeAdapterByID(uint id) { lock (this) { foreach (IAdapterCollection collection in this) { if (collection.TryInitializeAdapterByID(id)) return true; } } return false; } /// /// This method is not implemented in . /// [EditorBrowsable(EditorBrowsableState.Never)] public override bool TryCreateAdapter(DataRow adapterRow, out IAdapterCollection adapter) { throw new NotImplementedException(); } /// /// This method is not implemented in . /// [EditorBrowsable(EditorBrowsableState.Never)] public override bool TryGetAdapterByID(uint id, out IAdapterCollection adapter) { throw new NotImplementedException(); } #endregion } }