コード例 #1
0
ファイル: TenantCache.cs プロジェクト: brhinescot/Loom
        private static Dictionary <string, TenantData> RegisterAllSectionsEx()
        {
            Dictionary <string, TenantData> tenantDataCache = new Dictionary <string, TenantData>(StringComparer.OrdinalIgnoreCase);

            BuildManagerWrapper buildManager    = new BuildManagerWrapper();
            IEnumerable <Type>  sectionTypes    = TypeCache.GetFilteredTypesFromAssemblies("cportal_sections", t => typeof(SectionBase).IsAssignableFrom(t) && t.GetConstructor(new Type[0]) != null, buildManager);
            IEnumerable <Type>  controllerTypes = TypeCache.GetFilteredTypesFromAssemblies("cportal_controllers", t => typeof(MvcController).IsAssignableFrom(t) && t.GetConstructor(new Type[0]) != null, buildManager);

            foreach (Type type in sectionTypes)
            {
                SectionBase    sectionBase = (SectionBase)Activator.CreateInstance(type);
                SectionContext context     = new SectionContext();

                if (Compare.IsNullOrEmpty(sectionBase.Name))
                {
                    sectionBase.Name = type.Name.EndsWith("section", StringComparison.OrdinalIgnoreCase) ? type.Name.Substring(0, type.Name.Length - 7) : type.Name;
                }

                sectionBase.OnRegister(context);

                if (Compare.IsNullOrEmpty(sectionBase.Name))
                {
                    throw new PortalFatalException("The 'Name' property of the SectionContext has not been properly initialized or has an empty value.");
                }

                SectionData section = new SectionData(sectionBase.Name, type.Namespace);
                section.AddPrimaryRoute(sectionBase.DefaultRoute);

                foreach (Route route in context.Routes)
                {
                    section.AddPrimaryRoute(route);
                }

                Match  match      = Expressions.TenantPathRegex.Match(type.FullName);
                string tenantName = match.Success ? match.Groups["TENANT"].Value : "DefaultTenant";

                TenantData tenantData;
                if (tenantDataCache.ContainsKey(tenantName))
                {
                    tenantData = tenantDataCache[tenantName];

                    if (tenantData.Sections.ContainsKey(section.Name))
                    {
                        throw new PortalFatalException(string.Format("A section named {0} has already been added.", section.Name));
                    }

                    tenantData.Sections.Add(section.Name, section);
                }
                else
                {
                    tenantData = new TenantData();
                    tenantData.Sections.Add(section.Name, section);
                    tenantDataCache.Add(tenantName, tenantData);
                }

                if (match.Groups["NAMESPACE"].Length > 0)
                {
                    tenantData.Namespace = match.Groups["NAMESPACE"].Value;
                }
                if (match.Groups["TENANT"].Length > 0)
                {
                    tenantData.Name = match.Groups["TENANT"].Value;
                }

                foreach (string host in context.Hosts)
                {
                    if (TenantHostCache.ContainsKey(host))
                    {
                        throw new PortalFatalException(string.Format("The host {0} has already been added.", host));
                    }

                    TenantHostCache.Add(host, tenantData.Name);
                }
            }

            foreach (TenantData tenantData in tenantDataCache.Values)
            {
                if (!tenantData.Sections.ContainsKey(DefaultSection.DefaultName))
                {
                    tenantData.DefaultSection = new DefaultSection();
                    tenantData.DefaultSection.AddPrimaryRoute(new Route("Default", "/{controller,0,1}/{action,0,1}/{arguments,*}/"));

                    tenantData.Sections.Add(DefaultSection.DefaultName, tenantData.DefaultSection);
                }
                else
                {
                    tenantData.DefaultSection = tenantData.Sections[DefaultSection.DefaultName];
                }
            }

            // Register controllers with matching tenant.
            foreach (Type controllerType in controllerTypes)
            {
                string controllerNamespace = controllerType.Namespace;
                if (controllerNamespace == null)
                {
                    throw new PortalFatalException("Controllers must have a namespace.");
                }

                TenantData foundTenant = controllerNamespace.Contains("Tenants")
                    ? tenantDataCache.Values.FirstOrDefault(td => td.Namespace != null && td.Sections.Values.Any(sd => controllerNamespace.StartsWith(sd.Namespace)))
                    : tenantDataCache["DefaultTenant"];

                if (foundTenant == null)
                {
                    throw new PortalFatalException("Cannot find a tenant.");
                }

                RegisterControllerEx(controllerType, foundTenant, null);
            }

            PortalSettingsSection config = (PortalSettingsSection)ConfigurationManager.GetSection(ConfigSectionName);

            if (config != null && config.Routes.AllowDatabaseRoutes)
            {
                InitializeDatabaseRoutes(tenantDataCache);
            }

            if (config != null && config.Setup)
            {
                RegisterControllerEx(typeof(SetupController), new TenantData(), null);
            }

            return(tenantDataCache);
        }
コード例 #2
0
 public virtual void OnRegister(SectionContext context)
 {
 }