Example #1
0
        public Hashtable GetClass(Alachisoft.NCache.Config.Dom.Class[] cl, AssemblyDef asm)
        {
            Hashtable hash = new Hashtable();
            Hashtable latt = new Hashtable();

            Alachisoft.NCache.Config.Dom.Class c = new Alachisoft.NCache.Config.Dom.Class();
            //c.Assembly = asm.ToString(); //cg

            c.Name = Class;
            TypeDef type        = asm.GetType(Class);
            string  assemblySrt = null;

            assemblySrt = asm.FullName;//= c.Assembly ; //cg

            String fullVersion = String.Empty;

            if (!String.IsNullOrEmpty(assemblySrt))
            {
                String version = assemblySrt.Split(',')[1];
                fullVersion = version.Split('=')[1];
            }
            c.ID = Class;
            if (cl != null)
            {
                hash = ClassToHashtable(cl);
            }
            defaults = CreateLuceneDefaults();
            if (hash.Contains(c.Name))
            {
                Class existingClass = (Class)hash[c.Name];
                if (existingClass.LuceneSection != null)
                {
                    defaults = existingClass.LuceneSection;
                    latt     = LuceneAttribToHashtable(existingClass.LuceneSection.Attributes);
                    if (existingClass.Attributes != null)
                    {
                        c.Attributes = existingClass.Attributes;
                    }
                }
            }
            c.LuceneSection = defaults;

            if (Attributes != null || Attributes != string.Empty)
            {
                latt = LuceneAttributesToAdd(latt, type);
                if (c.Attributes != null)
                {
                    ConflictWithNormalAttributes(c.Attributes, latt);
                }
                c.LuceneSection.Attributes = latt.Values.Cast <LuceneAttrib>().ToArray();
            }


            hash[c.Name] = c;
            return(hash);
        }
        public Hashtable GetClass(Alachisoft.NCache.Config.Dom.Class[] cl, AssemblyDef asm)
        {
            Hashtable hash = new Hashtable();
            Hashtable latt = new Hashtable();

            Alachisoft.NCache.Config.Dom.Class c = new Alachisoft.NCache.Config.Dom.Class();
            //c.Assembly = asm.ToString(); //cg

            c.Name = Class;
            TypeDef type        = asm.GetType(Class);
            string  assemblySrt = null;

            assemblySrt = asm.FullName;//= c.Assembly ; //cg

            String fullVersion = String.Empty;

            if (!String.IsNullOrEmpty(assemblySrt))
            {
                String version = assemblySrt.Split(',')[1];
                fullVersion = version.Split('=')[1];
            }
            c.ID = Class;
            if (cl != null)
            {
                hash = ClassToHashtable(cl);
            }
            //defaults = CreateLuceneDefaults();
            c.LuceneSection = new LuceneAttributes();
            if (hash.Contains(c.Name))
            {
                Class existingClass = (Class)hash[c.Name];
                if (existingClass.LuceneSection != null)
                {
                    c.LuceneSection = existingClass.LuceneSection;
                }
            }
            c.LuceneSection = WriteLuceneDefaults(c.LuceneSection);

            hash[c.Name] = c;
            return(hash);
        }
        private void AddLuceneQueryIndexDefaults()
        {
            if (!ValidateParameters())
            {
                return;
            }
            bool        successful = true;
            AssemblyDef asm        = null;

            //ArrayList cc = new ArrayList();
            Alachisoft.NCache.Config.Dom.Class[] queryClasses = null;
            string failedNodes = string.Empty;
            string serverName  = string.Empty;

            try
            {
                if (Port == -1)
                {
                    NCache.Port = NCache.UseTcp ? CacheConfigManager.NCacheTcpPort : CacheConfigManager.HttpPort;
                }
                if (Server != null && Server != string.Empty)
                {
                    NCache.ServerName = Server;
                }
                if (Port != -1)
                {
                    NCache.Port = Port;
                }
                try
                {
                    cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                }
                catch (Exception e)
                {
                    OutputProvider.WriteErrorLine("Error: NCache service could not be contacted on server.");
                    return;
                }

                ToolsUtil.VerifyClusterConfigurations(cacheServer.GetNewConfiguration(CacheName), CacheName);


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

                    serverConfig = cacheServer.GetNewConfiguration(CacheName);
                    //ConfiguredCacheInfo[] configuredCaches = cacheServer.GetAllConfiguredCaches();

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

                    //if (! Unregister)
                    //{
                    try
                    {
                        asm = AssemblyDef.LoadFrom(AssemblyPath);

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

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

                    TypeDef type = asm.GetType(Class);


                    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));


                    byte[] userId = null;
                    byte[] paswd  = null;
                    if (UserId != string.Empty && Password != string.Empty)
                    {
                        userId = EncryptionUtil.Encrypt(UserId);
                        paswd  = EncryptionUtil.Encrypt(Password);
                    }
                    serverConfig.ConfigVersion++;
                    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(CacheName))
                                {
                                    throw new Exception(CacheName + " is Running on " + serverName +
                                                        "\nStop the cache first and try again.");
                                }

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