Пример #1
0
        private int findAction(String key)
        {
            int kl = key.Length;

            for (int i = 0; i < actions.Count; i++)
            {
                ActionAdmin a = actions[i];
                if (a.KeyLen < kl)
                {
                    continue;
                }
                if (a.KeyLen > kl)
                {
                    return(-1);
                }
                int rc = String.CompareOrdinal(a.Key, key);
                if (rc < 0)
                {
                    continue;
                }
                if (rc > 0)
                {
                    return(-1);
                }

                return(i);
            }
            return(-1);
        }
Пример #2
0
        private void prepareActions()
        {
            actionDict = new Dictionary <string, ActionAdmin>(actions.Count);
            if (actions.Count == 0)
            {
                return;
            }
            int equalityID = 0;

            actions.Sort(cbSortAction);
            ActionAdmin prev = actions[0];

            actionDict.Add(prev.Key, prev);
            prev.EqualToPrev = false;
            prev.Index       = 0;
            prev.EqualityID  = equalityID;
            for (int i = 1; i < actions.Count; i++)
            {
                ActionAdmin a = actions[i];
                a.Index       = i;
                a.EqualityID  = equalityID;
                a.EqualToPrev = (a.Key == prev.Key);
                if (a.EqualToPrev)
                {
                    continue;
                }
                prev         = a;
                a.EqualityID = ++equalityID;
                actionDict.Add(a.Key, a);
            }
        }
Пример #3
0
        private static int cbSortAction(ActionAdmin left, ActionAdmin right)
        {
            var intComparer = Comparer <int> .Default;
            int rc          = intComparer.Compare(left.KeyLen, right.KeyLen);

            if (rc != 0)
            {
                return(rc);
            }

            rc = String.CompareOrdinal(left.Key, right.Key);
            if (rc != 0)
            {
                return(rc);
            }

            return(intComparer.Compare(left.Order, right.Order));
        }
Пример #4
0
        public void Start(PipelineContext ctx)
        {
            if (trace)
            {
                ctx.ImportFlags |= _ImportFlags.TraceValues;
            }
            missed = new StringDict();
            if (ScriptTypeName != null) //NB: always create a new script object. Never reuse an existing one.
            {
                ScriptObject = Objects.CreateObject(ScriptTypeName, ctx);
                logger.Log("Script({0})={1}", ScriptTypeName, ScriptObject);
            }
            if (ImportEngine.ScriptExpressions.Count > 0) //NB: always create a new script object. Never reuse an existing one.
            {
                String cls = ImportEngine.ScriptExpressions.FullClassName;
                ScriptExprObject = Objects.CreateObject(cls, ctx);
                logger.Log("ScriptExpr({0})={1}", cls, ScriptExprObject);
            }

            //Clone the list of actions and strat them
            actions = new List <ActionAdmin>(definedActions.Count);
            for (int i = 0; i < definedActions.Count; i++)
            {
                ActionAdmin act = definedActions[i];
                act.Action.Start(ctx);
                actions.Add(act);
            }
            prepareActions();

            if (endPointCache != null)
            {
                foreach (var kvp in this.endPointCache)
                {
                    kvp.Value.Endpoint.Start(ctx);
                }
            }

            started = true;
        }
Пример #5
0
        public Object HandleValue(PipelineContext ctx, String key, Object value)
        {
            Object orgValue   = value;
            Object ret        = null;
            Object lastAction = null;

            try
            {
                ctx.ActionFlags = 0;

                if ((ctx.ImportFlags & _ImportFlags.TraceValues) != 0)
                {
                    logger.Log("HandleValue ({0}, {1} [{2}]", key, value, value == null ? "null" : value.GetType().Name);
                }

                if (key == null)
                {
                    goto EXIT_RTN;
                }
                String lcKey  = key.ToLowerInvariant();
                int    keyLen = lcKey.Length;

                if (ctx.SkipUntilKey != null)
                {
                    ctx.ActionFlags |= _ActionFlags.Skipped;
                    if (ctx.SkipUntilKey.Length == keyLen && lcKey.Equals(ctx.SkipUntilKey, StringComparison.OrdinalIgnoreCase))
                    {
                        ctx.SkipUntilKey = null;
                    }
                    goto EXIT_RTN;
                }

                int ixStart = findAction(lcKey);
                if (ixStart < 0)
                {
                    if (templates.Count == 0 || !checkTemplates(ctx, key, ref lastAction)) //templates==0: otherwise checkTemplates() inserts a NOP action...
                    {
                        missed[lcKey] = null;
                        goto EXIT_RTN;
                    }
                    ixStart = findAction(lcKey);
                    if (ixStart < 0)
                    {
                        goto EXIT_RTN;          //Should not happen, just to be sure!
                    }
                }

                for (int i = ixStart; i < actions.Count; i++)
                {
                    ActionAdmin a = actions[i];
                    if (i > ixStart && !a.EqualToPrev)
                    {
                        break;
                    }

                    lastAction = ctx.SetAction(a.Action);
                    Object tmp = a.Action.HandleValue(ctx, key, value);
                    ClearVariables(a.Action.VarsToClear);
                    if (tmp != null)
                    {
                        ret = tmp;
                    }
                    if ((ctx.ActionFlags & (_ActionFlags.SkipRest | _ActionFlags.ConditionMatched)) != 0)
                    {
                        if ((ctx.ActionFlags & _ActionFlags.ConditionMatched) != 0)
                        {
                            if (!a.IsCondition)
                            {
                                throw new BMException("Action [{0}] is not a condition.", a.Key);
                            }
                            i += a.ActionsToSkipIfCond;
                            continue;
                        }
                        break;
                    }
                }

                //Make sure the skipUntil can also be set from the last action in a chain...
                if (ctx.SkipUntilKey != null && ctx.SkipUntilKey.Length == keyLen && lcKey.Equals(ctx.SkipUntilKey, StringComparison.OrdinalIgnoreCase))
                {
                    ctx.SkipUntilKey = null;
                }

                EXIT_RTN : return(ret);
            }
            catch (Exception e)
            {
                String type;
                if (orgValue == value)
                {
                    type = String.Format("[{0}]", getType(orgValue));
                }
                else
                {
                    type = String.Format("[{0}] (was [{1}])", getType(value), getType(orgValue));
                }
                ctx.ErrorLog.Log("Exception while handling event. Key={0}, value type={1}, action={2}", key, type, lastAction);
                ctx.ErrorLog.Log("-- value={0}", value);
                if (orgValue != value)
                {
                    ctx.ErrorLog.Log("-- orgvalue={0}", orgValue);
                }

                ctx.ErrorLog.Log(e);
                PipelineAction act = lastAction as PipelineAction;
                if (act == null)
                {
                    ctx.ErrorLog.Log("Cannot dump accu: no current action found.");
                }
                else
                {
                    var accu = (JObject)act.Endpoint.GetFieldAsToken(null);
                    ctx.ErrorLog.Log("Dumping content of current accu: fieldcount={0}", accu.Count);
                    String content = accu.ToString();
                    if (content != null && content.Length > 1000)
                    {
                        content = content.Substring(0, 1000) + "...";
                    }
                    ctx.ErrorLog.Log(content);
                }
                if (MaxAddsExceededException.ContainsMaxAddsExceededException(e))
                {
                    throw;
                }


                throw new BMException(e, "{0}\r\nKey={1}, valueType={2}.", e.Message, key, type);
            }
        }