Exemple #1
0
        public void BuildCatalogFromWorkspace(string workspacePath, params string[] folders)
        {
            if (string.IsNullOrEmpty(workspacePath))
            {
                throw new ArgumentNullException("workspacePath");
            }
            if (folders == null)
            {
                throw new ArgumentNullException("folders");
            }

            if (folders.Length == 0 || !Directory.Exists(workspacePath))
            {
                return;
            }

            var streams = new List <ResourceBuilderTO>();

            try
            {
                foreach (var path in folders.Where(f => !string.IsNullOrEmpty(f) && !f.EndsWith("VersionControl")).Select(f => Path.Combine(workspacePath, f)))
                {
                    if (!Directory.Exists(path))
                    {
                        continue;
                    }

                    var files = Directory.GetFiles(path, "*.xml");
                    foreach (var file in files)
                    {
                        FileAttributes fa = File.GetAttributes(file);

                        if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        {
                            Dev2Logger.Log.Info("Removed READONLY Flag from [ " + file + " ]");
                            File.SetAttributes(file, FileAttributes.Normal);
                        }

                        // Use the FileStream class, which has an option that causes asynchronous I/O to occur at the operating system level.
                        // In many cases, this will avoid blocking a ThreadPool thread.
                        var sourceStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true);
                        streams.Add(new ResourceBuilderTO {
                            FilePath = file, FileStream = sourceStream
                        });
                    }
                }

                // Use the parallel task library to process file system ;)
                Parallel.ForEach(streams.ToArray(), currentItem =>
                {
                    XElement xml = null;

                    try
                    {
                        xml = XElement.Load(currentItem.FileStream);
                    }
                    catch (Exception e)
                    {
                        Dev2Logger.Log.Error("Resource [ " + currentItem.FilePath + " ] caused " + e.Message);
                    }

                    StringBuilder result = xml.ToStringBuilder();

                    var isValid = xml != null && HostSecurityProvider.Instance.VerifyXml(result);
                    if (isValid)
                    {
                        var resource = new Resource(xml)
                        {
                            FilePath = currentItem.FilePath
                        };

                        //2013.08.26: Prevent duplicate unassigned folder in save dialog and studio explorer tree by interpreting 'unassigned' as blank
                        if (resource.ResourcePath.ToUpper() == "UNASSIGNED")
                        {
                            resource.ResourcePath = string.Empty;
                            // DON'T FORCE A SAVE HERE - EVER!!!!
                        }
                        xml = _resourceUpgrader.UpgradeResource(xml, Assembly.GetExecutingAssembly().GetName().Version, (a =>
                        {
                            StringBuilder updateXml = a.ToStringBuilder();
                            var signedXml = HostSecurityProvider.Instance.SignXml(updateXml);
                            signedXml.WriteToFile(currentItem.FilePath, Encoding.UTF8);
                        }));
                        if (resource.IsUpgraded)
                        {
                            // Must close the source stream first and then add a new target stream
                            // otherwise the file will be remain locked
                            currentItem.FileStream.Close();

                            xml = resource.UpgradeXml(xml, resource);

                            StringBuilder updateXml = xml.ToStringBuilder();
                            var signedXml           = HostSecurityProvider.Instance.SignXml(updateXml);
                            signedXml.WriteToFile(currentItem.FilePath, Encoding.UTF8);
                        }
                        if (resource.VersionInfo == null)
                        {
                        }

                        lock (_addLock)
                        {
                            AddResource(resource, currentItem.FilePath);
                        }
                    }
                    else
                    {
                        Dev2Logger.Log.Debug(string.Format("'{0}' wasn't loaded because it isn't signed or has modified since it was signed.", currentItem.FilePath));
                    }
                });
            }
            finally
            {
                // Close all FileStream instances in a finally block after the tasks are complete.
                // If each FileStream was instead created in a using statement, the FileStream
                // might be disposed of before the task was complete
                foreach (var stream in streams)
                {
                    stream.FileStream.Close();
                }
            }
        }
