ComponentExists() public method

Method to check if a config section exists for a component, prior to calling GetConfigXml or GetParametersAsHashTable (which throw exceptions if you request an invalid component name).
public ComponentExists ( string component ) : bool
component string The component or section of the config file, used to /// locate the parameter.
return bool
Beispiel #1
0
        /// <summary>
        /// This handles running through the configuration and getting the request
        /// and response processors.
        /// </summary>
        /// <param name="config">The config file we are using.</param>
        /// <param name="sectionName">The config section we are using to look for RequestProcessors and ResponseProcessors.</param>
        private void GetProcessors(Config config, string sectionName)
        {
            if (config.ComponentExists(sectionName))
            {
                IList<KeyValuePair<string, string>> kvps = config.GetParametersAsList(sectionName);
                foreach (KeyValuePair<string, string> kvp in kvps)
                {
                    string typeName = kvp.Value;
                    Type processorType = Type.GetType(typeName);
                    if (processorType != null && typeof(IProcessor).IsAssignableFrom(processorType))
                    {
                        ConstructorInfo ci = processorType.GetConstructor(new [] {typeof (Config), typeof (string)});
                        IProcessor p;
                        if (ci != null)
                        {
                            p = (IProcessor) ci.Invoke(new object[] {config, kvp.Key});
                        }
                        else
                        {
                            ci = processorType.GetConstructor(new Type[] {});
                            if (ci == null)
                            {
                                throw new ConfigurationErrorsException("Processor '" + typeName +
                                                                       "' was specified, but we were unable to get constructor info.");
                            }
                            p = (IProcessor) ci.Invoke(new object[] {});
                        }

                        // At this point we have a processor object.  Add it to the dictionary.
                        if (p as IRequestProcessor != null)
                        {
                            _processors[typeof (IRequestProcessor)].Add(p);
                        }
                        if (p as IResponseProcessor != null)
                        {
                            _processors[typeof (IResponseProcessor)].Add(p);
                        }
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// This is a factory method, that will load the appropriate type of connection
 /// descriptor using the given config.
 /// 
 /// It first searches for config item(s) called "ConnectionConfigSection" and/or
 /// "ConnectionConfig".  (ConnectionConfig should be an "app name" for a config, not a file name).
 /// If present, it will use those to load from another section in this or another
 /// config file.  This allows more dynamic install-time configuration of DB connections.
 /// You may daisy-chain the configuration if you wish.
 /// 
 /// Once in the connection configuration section, it will first search for the "DescriptorClass"
 /// config item, and use that class if specified.  If not, defaults to an OleDbDescriptor
 /// (which means it should be backwards compatible for all our existing config files).
 /// </summary>
 /// <param name="cfg">Config to load the descriptor info from.</param>
 /// <param name="section">What section of that config has the DB connection info in it.</param>
 /// <param name="decryptionDelegate">Method to call to decrypt information, if the actual
 ///                                  connection descriptor type supports decryption.  May be null.</param>
 /// <returns>A fully populated ConnectionDescriptor.</returns>
 public static IConnectionDescriptor LoadFromConfig(Config cfg, string section,
     ConnectionInfoDecryptionDelegate decryptionDelegate)
 {
     if (!cfg.ComponentExists(section))
     {
         throw new BadDaoConfigurationException("Config section " + section +
                                                " does not exist in " + cfg.Application);
     }
     IConnectionDescriptor retVal;
     // First see if we're redirected to another config and/or section.
     if (cfg.ParameterExists(section, "ConnectionConfig") ||
         cfg.ParameterExists(section, "ConnectionConfigSection"))
     {
         string otherName = cfg.GetParameter(section, "ConnectionConfig", cfg.Application);
         string otherSection = cfg.GetParameter(section, "ConnectionConfigSection", section);
         if (_log.IsDebugEnabled)
         {
             _log.Debug("Loading " + section + " connection info from "
                        + otherName + "[" + otherSection + "]");
         }
         // Recurse with different config values.
         retVal = LoadFromConfig(Config.GetConfig(otherName), otherSection, decryptionDelegate);
     }
     else
     {
         // Not overridden, read from this config section.
         // For backwards compatibility, default to using an OleDb descriptor.
         string typeName = cfg.GetParameter(section, "DescriptorClass",
             "Azavea.Open.DAO.OleDb.OleDbDescriptor,Azavea.Open.DAO.OleDb");
         Type[] paramTypes = new Type[] {typeof (Config), typeof (string),
             typeof(ConnectionInfoDecryptionDelegate)};
         Type descType = Type.GetType(typeName);
         if (descType == null)
         {
             throw new BadDaoConfigurationException("DescriptorClass '" + typeName +
                                                    "' was specified, but we were unable to get type info.  Are you missing a DLL?");
         }
         ConstructorInfo constr = descType.GetConstructor(paramTypes);
         if (constr == null)
         {
             throw new BadDaoConfigurationException("DescriptorClass '" + typeName +
                                                    "' was specified, but we were unable to get constructor info.");
         }
         retVal = (IConnectionDescriptor)constr.Invoke(new object[] { cfg, section, decryptionDelegate });
     }
     return retVal;
 }