Beispiel #1
0
        private static void LogDuplicitPlugin(IConnectionPlugin loaded)
        {
            const string MESSAGE_FORMAT = "Plugin for protocol {0} ({1}:{2}) already present.";
            var          pluginType     = loaded.GetType();
            var          assemblyPath   = pluginType.Assembly.CodeBase;
            var          message        = string.Format(MESSAGE_FORMAT, loaded.PortName, pluginType, assemblyPath);

            Logging.Warn(message);
        }
Beispiel #2
0
        private static ProtocolOptions SwitchPropertiesIfNotTheSameType(ProtocolOptions currentOptions,
                                                                        IConnectionPlugin plugin)
        {
            // prevent to reset properties
            if (currentOptions == null || currentOptions.GetType() != plugin.GetOptionsType())
            {
                return(plugin.CreateOptions());
            }

            return(currentOptions);
        }
Beispiel #3
0
        /// <summary>重新加载</summary>
        /// <param name="storageSchemaId">数据存储架构标识</param>
        /// <param name="ibatisMapping">IBatis映射文件</param>
        public Dictionary <string, ISqlMapper> CreateSqlMappers(string storageSchemaId, string ibatisMapping)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug("X3Platform.Storages.StorageContext.Instance.CreateSqlMappers(storageSchemaId:\"" + storageSchemaId + "\", ibatisMapping:\"" + ibatisMapping + "\") begin");
            }

            // 一个存储节点对应一个IBatisMapper对象, 每个节点都需要重复创建对象
            Dictionary <string, ISqlMapper> ibatisMappers = new Dictionary <string, ISqlMapper>();

            IList <IStorageNode> storageNodes = StorageContext.Instance.StorageNodeService.FindAllBySchemaId(storageSchemaId);

            logger.Debug("X3Platform.Storages.StorageContext.Instance.CreateSqlMappers() A");

            foreach (IStorageNode storageNode in storageNodes)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("X3Platform.Storages.StorageContext.Instance.CreateSqlMappers() creating " + storageNode.Name);
                }

                var ibatisMapper = ISqlMapHelper.CreateSqlMapper(ibatisMapping, true);

                ibatisMapper.DataSource.ConnectionString = KernelConfigurationView.Instance.ReplaceKeyValue(storageNode.ConnectionString);

                // MySQL 数据库 自动增加非默认端口信息
                if (ibatisMapper.DataSource.DbProvider.Name == "MySql" && ibatisMapper.DataSource.ConnectionString.ToLower().IndexOf("port=") == -1)
                {
                    IConnectionPlugin connection = KernelConfigurationView.Instance.ConnectionPlugin;

                    string port = connection.Port;

                    if (!string.IsNullOrEmpty(port) && port != "3306")
                    {
                        ibatisMapper.DataSource.ConnectionString = StringHelper.TrimEnd(ibatisMapper.DataSource.ConnectionString, ";") + ";port=" + port + ";";
                    }
                }

                ibatisMappers.Add(storageNode.Name, ibatisMapper);
            }

            if (logger.IsDebugEnabled)
            {
                logger.Debug("X3Platform.Storages.StorageContext.Instance.CreateSqlMappers(storageSchemaId:\"" + storageSchemaId + "\", ibatisMapping:\"" + ibatisMapping + "\") end");
            }

            return(ibatisMappers);
        }
        /// <summary>加载选项</summary>
        private void LoadOptions()
        {
            // 加载配置文件信息
            this.configurationSource = (KernelConfiguration)ConfigurationManager.GetSection(KernelConfiguration.SectionName);

            if (this.configurationSource == null)
            {
                this.configurationSource = new KernelConfiguration();

                // 加载默认配置文件
                string configurationFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "config\\X3Platform.config";

                if (File.Exists(configurationFilePath))
                {
                    XmlDocument doc = new XmlDocument();

                    doc.Load(configurationFilePath);

                    XmlNodeList nodes = doc.SelectNodes("configuration/" + KernelConfiguration.SectionName + "/key");

                    foreach (XmlNode node in nodes)
                    {
                        this.AddKeyValue(node.Attributes["name"].Value, node.Attributes["value"].Value, true);
                    }
                }
            }

            Type objectType = Type.GetType(this.Configuration.Keys.ContainsKey("DatabaseSettings.Plugin")
                ? this.Configuration.Keys["DatabaseSettings.Plugin"].Value
                : "X3Platform.Data.ConnectionPlugins.InnerConnectionPlugin,X3Platform.Support");

            if (objectType != null)
            {
                IConnectionPlugin connection = this.m_ConnectionPlugin = (IConnectionPlugin)Activator.CreateInstance(objectType, this.Configuration);

                if (connection.Valid)
                {
                    try
                    {
                        // 由于 GenericSqlCommand 使用了日志功能, 初始化时会调用 KernelConfigurationView 对象
                        // 所以这里使用原生的对象读取数据
                        DbProviderFactory providerFactory = DbProviderFactories.GetFactory(GetProviderName(connection.Provider));

                        using (DbConnection conn = providerFactory.CreateConnection())
                        {
                            conn.ConnectionString = connection.ConnectionString;

                            conn.Open();

                            using (DbCommand cmd = providerFactory.CreateCommand())
                            {
                                cmd.Connection = conn;

                                cmd.CommandText = this.OptionCommandText;

                                DbDataReader reader = cmd.ExecuteReader();

                                if (reader != null)
                                {
                                    while (reader.Read())
                                    {
                                        this.AddKeyValue(reader.GetString(0), reader.GetString(1), true);
                                    }

                                    reader.Close();
                                }
                            }

                            conn.Close();
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #5
0
 private static void SortPlugin(Dictionary <string, IConnectionPlugin> sortedPlugins, IConnectionPlugin loaded)
 {
     if (sortedPlugins.ContainsKey(loaded.PortName))
     {
         LogDuplicitPlugin(loaded);
     }
     else
     {
         sortedPlugins.Add(loaded.PortName, loaded);
     }
 }
Beispiel #6
0
 private static bool PluginIsOnPort(int port, IConnectionPlugin plugin)
 {
     return(plugin.Port == port);
 }
Beispiel #7
0
        internal Connection CreateConnection(IFavorite favorite)
        {
            IConnectionPlugin plugin = FindPlugin(favorite.Protocol);

            return(plugin.CreateConnection());
        }
Beispiel #8
0
        /// <summary>
        /// Explicit call of update properties container depending on selected protocol.
        /// Don't call this in property setter, because of serializer.
        /// Returns never null instance of the options, in case the protocol is identical, returns currentOptions.
        /// </summary>
        internal ProtocolOptions UpdateProtocolPropertiesByProtocol(string newProtocol, ProtocolOptions currentOptions)
        {
            IConnectionPlugin plugin = FindPlugin(newProtocol);

            return(SwitchPropertiesIfNotTheSameType(currentOptions, plugin));
        }
Beispiel #9
0
        internal Control[] CreateControls(string newProtocol)
        {
            IConnectionPlugin plugin = FindPlugin(newProtocol);

            return(plugin.CreateOptionsControls());
        }
Beispiel #10
0
        internal int GetPort(string name)
        {
            IConnectionPlugin plugin = FindPlugin(name);

            return(plugin.Port);
        }