Exemple #1
0
        /// <summary>
        /// Task to read response in loop.
        /// </summary>
        /// <param name="call"></param>
        /// <returns></returns>
        private Task ReadResponse(AsyncDuplexStreamingCall <RequestBlockInfo, TResponse> call)
        {
            var responseReaderTask = Task.Run(async() =>
            {
                while (await call.ResponseStream.MoveNext())
                {
                    var response = call.ResponseStream.Current;

                    // request failed or useless response
                    if (!response.Success)
                    {
                        _realInterval = AdjustInterval();
                        continue;
                    }
                    if (response.Height != _next || !ToBeIndexedInfoQueue.TryAdd(response.BlockInfoResult))
                    {
                        continue;
                    }

                    _next++;
                    _realInterval = _interval;
                    _logger?.Trace($"Received response from chain {response.BlockInfoResult.ChainId} at height {response.Height}");
                }
            });

            return(responseReaderTask);
        }
Exemple #2
0
 /// <inheritdoc />
 public void LogTrace <T>(T value)
 {
     if (IsTraceLoggingEnabled)
     {
         _logger?.Trace(value);
     }
 }
        protected virtual bool CheckInputData(Image <Gray, byte> originalImage, Image <Gray, byte> maskImage, Dictionary <SearchOrientations, Rectangle> calcAreas)
        {
            if (originalImage == null || maskImage == null || calcAreas == null)
            {
                _logger?.Trace("EdgeLineFinder - the input data is not proper, some of them is null.");
                return(false);
            }

            if (originalImage?.Width != maskImage?.Width || originalImage?.Height != maskImage?.Height)
            {
                _logger?.Trace("EdgeLineFinder - the side length of input images is not proper.");
                return(false);
            }

            foreach (var calcarea in _calcAreas)
            {
                if (calcarea.Value.X < 0 || calcarea.Value.Y < 0 || calcarea.Value.Width < 0 || calcarea.Value.Height < 0 ||
                    calcarea.Value.X + calcarea.Value.Width > originalImage.Width ||
                    calcarea.Value.Y + calcarea.Value.Height > originalImage.Height)
                {
                    _logger?.Trace("EdgeLineFinder - the calculation area is not proper.");
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// TODO: description
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            Stopwatch timer = Stopwatch.StartNew();

            int bytesReceived = 0;

            while (bytesReceived < count)
            {
                // read any data available in the receive buffer
                int bytesRead = _stream.Read(buffer, offset + bytesReceived, count - bytesReceived);
                bytesReceived += bytesRead;

                _logger?.Trace("Read {Count} bytes from buffer {Buffer} at offset {Offset}", bytesRead, buffer.GetHashCode(), offset);

                // reset the timeout if still actively receiving data
                if (bytesRead > 0)
                {
                    timer = Stopwatch.StartNew();
                }

                // check for timeouts
                if (timer.ElapsedMilliseconds > _stream.ReadTimeout)
                {
                    throw new TimeoutException();
                }
            }

            return(bytesReceived);
        }
Exemple #5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="dir"></param>
 /// <param name="log"></param>
 public static void Clear(this DirectoryInfo dir, ILogger log = null)
 {
     foreach (var file in dir.EnumerateFiles())
     {
         try
         {
             file.Delete();
             log?.Trace($"{file.FullName} deleted");
         }
         catch (Exception)
         {
             log?.Error($"failed to delete {file.FullName}");
             throw;
         }
     }
     foreach (var d in dir.EnumerateDirectories())
     {
         int retryCount = 1;
         while (retryCount > 0)
         {
             d.Clear(log);
             try
             {
                 d.Delete();
                 log?.Trace($"{d.FullName} deleted");
             }
             catch (Exception)
             {
                 log?.Error($"failed to delete {d.FullName}");
                 Thread.Sleep(2000);
                 --retryCount;
             }
         }
     }
 }
Exemple #6
0
        public Emitter(IDictionary<string,
            TypeDefinition> typeDefinitions,
            BridgeTypes bridgeTypes,
            List<ITypeInfo> types,
            IValidator validator,
            IMemberResolver resolver,
            Dictionary<string, ITypeInfo> typeInfoDefinitions,
            ILogger logger)
        {
            this.Log = logger;

            this.Resolver = resolver;
            this.TypeDefinitions = typeDefinitions;
            this.TypeInfoDefinitions = typeInfoDefinitions;
            this.Types = types;
            this.BridgeTypes = bridgeTypes;

            this.BridgeTypes.InitItems(this);

            logger.Trace("Sorting types infos by name...");
            this.Types.Sort(this.CompareTypeInfosByName);
            logger.Trace("Sorting types infos by name done");

            this.SortTypesByInheritance();

            this.Validator = validator;
            this.AssignmentType = ICSharpCode.NRefactory.CSharp.AssignmentOperatorType.Any;
            this.UnaryOperatorType = ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Any;
            this.JsDoc = new JsDoc();
        }
Exemple #7
0
        public string Invoke(string methodName, IDictionary <string, string> parameters, bool skipAuthorization = false)
        {
            if (!skipAuthorization && !IsAuthorized)
            {
                var message = $"Метод '{methodName}' нельзя вызывать без авторизации";
                _logger?.Error(message: message);

                throw new AccessTokenInvalidException(message: message);
            }

            var url    = $"https://api.vk.com/method/{methodName}";
            var answer = "";

            void SendRequest(string method, IDictionary <string, string> @params)
            {
                LastInvokeTime = DateTimeOffset.Now;

                var response = RestClient.PostAsync(uri: new Uri(uriString: $"https://api.vk.com/method/{method}"), parameters: @params)
                               .Result;

                answer = response.Value ?? response.Message;
            }

            // Защита от превышения количества запросов в секунду
            if (RequestsPerSecond > 0 && LastInvokeTime.HasValue)
            {
                if (_expireTimer == null)
                {
                    SetTimer(expireTime: 0);
                }

                lock (_expireTimerLock)
                {
                    var span = LastInvokeTimeSpan?.TotalMilliseconds;

                    if (span < _minInterval)
                    {
                        var timeout = (int)_minInterval - (int)span;
                                        #if NET40
                        Thread.Sleep(millisecondsTimeout: timeout);
                                        #else
                        Task.Delay(millisecondsDelay: timeout).Wait();
                                        #endif
                    }

                    SendRequest(method: methodName, @params: parameters);
                }
            }
            else if (skipAuthorization)
            {
                SendRequest(method: methodName, @params: parameters);
            }

            _logger?.Trace(message: $"Uri = \"{url}\"");
            _logger?.Trace(message: $"Json ={Environment.NewLine}{Utilities.PreetyPrintJson(json: answer)}");

            VkErrors.IfErrorThrowException(json: answer);

            return(answer);
        }
Exemple #8
0
 private void LogBuffer(byte[] bytes, int start, int size)
 {
     if (_logger != null && _logger.IsTraceEnabled())
     {
         _logger?.Trace("S: {0}", bytes.ToHexString(start, size));
     }
 }
Exemple #9
0
 public void LogDPoSInformation(ulong height)
 {
     _logger?.Trace("Log dpos information - Start");
     _logger?.Trace(GetDPoSInfoToStringOfLatestRounds(GlobalConfig.AElfDPoSLogRoundCount) +
                    $". Current height: {height}");
     _logger?.Trace("Log dpos information - End");
 }
Exemple #10
0
        public async Task <List <Transaction> > RollbackTransactions(Hash chainId, ulong currentHeight, ulong specificHeight)
        {
            var txs = new List <Transaction>();

            for (var i = currentHeight - 1; i >= specificHeight; i--)
            {
                var rollBackBlockHash =
                    await _dataStore.GetAsync <Hash>(
                        DataPath.CalculatePointerForGettingBlockHashByHeight(chainId, i));

                var header = await _dataStore.GetAsync <BlockHeader>(rollBackBlockHash);

                var body = await _dataStore.GetAsync <BlockBody>(
                    Hash.Xor(
                        header.GetHash(), header.MerkleTreeRootOfTransactions));

                foreach (var txId in body.Transactions)
                {
                    var tx = await _dataStore.GetAsync <Transaction>(txId);

                    if (tx == null)
                    {
                        _logger?.Trace($"tx {txId} is null.");
                    }
                    txs.Add(tx);
                    await _dataStore.RemoveAsync <Transaction>(txId);
                }

                _logger?.Trace($"Rollback block hash: {rollBackBlockHash.Value.ToByteArray().ToHex()}");
            }

            return(txs);
        }
        public async Task OnNewBlockHeader(BlockHeader header)
        {
            var height = header.Index;

            if (_blocks.Count == 0)
            {
                // If empty, just add
                AddToBlocks(height, header.GetHash());
            }
            else if (_blocks.TryGetValue(height - 1, out var prevHash) && prevHash == header.PreviousBlockHash)
            {
                // Current fork
                AddToBlocks(height, header.GetHash());
                if (height > GlobalConfig.ReferenceBlockValidPeriod)
                {
                    var toRemove = height - GlobalConfig.ReferenceBlockValidPeriod - 1;
                    if (_blocks.TryRemove(toRemove, out _))
                    {
                        _logger?.Trace($"Removing Canonical Hash of height {toRemove}.");
                    }
                }
            }
            else
            {
                // Switch fork
                //_blocks.Clear();
                AddToBlocks(height, header.GetHash());
            }

            _currentHeight = height;
            await MaybeFillBlocks();
        }
Exemple #12
0
        private NtStatus Trace(string method, string fileName, DokanFileInfo info, NtStatus result, params string[] parameters)
        {
            var extraParameters = parameters != null && parameters.Length > 0 ? ", " + string.Join(", ", parameters) : string.Empty;

            logger?.Trace($"{System.Threading.Thread.CurrentThread.ManagedThreadId:D2} / {drive.DisplayRoot} {method}({fileName}, {info.ToTrace()}{extraParameters}) -> {result}".ToString(CultureInfo.CurrentCulture));

            return(result);
        }
Exemple #13
0
        /// <summary>
        /// The main loop that sends queud up messages from the message queue.
        /// </summary>
        internal void DequeueOutgoingLoop()
        {
            while (!IsDisposed && _outboundMessages != null)
            {
                WriteJob job;

                try
                {
                    job = _outboundMessages.Take();
                }
                catch (Exception)
                {
                    Dispose(); // if already disposed will do nothing
                    break;
                }

                var p = job.Message;

                if (p == null)
                {
                    _logger?.Warn("Cannot write a null message.");
                    continue;
                }

                try
                {
                    if (p.Payload.Length > MaxOutboundPacketSize)
                    {
                        var partials = PayloadToPartials(p.Type, p.Payload, MaxOutboundPacketSize);

                        _logger?.Trace($"Message split into {partials.Count} packets.");

                        foreach (var msg in partials)
                        {
                            SendPartialPacket(msg);
                        }
                    }
                    else
                    {
                        // Send without splitting
                        SendPacketFromMessage(p);
                    }

                    job.SuccessCallback?.Invoke(p);
                }
                catch (Exception e) when(e is IOException || e is ObjectDisposedException)
                {
                    _logger?.Trace("Exception with the underlying socket or stream closed.");
                    Dispose();
                }
                catch (Exception e)
                {
                    _logger?.Trace(e, "Exception while dequeing message.");
                }
            }

            _logger?.Trace("Finished writting messages.");
        }
Exemple #14
0
        public async Task <BlockExecutionResultCC> ExecuteBlock(IBlock block)
        {
            if (!await Prepare(block))
            {
                return(BlockExecutionResultCC.Failed);
            }

            _logger?.Trace($"Executing block {block.GetHash()}");

            var uncompressedPrivateKey = block.Header.P.ToByteArray();
            var recipientKeyPair       = ECKeyPair.FromPublicKey(uncompressedPrivateKey);
            var blockProducerAddress   = recipientKeyPair.GetAddress();

            _stateDictator.ChainId     = block.Header.ChainId;
            _stateDictator.BlockHeight = block.Header.Index - 1;
            _stateDictator.BlockProducerAccountAddress = blockProducerAddress;

            var txs = new List <Transaction>();

            try
            {
                txs = block.Body.TransactionList.ToList();
                var txResults = await ExecuteTransactions(txs, Hash.LoadHex(NodeConfig.Instance.ChainId));
                await InsertTxs(txs, txResults, block);

                var res = await UpdateState(block);

                var blockchain = _chainService.GetBlockChain(block.Header.ChainId);

                if (!res)
                {
                    var txToRevert = await blockchain.RollbackOneBlock();

                    await _txPoolService.Revert(txToRevert);

                    return(BlockExecutionResultCC.Failed);
                }

                await blockchain.AddBlocksAsync(new List <IBlock> {
                    block
                });

                await _binaryMerkleTreeManager.AddTransactionsMerkleTreeAsync(block.Body.BinaryMerkleTree,
                                                                              block.Header.ChainId,
                                                                              block.Header.Index);

                await _binaryMerkleTreeManager.AddSideChainTransactionRootsMerkleTreeAsync(
                    block.Body.BinaryMerkleTreeForSideChainTransactionRoots, block.Header.ChainId, block.Header.Index);
            }
            catch (Exception e)
            {
                await Interrupt($"ExecuteBlock - Execution failed with exception {e}", txs, e);

                return(BlockExecutionResultCC.Failed);
            }

            return(BlockExecutionResultCC.Success);
        }
Exemple #15
0
        private static async Task RunImport(Db db, ConfigurationContainer config, int clientId, int accountId, ILogger logger)
        {
            var watch = new Stopwatch();

            watch.Start();

            var importLog = new ImportLog
            {
                Date         = DateTime.UtcNow,
                AccountId    = accountId,
                Type         = ImportLogType.AutomaticOnServer,
                Milliseconds = (int)watch.ElapsedMilliseconds
            };

            try
            {
                var tempFilePath = RunRecipe(config, logger);

                if (File.Exists(tempFilePath) && new FileInfo(tempFilePath).Length > 0)
                {
                    var importResult = await ParseAndSaveToDatabase(db, clientId, accountId, tempFilePath, config, logger);

                    importLog.LinesDuplicatesCount = importResult.DuplicateLinesCount;
                    importLog.LinesFoundCount      = importResult.NewLinesCount + importResult.DuplicateLinesCount;
                    importLog.LinesImportedCount   = importResult.NewLinesCount;
                }
                else
                {
                    throw new Exception("File (to import from) does not exist or is empty.");
                }

                if (File.Exists(tempFilePath))
                {
                    logger?.Trace("Deleting temporary file {0} ...", tempFilePath);
                    File.Delete(tempFilePath);
                }
            }
            catch (Exception ex)
            {
                importLog.Log = ex.Message;
                logger?.Error(ex.ToString());
                LogException(ex);
            }
            finally
            {
                logger?.Trace("Saving to import log ...");

                watch.Stop();
                importLog.Milliseconds = (int)watch.ElapsedMilliseconds;

                db.ImportLog.Add(importLog);
                db.SaveChanges();
            }
        }
        public void Send()
        {
            if (_logger != null)
            {
                byte[] buffer = _chunkStream.ToArray();

                _logger?.Trace("C: ", buffer, 0, buffer.Length);
            }

            _chunkStream.Position = 0;
            _chunkStream.CopyTo(_downStream);

            Cleanup();
        }
Exemple #17
0
        private void HandleAnnouncement(Message msg, Peer peer)
        {
            try
            {
                Announce a = Announce.Parser.ParseFrom(msg.Payload);

                byte[] blockHash = a.Id.ToByteArray();

                peer.OnAnnouncementMessage(a); // todo - impr - move completely inside peer class.

                // If we already know about this block don't stash the announcement and return.
                if (_blockSynchronizer.GetBlockByHash(Hash.LoadByteArray(blockHash)) != null)
                {
                    _logger?.Debug($"{peer} announced an already known block : {{ id: {blockHash.ToHex()}, height: {a.Height} }}");
                    return;
                }

                if (CurrentSyncSource != null && CurrentSyncSource.IsSyncingHistory &&
                    a.Height <= CurrentSyncSource.SyncTarget)
                {
                    _logger?.Trace($"{peer} : ignoring announce {a.Height} because history sync will fetch (sync target {CurrentSyncSource.SyncTarget}).");
                    return;
                }

                if (UnlinkableHeaderIndex != 0)
                {
                    _logger?.Trace($"{peer} : ignoring announce {a.Height} because we're syncing unlinkable.");
                    return;
                }

                lock (_syncLock)
                {
                    // stash inside the lock because BlockAccepted will clear
                    // announcements after the chain accepts
                    peer.StashAnnouncement(a);

                    if (CurrentSyncSource == null)
                    {
                        CurrentSyncSource = peer;
                        CurrentSyncSource.SyncNextAnnouncement();

                        FireSyncStateChanged(true);
                    }
                }
            }
            catch (Exception e)
            {
                _logger?.Error(e, "Error while handling annoucement.");
            }
        }
        public async Task <GraphAccount[]> GetAvailableAccountsAsync()
        {
            _logger?.Trace("GetAvailableAccountsAsync called");

            var accounts = await _appClient.GetAccountsAsync().ConfigureAwait(false);

            if (accounts is null)
            {
                return(Array.Empty <GraphAccount>());
            }
            else
            {
                return(accounts.Select(a => a.ToGraphAccount()).ToArray());
            }
        }
Exemple #19
0
 public async Task <Miners> GetMiners()
 {
     try
     {
         var miners =
             Miners.Parser.ParseFrom(
                 await GetBytes <Miners>(Hash.FromString(GlobalConfig.AElfDPoSBlockProducerString)));
         return(miners);
     }
     catch (Exception ex)
     {
         _logger?.Trace(ex, "Failed to get miners list.");
         return(new Miners());
     }
 }
Exemple #20
0
        public int ReadNextMessages(Stream messageStream)
        {
            var messages = 0;

            var previousPosition = messageStream.Position;

            try
            {
                messageStream.Position = messageStream.Length;

                while (true)
                {
                    // Read next available bytes from the down stream and process it.
                    var read = _downStream.Read(_buffer, _lastWritePosition, _buffer.Length - _lastWritePosition);
                    if (read <= 0)
                    {
                        throw new IOException($"Unexpected end of stream, read returned {read}");
                    }

                    _logger?.Trace("S: ", _buffer, _lastWritePosition, read);

                    _lastWritePosition += read;

                    // Can we read a whole message from what we have?
                    if (TryReadOneCompleteMessageFromBuffer(messageStream))
                    {
                        messages += 1;

                        break;
                    }
                }

                // Try to consume more messages from the left-over data in the buffer
                var readFromBuffer = TryReadOneCompleteMessageFromBuffer(messageStream);
                while (readFromBuffer)
                {
                    messages += 1;

                    readFromBuffer = TryReadOneCompleteMessageFromBuffer(messageStream);
                }
            }
            finally
            {
                messageStream.Position = previousPosition;
            }

            return(messages);
        }
Exemple #21
0
        // ---

        private bool setMutex(string mutex, string locker, TimeSpan lockTimeout)
        {
            if (string.IsNullOrWhiteSpace(mutex))
            {
                throw new ArgumentException("Mutex is empty");
            }
            if (string.IsNullOrWhiteSpace(locker))
            {
                throw new ArgumentException("Locker is empty");
            }
            if (lockTimeout == null || lockTimeout.TotalSeconds < 1)
            {
                throw new ArgumentException("Timeout is invalid");
            }

            if (_dict.TryAdd(mutex, locker))
            {
                return(true);
            }
            else
            {
                _logger?.Trace("Failed to acquire mutex `{0}` for locker `{1}`", mutex, locker);
                return(false);
            }
        }
Exemple #22
0
        private void LogConnection(TcpClient client)
        {
            IPEndPoint remoteIpEndPoint = client?.Client?.RemoteEndPoint as IPEndPoint;
            IPEndPoint localIpEndPoint  = client?.Client?.LocalEndPoint as IPEndPoint;

            _logger?.Trace($"[{localIpEndPoint?.Address}:{localIpEndPoint?.Port}] Accepted a connection from {remoteIpEndPoint?.Address}:{remoteIpEndPoint?.Port}.");
        }
Exemple #23
0
 public void LogTrace(string messageToLog)
 {
     if (messageToLog != null)
     {
         _logger?.Trace(messageToLog);
     }
 }
        private static IEnumerable<IConceptInfo> InitializeNonparsablePropertiesRecursive(IAlternativeInitializationConcept alternativeInitializationConcept, HashSet<string> alreadyCreated, int depth, ILogger traceLogger)
        {
            if (depth > 10)
                throw new DslSyntaxException(alternativeInitializationConcept, "Macro concept references cannot be resolved.");

            List<IConceptInfo> result = new List<IConceptInfo>();

            IEnumerable<IConceptInfo> createdConcepts;
            alternativeInitializationConcept.InitializeNonparsableProperties(out createdConcepts);
            CsUtility.Materialize(ref createdConcepts);

            if (createdConcepts != null && createdConcepts.Count() > 0)
            {
                traceLogger.Trace(() => alternativeInitializationConcept.GetShortDescription() + " generated on alternative initialization: "
                    + string.Join(", ", createdConcepts.Select(c => c.GetShortDescription())) + ".");

                result.AddRange(createdConcepts);
                foreach (var concept in createdConcepts.OfType<IAlternativeInitializationConcept>())
                    if (!alreadyCreated.Contains(concept.GetFullDescription()))
                    {
                        alreadyCreated.Add(concept.GetFullDescription());
                        result.AddRange(InitializeNonparsablePropertiesRecursive(concept, alreadyCreated, depth + 1, traceLogger));
                    }
            }

            return result;
        }
Exemple #25
0
        public async Task <BlockValidationResult> ValidateBlockAsync(IBlock block, IChainContext context)
        {
            if (_doingRollback)
            {
                _logger?.Trace("Could not validate block during rollbacking!");
                return(BlockValidationResult.DoingRollback);
            }

            MessageHub.Instance.Publish(new ValidationStateChanged(block.BlockHashToHex, block.Index, true,
                                                                   BlockValidationResult.Success));

            var resultCollection = new List <BlockValidationResult>();

            foreach (var filter in _filters)
            {
                var result = await filter.ValidateBlockAsync(block, context);

                if (result != BlockValidationResult.Success)
                {
                    _logger?.Warn($"Result of {filter.GetType().Name}: {result} - {block.BlockHashToHex}");
                }
                resultCollection.Add(result);
            }

            var finalResult = resultCollection.Max();

            MessageHub.Instance.Publish(new ValidationStateChanged(block.BlockHashToHex, block.Index, false,
                                                                   finalResult));

            return(finalResult);
        }
Exemple #26
0
 public void Trace(string message, params object[] args)
 {
     if (IsTraceEnabled())
     {
         _delegate?.Trace(Reformat(message), args);
     }
 }
Exemple #27
0
        /// <summary>
        /// Returns a default revision format based on the available data.
        /// </summary>
        /// <param name="logger">A logger.</param>
        /// <returns>The default revision format.</returns>
        public string GetDefaultRevisionFormat(ILogger logger)
        {
            if (!string.IsNullOrEmpty(CommitHash) && !Regex.IsMatch(CommitHash, "^0+$"))
            {
                logger?.Trace("No format available, using default format for commit hash.");
                return("{semvertag+chash}");
            }
            if (RevisionNumber > 0)
            {
                logger?.Trace("No format available, using default format for revision number.");
                return("0.0.{revnum}");
            }

            logger?.Trace("No format available, using empty format.");
            return("0.0.1");
        }
 public void LogBuffer(ILogger logger)
 {
     if (logger != null && logger.IsTraceEnabled())
     {
         logger?.Trace("S: {0}", Buffer.ToHexString(0, Size));
     }
 }
Exemple #29
0
        /// <summary>
        /// This method sends authentification information to the distant peer and
        /// start the authentification timer.
        /// </summary>
        /// <returns></returns>
        private void StartAuthentification()
        {
            var nodeInfo = new NodeData {
                Port = _port
            };

            ECSigner    signer = new ECSigner();
            ECSignature sig    = signer.Sign(_nodeKey, nodeInfo.ToByteArray());

            var nd = new Handshake
            {
                NodeInfo  = nodeInfo,
                PublicKey = ByteString.CopyFrom(_nodeKey.GetEncodedPublicKey()),
                Height    = CurrentHeight,
                Version   = GlobalConfig.ProtocolVersion,
                R         = ByteString.CopyFrom(sig.R),
                S         = ByteString.CopyFrom(sig.S)
            };

            byte[] packet = nd.ToByteArray();

            _logger?.Trace($"Sending authentification : {{ port: {nd.NodeInfo.Port}, addr: {nd.PublicKey.ToByteArray().ToHex()} }}");

            _messageWriter.EnqueueMessage(new Message {
                Type = (int)MessageType.Auth, HasId = false, Length = packet.Length, Payload = packet
            });

            StartAuthTimer();
        }
        public override IResourceDataResult Filter(IResourceDataRequest request, ISynchronousFilterChain chain, ILogger logger)
        {
            bool cacheEnabled = this.cacheResolver.IsSynchronousSupported;
            if (!cacheEnabled)
            {
                return chain.Filter(request, logger);
            }

            bool isDelete = request.Action == ResourceAction.Delete;
            bool isCustomDataPropertyRequest = request.Uri.ResourcePath.ToString().Contains("/customData/");

            if (isCustomDataPropertyRequest && isDelete)
            {
                logger.Trace($"Request {request.Action} {request.Uri} is a custom data property delete, deleting cached property name if exists", "WriteCacheFilter.FilterAsync");
                this.UncacheCustomDataProperty(request.Uri.ResourcePath, logger);
            }
            else if (isDelete)
            {
                logger.Trace($"Request {request.Action} {request.Uri} is a resource deletion, purging from cache if exists", "WriteCacheFilter.Filter");
                var cacheKey = this.GetCacheKey(request);
                this.Uncache(request.Type, cacheKey);
            }

            var result = chain.Filter(request, logger);

            bool isEmailVerificationResponse = result.Type == typeof(IEmailVerificationToken);
            if (isEmailVerificationResponse)
            {
                logger.Trace($"Request {request.Action} {request.Uri} is an email verification request, purging account from cache if exists", "WriteCacheFilter.Filter");
                this.UncacheAccountOnEmailVerification(result);
            }

            bool possibleCustomDataUpdate = (request.Action == ResourceAction.Create || request.Action == ResourceAction.Update) &&
                AbstractExtendableInstanceResource.IsExtendable(request.Type);
            if (possibleCustomDataUpdate)
            {
                this.CacheNestedCustomDataUpdates(request, result, logger);
            }

            if (IsCacheable(request, result))
            {
                logger.Trace($"Caching request {request.Action} {request.Uri}", "WriteCacheFilter.Filter");
                this.Cache(result.Type, result.Body, logger);
            }

            return result;
        }
 private void LogStream(MemoryStream stream)
 {
     if (_logger != null && _logger.IsTraceEnabled())
     {
         var buffer = stream.ToArray();
         _logger?.Trace("C: {0}", buffer.ToHexString(0, buffer.Length));
     }
 }
Exemple #32
0
 protected void Screenshot(IWebDriver driver, string filename, ILogger logger)
 {
     if (SaveScreenshots)
     {
         logger?.Trace("Saving screenshot as " + filename + ".png ...");
         driver.TakeScreenshot().SaveAsFile(filename + ".png", ImageFormat.Png);
     }
 }
Exemple #33
0
        /// <summary>
        /// The entrance of block syncing.
        /// First step is to validate block header of this block.
        /// </summary>
        /// <param name="block"></param>
        /// <returns></returns>
        public async Task ReceiveBlock(IBlock block)
        {
            if (_terminated)
            {
                return;
            }

            var isReceiveBlock = await IsReceiveBlock(block);

            if (!isReceiveBlock)
            {
                return;
            }

            if (!_blockSet.IsBlockReceived(block.GetHash(), block.Index))
            {
                _blockSet.AddBlock(block);
                // Notify the network layer the block has been accepted.
                MessageHub.Instance.Publish(new BlockAccepted(block));
            }

            if (CurrentState != NodeState.Catching && CurrentState != NodeState.Caught)
            {
                return;
            }

            var blockHeaderValidationResult =
                await _blockHeaderValidator.ValidateBlockHeaderAsync(block.Header);

            _logger?.Trace(
                $"BlockHeader validation result: {blockHeaderValidationResult.ToString()} - {block.BlockHashToHex}. Height: *{block.Index}*");

            if (blockHeaderValidationResult == BlockHeaderValidationResult.Success)
            {
                // Catching -> BlockValidating
                // Caught -> BlockValidating
                MessageHub.Instance.Publish(StateEvent.ValidBlockHeader);
                await HandleBlock(block);
            }

            if (blockHeaderValidationResult == BlockHeaderValidationResult.Unlinkable)
            {
                MessageHub.Instance.Publish(new UnlinkableHeader(block.Header));
            }

            if (blockHeaderValidationResult == BlockHeaderValidationResult.MaybeForked)
            {
                MessageHub.Instance.Publish(StateEvent.LongerChainDetected);
            }

            if (blockHeaderValidationResult == BlockHeaderValidationResult.Branched)
            {
                MessageHub.Instance.Publish(new LockMining(false));
            }

            if (await IsRequestNextBlockAgain(block))
            {
                MessageHub.Instance.Publish(new BlockAccepted(block));
            }
        }
 public ContactGalleryAlbumsHub(IContactRepository contactRepository, IEnumerable<IGalleryProvider> galleryProviders, ILoginProvider sessionProvider, ILoggerFactory loggerFactory)
 {
     Debug.Print("ContactGalleryAlbumsHub.ctor()");
     _contactRepository = contactRepository;
     _galleryProviders = galleryProviders.ToArray();
     _sessionProvider = sessionProvider;
     _logger = loggerFactory.CreateLogger(GetType());
     _logger.Trace("ContactGalleryAlbumsHub.ctor(galleryProviders:{0})", string.Join(",", _galleryProviders.Select(cp => cp.GetType().Name)));
 }
 public ContactCalendarEventsHub(IContactRepository contactRepository, IEnumerable<ICalendarProvider> calendarProviders, ILoginProvider sessionProvider, ILoggerFactory loggerFactory)
 {
     Debug.Print("ContactCalendarEventsHub.ctor()");
     _contactRepository = contactRepository;
     _calendarProviders = calendarProviders.ToArray();
     _sessionProvider = sessionProvider;
     _logger = loggerFactory.CreateLogger(GetType());
     _logger.Trace("ContactCalendarEventsHub.ctor(calendarProviders:{0})", string.Join(",", _calendarProviders.Select(cp=>cp.GetType().Name)));
 }
        public override async Task<IResourceDataResult> FilterAsync(IResourceDataRequest request, IAsynchronousFilterChain chain, ILogger logger, CancellationToken cancellationToken)
        {
            bool cacheEnabled =
                this.cacheResolver.IsAsynchronousSupported
                && this.IsCacheRetrievalEnabled(request)
                && !request.SkipCache;
            if (cacheEnabled)
            {
                logger.Trace($"Checking cache for resource {request.Uri}", "ReadCacheFilter.FilterAsync");
                var result = await this.GetCachedResourceDataAsync(request, logger, cancellationToken).ConfigureAwait(false);

                if (result != null)
                {
                    logger.Trace($"Cache hit for {request.Uri}; returning cached data", "ReadCacheFilter.FilterAsync");
                    return result; // short-circuit the remainder of the filter chain
                }

                logger.Trace($"Cache miss for {request.Uri}", "ReadCacheFilter.FilterAsync");
            }

            return await chain.FilterAsync(request, logger, cancellationToken).ConfigureAwait(false);
        }
        public override IResourceDataResult Filter(IResourceDataRequest request, ISynchronousFilterChain chain, ILogger logger)
        {
            bool cacheEnabled =
                this.cacheResolver.IsAsynchronousSupported
                && this.IsCacheRetrievalEnabled(request)
                && !request.SkipCache;
            if (cacheEnabled)
            {
                logger.Trace($"Checking cache for resource {request.Uri}", "ReadCacheFilter.Filter");
                var result = this.GetCachedResourceData(request, logger);

                if (result != null)
                {
                    logger.Trace($"Cache hit for {request.Uri}; returning cached data", "ReadCacheFilter.Filter");
                    return result; // short-circuit the remainder of the filter chain
                }

                logger.Trace($"Cache miss for {request.Uri}", "ReadCacheFilter.Filter");
            }

            return chain.Filter(request, logger);
        }
        IResourceDataResult ISynchronousFilter.Filter(IResourceDataRequest request, ISynchronousFilterChain chain, ILogger logger)
        {
            var result = chain.Filter(request, logger);

            if (!IsCreateOrUpdate(request))
            {
                return result; // short-circuit
            }

            if (!IsAccountStoreMapping(result))
            {
                return result; // short-circuit
            }

            var applicationHref = GetContainerHref("application", result);
            if (!string.IsNullOrEmpty(applicationHref))
            {
                var application = chain.DataStore.GetResourceSkipCache<IApplication>(applicationHref);
                var allMappings = application.GetAccountStoreMappings().Synchronously().ToList();

                logger.Trace($"AccountStoreMapping update detected; refreshing all {allMappings.Count} AccountStoreMappings in cache for Application '{applicationHref}'", "AccountStoreMappingCacheInvalidationFilter.Filter");

                return result; // done
            }

            var organizationHref = GetContainerHref("organization", result);
            if (!string.IsNullOrEmpty(organizationHref))
            {
                var organization = chain.DataStore.GetResourceSkipCache<IOrganization>(organizationHref);
                var allMappings = organization.GetAccountStoreMappings().Synchronously().ToList();

                logger.Trace($"AccountStoreMapping update detected; refreshing all {allMappings.Count} AccountStoreMappings in cache for Organization '{organizationHref}'", "AccountStoreMappingCacheInvalidationFilter.Filter");

                return result; // done
            }

            throw new NotSupportedException($"Unsupported AccountStore container type: {request.Type.Name}");
        }
Exemple #39
0
 internal static void LogResult(JobResult result, ILogger logger, string jobName) {
     if (result != null) {
         if (result.IsCancelled)
             logger.Warn(result.Error, "Job run \"{0}\" cancelled: {1}", jobName, result.Message);
         else if (!result.IsSuccess)
             logger.Error(result.Error, "Job run \"{0}\" failed: {1}", jobName, result.Message);
         else if (!String.IsNullOrEmpty(result.Message))
             logger.Info("Job run \"{0}\" succeeded: {1}", jobName, result.Message);
         else
             logger.Trace("Job run \"{0}\" succeeded.", jobName);
     } else {
         logger.Error("Null job run result for \"{0}\".", jobName);
     }
 }
        public RouterManager()
        {
            _logger = LogManager.GetCurrentClassLogger();
            _logger.Trace("Creating RouteManager");

            _config = new Config();

            _homePageUrl = _config.BaseUrl + "/html/home.html";
            _publicRsaKeyUrl = _config.BaseUrl + "/api/webserver/publickey";
            _loginPageUrl = _config.BaseUrl + "/api/user/login";
            _networkChangeUrl = _config.BaseUrl + "/api/net/net-mode";
            _connectionStatusUrl = _config.BaseUrl + "/api/monitoring/status";

            _logger.Trace("Initializing JS engine");
            InitJsEngine();
            _logger.Trace("Initializing JS engine... Done");

            _website = new WebSite();

            _logger.Trace("Creating RouteManager... Done");

            IsInitialized = true;
        }
Exemple #41
0
 private static void LogWithLogger(LogItem logItem, ILogger logger)
 {
     switch (logItem.LogLevel)
     {
         case LogLevel.Trace:
             logger.Trace(() => logItem.Message, logItem.Exception);
             break;
         case LogLevel.Info:
             logger.Info(() => logItem.Message, logItem.Exception);
             break;
         case LogLevel.Warn:
             logger.Warn(() => logItem.Message, logItem.Exception);
             break;
         case LogLevel.Error:
             logger.Error(() => logItem.Message, logItem.Exception);
             break;
         case LogLevel.Fatal:
             logger.Fatal(() => logItem.Message, logItem.Exception);
             break;
     }
 }
Exemple #42
0
        /// <summary>
        ///     Построить модель с помощью EntityTypeConfiguration
        /// </summary>
        /// <param name="modelBuilder">Построитель модели</param>
        /// <param name="logger">Логировщик</param>
        /// <param name="namespaceMap">Пространство имен для мапинга</param>
        private static void ModelCreatingByEntityTypeConfiguration(
            DbModelBuilder modelBuilder,
            ILogger logger,
            params string[] namespaceMap)
        {
            logger.Trace("Начинаем конфигурировать контекст БД");

            var typesToRegister =
                PathExtension.GetAssemblyCurrentDirectory()
                    .AsParallel()
                    .SelectMany(
                        a =>
                            a.GetTypes()
                                .Where(type => CheckNamespace(type.Namespace, namespaceMap))
                                .Where(type => !type.IsAbstract)
                                .Where(IsEntityTypeConfiguration))
                    .ToList();

            logger.Trace("Найдено: {0} мапинг объектов", typesToRegister.Count);

            foreach (var type in typesToRegister)
            {
                try
                {
                    dynamic configurationInstance = Activator.CreateInstance(type);
                    modelBuilder.Configurations.Add(configurationInstance);

                    logger.Trace("Тип {0} зарегистрирован", type.FullName);
                }
                catch (Exception exception)
                {
                    logger.Trace("Тип {0} ошибка", type.FullName);
                    logger.Error(exception);
                }
            }

            logger.Trace("Закончили конфигурировать контекст БД");
        }
Exemple #43
0
        private static void GenerateApplication(ILogger logger, Arguments arguments)
        {
            if (!arguments.DeployDatabaseOnly)
            {
                // The old plugins must be deleted before loading the application generator plugins.
                new FilesUtility(DeploymentUtility.InitializationLogProvider).EmptyDirectory(Paths.GeneratedFolder);
                if (File.Exists(Paths.DomAssemblyFile)) // Generated DomAssemblyFile is not in GeneratedFolder.
                    File.Delete(Paths.DomAssemblyFile);
            }
            else
                logger.Info("Skipped deleting old generated files (DeployDatabaseOnly).");

            logger.Trace("Loading plugins.");
            var stopwatch = Stopwatch.StartNew();

            var builder = new ContainerBuilder();
            builder.RegisterModule(new AutofacModuleConfiguration(
                deploymentTime: true,
                shortTransaction: arguments.ShortTransactions,
                deployDatabaseOnly: arguments.DeployDatabaseOnly));
            using (var container = builder.Build())
            {
                var performanceLogger = container.Resolve<ILogProvider>().GetLogger("Performance");
                performanceLogger.Write(stopwatch, "DeployPackages.Program: Modules and plugins registered.");
                Plugins.LogRegistrationStatistics("Generating application", container);

                if (arguments.Debug)
                    container.Resolve<DomGeneratorOptions>().Debug = true;

                container.Resolve<ApplicationGenerator>().ExecuteGenerators(arguments.DeployDatabaseOnly);
            }
        }
 protected override void Log(ILogger logger, string format, params object[] args)
 {
     logger.Trace(format, args);
 }
 protected override void Log(ILogger logger, Exception exception, string format, params object[] args)
 {
     logger.Trace(exception, format, args);
 }
Exemple #46
0
        protected static void CalculateApplicationsToBeRemovedAndInserted(
            IEnumerable<ConceptApplication> oldApplications, IEnumerable<NewConceptApplication> newApplications,
            out List<ConceptApplication> toBeRemoved, out List<NewConceptApplication> toBeInserted,
            ILogger logger)
        {
            var oldApplicationsByKey = oldApplications.ToDictionary(a => a.GetConceptApplicationKey());
            var newApplicationsByKey = newApplications.ToDictionary(a => a.GetConceptApplicationKey());

            // Find directly inserted and removed concept applications:
            var directlyRemoved = oldApplicationsByKey.Keys.Except(newApplicationsByKey.Keys).ToList();
            var directlyInserted = newApplicationsByKey.Keys.Except(oldApplicationsByKey.Keys).ToList();

            foreach (string ca in directlyRemoved)
                logger.Trace("Directly removed concept application: " + ca);
            foreach (string ca in directlyInserted)
                logger.Trace("Directly inserted concept application: " + ca);

            // Find changed concept applications (different create sql query):
            var existingApplications = oldApplicationsByKey.Keys.Intersect(newApplicationsByKey.Keys).ToList();
            var changedApplications = existingApplications.Where(appKey => !string.Equals(
                oldApplicationsByKey[appKey].CreateQuery,
                newApplicationsByKey[appKey].CreateQuery)).ToList();

            foreach (string ca in changedApplications)
                logger.Trace("Changed concept application: " + ca);

            // Find dependent concepts applications to be regenerated:
            var toBeRemovedKeys = directlyRemoved.Union(changedApplications).ToList();
            var oldDependencies = GetDependencyPairs(oldApplications).Select(dep => Tuple.Create(dep.Item1.GetConceptApplicationKey(), dep.Item2.GetConceptApplicationKey()));
            var dependentRemovedApplications = DirectedGraph.IncludeDependents(toBeRemovedKeys, oldDependencies).Except(toBeRemovedKeys);

            var toBeInsertedKeys = directlyInserted.Union(changedApplications).ToList();
            var newDependencies = GetDependencyPairs(newApplications).Select(dep => Tuple.Create(dep.Item1.GetConceptApplicationKey(), dep.Item2.GetConceptApplicationKey()));
            var dependentInsertedApplications = DirectedGraph.IncludeDependents(toBeInsertedKeys, newDependencies).Except(toBeInsertedKeys);

            var refreshDependents = dependentRemovedApplications.Union(dependentInsertedApplications).ToList();
            toBeRemovedKeys.AddRange(refreshDependents.Intersect(oldApplicationsByKey.Keys));
            toBeInsertedKeys.AddRange(refreshDependents.Intersect(newApplicationsByKey.Keys));

            foreach (string ca in refreshDependents)
                logger.Trace("Dependent on changed concept application: " + ca);

            // Result:
            toBeRemoved = toBeRemovedKeys.Select(key => oldApplicationsByKey[key]).ToList();
            toBeInserted = toBeInsertedKeys.Select(key => newApplicationsByKey[key]).ToList();
        }
        async Task<IResourceDataResult> IAsynchronousFilter.FilterAsync(IResourceDataRequest request, IAsynchronousFilterChain chain, ILogger logger, CancellationToken cancellationToken)
        {
            var result = await chain.FilterAsync(request, logger, cancellationToken).ConfigureAwait(false);

            if (!IsCreateOrUpdate(request))
            {
                return result; // short-circuit
            }

            if (!IsAccountStoreMapping(result))
            {
                return result; // short-circuit
            }

            var applicationHref = GetContainerHref("application", result);
            if (!string.IsNullOrEmpty(applicationHref))
            {
                var application = await chain.DataStore.GetResourceSkipCacheAsync<IApplication>(applicationHref, cancellationToken).ConfigureAwait(false);
                var allMappings = await application.GetAccountStoreMappings().ToListAsync(cancellationToken).ConfigureAwait(false);

                logger.Trace($"AccountStoreMapping update detected; refreshing all {allMappings.Count} AccountStoreMappings in cache for Application '{applicationHref}'", "AccountStoreMappingCacheInvalidationFilter.FilterAsync");

                return result; // done
            }

            var organizationHref = GetContainerHref("organization", result);
            if (!string.IsNullOrEmpty(organizationHref))
            {
                var organization = await chain.DataStore.GetResourceSkipCacheAsync<IOrganization>(organizationHref, cancellationToken).ConfigureAwait(false);
                var allMappings = await organization.GetAccountStoreMappings().ToListAsync(cancellationToken).ConfigureAwait(false);

                logger.Trace($"AccountStoreMapping update detected; refreshing all {allMappings.Count} AccountStoreMappings in cache for Organization '{organizationHref}'", "AccountStoreMappingCacheInvalidationFilter.FilterAsync");

                return result; // done
            }

            throw new NotSupportedException($"Unsupported AccountStore container type: {request.Type.Name}");
        }
Exemple #48
0
        public static IPlugins GetPlugins(ITranslator translator, IAssemblyInfo config, ILogger logger)
        {
            logger.Info("Discovering plugins...");

            if (!Plugins.IsLoaded)
            {
                var resolver = new AssemblyResolver() { Logger = logger };

                AppDomain.CurrentDomain.AssemblyResolve += resolver.CurrentDomain_AssemblyResolve;

                AppDomain.CurrentDomain.AssemblyLoad += resolver.CurrentDomain_AssemblyLoad;

                Plugins.IsLoaded = true;

                logger.Trace("Set assembly Resolve and Load events for domain " + AppDomain.CurrentDomain.FriendlyName);
            }

            logger.Trace("Current domain " + AppDomain.CurrentDomain.FriendlyName);

            logger.Trace("Application base: " + AppDomain.CurrentDomain.SetupInformation.ApplicationBase);

            logger.Trace("Loaded assemblies:");
            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                logger.Trace("\t" + a.FullName);
            }

            var path = GetPluginPath(translator, config);
            logger.Info("Will use the following plugin path \"" + path + "\"");

            var catalogs = new List<ComposablePartCatalog>();

            if (Directory.Exists(path))
            {
                catalogs.Add(new DirectoryCatalog(path, "*.dll"));
                logger.Info("The plugin path exists. Will use it as DirectoryCatalog");
            }
            else
            {
                logger.Info("The plugin path does not exist. Skipping searching test framework plugins in the plugin folder.");
            }

            string[] skipPluginAssemblies = null;
            var translatorInstance = translator as Translator;
            if (translatorInstance != null)
            {
                skipPluginAssemblies = translatorInstance.SkipPluginAssemblies;
            }

            logger.Trace("Will search all translator references to find resource(s) with names starting from \"" + PLUGIN_RESOURCE_NAME_PREFIX + "\" ...");

            foreach (var reference in translator.References)
            {
                logger.Trace("Searching plugins in reference " + reference.FullName + " ...");

                if (skipPluginAssemblies != null && skipPluginAssemblies.FirstOrDefault(x => reference.Name.FullName.Contains(x)) != null)
                {
                    logger.Trace("Skipping the reference " + reference.Name.FullName + " as it is in skipPluginAssemblies");
                    continue;
                }
                else
                {
                    logger.Trace("skipPluginAssemblies is not set");
                }

                var assemblies = reference.MainModule.Resources.Where(res => res.Name.StartsWith(PLUGIN_RESOURCE_NAME_PREFIX));

                logger.Trace("The reference contains " + assemblies.Count() + " resource(s) needed");

                if (assemblies.Any())
                {
                    foreach (var res_assembly in assemblies)
                    {
                        logger.Trace("Searching plugins in resource " + res_assembly.Name + " ...");

                        try
                        {
                            using (var resourcesStream = ((EmbeddedResource)res_assembly).GetResourceStream())
                            {
                                var ba = new byte[(int)resourcesStream.Length];
                                resourcesStream.Read(ba, 0, (int)resourcesStream.Length);

                                logger.Trace("Read the assembly resource stream of " + resourcesStream.Length + " bytes length");

                                var trimmedName = Plugins.TrimResourceAssemblyName(res_assembly, PLUGIN_RESOURCE_NAME_PREFIX);

                                var assembly = CheckIfAssemblyLoaded(logger, ba, null, trimmedName);

                                catalogs.Add(new AssemblyCatalog(assembly));
                                logger.Trace("The assembly " + assembly.FullName + " added to the catalogs");
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error("Exception occurred:");
                            logger.Error(ex.Message);
                        }

                    }
                }
            }

            if (catalogs.Count == 0)
            {
                logger.Info("No AssemblyCatalogs found");
                return new Plugins() { plugins = new IPlugin[0] };
            }

            var catalog = new AggregateCatalog(catalogs);

            CompositionContainer container = new CompositionContainer(catalog);
            var plugins = new Plugins();

            logger.Info("ComposingParts to discover plugins...");

            try
            {
                container.ComposeParts(plugins);
            }
            catch (Exception ex)
            {
                logger.Error("Exception occurred:");
                logger.Error(ex.Message);
            }

            if (plugins.Parts != null)
            {

                foreach (var plugin in plugins.Parts)
                {
                    plugin.Logger = translator.Log;
                }

                logger.Info("Discovered " + plugins.Parts.Count() + " plugin(s)");
            }

            return plugins;
        }
 public ContractorsController(IRepository<Contractor> contractorRepository, ILogger logger)
 {
     _contractorRepository = contractorRepository;
     _logger = logger;
     _logger.Trace("STARTING CONTRACTORS");
 }
        private void Cache(Type resourceType, Map data, ILogger logger)
        {
            string href = data[AbstractResource.HrefPropertyName].ToString();

            var cacheData = new Dictionary<string, object>();

            bool isCustomData = resourceType == typeof(ICustomData);
            if (isCustomData)
            {
                logger.Trace($"Response {href} is a custom data resource, caching directly", "WriteCacheFilter.Cache");

                var cache = this.GetSyncCache(resourceType);
                cache.Put(this.GetCacheKey(href), data);
                return; // simple! return early
            }

            bool isCacheable = true;

            foreach (var item in data)
            {
                string key = item.Key;
                object value = item.Value;

                // TODO DefaultModelMap edge case
                // TODO ApiEncryptionMetadata edge case
                var asNestedResource = value as ExpandedProperty;
                var asNestedMapArray = value as IEnumerable<Map>;
                var asNestedScalarArray = value as IEnumerable<object>;

                if (asNestedResource != null && IsResource(asNestedResource.Data))
                {
                    logger.Trace($"Attribute {key} on response {href} is an expanded resource, caching recursively", "WriteCacheFilter.Cache");

                    var nestedType = this.typeLookup.GetInterfaceByPropertyName(item.Key);
                    if (nestedType == null)
                    {
                        logger.Warn($"Cannot cache nested item. Item type for '{key}' unknown. '{href}' will not be cached.");
                        isCacheable = false; // gracefully disable caching
                        break;
                    }

                    this.Cache(nestedType, asNestedResource.Data, logger);
                    value = ToCanonicalReference(key, asNestedResource.Data);
                }
                else if (asNestedMapArray != null)
                {
                    logger.Trace($"Attribute {key} on response {href} is an array, caching items recursively", "WriteCacheFilter.CacheAsync");

                    var nestedType = this.typeLookup.GetInnerCollectionInterface(resourceType);
                    if (nestedType == null)
                    {
                        logger.Warn($"Can not cache array '{key}'. Item type for '{resourceType.Name}' unknown. '{href}' will not be cached.");
                        isCacheable = false; // gracefully disable caching
                        break;
                    }

                    // This is a CollectionResponsePage<T>.Items property.
                    // Recursively cache nested resources and create a new collection that only has references
                    var canonicalList = new List<object>();
                    foreach (var element in asNestedMapArray)
                    {
                        object canonicalElement = element;
                        var resourceElement = canonicalElement as Map;
                        if (resourceElement != null)
                        {
                            if (IsResource(resourceElement))
                            {
                                this.Cache(nestedType, resourceElement, logger);
                                canonicalElement = ToCanonicalReference(key, resourceElement);
                            }
                        }

                        canonicalList.Add(canonicalElement);
                    }

                    value = canonicalList;
                }
                else if (asNestedScalarArray != null)
                {
                    var nestedType = this.typeLookup.GetInterfaceByPropertyName(key);
                    if (nestedType == null)
                    {
                        logger.Warn($"Can not cache array '{key}'. Item type for '{resourceType.Name}' unknown. '{href}' will not be cached.");
                        isCacheable = false; // gracefully disable caching
                        break;
                    }

                    // This is an array of primitives, such as string[].
                    // (Not currently supported. Will be supported in the future.)
                    logger.Warn($"Can not cache array '{key}'. Array caching is currently unsupported. '{href}' will not be cached.");
                    isCacheable = false; // gracefully disable caching
                    break;
                }

                if (!IsSensitive(key))
                {
                    cacheData.Add(key, value);
                }
            }

            if (isCacheable && !this.typeLookup.IsCollectionResponse(resourceType))
            {
                logger.Trace($"Caching {href} as type {resourceType.Name}", "WriteCacheFilter.Cache");

                var cache = this.GetSyncCache(resourceType);
                if (cache == null)
                {
                    return;
                }

                var cacheKey = this.GetCacheKey(href);
                cache.Put(cacheKey, cacheData);
            }
        }
Exemple #51
0
        private static void DownloadPackages(ILogger logger, Arguments arguments)
        {
            var obsoleteFolders = new string[] { Path.Combine(Paths.RhetosServerRootPath, "DslScripts"), Path.Combine(Paths.RhetosServerRootPath, "DataMigration") };
            var obsoleteFolder = obsoleteFolders.FirstOrDefault(folder => Directory.Exists(folder));
            if (obsoleteFolder != null)
                throw new UserException("Backup all Rhetos server folders and delete obsolete folder '" + obsoleteFolder + "'. It is no longer used.");

            if (!arguments.DeployDatabaseOnly)
            {
                logger.Trace("Getting packages.");
                var config = new DeploymentConfiguration(DeploymentUtility.InitializationLogProvider);
                var packageDownloaderOptions = new PackageDownloaderOptions { IgnorePackageDependencies = arguments.IgnorePackageDependencies };
                var packageDownloader = new PackageDownloader(config, DeploymentUtility.InitializationLogProvider, packageDownloaderOptions);
                var packages = packageDownloader.GetPackages();

                InstalledPackages.Save(packages);
            }
            else
                logger.Info("Skipped download packages (DeployDatabaseOnly).");
        }
        private void CacheNestedCustomDataUpdates(IResourceDataRequest request, IResourceDataResult result, ILogger logger)
        {
            object customDataObj = null;
            Map customData = null;

            if (!request.Properties.TryGetValue(AbstractExtendableInstanceResource.CustomDataPropertyName, out customDataObj))
            {
                return;
            }

            customData = customDataObj as Map;
            if (customData.IsNullOrEmpty())
            {
                return;
            }

            bool creating = request.Action == ResourceAction.Create;

            var parentHref = request.Uri.ResourcePath.ToString();
            if (creating && !result.Body.TryGetValueAsString(AbstractResource.HrefPropertyName, out parentHref))
            {
                return;
            }

            var customDataHref = parentHref + "/customData";

            var dataToCache = this.GetCachedValue(typeof(ICustomData), customDataHref);
            if (!creating && dataToCache == null)
            {
                logger.Trace($"Request {request.Uri} has nested custom data updates, but no authoritative cached custom data exists; aborting", "WriteCacheFilter.CacheNestedCustomDataUpdates");
                return;
            }

            logger.Trace($"Request {request.Uri} has nested custom data updates, updating cached custom data", "WriteCacheFilter.CacheNestedCustomDataUpdates");

            if (dataToCache.IsNullOrEmpty())
            {
                dataToCache = new Dictionary<string, object>();
            }

            foreach (var updatedItem in customData)
            {
                dataToCache[updatedItem.Key] = updatedItem.Value;
            }

            // Ensure the href property exists
            dataToCache[AbstractResource.HrefPropertyName] = customDataHref;

            this.Cache(typeof(ICustomData), dataToCache, logger);
        }
        /// <summary>
        /// Initializes a new <see cref="SimpleRepositoryInfo"/> based on a (more complex) <see cref="Info"/>.
        /// </summary>
        /// <param name="logger">Logger that will be used.</param>
        /// <param name="info">The simplified repository information.</param>
        public SimpleRepositoryInfo( ILogger logger, RepositoryInfo info )
        {
            if( logger == null ) throw new ArgumentNullException( nameof(logger) );
            if( info == null ) throw new ArgumentNullException( nameof( info ) );

            Info = info;
            if( !HandleRepositoryInfoError( logger, info ) )
            {
                CommitSha = info.CommitSha;
                CommitDateUtc = info.CommitDateUtc;
                var t = info.ValidReleaseTag;
                // Always warn on non standard pre release name.
                if( t != null && t.IsPreRelease && !t.IsPreReleaseNameStandard )
                {
                    logger.Warn( "Non standard pre release name '{0}' is mapped to '{1}'.", t.PreReleaseNameFromTag, t.PreReleaseName );
                }
                if( info.IsDirty && !info.Options.IgnoreDirtyWorkingFolder )
                {
                    SetInvalidValuesAndLog( logger, "Working folder has non committed changes.", false );
                    logger.Info( info.IsDirtyExplanations );
                }
                else
                {
                    Debug.Assert( info.PossibleVersions != null );
                    if( info.IsDirty )
                    {
                        logger.Warn( "Working folder is Dirty! Checking this has been disabled since RepositoryInfoOptions.IgnoreDirtyWorkingFolder is true." );
                        logger.Warn( info.IsDirtyExplanations );
                    }
                    if( info.PreviousRelease != null )
                    {
                        logger.Trace( "Previous release found '{0}' on commit '{1}'.", info.PreviousRelease.ThisTag, info.PreviousRelease.CommitSha );
                    }
                    if( info.PreviousMaxRelease != null && info.PreviousMaxRelease != info.PreviousRelease )
                    {
                        logger.Trace( "Previous max release found '{0}' on commit '{1}'.", info.PreviousMaxRelease.ThisTag, info.PreviousMaxRelease.CommitSha );
                    }
                    if( info.PreviousRelease == null && info.PreviousMaxRelease == null )
                    {
                        logger.Trace( "No previous release found'." );
                    }

                    if( info.CIRelease != null )
                    {
                        IsValidCIBuild = true;
                        SetNumericalVersionValues( info.CIRelease.BaseTag, true );
                        NuGetVersion = info.CIRelease.BuildVersionNuGet;
                        SemVer = info.CIRelease.BuildVersion;
                        logger.Info( "CI release: '{0}'.", SemVer );
                        LogValidVersions( logger, info );
                    }
                    else
                    {
                        if( t == null )
                        {
                            SetInvalidValuesAndLog( logger, "No valid release tag.", false );
                            LogValidVersions( logger, info );
                        }
                        else
                        {
                            IsValidRelease = true;
                            OriginalTagText = t.OriginalTagText;
                            SetNumericalVersionValues( t, false );
                            NuGetVersion = t.ToString( ReleaseTagFormat.NuGetPackage );
                            SemVer = t.ToString( ReleaseTagFormat.SemVerWithMarker );
                            logger.Info( "Release: '{0}'.", SemVer );
                        }
                    }
                }
            }
            MajorMinor = string.Format( "{0}.{1}", Major, Minor );
            MajorMinorPatch = string.Format( "{0}.{1}", MajorMinor, Patch );
        }
Exemple #54
0
        private static void InitializeGeneratedApplication(ILogger logger, Arguments arguments)
        {
            // Creating a new container builder instead of using builder.Update, because of severe performance issues with the Update method.
            Plugins.ClearCache();

            logger.Trace("Loading generated plugins.");
            var stopwatch = Stopwatch.StartNew();

            var builder = new ContainerBuilder();
            builder.RegisterModule(new AutofacModuleConfiguration(
                deploymentTime: false,
                shortTransaction: arguments.ShortTransactions,
                deployDatabaseOnly: arguments.DeployDatabaseOnly));
            using (var container = builder.Build())
            {
                var performanceLogger = container.Resolve<ILogProvider>().GetLogger("Performance");
                performanceLogger.Write(stopwatch, "DeployPackages.Program: New modules and plugins registered.");
                Plugins.LogRegistrationStatistics("Initializing application", container);

                container.Resolve<ApplicationInitialization>().ExecuteInitializers();
            }
        }
Exemple #55
0
        public static Assembly CheckIfAssemblyLoaded(ILogger logger, byte[] ba, AssemblyName assemblyName, string trimmedName)
        {
            logger.Trace("Check if assembly " + trimmedName + " already loaded");
            Assembly assembly = AssemblyResolver.CheckIfAssemblyLoaded(trimmedName, AppDomain.CurrentDomain);
            if (assembly != null)
            {
                logger.Trace("The assembly " + trimmedName + " is already loaded");
            }
            else
            {
                logger.Trace("Loading the assembly into domain " + AppDomain.CurrentDomain.FriendlyName + " ...");

                if (ba != null)
                {
                    assembly = Assembly.Load(ba);
                }
                else
                {
                    assembly = Assembly.Load(assemblyName);
                }

                logger.Trace("Assembly " + assembly.FullName + " is loaded into domain " + AppDomain.CurrentDomain.FriendlyName);
            }
            return assembly;
        }
Exemple #56
0
        public string GenerateSqlDiff(
            IEnumerable<IMap> fromMaps,
            IEnumerable<IMap> toMaps,
            IAnswerProvider answerProvider,
            ILogger logger,
            IEnumerable<string> indexesToIgnore,
            IEnumerable<string> tablesToIgnore,
            out IEnumerable<string> warnings,
            out IEnumerable<string> errors) {
            // fetch data for current database
            IDictionary<string, Statistics> currentData = new Dictionary<string, Statistics>();
            if (fromMaps.Any()) {
                currentData = this.statisticsProvider.GetStatistics(fromMaps);
            }

            var sql = new StringBuilder();
            var warningList = new List<string>();
            var errorList = new List<string>();
            var renamePrimaryKeyModifications = new Dictionary<Tuple<string, string>, bool>();
            var from = fromMaps.ToArray();
            var to = toMaps.ToArray();

            // get additions and removals
            var mapComparer = new TableNameEqualityComparer();
            var additions = to.Except(from, mapComparer).Where(m => !tablesToIgnore.Contains(m.Table)).ToList();
            var removals = from.Except(to, mapComparer).ToList();
            var matches = from.Join(to, f => f.Table.ToLowerInvariant(), t => t.Table.ToLowerInvariant(), MigrationPair.Of).ToList();

            // trace output
            logger.Trace("Additions:");
            logger.Trace("");
            logger.Trace(additions.Select(a => new { a.Table, a.Type.Name }), new[] { "Table", "Map Name" });
            logger.Trace("Removals:");
            logger.Trace("");
            logger.Trace(removals.Select(r => new { r.Table, r.Type.Name }), new[] { "Table", "Map Name" });
            logger.Trace("Matches:");
            logger.Trace("");
            logger.Trace(
                matches.Select(m => new { FromTable = m.From.Table, FromMap = m.From.Type.Name, ToTable = m.To.Table, ToMap = m.To.Type.Name }),
                new[] { "From Table", "From Map", "To Table", "To Map" });

            // look for possible entity name changes
            if (additions.Any() && removals.Any()) {
                // TODO do something a bit more sensible with regards to likelihood of rename
                foreach (var removed in removals.Select(r => r).ToArray()) {
                    // copy the array as we'll update
                    var answer =
                        answerProvider.GetMultipleChoiceAnswer(
                            string.Format("The entity {0} has been removed. If it has been renamed please specify what to:", removed.Type.Name),
                            new[] { new MultipleChoice<string> { DisplayString = "Not renamed - please delete", Choice = NoRename } }.Union(
                                additions.Select(a => new MultipleChoice<string> { Choice = a.Type.Name, DisplayString = a.Type.Name })));
                    if (answer.Choice != NoRename) {
                        // rename the table
                        var renameFrom = removed;
                        var renameTo = additions.First(a => a.Type.Name == answer.Choice);
                        sql.AppendSql(this.alterTableWriter.RenameTable(renameFrom, renameTo));

                        // add to the matches
                        matches.Add(MigrationPair.Of(renameFrom, renameTo));

                        // modify additions and removals
                        removals.Remove(renameFrom);
                        additions.Remove(renameTo);

                        // sort out the primary key
                        var fromPrimaryKey = renameFrom.PrimaryKey;
                        var toPrimaryKey = renameTo.PrimaryKey;
                        if (!AreColumnDefinitionsEqual(fromPrimaryKey, toPrimaryKey)) {
                            if (fromPrimaryKey.DbName != toPrimaryKey.DbName && fromPrimaryKey.DbType == toPrimaryKey.DbType
                                && (!fromPrimaryKey.DbType.TypeTakesLength() || (fromPrimaryKey.MaxLength && toPrimaryKey.MaxLength)
                                    || (fromPrimaryKey.Length == toPrimaryKey.Length))
                                && (!fromPrimaryKey.DbType.TypeTakesPrecisionAndScale()
                                    || (fromPrimaryKey.Precision == toPrimaryKey.Precision && fromPrimaryKey.Scale == toPrimaryKey.Scale))) {
                                // just a change in name
                                sql.AppendSql(this.alterTableWriter.ChangeColumnName(fromPrimaryKey, toPrimaryKey));
                            }
                            else {
                                // ask the question
                                // TODO may things more sensible based on the type
                                var attemptChange =
                                    answerProvider.GetBooleanAnswer(
                                        "The primary key change required for this table rename involves a change "
                                        + (fromPrimaryKey.DbType != toPrimaryKey.DbType ? "of data type" : "of specification")
                                        + ". Would you like to attempt the change? (Selecting No will drop and re-create the column)");
                                if (attemptChange) {
                                    if (fromPrimaryKey.DbName != toPrimaryKey.DbName) {
                                        sql.AppendSql(this.alterTableWriter.ChangeColumnName(fromPrimaryKey, toPrimaryKey));
                                    }

                                    sql.AppendSql(this.alterTableWriter.ModifyColumn(fromPrimaryKey, toPrimaryKey));
                                    renamePrimaryKeyModifications.Add(Tuple.Create(fromPrimaryKey.Map.Type.Name, toPrimaryKey.Map.Type.Name), true);
                                }
                                else {
                                    // drop and re-create
                                    sql.AppendSql(this.alterTableWriter.DropColumn(fromPrimaryKey));
                                    sql.AppendSql(this.alterTableWriter.AddColumn(toPrimaryKey));
                                    renamePrimaryKeyModifications.Add(Tuple.Create(fromPrimaryKey.Map.Type.Name, toPrimaryKey.Map.Type.Name), false);
                                }
                            }
                        }
                    }
                }
            }

            // do removal of foreign keys and indexes that we don't need
            // only do this on remaining tables as drops should be deleted automatically
            foreach (var matchPair in matches) {
                var fkRemovals = matchPair.From.ForeignKeys.Except(matchPair.To.ForeignKeys);
                foreach (var foreignKey in fkRemovals) {
                    sql.AppendSql(this.alterTableWriter.DropForeignKey(foreignKey));
                }

                var indexRemovals = matchPair.From.Indexes.Except(matchPair.To.Indexes);
                foreach (var index in indexRemovals.Where(i => !indexesToIgnore.Contains(i.Name))) {
                    sql.AppendSql(this.alterTableWriter.DropIndex(index));
                }
            }

            // do renames of columns
            var columnKeyValuePairEqualityComparer = new ColumnKeyValuePairEqualityComparer();
            var addedProperties = new List<IColumn>();
            foreach (var pair in matches) {
                var fromCols = pair.From.OwnedColumns(true).ToDictionary(c => c.DbName, c => c);
                var toCols = pair.To.OwnedColumns(true).ToDictionary(c => c.DbName, c => c);

                var removedColumns = fromCols.Except(toCols, columnKeyValuePairEqualityComparer);
                var addedColumns = toCols.Except(fromCols, columnKeyValuePairEqualityComparer).ToList();
                if (removedColumns.Any()) {
                    // handle drops and renames
                    foreach (var removal in removedColumns) {
                        if (pair.From.Type.Name != pair.To.Type.Name && removal.Value.IsPrimaryKey) {
                            // ignore this one and get the new pk and remove it as handled above
                            var newPrimaryKey = toCols.Single(c => c.Value.IsPrimaryKey);
                            addedColumns.Remove(newPrimaryKey);
                            continue;
                        }

                        if (addedColumns.Any()) {
                            var answer =
                                answerProvider.GetMultipleChoiceAnswer(
                                    string.Format(
                                        "The property {0} has been removed. If it has been renamed please specify what to:",
                                        removal.Value.Name),
                                    new[] { new MultipleChoice<string> { DisplayString = "Not renamed - please delete", Choice = NoRename } }.Union(
                                        addedColumns.Select(a => new MultipleChoice<string> { Choice = a.Value.Name, DisplayString = a.Value.Name })));
                            if (answer.Choice == NoRename) {
                                // drop the column
                                if (pair.From.Type.Name != pair.To.Type.Name) {
                                    removal.Value.Map = pair.To; // want to delete from the correctly named table in the event of a rename
                                }

                                sql.AppendSql(this.alterTableWriter.DropColumn(removal.Value));
                            }
                            else {
                                // rename the column
                                var toColumn = addedColumns.First(c => c.Value.Name == answer.Choice);
                                sql.AppendSql(this.alterTableWriter.ChangeColumnName(removal.Value, toColumn.Value));

                                // if need be perform a modify statement
                                if (this.RequiresColumnSpecificationChange(removal.Value, toColumn.Value)) {
                                    sql.AppendSql(this.alterTableWriter.ModifyColumn(removal.Value, toColumn.Value));
                                }

                                // remove the column from the additions
                                addedColumns.Remove(toColumn);
                            }
                        }
                        else {
                            // drop the column
                            if (pair.From.Type.Name != pair.To.Type.Name) {
                                removal.Value.Map = pair.To; // want to delete from the correctly named table in the event of a rename
                            }

                            sql.AppendSql(this.alterTableWriter.DropColumn(removal.Value));
                        }
                    }
                }

                // go through existing columns and handle modifications
                foreach (var fromProp in pair.From.Columns) {
                    logger.Trace("Examining {1}.{0}", fromProp.Value.Name, pair.From.Table);
                    var matchingToProp = pair.To.Columns.Select(p => p.Value).FirstOrDefault(p => p.Name == fromProp.Key);
                    if (matchingToProp != null) {
                        if (this.RequiresColumnSpecificationChange(fromProp.Value, matchingToProp)) {
                            // check for potential errors
                            if (fromProp.Value.DbType != matchingToProp.DbType) {
                                bool skipQuestion = false;
                                bool wasPrimaryKeyDroppedAndRecreated = false;
                                var renamePrimaryKeyModificationsKey =
                                    Tuple.Create(
                                        fromProp.Value.Relationship == RelationshipType.ManyToOne
                                            ? fromProp.Value.ParentMap.Type.Name
                                            : fromProp.Value.OppositeColumn.ParentMap.Type.Name,
                                        matchingToProp.Relationship == RelationshipType.ManyToOne
                                            ? matchingToProp.ParentMap.Type.Name
                                            : matchingToProp.OppositeColumn.Map.Type.Name);
                                if ((fromProp.Value.Relationship == RelationshipType.OneToOne
                                     || fromProp.Value.Relationship == RelationshipType.ManyToOne)
                                    && (matchingToProp.Relationship == RelationshipType.ManyToOne
                                        || matchingToProp.Relationship == RelationshipType.OneToOne)
                                    && renamePrimaryKeyModifications.ContainsKey(renamePrimaryKeyModificationsKey)) {
                                    // skip the question as we've already attempted the modify for the pk so may as well here as well!
                                    skipQuestion = true;
                                    wasPrimaryKeyDroppedAndRecreated = !renamePrimaryKeyModifications[renamePrimaryKeyModificationsKey];
                                }

                                bool dropAndRecreate = wasPrimaryKeyDroppedAndRecreated;
                                if (!skipQuestion) {
                                    dropAndRecreate =
                                        answerProvider.GetBooleanAnswer(
                                            string.Format(
                                                "Attempting to change DbType for property {0} on {1} from {2} to {3}. Would you like to attempt the change? (selecting \"No\" will drop and re-create the column)",
                                                matchingToProp.Name,
                                                matchingToProp.Map.Type.Name,
                                                fromProp.Value.DbType,
                                                matchingToProp.DbType));
                                }

                                if (dropAndRecreate) {
                                    sql.AppendSql(this.alterTableWriter.DropColumn(matchingToProp));
                                    sql.AppendSql(this.alterTableWriter.AddColumn(matchingToProp));
                                    continue;
                                }

                                warningList.Add(
                                    string.Format(
                                        "Changing DB Type is not guaranteed to work: {0} on {1}",
                                        fromProp.Value.Name,
                                        fromProp.Value.Map.Type.Name));
                            }

                            if ((this.RequiresLengthChange(fromProp.Value, matchingToProp)
                                 && (fromProp.Value.MaxLength || fromProp.Value.Length < matchingToProp.Length))
                                || (this.RequiresPrecisionOrScaleChange(fromProp.Value, matchingToProp)
                                    && (fromProp.Value.Precision > matchingToProp.Precision || fromProp.Value.Scale > matchingToProp.Scale))) {
                                warningList.Add(
                                    string.Format(
                                        "{0} on {1} is having its precision, scale or length reduced. This may result in loss of data",
                                        fromProp.Value.Name,
                                        fromProp.Value.Map.Type.Name));
                            }

                            sql.AppendSql(this.alterTableWriter.ModifyColumn(fromProp.Value, matchingToProp));
                        }
                        else {
                            if ((matchingToProp.Relationship == RelationshipType.ManyToOne || matchingToProp.Relationship == RelationshipType.OneToOne)
                                && (fromProp.Value.Relationship == RelationshipType.ManyToOne
                                    || fromProp.Value.Relationship == RelationshipType.OneToOne)
                                && fromProp.Value.Type.Name != matchingToProp.Type.Name && currentData != null
                                && currentData[matchingToProp.Map.Type.Name].HasRows) {
                                warningList.Add(
                                    string.Format(
                                        "Property {0} on {1} has changed type but the column was not dropped. There is data in that table, please empty that column if necessary",
                                        matchingToProp.Name,
                                        matchingToProp.Map.Type.Name));
                            }
                        }
                    }
                }

                // add the added columns to the addedProperties list
                addedProperties.AddRange(addedColumns.Select(c => c.Value));
            }

            // do deletes of entities
            foreach (var removal in removals) {
                sql.AppendSql(this.dropTableWriter.DropTable(removal));
            }

            // do additions of entities
            foreach (var addition in additions) {
                sql.AppendSql(this.createTableWriter.CreateTable(addition));
            }

            // do additions of properties
            foreach (var newProp in addedProperties) {
                // check for relationships where the related table is not empty and the prop is not null
                if ((newProp.Relationship == RelationshipType.ManyToOne || newProp.Relationship == RelationshipType.OneToOne) && !newProp.IsNullable
                    && string.IsNullOrWhiteSpace(newProp.Default) && currentData.ContainsKey(newProp.Map.Type.Name)
                    && currentData[newProp.Map.Type.Name].HasRows) {
                    var foreignKeyPrimaryKeyType = newProp.Relationship == RelationshipType.ManyToOne
                                                       ? newProp.ParentMap.PrimaryKey.Type
                                                       : newProp.OppositeColumn.Map.PrimaryKey.Type;
                    var answer = answerProvider.GetType()
                                               .GetMethod("GetAnswer")
                                               .MakeGenericMethod(foreignKeyPrimaryKeyType)
                                               .Invoke(
                                                   answerProvider,
                                                   new object[] {
                                                                    string.Format(
                                                                        "You are adding a property {0} on {1} that has a foreign key to {2} (which is non-empty) with primary key type {3}. Please specify a default value for the column:",
                                                                        newProp.Name,
                                                                        newProp.Map.Type.Name,
                                                                        newProp.Type.Name,
                                                                        foreignKeyPrimaryKeyType)
                                                                });
                    newProp.Default = answer.ToString();
                }

                sql.AppendSql(this.alterTableWriter.AddColumn(newProp));
            }

            // add in new foreign keys for additions
            foreach (var map in additions) {
                var statements = this.createTableWriter.CreateForeignKeys(map);
                foreach (var statement in statements) {
                    sql.AppendSql(statement);
                }
            }

            // add in new indexes for additions
            foreach (var map in additions) {
                var statements = this.createTableWriter.CreateIndexes(map);
                foreach (var statement in statements) {
                    sql.AppendSql(statement);
                }
            }

            // add in missing foreign keys and indexes
            foreach (var matchPair in matches) {
                var fkAdditions = matchPair.To.ForeignKeys.Except(matchPair.From.ForeignKeys);
                var fkStatements = this.createTableWriter.CreateForeignKeys(fkAdditions);
                foreach (var statement in fkStatements) {
                    sql.AppendSql(statement);
                }

                var indexAdditions = matchPair.To.Indexes.Except(matchPair.From.Indexes);
                var indexStatements = this.createTableWriter.CreateIndexes(indexAdditions);
                foreach (var statement in indexStatements) {
                    sql.AppendSql(statement);
                }
            }

            warnings = warningList;
            errors = errorList;
            return sql.ToString();
        }
        private static void Setup()
        {
            IoC.Register<ILoggerFactory>(
                new LoggerFactory(
                    new List<LoggerCtorDelegate>
                        {
                            (type, log) => new Log4NetLogger(type, log)
                        }));

            Log4NetLogger.ConfigureAndWatch("Log4Net.config");
            logger = IoC.GetLoggerFor<Program>();
            logger.Trace("Test Logger");
            RegisterVariablesForResolution();
            RegistrationModule();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += ApplicationThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            // Add the event handler for handling non-UI thread exceptions to the event.
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            logger.Trace(
                "Setup complete all IoC registrations finished, this log file proves that IoC is now working correctly.");
        }
Exemple #58
0
            public InternalMethodTracer(string methodName, ILogger logger)
            {
                if (string.IsNullOrEmpty(methodName))
                {
                    throw new ArgumentException("methodName was null or empty", "methodName");
                }
                if (logger == null)
                {
                    throw new ArgumentNullException("logger");
                }

                m_logger = logger;
                m_methodName = methodName;
                m_logger.Trace(methodName + MethodBeginSuffix);
            }
        private void UncacheCustomDataProperty(Uri resourceUri, ILogger logger)
        {
            var href = resourceUri.ToString();
            var propertyName = href.Substring(href.LastIndexOf('/') + 1);
            href = href.Substring(0, href.LastIndexOf('/'));

            if (string.IsNullOrEmpty(propertyName) ||
                string.IsNullOrEmpty(href))
            {
                throw new ApplicationException("Could not update cache for removed custom data entry.");
            }

            var cache = this.GetSyncCache(typeof(ICustomData));
            var cacheKey = this.GetCacheKey(href);

            var existingData = cache.Get(cacheKey);
            if (existingData.IsNullOrEmpty())
            {
                return;
            }

            logger.Trace($"Deleting custom data property '{propertyName}' from resource {href}", "WriteCacheFilter.UncacheCustomDataProperty");

            existingData.Remove(propertyName);
            cache.Put(cacheKey, existingData);
        }
        private async Task UncacheCustomDataPropertyAsync(Uri resourceUri, ILogger logger, CancellationToken cancellationToken)
        {
            var href = resourceUri.ToString();
            var propertyName = href.Substring(href.LastIndexOf('/') + 1);
            href = href.Substring(0, href.LastIndexOf('/'));

            if (string.IsNullOrEmpty(propertyName) ||
                string.IsNullOrEmpty(href))
            {
                throw new ApplicationException("Could not update cache for removed custom data entry.");
            }

            var cache = this.GetAsyncCache(typeof(ICustomData));
            var cacheKey = this.GetCacheKey(href);

            var existingData = await cache.GetAsync(cacheKey, cancellationToken).ConfigureAwait(false);
            if (existingData.IsNullOrEmpty())
            {
                return;
            }

            logger.Trace($"Deleting custom data property '{propertyName}' from resource {href}", "WriteCacheFilter.UncacheCustomDataPropertyAsync");

            existingData.Remove(propertyName);
            await cache.PutAsync(cacheKey, existingData, cancellationToken).ConfigureAwait(false);
        }