Beispiel #1
0
 public TnsNamesFileInfo(string filepath, TnsNamesSource source, TnsNamesFileInfo parent = null, int sourceOrder = 1)
 {
     Filepath    = filepath ?? throw new ArgumentNullException(nameof(filepath));
     Source      = source;
     SourceOrder = sourceOrder;
     Parent      = parent;
 }
Beispiel #2
0
        /// <summary>
        /// Opens the TNS names file. This API does not follow IFile entries.
        /// </summary>
        /// <param name="tnsNamesFileInfo">The TNS names file information.</param>
        /// <returns>Contents of TNS names file.</returns>
        /// <exception cref="erl.Oracle.TnsNames.TnsNamesParseException">TNS names file has errors.</exception>
        public static TnsNamesFile OpenTnsNamesFile([NotNull] TnsNamesFileInfo tnsNamesFileInfo)
        {
            TnsNamesListenerErrorNode[] errors;
            var result = OpenTnsNamesFileIncludeErrors(tnsNamesFileInfo, out errors);

            if (errors.Any())
            {
                throw new TnsNamesParseException("TNS names file has errors.")
                      .AddData("FILE", tnsNamesFileInfo.Filepath);
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TnsNamesFile"/> class.
        /// </summary>
        /// <param name="info">The information.</param>
        /// <param name="iFileEntries">The iFile entries.</param>
        /// <param name="tnsNames">The TNS name entries.</param>
        /// <exception cref="System.ArgumentNullException">
        /// info
        /// or
        /// iFileEntries
        /// or
        /// tnsNames
        /// </exception>
        public TnsNamesFile(TnsNamesFileInfo info, [NotNull] TnsNamesFileInfo[] iFileEntries, [NotNull] TnsNameInfo[] tnsNames)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            Parent       = info.Parent;
            Filepath     = info.Filepath;
            Source       = info.Source;
            SourceOrder  = info.SourceOrder;
            TnsNames     = tnsNames ?? throw new ArgumentNullException(nameof(tnsNames));
            IfileEntries = iFileEntries ?? throw new ArgumentNullException(nameof(iFileEntries));

            foreach (var tnsNameInfo in tnsNames)
            {
                tnsNameInfo.Source = this;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Opens the TNS names file. This API includes an error list.
        /// </summary>
        /// <param name="tnsNamesFileInfo">The TNS names file information.</param>
        /// <param name="errors">The error list.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">tnsNamesFileInfo</exception>
        public static TnsNamesFile OpenTnsNamesFileIncludeErrors([NotNull] TnsNamesFileInfo tnsNamesFileInfo, out TnsNamesListenerErrorNode[] errors)
        {
            if (tnsNamesFileInfo == null)
            {
                throw new ArgumentNullException(nameof(tnsNamesFileInfo));
            }

            var listener = new TnsNamesListener();

            using (var fileStream = new FileStream(tnsNamesFileInfo.Filepath, FileMode.Open, FileAccess.Read))
            {
                var inputStream       = new AntlrInputStream(fileStream);
                var lexer             = new TnsNamesLexer(inputStream);
                var commonTokenStream = new CommonTokenStream(lexer);
                var parser            = new TnsNamesParser(commonTokenStream);

#if RELEASE
                lexer.ErrorListeners.Clear();
                parser.ErrorListeners.Clear();
#endif

                var parse = parser.configuration_file();
                ParseTreeWalker.Default.Walk(listener, parse);
            }

            // Convert internal representation to public API representation
            var tnsNames = from tnsNameNode in listener.TnsItems
                           let databaseAddressNodes = tnsNameNode.DatabaseAddresses
                                                      let databaseAddresses = databaseAddressNodes.Select(CreateDatabaseAddress).ToArray()
                                                                              let rawDescription = tnsNameNode.ParameterContext.parameterValue().children[1].GetText().ToUpperInvariant()
                                                                                                   select new TnsNameInfo(tnsNameNode.TnsName, rawDescription, tnsNameNode.ServiceName, databaseAddresses);

            var iFileEntries = listener.IfileEntries.Select(p => new TnsNamesFileInfo(p, TnsNamesSource.IfileEntry, tnsNamesFileInfo));

            errors = listener.ErrorNodes.ToArray();
            return(new TnsNamesFile(tnsNamesFileInfo, iFileEntries.ToArray(), tnsNames.ToArray()));
        }
Beispiel #5
0
        private static TnsNamesFile[] OpenTnsNamesFiles(TnsNamesFileInfo tnsNamesFileInfo, bool ignoreErrors, bool followIFileEntries)
        {
            var items = new List <TnsNamesFile>();


            var loopDetector = new HashSet <int>();
            var fileStack    = new Stack <TnsNamesFileInfo>(new[] { tnsNamesFileInfo });

            while (fileStack.Count > 0)
            {
                var item = fileStack.Pop();

                var fileInfo = new FileInfo(item.Filepath.ToLowerInvariant());

                // Skip files that do not exist
                if (ignoreErrors && !FileExists(item.Filepath))
                {
                    continue;
                }

                // Loop detection amongst TNS names files
                int hashCode = fileInfo.FullName.GetHashCode();

                if (loopDetector.Contains(hashCode))
                {
                    continue;
                }

                loopDetector.Add(hashCode);

                // Open TNS names file
                try
                {
                    TnsNamesListenerErrorNode[] errors;
                    TnsNamesFile tnsNamesFile = OpenTnsNamesFileIncludeErrors(item, out errors);

                    items.Add(tnsNamesFile);

                    if (!ignoreErrors && errors.Any())
                    {
                        throw new TnsNamesParseException("TNS names file has errors.")
                              .AddData("FILE", tnsNamesFileInfo.Filepath);
                    }

                    if (followIFileEntries)
                    {
                        // Add IFile entries to stack
                        foreach (var iFile in tnsNamesFile.IfileEntries)
                        {
                            fileStack.Push(iFile);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!ignoreErrors)
                    {
                        throw new TnsNamesParseException("An error occured during parse.", e)
                              .AddData("FILE", tnsNamesFileInfo.Filepath);
                    }
                }
            }

            return(items.ToArray());
        }
Beispiel #6
0
 /// <summary>
 /// Opens the TNS names file. This API does follow IFile entries. May return multiple result due to IFile entries (recursively).
 /// </summary>
 /// <param name="tnsNamesFileInfo">The TNS names file information.</param>
 /// <param name="ignoreErrors">if set to <c>true</c> ignore files with parse errors.</param>
 /// <returns></returns>
 public static TnsNamesFile[] OpenTnsNamesFiles(TnsNamesFileInfo tnsNamesFileInfo, bool ignoreErrors)
 {
     return(OpenTnsNamesFiles(tnsNamesFileInfo, ignoreErrors, true));
 }