コード例 #1
0
        /// <summary>
        /// Lookup the connection factory assembly filename and class name.
        /// Read an external configuration file that maps scheme to provider implementation.
        /// Load XML config files named: nmsprovider-{scheme}.config
        /// Following is a sample configuration file named nmsprovider-jms.config.  Replace
        /// the parenthesis with angle brackets for proper XML formatting.
        ///
        ///     (?xml version="1.0" encoding="utf-8" ?)
        ///     (configuration)
        ///         (provider assembly="MyCompany.NMS.JMSProvider.dll" classFactory="MyCompany.NMS.JMSProvider.ConnectionFactory"/)
        ///     (/configuration)
        ///
        /// This configuration file would be loaded and parsed when a connection uri with a scheme of 'jms'
        /// is used for the provider.  In this example the connection string might look like:
        ///     jms://localhost:7222
        ///
        /// </summary>
        /// <param name="paths">Folder paths to look in.</param>
        /// <param name="scheme">The scheme.</param>
        /// <param name="assemblyFileName">Name of the assembly file.</param>
        /// <param name="factoryClassName">Name of the factory class.</param>
        /// <returns><c>true</c> if the configuration file for the specified <paramref name="scheme" /> could
        /// be found; otherwise, <c>false</c>.</returns>
        private static bool LookupConnectionFactoryInfo(string[] paths, string scheme, out string assemblyFileName, out string factoryClassName)
        {
            bool   foundFactory = false;
            string schemeLower  = scheme.ToLower();
            ProviderFactoryInfo pfi;

            // Look for a custom configuration to handle this scheme.
            string configFileName = String.Format("nmsprovider-{0}.config", schemeLower);

            assemblyFileName = String.Empty;
            factoryClassName = String.Empty;

            Tracer.DebugFormat("Attempting to locate provider configuration: {0}", configFileName);
            foreach (string path in paths)
            {
                string fullpath = Path.Combine(path, configFileName);
                Tracer.DebugFormat("Looking for: {0}", fullpath);

                try
                {
                    if (File.Exists(fullpath))
                    {
                        Tracer.DebugFormat("\tConfiguration file found in {0}", fullpath);
                        XmlDocument configDoc = new XmlDocument();

                        configDoc.Load(fullpath);
                        XmlElement providerNode = (XmlElement)configDoc.SelectSingleNode("/configuration/provider");

                        if (null != providerNode)
                        {
                            assemblyFileName = providerNode.GetAttribute("assembly");
                            factoryClassName = providerNode.GetAttribute("classFactory");
                            if (!String.IsNullOrEmpty(assemblyFileName) && !String.IsNullOrEmpty(factoryClassName))
                            {
                                foundFactory = true;
                                Tracer.DebugFormat("Selected custom provider for {0}: {1}, {2}", schemeLower, assemblyFileName, factoryClassName);
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Tracer.DebugFormat("Exception while scanning {0}: {1}", fullpath, ex.Message);
                }
            }

            if (!foundFactory)
            {
                // Check for standard provider implementations.
                if (schemaProviderFactoryMap.TryGetValue(schemeLower, out pfi))
                {
                    assemblyFileName = pfi.assemblyFileName;
                    factoryClassName = pfi.factoryClassName;
                    foundFactory     = true;
                    Tracer.DebugFormat("Selected standard provider for {0}: {1}, {2}", schemeLower, assemblyFileName, factoryClassName);
                }
            }

            return(foundFactory);
        }
コード例 #2
0
        /// <summary>
        /// Finds the <see cref="System.Type" /> associated with the given scheme.
        /// </summary>
        /// <param name="scheme">The scheme (e.g. <c>tcp</c>, <c>activemq</c> or <c>stomp</c>).</param>
        /// <returns>The <see cref="System.Type" /> of the ConnectionFactory that will be used
        /// to create the connection for the specified <paramref name="scheme" />.</returns>
        private static Type GetTypeForScheme(string scheme)
        {
            string[] paths = GetConfigSearchPaths();
            string   assemblyFileName;
            string   factoryClassName;
            Type     factoryType = null;

            Tracer.DebugFormat("Locating provider for scheme: {0}", scheme);
            if (LookupConnectionFactoryInfo(paths, scheme, out assemblyFileName, out factoryClassName))
            {
                Assembly assembly = null;

                Tracer.DebugFormat("Attempting to load provider assembly: {0}", assemblyFileName);
                try
                {
                    assembly = Assembly.Load(assemblyFileName);
                    if (null != assembly)
                    {
                        Tracer.Debug("Succesfully loaded provider.");
                    }
                }
                catch (Exception ex)
                {
                    Tracer.ErrorFormat("Exception loading assembly failed: {0}", ex.Message);
                    assembly = null;
                }

                if (null == assembly)
                {
                    foreach (string path in paths)
                    {
                        string fullpath = Path.Combine(path, assemblyFileName) + ".dll";

                        Tracer.DebugFormat("Looking for: {0}", fullpath);
                        if (File.Exists(fullpath))
                        {
                            Tracer.Debug("\tAssembly found!  Attempting to load...");
                            try
                            {
                                assembly = Assembly.LoadFrom(fullpath);
                            }
                            catch (Exception ex)
                            {
                                Tracer.ErrorFormat("Exception loading assembly failed: {0}", ex.Message);
                                assembly = null;
                            }

                            if (null != assembly)
                            {
                                Tracer.Debug("Successfully loaded provider.");
                                break;
                            }

                            Tracer.Debug("Failed to load provider.  Continuing search...");
                        }
                    }
                }

                if (null != assembly)
                {
#if NETCF
                    factoryType = assembly.GetType(factoryClassName, true);
#else
                    factoryType = assembly.GetType(factoryClassName, true, true);
#endif
                    if (null == factoryType)
                    {
                        Tracer.Fatal("Failed to load class factory from provider.");
                    }
                }
                else
                {
                    Tracer.Fatal("Failed to load provider assembly.");
                }
            }

            return(factoryType);
        }