Example #1
0
        private void CheckForNewServices(DbBase ctx, String configPath)
        {
            
            string[] confFiles = Directory.GetFiles(configPath, "*.conf");

            foreach (string file in confFiles)
            {

                try
                {
                    CASPluginConfig cfg = new CASPluginConfig();
                    try
                    {
                        cfg.LoadFromXML(new FileInfo(file));
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error parsing config file '" + file + "'", ex);
                    }

                    
                    Uri svc = CASPluginService.Normalize(cfg.Service);

                    //Verifica se o contexto é novo
                    if (ctx.ExecuteScalar<Int64>("select count(*) from [CAS_Context] where [Name] = '" + cfg.Context + "'") == 0)
                    {
                        //Adiciona
                        ctx.ExecuteNonQuery("INSERT INTO [CAS_Context] ([Name],[Host]) VALUES ('" + cfg.Context + "','" + Environment.MachineName + "');");
                    }


                    //Verifica se o serviço é novo
                    if (ctx.ExecuteScalar<Int64>("select count(*) from [CAS_Service] where [Uri] = '" + svc.AbsoluteUri + "'") == 0)
                    {

                        //Adiciona o serviço
                        ctx.ExecuteNonQuery("INSERT INTO [CAS_Service] ([Context_Name],[Uri],[Plugin_Assembly],[Permit_Password_Recover],[External_Password_Recover],[Password_RecoverUri],[Permit_Change_Password],[Admin]) VALUES ('" + cfg.Context + "','" + svc.AbsoluteUri + "','" + cfg.PluginAssembly + "'," + (cfg.PermitPasswordRecover ? 1 : 0) + "," + (cfg.ExternalPasswordRecover ? 1 : 0) + ",'" + cfg.PasswordRecoverUri + "'," + (cfg.PermitChangePassword ? 1 : 0) + ",'" + cfg.Admin + "');");
                        
                    }
                    else
                    {
                        //Atualiza o serviço
                        ctx.ExecuteNonQuery("update [CAS_Service] set [Context_Name] = '" + cfg.Context + "', [Plugin_Assembly] = '" + cfg.PluginAssembly + "',[Permit_Password_Recover] = " + (cfg.PermitPasswordRecover ? 1 : 0) + ",[External_Password_Recover] = " + (cfg.ExternalPasswordRecover ? 1 : 0) + ",[Password_RecoverUri] = '" + cfg.PasswordRecoverUri + "',[Permit_Change_Password] = " + (cfg.PermitChangePassword ? 1 : 0) + ",[Admin] = '" + cfg.Admin + "' where [Uri] = '" + svc.AbsoluteUri + "'");

                        //Apaga as propriedades
                        ctx.ExecuteNonQuery("delete from [CAS_Service_Attributes] where [Service_Uri] = '" + svc.AbsoluteUri + "'");
                    }

                    //Adiciona as propriedades
                    foreach(String key in cfg.Attributes.Keys)
                        ctx.ExecuteNonQuery("INSERT INTO [CAS_Service_Attributes] ([Service_Uri],[Key],[Value]) VALUES ('" + svc.AbsoluteUri + "','" + key + "','" + (cfg.Attributes[key] is DateTime ? ((DateTime)cfg.Attributes[key]).ToString("o") : cfg.Attributes[key].ToString()) + "');");


                }
                catch(Exception ex) {
                    throw ex;
                }
            }

        }
Example #2
0
        public static List <CASPluginService> GetPlugins2(String configPath, String assemblyPath, out String outLog)
        {
            List <CASPluginService> ret = new List <CASPluginService>();
            StringBuilder           log = new StringBuilder();

            outLog = "";
            try
            {
                log.AppendLine("Starting GetPlugins2");

                log.AppendLine("configPath exists? " + Directory.Exists(configPath));
                log.AppendLine("assemblyPath exists? " + Directory.Exists(assemblyPath));

                if (!Directory.Exists(configPath) || !Directory.Exists(assemblyPath))
                {
                    return(ret);
                }

                string[] confFiles = Directory.GetFiles(configPath, "*.conf");

                log.AppendLine("confFiles.Length = " + confFiles.Length);

                foreach (string file in confFiles)
                {
                    try
                    {
                        log.AppendLine("1");
                        CASPluginConfig cfg = new CASPluginConfig();
                        try
                        {
                            cfg.LoadFromXML(new FileInfo(file));
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Error parsing config file '" + file + "'", ex);
                        }


                        log.AppendLine("2");
                        if (!String.IsNullOrEmpty(cfg.PluginAssembly))
                        {
                            log.AppendLine("3");
                            FileInfo asmFile = new FileInfo(Path.Combine(assemblyPath, cfg.PluginAssembly));
                            if (asmFile.Exists)
                            {
                                log.AppendLine("4");
                                Assembly assembly = Assembly.LoadFile(asmFile.FullName);

                                log.AppendLine("5");
                                CASPluginService newItem = new CASPluginService();
                                newItem.Config = cfg;

                                log.AppendLine("6");
                                Type[] classes = assembly.GetTypes();

                                log.AppendLine("7");
                                foreach (Type type in assembly.GetTypes())
                                {
                                    log.AppendLine("8");
                                    if (!type.IsClass || type.IsNotPublic)
                                    {
                                        continue;
                                    }

                                    log.AppendLine("9");
                                    if (type.BaseType.Equals(typeof(CASConnectorBase))) //Primeiro nível
                                    {
                                        newItem.Plugin = type;

                                        /*object obj = Activator.CreateInstance(type);
                                         * CASConnectorBase t = (CASConnectorBase)obj;
                                         * newItem.Plugin = t;*/
                                    }
                                    else if ((type.BaseType.BaseType != null) && type.BaseType.BaseType.Equals(typeof(CASConnectorBase))) //Segundo nível
                                    {
                                        newItem.Plugin = type;

                                        /*
                                         * object obj = Activator.CreateInstance(type);
                                         * CASConnectorBase t = (CASConnectorBase)obj;
                                         * newItem.Plugin = t;*/
                                    }
                                }

                                log.AppendLine("10");
                                if (newItem.Plugin != null)
                                {
                                    ret.Add(newItem);
                                }

                                log.AppendLine("11");
                                log.AppendLine("Config file '" + file + "' loaded as " + newItem.Config.Service);
                            }
                            else
                            {
                                log.AppendLine("Erro on load config file '" + file + "': Assembly file not exists (" + asmFile.FullName + ")");
                                cfg = null;
                            }
                        }
                        else
                        {
                            log.AppendLine("Erro on load config file '" + file + "': Parameter PluginAssembly is empty");
                            cfg = null;
                        }
                    }
                    catch (Exception ex) {
                        log.AppendLine("Erro on load config file '" + file + "': " + ex.Message);
                    }
                    finally
                    {
                    }

                    log.AppendLine("");
                }
            }
            finally
            {
                outLog = log.ToString();
                log.Clear();
                log = null;
            }

            return(ret);
        }