Example #1
0
 /// <inheritdoc/>
 public object Invoke(Type scopingType, object scopingKey, ParameterCollection parameters)
 {
     if (this.m_dispatchers == null)
     {
         this.m_dispatchers = DispatcherFactoryUtil.GetFactories().Select(o => o.Value.Id).ToArray();
     }
     return(new GenericRestResultCollection()
     {
         Values = this.m_dispatchers.OfType <Object>().ToList()
     });
 }
Example #2
0
        /// <summary>
        /// Get all dispatchers and subscriptions
        /// </summary>
        protected IEnumerable <IPubSubDispatcher> GetDispatchers(PubSubEventType eventType, Object data)
        {
            using (AuthenticationContext.EnterSystemContext())
            {
                var resourceName  = data.GetType().GetSerializationName();
                var subscriptions = this.m_pubSubManager
                                    .FindSubscription(o => o.ResourceTypeName == resourceName && o.IsActive && (o.NotBefore == null || o.NotBefore < DateTimeOffset.Now) && (o.NotAfter == null || o.NotAfter > DateTimeOffset.Now))
                                    .Where(o => o.Event.HasFlag(eventType))
                                    .Where(s =>
                {
                    // Attempt to compile the filter criteria into an executable function
                    if (!this.m_filterCriteria.TryGetValue(s.Key.Value, out Func <Object, bool> fn))
                    {
                        Expression dynFn = null;
                        var parameter    = Expression.Parameter(data.GetType());

                        foreach (var itm in s.Filter)
                        {
                            var fFn = QueryExpressionParser.BuildLinqExpression(data.GetType(), NameValueCollection.ParseQueryString(itm), "p", forceLoad: true, lazyExpandVariables: true);
                            if (dynFn is LambdaExpression le)
                            {
                                dynFn = Expression.Lambda(
                                    Expression.And(
                                        Expression.Invoke(le, parameter),
                                        Expression.Invoke(fFn, parameter)
                                        ), parameter);
                            }
                            else
                            {
                                dynFn = fFn;
                            }
                        }

                        if (dynFn == null)
                        {
                            dynFn = Expression.Lambda(Expression.Constant(true), parameter);
                        }
                        parameter = Expression.Parameter(typeof(object));
                        fn        = Expression.Lambda(Expression.Invoke(dynFn, Expression.Convert(parameter, data.GetType())), parameter).Compile() as Func <Object, bool>;
                        this.m_filterCriteria.TryAdd(s.Key.Value, fn);
                    }
                    return(fn(data));
                });

                // Now we want to filter by channel, since the channel is really what we're interested in
                foreach (var chnl in subscriptions.GroupBy(o => o.ChannelKey))
                {
                    var channelDef = this.m_pubSubManager.GetChannel(chnl.Key);
                    var factory    = DispatcherFactoryUtil.FindDispatcherFactoryById(channelDef.DispatcherFactoryId);
                    yield return(factory.CreateDispatcher(chnl.Key, new Uri(channelDef.Endpoint), channelDef.Settings.ToDictionary(o => o.Name, o => o.Value)));
                }
            }
        }
 /// <summary>
 /// Regiser a channel
 /// </summary>
 public PubSubChannelDefinition RegisterChannel(string name, Type dispatcherFactoryType, Uri endpoint, IDictionary <string, string> settings)
 {
     return(this.RegisterChannel(name, DispatcherFactoryUtil.FindDispatcherFactoryByType(dispatcherFactoryType)?.Id, endpoint, settings));
 }
        /// <summary>
        /// Register the specified channel
        /// </summary>
        public PubSubChannelDefinition RegisterChannel(string name, string dispatcherFactoryId, Uri endpoint, IDictionary <string, string> settings)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            else if (String.IsNullOrEmpty(dispatcherFactoryId))
            {
                var dispatchFactory = DispatcherFactoryUtil.FindDispatcherFactoryByUri(endpoint);
                if (dispatchFactory == null)
                {
                    throw new InvalidOperationException("Cannot find dispatcher factory for scheme!");
                }
                dispatcherFactoryId = dispatchFactory.Id;
            }
            else if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }

            // Validate state
            this.m_policyEnforcementService.Demand(PermissionPolicyIdentifiers.CreatePubSubSubscription);

            var channel = new PubSubChannelDefinition()
            {
                Name = name,
                DispatcherFactoryId = dispatcherFactoryId,
                Endpoint            = endpoint.ToString(),
                IsActive            = false,
                Settings            = settings.Select(o => new PubSubChannelSetting()
                {
                    Name = o.Key, Value = o.Value
                }).ToList()
            };

            using (var conn = this.m_configuration.Provider.GetWriteConnection())
            {
                try
                {
                    conn.Open();
                    using (var tx = conn.BeginTransaction())
                    {
                        var dbChannel = this.m_mapper.MapModelInstance <PubSubChannelDefinition, DbChannel>(channel);
                        dbChannel.Endpoint = channel.Endpoint.ToString();

                        // Get the authorship
                        var se = this.m_securityRepository.GetSecurityEntity(AuthenticationContext.Current.Principal);
                        if (se == null)
                        {
                            throw new KeyNotFoundException($"Unable to determine structure data for {AuthenticationContext.Current.Principal.Identity.Name}");
                        }

                        dbChannel.CreatedByKey = se.Key.Value;
                        dbChannel.CreationTime = DateTimeOffset.Now;
                        dbChannel = conn.Insert(dbChannel);

                        // Insert settings
                        foreach (var itm in channel.Settings)
                        {
                            conn.Insert(new DbChannelSetting()
                            {
                                ChannelKey = dbChannel.Key.Value,
                                Name       = itm.Name,
                                Value      = itm.Value
                            });
                        }

                        tx.Commit();
                        channel = this.MapInstance(conn, dbChannel);

                        this.m_cache?.Add(channel);
                        return(channel);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception($"Error creating channel {channel}", e);
                }
            }
        }