Пример #1
0
        /// <summary>
        /// The main entry point for the tool.
        /// </summary>ju
        static public void Run(string[] args)
        {
            bool successful = true;

            System.Reflection.Assembly           asm          = null;
            Alachisoft.NCache.Config.Dom.Class[] queryClasses = null;
            string failedNodes = string.Empty;
            string serverName  = string.Empty;

            try
            {
                object param = new ConfigureQueryIndexParam();
                CommandLineArgumentParser.CommandLineParser(ref param, args);
                cParam = (ConfigureQueryIndexParam)param;
                if (cParam.IsUsage)
                {
                    AssemblyUsage.PrintLogo(cParam.IsLogo);
                    AssemblyUsage.PrintUsage();
                    return;
                }
                if (!ValidateParameters())
                {
                    successful = false;
                    return;
                }
                if (cParam.Port == -1)
                {
                    NCache.Port = NCache.UseTcp ? CacheConfigManager.NCacheTcpPort : CacheConfigManager.HttpPort;
                }
                if (cParam.Server != null && cParam.Server != string.Empty)
                {
                    NCache.ServerName = cParam.Server;
                }
                if (cParam.Port != -1)
                {
                    NCache.Port = cParam.Port;
                }

                cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                string extension = ".dll";
                if (cacheServer != null)
                {
                    serverName = cacheServer.GetClusterIP();
                    if (cacheServer.IsRunning(cParam.CacheId))
                    {
                        throw new Exception(cParam.CacheId + " is Running on " + serverName +
                                            "\nStop the cache first and try again.");
                    }

                    Alachisoft.NCache.Config.NewDom.CacheServerConfig serverConfig =
                        cacheServer.GetNewConfiguration(cParam.CacheId);

                    if (serverConfig == null)
                    {
                        throw new Exception("Specified cache is not registered on the given server.");
                    }

                    try
                    {
                        asm = System.Reflection.Assembly.LoadFrom(cParam.AsmPath);

                        extension = Path.GetExtension(asm.FullName);
                    }
                    catch (Exception e)
                    {
                        string message = string.Format("Could not load assembly \"" + cParam.AsmPath + "\". {0}",
                                                       e.Message);
                        Console.Error.WriteLine("Error : {0}", message);
                        LogEvent(e.Message);
                        successful = false;
                        return;
                    }

                    if (asm == null)
                    {
                        throw new Exception("Could not load specified Assembly");
                    }

                    System.Type type = asm.GetType(cParam.Class, true);


                    if (serverConfig.CacheSettings.QueryIndices == null)
                    {
                        serverConfig.CacheSettings.QueryIndices         = new Alachisoft.NCache.Config.Dom.QueryIndex();
                        serverConfig.CacheSettings.QueryIndices.Classes = queryClasses;
                    }

                    queryClasses = serverConfig.CacheSettings.QueryIndices.Classes;

                    serverConfig.CacheSettings.QueryIndices.Classes = GetSourceClass(GetClass(queryClasses, asm));

                    if (serverConfig.CacheSettings.CacheType == "clustered-cache")
                    {
                        foreach (Address node in serverConfig.CacheDeployment.Servers.GetAllConfiguredNodes())
                        {
                            NCache.ServerName = node.IpAddress.ToString();
                            try
                            {
                                cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                                if (cacheServer.IsRunning(cParam.CacheId))
                                {
                                    throw new Exception(cParam.CacheId + " is Running on " + serverName +
                                                        "\nStop the cache first.");
                                }

                                Console.WriteLine("Adding query indexes on node '{0}' to cache '{1}'.", serverName, cParam.CacheId);
                                cacheServer.RegisterCache(cParam.CacheId, serverConfig, "", true,
                                                          cParam.IsHotApply);
                            }
                            catch (Exception ex)
                            {
                                Console.Error.WriteLine("Failed to Add Query Index on '{0}'. ", serverName);
                                Console.Error.WriteLine("Error Detail: '{0}'. ", ex.Message);
                                failedNodes = failedNodes + "/n" + node.IpAddress.ToString();
                                LogEvent(ex.Message);
                                successful = false;
                            }
                            finally
                            {
                                cacheServer.Dispose();
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            Console.WriteLine("Adding query indexes on node '{0}' to cache '{1}'.", serverName, cParam.CacheId);
                            cacheServer.RegisterCache(cParam.CacheId, serverConfig, "", true, cParam.IsHotApply);
                        }
                        catch (Exception ex)
                        {
                            Console.Error.WriteLine("Failed to Add Query Index on '{0}'. ", NCache.ServerName);
                            Console.Error.WriteLine("Error Detail: '{0}'. ", ex.Message);
                            LogEvent(ex.Message);
                            successful = false;
                        }
                        finally
                        {
                            cacheServer.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error : {0}", e.Message);
                LogEvent(e.Message);
                successful = false;
            }
            finally
            {
                NCache.Dispose();
                if (successful && !cParam.IsUsage)
                {
                    Console.WriteLine("Query indexes successfully added.");
                }
            }
        }