private IConfigCollection LoadCollection(string path, Type keyType, Type itemType, Type fieldType, IICIndex <IICConfigItemBuffer> index) { IList <IICConfigItemBuffer> items = index.Find(path); string lastKey = null; List <string> keys = new List <string>(); foreach (IICConfigItemBuffer buffer in items) { if (string.IsNullOrEmpty(lastKey) || buffer.Key != lastKey) { if (!string.IsNullOrEmpty(buffer.Key)) { keys.Add(buffer.Key); lastKey = buffer.Key; } } } IConfigCollection collection = (IConfigCollection)Activator.CreateInstance(fieldType); foreach (string key in keys) { object itemObj = LoadItem(path, key, itemType, index); object keyObj = ObjectHelper.ConvertTo(key, keyType); collection.Add(keyObj, itemObj); } return(collection); }
private ConfigDescriptor GetDescriptor(Func <ConfigDescriptor, bool> predicate) { var descriptor = ConfigWidthInstanceCollection.FirstOrDefault(predicate); if (descriptor == null) { descriptor = _configs.Where(predicate).Select(m => new ConfigDescriptor { Type = m.Type, Code = m.Code, ImplementType = m.ImplementType, ChangeEvents = m.ChangeEvents }).FirstOrDefault(); ConfigWidthInstanceCollection.Add(descriptor); } if (descriptor == null) { throw new NullReferenceException("指定类型的配置实例不存在"); } return(descriptor); }
public static void Save(DataSet ds, Selector sel) { IConfigCollection Inserts = null; IConfigCollection Updates = null; IConfigCollection Deletes = null; // Obtain the changed rows in the DataSet DataSet changes = ds.GetChanges(); if (null == changes) { return; } // enumerate all changed tables foreach (DataTable table in changes.Tables) { // Create an empty configuration collection to hold the changed items Inserts = ConfigManager.GetEmptyConfigCollection(table.TableName); Inserts.Selector = sel; // identify where the items are to be stored Updates = ConfigManager.GetEmptyConfigCollection(table.TableName); Updates.Selector = sel; // identify where the items are to be stored Deletes = ConfigManager.GetEmptyConfigCollection(table.TableName); Deletes.Selector = sel; // identify where the items are to be stored // enumerate all changed rows in this table foreach (DataRow row in table.Rows) { if (DataRowState.Deleted == row.RowState) { // If the row has been deleted: // Create an empty configuration item for this row IConfigItem item = ConfigManager.GetEmptyConfigItem(table.TableName); // Copy the original values from the row into the item for (int i = 0; i < item.Count; i++) { item[i] = row[i, DataRowVersion.Original]; } Deletes.Add(item); Trace.WriteLine("Deleted: " + item); } else if (DataRowState.Modified == row.RowState || DataRowState.New == row.RowState) { // If the row has been modified or added: // Create an empty configuration item for this row IConfigItem item = ConfigManager.GetEmptyConfigItem(table.TableName); // Copy the values from the row into the item for (int i = 0; i < item.Count; i++) { item[i] = row[i]; } // Add the item to the proper collection if (DataRowState.Modified == row.RowState) { // If the row has been modified, add it to the Updates collection Updates.Add(item); Trace.WriteLine("Updated: " + item); } else if (DataRowState.New == row.RowState) { // If the row has been created, add it to Inserts collection Inserts.Add(item); Trace.WriteLine("Inserted: " + item); } else { // should never get here... throw(new ApplicationException("internal error")); } } } if (Deletes.Count > 0) // any deleted rows? { // Delete them from the store ConfigManager.Delete(Deletes); } if (Inserts.Count > 0) // any inserted rows? { // Add them to the store ConfigManager.Put(Inserts, PutFlags.CreateOnly); } if (Updates.Count > 0) // any modified rows? { // Change them in the store ConfigManager.Put(Updates, PutFlags.UpdateOnly); } // Get ready for the next table/config type... Inserts = null; Updates = null; Deletes = null; } }
public Object Read(String configType, Selector selector, int los, Object currentObject) { Selector url = null; ConfigQuery query = null; Object val = null; IConfigCollection result = null; IConfigItem node = null; if (selector is ConfigQuery && ((ConfigQuery)selector).Count > 0) { query = (ConfigQuery)selector; url = query.Selector; } else { if (selector is NullSelector) { url = null; } else { url = selector; } query = null; } if (url != null && !(url is HttpSelector) && !(url is WebServiceSelector)) { throw new ConfigException("Invalid Selector"); } switch (configType) { case ctNodes: // LogicalName, PhysicalName, Parent, NodeType // Q by ParentName==NULL: "IISWebService" entry (LogicalName = ?) // Q by "IISWebService" LogicalName: all IIS web sites // Q by LogicalName: UNC from URL - site binding // NodeType: IIS6! // PhysicalName: UNC from URL (MB) if (query != null) { if (query.Count > 1) { throw new ConfigException("Can't query by more than one property"); } val = query[0].Value; } if ((query != null && query[0].PropertyIndex == 2) || url == null) // Query by Parent? { if (val == null || (val is String && ((String)val) == String.Empty)) { // return IISWebService node result = ConfigManager.GetEmptyConfigCollection(configType); node = ConfigManager.GetEmptyConfigItem(configType); WebServiceSelector ws = new WebServiceSelector(); node["LogicalName"] = ws.ToString(); node["PhysicalName"] = new WebServiceSelector().Argument; node["Parent"] = ""; node["RelativeName"] = ".NET Web Service"; node["NodeType"] = nodetypeWebService; result.Add(node); return(result); } if (val is String) { if ((String)val == new WebServiceSelector().ToString()) { // return all sites result = ConfigManager.GetEmptyConfigCollection(configType); node = ConfigManager.GetEmptyConfigItem(configType); // BUGBUG really enumerate the metabase node["LogicalName"] = "http://localhost"; node["PhysicalName"] = IISAdminHelper.GetPathForUrl((String)node["LogicalName"]); node["Parent"] = new WebServiceSelector().ToString(); node["RelativeName"] = node["LogicalName"]; node["NodeType"] = nodetypeWebServer; result.Add(node); return(result); } // read children from metabase String[] children = IISAdminHelper.GetChildrenForUrl((String)val); result = ConfigManager.GetEmptyConfigCollection(configType); foreach (String child in children) { node = ConfigManager.GetEmptyConfigItem(configType); // BUGBUG really enumerate the metabase node["Parent"] = (String)val; node["LogicalName"] = ((String)val) + "/" + child; node["PhysicalName"] = IISAdminHelper.GetPathForUrl((String)node["LogicalName"]); node["RelativeName"] = child; node["NodeType"] = nodetypeWebDirectory; result.Add(node); } return(result); } throw new ConfigException("Invalid query for UINavigationNodes.Parent!"); } if ((query == null && url != null) || (query != null && query[0].PropertyIndex == 0)) // Query by LogicalName? { if (query == null) { val = url.ToString(); } if (!(val is String) || ((String)val) == String.Empty) { throw new ConfigException("Invalid Query for UINavigationNode.LogicalName!"); } WebServiceSelector ws = new WebServiceSelector(); if ((String)val == ws.ToString()) { // return IISWebService node result = ConfigManager.GetEmptyConfigCollection(configType); node = ConfigManager.GetEmptyConfigItem(configType); node["LogicalName"] = ws.ToString(); node["PhysicalName"] = ws.Argument.Substring(0, ws.Argument.LastIndexOf('\\')); node["Parent"] = ""; node["RelativeName"] = ""; node["NodeType"] = nodetypeWebService; result.Add(node); return(result); } result = ConfigManager.GetEmptyConfigCollection(configType); node = ConfigManager.GetEmptyConfigItem(configType); node["LogicalName"] = (String)val; node["PhysicalName"] = IISAdminHelper.GetPathForUrl((String)val); // BUGBUG compute only the immediate parent! String[] parents = IISAdminHelper.GetParentUrlsForUrl((String)val); if (parents.Length >= 2) { node["Parent"] = parents[parents.Length - 2]; node["RelativeName"] = ((String)val).Substring(((String)val).LastIndexOf('/')); } else { node["Parent"] = new WebServiceSelector().ToString(); node["RelativeName"] = node["LogicalName"]; } node["NodeType"] = nodetypeWebDirectory; result.Add(node); return(result); } throw new ConfigException("Invalid Query for UINavigationNode.LogicalName!"); //break; // ******************************************************************************************************* case ctConfigFileHierarchy: // LogicalName, ConfigFilePath, Location // Q by LogicalName=all config file for this node and it's parents if (query == null || query.Count == 0) { if (url == null) { throw new ConfigException("Can't enumerate all nodes!"); } } if (query != null) { if (query.Count > 1) { throw new ConfigException("Can't query by more than one property"); } val = query[0].Value; } if ((query != null && query[0].PropertyIndex == 0) || (query == null && url != null)) // Query by LogicalName? { if (query == null) { val = url.ToString(); } if (val == null || (val is String && ((String)val) == String.Empty)) { throw new ConfigException("Can't query for all root nodes."); } if (val is String) { result = ConfigManager.GetEmptyConfigCollection(configType); node = ConfigManager.GetItem(ctNodes, url); do { IConfigCollection files = ConfigManager.Get(ctFileLocation, (String)node["LogicalName"]); foreach (IConfigItem file in files) { IConfigItem hierarchyfile = ConfigManager.GetEmptyConfigItem(configType); hierarchyfile["LogicalName"] = file["LogicalName"]; hierarchyfile["ConfigFilePath"] = file["ConfigFilePath"]; hierarchyfile["Location"] = ""; result.Add(hierarchyfile); } if (node["Parent"] != "") { node = ConfigManager.GetItem(ctNodes, (String)node["Parent"]); } else { break; } } while (true); return(result); } throw new ConfigException("Invalid query for " + ctConfigFileHierarchy + ".LogicalName!"); } throw new ConfigException("Invalid Query for " + ctHierarchy + "."); //break; case ctFileLocation: // LogicalName, ConfigFilePath, Description IConfigCollection nodes = (IConfigCollection)Read(ctNodes, selector, los, currentObject); if (nodes.Count > 1) { throw new ConfigException("More than one node for this URL!"); } result = ConfigManager.GetEmptyConfigCollection(configType); if (nodes.Count == 1) { node = ConfigManager.GetEmptyConfigItem(configType); node["LogicalName"] = nodes[0]["LogicalName"]; node["ConfigFilePath"] = ((String)nodes[0]["PhysicalName"]) + "\\config.web"; node["Description"] = ""; result.Add(node); } return(result); default: throw new ConfigException("Invalid ConfigType"); } }