Exemple #2
0
        private void BuildCatalogFromWorkspace(string workspacePath, string[] folders, List <ResourceBuilderTO> streams)
        {
            BuildStream(workspacePath, folders, streams);

            // Use the parallel task library to process file system ;)
            IList <Type> allTypes             = new List <Type>();
            var          connectionTypeName   = typeof(Connection).Name;
            var          dropBoxSourceName    = typeof(DropBoxSource).Name;
            var          sharepointSourceName = typeof(SharepointSource).Name;
            var          dbType = typeof(DbSource).Name;

            try
            {
                var resourceBaseType = typeof(IResourceSource);
                var assemblies       = AppDomain.CurrentDomain.GetAssemblies();
                var types            = assemblies
                                       .SelectMany(s => s.GetTypes())
                                       .Where(p => resourceBaseType.IsAssignableFrom(p));
                allTypes = types as IList <Type> ?? types.ToList();
            }
            catch (Exception e)
            {
                Dev2Logger.Error(ErrorResource.ErrorLoadingTypes, e, GlobalConstants.WarewolfError);
            }
            streams.ForEach(currentItem =>
            {
                try
                {
                    XElement xml = null;
                    try
                    {
                        xml = XElement.Load(currentItem._fileStream);
                    }
                    catch (Exception e)
                    {
                        Dev2Logger.Error("Resource [ " + currentItem._filePath + " ] caused " + e.Message,
                                         GlobalConstants.WarewolfError);
                    }

                    var result = xml?.ToStringBuilder();

                    var isValid  = result != null && HostSecurityProvider.Instance.VerifyXml(result);
                    var typeName = xml?.AttributeSafe("Type");
                    if (isValid)
                    {
                        //TODO: Remove this after V1 is released. All will be updated.
                        if (!IsWarewolfResource(xml))
                        {
                            return;
                        }

                        if (typeName == "Unknown")
                        {
                            var servertype = xml.AttributeSafe("ResourceType");
                            if (servertype != null && servertype == dbType)
                            {
                                xml.SetAttributeValue("Type", dbType);
                                typeName = dbType;
                            }
                        }

                        if (typeName == "Dev2Server" || typeName == "Server" || typeName == "ServerSource")
                        {
                            xml.SetAttributeValue("Type", connectionTypeName);
                            typeName = connectionTypeName;
                        }

                        if (typeName == "OauthSource")
                        {
                            xml.SetAttributeValue("Type", dropBoxSourceName);
                            typeName = dropBoxSourceName;
                        }

                        if (typeName == "SharepointServerSource")
                        {
                            xml.SetAttributeValue("Type", sharepointSourceName);
                            typeName = sharepointSourceName;
                        }

                        Type type = null;
                        if (allTypes.Count != 0)
                        {
                            type = allTypes.FirstOrDefault(type1 => type1.Name == typeName);
                        }

                        var resourceType = xml.AttributeSafe("ResourceType");
                        if (type is null && typeName == "" && resourceType == "WorkflowService")
                        {
                            type = typeof(Workflow);
                        }

                        Resource resource;
                        if (type != null)
                        {
                            resource = (Resource)Activator.CreateInstance(type, xml);
                        }
                        else
                        {
                            resource = new Resource(xml);
                        }

                        if (currentItem._filePath.EndsWith(".xml"))
                        {
                            _convertToBiteExtension.Add(currentItem._filePath);
                            resource.FilePath = currentItem._filePath.Replace(".xml", ".bite");
                        }
                        else
                        {
                            resource.FilePath = currentItem._filePath;
                        }

                        xml = _resourceUpgrader.UpgradeResource(xml, Assembly.GetExecutingAssembly().GetName().Version,
                                                                a =>
                        {
                            var fileManager = new TxFileManager();
                            using (TransactionScope tx = new TransactionScope())
                            {
                                try
                                {
                                    var updateXml = a.ToStringBuilder();
                                    var signedXml = HostSecurityProvider.Instance.SignXml(updateXml);
                                    signedXml.WriteToFile(currentItem._filePath, Encoding.UTF8, fileManager);
                                    tx.Complete();
                                }
                                catch
                                {
                                    try
                                    {
                                        Transaction.Current.Rollback();
                                    }
                                    catch (Exception err)
                                    {
                                        Dev2Logger.Error(err, GlobalConstants.WarewolfError);
                                    }

                                    throw;
                                }
                            }
                        });
                        if (resource.IsUpgraded)
                        {
                            // Must close the source stream first and then add a new target stream
                            // otherwise the file will be remain locked
                            currentItem._fileStream.Close();

                            xml = resource.UpgradeXml(xml, resource);

                            var updateXml   = xml.ToStringBuilder();
                            var signedXml   = HostSecurityProvider.Instance.SignXml(updateXml);
                            var fileManager = new TxFileManager();
                            using (TransactionScope tx = new TransactionScope())
                            {
                                try
                                {
                                    signedXml.WriteToFile(currentItem._filePath, Encoding.UTF8, fileManager);
                                    tx.Complete();
                                }
                                catch
                                {
                                    Transaction.Current.Rollback();
                                    throw;
                                }
                            }
                        }

                        lock (_addLock)
                        {
                            AddResource(resource, currentItem._filePath);
                        }
                    }
                    else
                    {
                        Dev2Logger.Debug(string.Format("'{0}' wasn't loaded because it isn't signed or has modified since it was signed.", currentItem._filePath), GlobalConstants.WarewolfDebug);
                    }
                }
                catch
                {
                    Dev2Logger.Warn($"Exception loading resource {currentItem._filePath}", GlobalConstants.WarewolfWarn);
                }
            });
        }
Exemple #3
0
        public void BuildCatalogFromWorkspace(string workspacePath, params string[] folders)
        {
            if (string.IsNullOrEmpty(workspacePath))
            {
                throw new ArgumentNullException("workspacePath");
            }
            if (folders == null)
            {
                throw new ArgumentNullException("folders");
            }
            if (folders.Length == 0 || !Directory.Exists(workspacePath))
            {
                return;
            }

            var streams = new List <ResourceBuilderTO>();

            try
            {
                foreach (var path in folders.Where(f => !string.IsNullOrEmpty(f) && !f.EndsWith("VersionControl")).Select(f => Path.Combine(workspacePath, f)))
                {
                    if (!Directory.Exists(path))
                    {
                        continue;
                    }

                    var files = Directory.GetFiles(path, "*.xml");
                    foreach (var file in files)
                    {
                        FileAttributes fa = File.GetAttributes(file);

                        if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        {
                            Dev2Logger.Info("Removed READONLY Flag from [ " + file + " ]");
                            File.SetAttributes(file, FileAttributes.Normal);
                        }

                        // Use the FileStream class, which has an option that causes asynchronous I/O to occur at the operating system level.
                        // In many cases, this will avoid blocking a ThreadPool thread.
                        var sourceStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true);
                        streams.Add(new ResourceBuilderTO {
                            FilePath = file, FileStream = sourceStream
                        });
                    }
                }

                // Use the parallel task library to process file system ;)
                IList <Type> allTypes             = new List <Type>();
                var          connectionTypeName   = typeof(Connection).Name;
                var          dropBoxSourceName    = typeof(DropBoxSource).Name;
                var          sharepointSourceName = typeof(SharepointSource).Name;
                var          dbType = typeof(DbSource).Name;
                try
                {
                    var resourceBaseType = typeof(IResourceSource);
                    var assemblies       = AppDomain.CurrentDomain.GetAssemblies();
                    var types            = assemblies
                                           .SelectMany(s => s.GetTypes())
                                           .Where(p => resourceBaseType.IsAssignableFrom(p));
                    allTypes = types as IList <Type> ?? types.ToList();
                }
                catch (Exception e)
                {
                    Dev2Logger.Error(ErrorResource.ErrorLoadingTypes, e);
                }
                streams.ForEach(currentItem =>
                {
                    XElement xml = null;
                    try
                    {
                        xml = XElement.Load(currentItem.FileStream);
                    }
                    catch (Exception e)
                    {
                        Dev2Logger.Error("Resource [ " + currentItem.FilePath + " ] caused " + e.Message);
                    }

                    StringBuilder result = xml?.ToStringBuilder();

                    var isValid = result != null && HostSecurityProvider.Instance.VerifyXml(result);
                    if (isValid)
                    {
                        //TODO: Remove this after V1 is released. All will be updated.
                        #region old typing to be removed after V1
                        var typeName = xml.AttributeSafe("Type");
                        if (typeName == "Unknown")
                        {
                            var servertype = xml.AttributeSafe("ResourceType");
                            if (servertype != null && servertype == dbType)
                            {
                                xml.SetAttributeValue("Type", dbType);
                                typeName = dbType;
                            }
                        }

                        if (typeName == "Dev2Server" || typeName == "Server" || typeName == "ServerSource")
                        {
                            xml.SetAttributeValue("Type", connectionTypeName);
                            typeName = connectionTypeName;
                        }

                        if (typeName == "OauthSource")
                        {
                            xml.SetAttributeValue("Type", dropBoxSourceName);
                            typeName = dropBoxSourceName;
                        }
                        if (typeName == "SharepointServerSource")
                        {
                            xml.SetAttributeValue("Type", sharepointSourceName);
                            typeName = sharepointSourceName;
                        }
                        #endregion

                        Type type = null;
                        if (allTypes.Count != 0)
                        {
                            type = allTypes.FirstOrDefault(type1 => type1.Name == typeName);
                        }
                        Resource resource;
                        if (type != null)
                        {
                            resource = (Resource)Activator.CreateInstance(type, xml);
                        }
                        else
                        {
                            resource = new Resource(xml);
                        }
                        resource.FilePath = currentItem.FilePath;
                        xml = _resourceUpgrader.UpgradeResource(xml, Assembly.GetExecutingAssembly().GetName().Version, a =>
                        {
                            var fileManager = new TxFileManager();
                            using (TransactionScope tx = new TransactionScope())
                            {
                                try
                                {
                                    StringBuilder updateXml = a.ToStringBuilder();
                                    var signedXml           = HostSecurityProvider.Instance.SignXml(updateXml);
                                    signedXml.WriteToFile(currentItem.FilePath, Encoding.UTF8, fileManager);
                                    tx.Complete();
                                }
                                catch
                                {
                                    try
                                    {
                                        Transaction.Current.Rollback();
                                    }
                                    catch (Exception err)
                                    {
                                        Dev2Logger.Error(err);
                                    }
                                    throw;
                                }
                            }
                        });
                        if (resource.IsUpgraded)
                        {
                            // Must close the source stream first and then add a new target stream
                            // otherwise the file will be remain locked
                            currentItem.FileStream.Close();

                            xml = resource.UpgradeXml(xml, resource);

                            StringBuilder updateXml = xml.ToStringBuilder();
                            var signedXml           = HostSecurityProvider.Instance.SignXml(updateXml);
                            var fileManager         = new TxFileManager();
                            using (TransactionScope tx = new TransactionScope())
                            {
                                try
                                {
                                    signedXml.WriteToFile(currentItem.FilePath, Encoding.UTF8, fileManager);
                                    tx.Complete();
                                }
                                catch
                                {
                                    Transaction.Current.Rollback();
                                    throw;
                                }
                            }
                        }

                        lock (_addLock)
                        {
                            AddResource(resource, currentItem.FilePath);
                        }
                    }
                    else
                    {
                        Dev2Logger.Debug(string.Format("'{0}' wasn't loaded because it isn't signed or has modified since it was signed.", currentItem.FilePath));
                    }
                });
            }
            finally
            {
                // Close all FileStream instances in a finally block after the tasks are complete.
                // If each FileStream was instead created in a using statement, the FileStream
                // might be disposed of before the task was complete
                foreach (var stream in streams)
                {
                    stream.FileStream.Close();
                }
            }
        }