예제 #1
0
        private List <PluginTypeInfo> GetPluginsToRegister(PluginAssemblyInfo assemblyInfo)
        {
            List <PluginTypeInfo> pluginsToRegister = new List <PluginTypeInfo>();

            if (Plugins == null || Plugins.Length == 0)
            {
                pluginsToRegister.AddRange(assemblyInfo.Plugins);
            }
            else
            {
                foreach (string pluginName in Plugins)
                {
                    PluginTypeInfo tempPluginType = assemblyInfo.Plugins.SingleOrDefault(p => p.TypeName.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase) || p.PluginId != Guid.Empty);
                    if (tempPluginType == null)
                    {
                        throw new Exception(string.Format("No Plugin named '{0}' found in assembly '{1}'.", pluginName, assemblyInfo.Name));
                    }
                    else
                    {
                        pluginsToRegister.Add(tempPluginType);
                    }
                }
            }

            return(pluginsToRegister);
        }
        public static void Execute(Guid assemblyId)
        {
            using (var dbContext = new OrationiDatabaseContext())
            {
                var pluginAssembly = dbContext.PluginAsseblies.FirstOrDefault(pa => pa.Id == assemblyId);
                if (pluginAssembly is null)
                {
                    throw new InvalidOperationException($"Не найдена сборка с Id - {assemblyId}");
                }

                var pluginAssemblyInfo = PluginAssemblyInfo.Create(pluginAssembly);

                if (!pluginAssemblyInfo.IsFolderExists)
                {
                    Directory.CreateDirectory(pluginAssemblyInfo.BaseFolder);
                }

                AppDomain.CurrentDomain.AppendPrivatePath(pluginAssemblyInfo.BaseFolder);

                var zipAssemblyPackage = $"{pluginAssemblyInfo.FullPath}.zip";

                using (var writer = new BinaryWriter(File.OpenWrite(zipAssemblyPackage)))
                {
                    writer.Write(pluginAssembly.Assembly, 0, pluginAssembly.Assembly.Length);
                }

                ZipFile.ExtractToDirectory(zipAssemblyPackage, pluginAssemblyInfo.BaseFolder);

                File.Delete(zipAssemblyPackage);
            }
        }
예제 #3
0
 public RunnerDataHolder( PluginAssemblyInfo[] allAssemblies,
                         PluginInfo[] oldPlugins,
                         ServiceInfo[] notFoundServices )
 {
     _allAssemblies = allAssemblies;
     _oldPlugins = oldPlugins;
     _notFoundServices = notFoundServices;
 }
        internal static string WarmupAssembly(PipelineStepDescription stepDescription)
        {
            var pluginAssemblyInfo = PluginAssemblyInfo.Create(stepDescription);

            if (!pluginAssemblyInfo.IsAssemblyExist)
            {
                Execute(stepDescription.AssemblyId);
            }

            return(pluginAssemblyInfo.FullPath);
        }
        public void UnregisterPlugins(PluginManifest manifest, IOrganizationService client, TracingHelper t)
        {
            t.Debug($"Entering PluginWrapper.UnregisterPlugins");

            // TODO - need to clobber Custom APIs and child parameters/properties too!!

            foreach (var pluginAssembly in manifest.PluginAssemblies)
            {
                t.Debug($"Getting PluginAssemblyInfo from file {pluginAssembly.Assembly}");
                if (!File.Exists(pluginAssembly.Assembly))
                {
                    t.Critical($"Assembly {pluginAssembly.Assembly} cannot be found!");
                    continue;
                }

                var pluginAssemblyInfo = new PluginAssemblyInfo(pluginAssembly.Assembly);
                var existingAssembly   = pluginAssembly.GetExistingQuery(pluginAssemblyInfo.Version).RetrieveSingleRecord(client);

                if (existingAssembly == null)
                {
                    return;
                }

                var childPluginTypesResults = GetChildPluginTypesQuery(existingAssembly.ToEntityReference()).RetrieveMultiple(client);
                var pluginsList             = childPluginTypesResults.Entities.Select(e => e.Id).ToList();

                GetChildCustomApisQuery(pluginsList).DeleteAllResults(client);

                var childStepsResults = GetChildPluginStepsQuery(pluginsList).RetrieveMultiple(client);
                var pluginStepsList   = childStepsResults.Entities.Select(e => e.Id).ToList();

                if (pluginStepsList.Count > 0)
                {
                    GetChildEntityImagesQuery(pluginStepsList).DeleteAllResults(client);
                }

                if (pluginsList.Count > 0)
                {
                    GetChildPluginStepsQuery(pluginsList).DeleteAllResults(client);
                }

                GetChildPluginTypesQuery(existingAssembly.ToEntityReference()).DeleteAllResults(client);
                pluginAssembly.GetExistingQuery(pluginAssemblyInfo.Version).DeleteSingleRecord(client);
            }

            t.Debug($"Exiting PluginWrapper.UnregisterPlugins");
        }
        public EntityReference Register(IOrganizationService client, PluginAssemblyInfo pluginAssemblyInfo = null)
        {
            if (pluginAssemblyInfo == null)
            {
                pluginAssemblyInfo = new PluginAssemblyInfo(this.Assembly);
            }

            var assemblyEntity = new PluginAssembly()
            {
                Name           = this.Name,
                Culture        = pluginAssemblyInfo.Culture,
                Version        = pluginAssemblyInfo.Version,
                PublicKeyToken = pluginAssemblyInfo.PublicKeyToken,
                SourceType     = PluginAssembly_SourceType.Database,   // Only database supported for now
                IsolationMode  = PluginAssembly_IsolationMode.Sandbox, // Only Sandbox supported for now
                Content        = Convert.ToBase64String(_fileSystem.File.ReadAllBytes(this.Assembly))
            };

            var existingAssemblyQuery = this.GetExistingQuery(pluginAssemblyInfo.Version);

            return(assemblyEntity.CreateOrUpdate(client, existingAssemblyQuery));
        }
        public bool Unregister(IOrganizationService client, PluginAssemblyInfo pluginAssemblyInfo = null)
        {
            if (pluginAssemblyInfo == null)
            {
                pluginAssemblyInfo = new PluginAssemblyInfo(this.Assembly);
            }

            var existingAssembly = this.GetExistingQuery(pluginAssemblyInfo.Version).RetrieveSingleRecord(client);

            if (existingAssembly == null)
            {
                return(false);
            }

            // Currently hard-coded to a max of 50 records to delete
            var deletedChildPlugins = new QueryExpression()
            {
                EntityName = PluginType.EntityLogicalName,
                ColumnSet  = new ColumnSet(PluginType.Fields.Name),
                Criteria   = new FilterExpression()
                {
                    Conditions =
                    {
                        new ConditionExpression(PluginType.Fields.PluginAssemblyId,
                                                ConditionOperator.Equal, existingAssembly.Id)
                    }
                }
            };
            var result = deletedChildPlugins.DeleteAllResults(client);

            if (!result)
            {
                return(false);
            }

            existingAssembly.Delete(client);
            return(true);
        }
예제 #8
0
        private Entity GenerateCrmEntity(PluginAssemblyInfo assemblyInfo)
        {
            Entity crmPluginAssembly = new Entity("pluginassembly")
            {
                Id         = assemblyInfo.AssemblyId,
                Attributes = new AttributeCollection()
            };

            crmPluginAssembly.Attributes.Add("sourcetype", new OptionSetValue((int)assemblyInfo.SourceType));
            crmPluginAssembly.Attributes.Add("isolationmode", new OptionSetValue((int)assemblyInfo.IsolationMode));
            crmPluginAssembly.Attributes.Add("culture", assemblyInfo.Culture);
            crmPluginAssembly.Attributes.Add("publickeytoken", assemblyInfo.PublicKeyToken);
            crmPluginAssembly.Attributes.Add("version", assemblyInfo.Version);
            crmPluginAssembly.Attributes.Add("name", assemblyInfo.Name);
            if (assemblyInfo.Description != null)
            {
                crmPluginAssembly.Attributes.Add("description", assemblyInfo.Description);
            }

            switch (assemblyInfo.SourceType)
            {
            case CrmAssemblySourceType.Database:
                crmPluginAssembly.Attributes.Add("content", Convert.ToBase64String(File.ReadAllBytes(assemblyInfo.LiteralPath)));
                break;

            case CrmAssemblySourceType.GAC:
                break;

            case CrmAssemblySourceType.Disk:
                crmPluginAssembly.Attributes.Add("path", assemblyInfo.ServerFileName);
                break;

            default:
                throw new NotImplementedException("SourceType = " + assemblyInfo.SourceType.ToString());
            }

            return(crmPluginAssembly);
        }
예제 #9
0
        public void Init()
        {
            // Mock up assembly file data and contents

            var random = new Random();
            var buffer = new byte[8000];

            random.NextBytes(buffer);

            var mockFileSystem   = new MockFileSystem();
            var mockAssemblyFile = new MockFileData(buffer);

            mockFileSystem.AddFile("../UnitTestAssembly.dll", mockAssemblyFile);

            _pluginAssemblyInfo = new PluginAssemblyInfo("1.0.0.0", "neutral", "615679ec018eccbc");

            _unitTestPluginAssembly = new CdsPluginAssembly(mockFileSystem)
            {
                Name         = "UnitTestAssembly",
                FriendlyName = "Updated Unit Test Assembly",
                Assembly     = "../UnitTestAssembly.dll",
            };
        }
예제 #10
0
 public void LoadPlugin(IPluginHost host, PluginAssemblyInfo assemblyInfo)
 {
     Host         = host;
     AssemblyInfo = assemblyInfo;
 }
예제 #11
0
        private void RegisterPluginAssembly(string path)
        {
            PluginAssemblyInfo assemblyInfo = PluginManagementHelper.RetrievePluginAssemblyInfo(path);

            PluginManagementHelper.RefreshFromExistingAssembly(_repository, assemblyInfo);

            assemblyInfo.SourceType    = AssemblyLocation ?? CrmAssemblySourceType.Database;
            assemblyInfo.IsolationMode = IsolationMode ?? CrmAssemblyIsolationMode.Sandbox;
            if (Description != null)
            {
                assemblyInfo.Description = Description;
            }
            if (assemblyInfo.AssemblyId != Guid.Empty && Force.ToBool() != true)
            {
                throw new Exception(string.Format("Assembly '{0}' is already registered. Use Force to overwrite the assembly.", assemblyInfo.Name));
            }

            List <PluginTypeInfo> pluginsToRegister = GetPluginsToRegister(assemblyInfo);

            if (pluginsToRegister.Count == 0)
            {
                throw new Exception("No Plugins for registration.");
            }
            if (assemblyInfo.IsolationMode == CrmAssemblyIsolationMode.Sandbox && pluginsToRegister.Any(p => p.Isolatable != true))
            {
                throw new Exception("Since some of the plug-ins cannot be isolated, the assembly cannot be marked as Isolated.");
            }
            if (string.IsNullOrWhiteSpace(assemblyInfo.PublicKeyToken) && pluginsToRegister.Any(p => p.PluginType == CrmPluginType.Plugin))
            {
                throw new Exception("Assemblies containing Plugins must be strongly signed. Sign the Assembly using a KeyFile.");
            }

            Entity pluginAssemblyEntity = GenerateCrmEntity(assemblyInfo);
            Guid   assemblyId           = pluginAssemblyEntity.Id;

            if (assemblyId == Guid.Empty)
            {
                assemblyId = _repository.Add(pluginAssemblyEntity);
            }
            else
            {
                _repository.Update(pluginAssemblyEntity);
            }

            foreach (PluginTypeInfo plugin in pluginsToRegister)
            {
                Entity pluginTypeEntity = GenerateCrmEntity(assemblyId, plugin);
                Guid   pluginTypeId     = pluginTypeEntity.Id;
                if (pluginTypeEntity.Id == Guid.Empty)
                {
                    pluginTypeId = _repository.Add(pluginTypeEntity);
                }
                else
                {
                    _repository.Update(pluginTypeEntity);
                }

                if (PassThru)
                {
                    WriteObject(_repository.Get("plugintype", pluginTypeId));
                }
            }
        }
예제 #12
0
 public void LoadPlugin(IPluginHost host, PluginAssemblyInfo assemblyInfo)
 {
     Host = host;
     AssemblyInfo = assemblyInfo;
 }
예제 #13
0
        /// <summary>
        /// For each FileInfo in _currentFiles, Process will try to create a new PluginAssemblyInfo
        /// based on the FileInfo (that is currently processed).
        /// After that, this method fill properties collections with IPluginInfo or IServiceInfo.
        /// </summary>
        public RunnerDataHolder Discover( IEnumerable<FileInfo> files )
		{
            // Transforms FileInfo into PluginAssemblyInfo.
            foreach( FileInfo f in files )
            {
                PluginAssemblyInfo a;
                string fName = f.FullName;
                if( !_filesProcessed.TryGetValue( fName, out a ) )
                {
                    Assembly assembly = null;
                    try
                    {
                        assembly = Assembly.ReflectionOnlyLoadFrom( fName );
                        a = new PluginAssemblyInfo( assembly, f );
                        _assembliesByName.Add( assembly.FullName, a );
                    }
                    catch( Exception ex )
                    {
                        a = new PluginAssemblyInfo( fName );
                        a.AddErrorLine( ex.Message );
                    }
                    _filesProcessed.Add( fName, a );
                }
            }
            foreach( var e in _assembliesByName )
            {
                e.Value.LoadDependencies();
            }
            foreach( var e in _assembliesByName )
            {
                PluginAssemblyInfo a = e.Value;
                if( !a.HasError )
                {
                    Debug.Assert( a.ErrorMessage == null );
                    try
                    {
                        a.ReadAllTypes( RegisterServiceInfo, RegisterUseServiceInfo );
                    }
                    catch( Exception ex )
                    {
                        a.AddErrorLine( ex.Message );
                    }
                }
            }
            
            // Consider DynamicService without any definition assembly as an error.
            foreach( ServiceInfo serviceInfo in _dicAllServices.Values )
            {
                if( serviceInfo.IsDynamicService && serviceInfo.AssemblyInfo == null )
                {
                    serviceInfo.AddErrorLine( R.AssemblyNotFoundForDynamicService );
                }
            }

            // Fills _existingPlugins, updates _pluginsById (keep only the best version), removes old plugins from Assembly.Plugins collection.
            foreach( var e in _assembliesByName )
            {
                PluginAssemblyInfo a = e.Value;
                if( !a.HasError )
                {
                    List<PluginInfo> toRemove = new List<PluginInfo>();
                    foreach( PluginInfo pluginInfo in a.Plugins )
                    {
                        // Transfer any pluginInfo.Service.HasError to pluginInfo.HasError.
                        if( pluginInfo.Service != null && pluginInfo.Service.HasError )
                        {
                            pluginInfo.AddErrorLine( R.ImplementedServiceIsOnError );
                        }
                        // Errors on the ServiceReferences are propagated up to the plugin object
                        // only if the reference "Must Exist": references that are optional do not transfer errors.
                        foreach( var i in pluginInfo.ServiceReferences )
                        {
                            if( i.HasError && i.Requirements > RunningRequirement.OptionalTryStart )
                            {
                                pluginInfo.AddErrorLine( String.Format( "Dependency {0} is on error: {1}", i.PropertyName, i.ErrorMessage ) );
                            }
                        }

                        _existingPlugins.Add( pluginInfo );
                        PluginInfo dicPlugin;
                        if( _pluginsById.TryGetValue( pluginInfo.PluginId, out dicPlugin ) )
                        {
                            if( dicPlugin.Version >= pluginInfo.Version )
                            {
                                pluginInfo.IsOldVersion = true;
                                if( pluginInfo.Service != null )
                                    pluginInfo.Service.Implementations.Remove( pluginInfo );
                                _oldPlugins.Add( pluginInfo );
                                toRemove.Add( pluginInfo );
                            }
                            else if( dicPlugin.Version < pluginInfo.Version )
                            {
                                _pluginsById.Remove( dicPlugin.PluginId );
                                _pluginsById.Add( pluginInfo.PluginId, pluginInfo );
                                dicPlugin.IsOldVersion = true;
                                if( dicPlugin.Service != null )
                                    dicPlugin.Service.Implementations.Remove( dicPlugin );
                                _oldPlugins.Add( dicPlugin );
                                toRemove.Add( dicPlugin );
                            }
                        }
                        else
                        {
                            _pluginsById.Add( pluginInfo.PluginId, pluginInfo );
                        }
                    }
                    // We remove old plugins from assemblyInfo
                    foreach( PluginInfo oldPlugin in toRemove )
                        a.Plugins.Remove( oldPlugin );
                }
            }
            // Then we put the editors into edited plugins.
            foreach( PluginInfo plugin in _pluginsById.Values )
            {
                foreach( PluginConfigAccessorInfo editor in plugin.EditorsInfo )
                {
                    PluginInfo editedPlugin;
                    if( _pluginsById.TryGetValue( editor.Source, out editedPlugin ) )
                        editedPlugin.EditableBy.Add( editor );
                }
            }

            foreach( PluginAssemblyInfo assembly in _filesProcessed.Values ) assembly.NormalizeCollections();
            foreach( PluginInfo plugin in _oldPlugins ) plugin.NormalizeCollections();
            foreach( PluginInfo plugin in _existingPlugins ) plugin.NormalizeCollections();
            foreach( ServiceInfo service in _dicAllServices.Values ) service.NormalizeCollections();

            PluginAssemblyInfo[] allAssemblies = _filesProcessed.Values.ToArray();
            Array.Sort( allAssemblies );
            PluginInfo[] oldPlugins = _oldPlugins.ToArray();
            Array.Sort( oldPlugins );
            ServiceInfo[] notFoundServices = _notFoundServices.ToArray();
            Array.Sort( notFoundServices );

            return new RunnerDataHolder(
                allAssemblies, 
                oldPlugins, 
                notFoundServices 
            );
		}
예제 #14
0
 // When a IServiceInfo (ex : interface ICommonTimer) is found in an assembly.
 ServiceInfo RegisterServiceInfo( PluginAssemblyInfo a, Type t )
 {
     string assemblyQualifiedName = t.AssemblyQualifiedName;
     ServiceInfo serv;
     if( !_dicAllServices.TryGetValue( assemblyQualifiedName, out serv ) )
     {
         serv = new ServiceInfo( a, t );
         _dicAllServices.Add( assemblyQualifiedName, serv );
         _services.Add( serv );
     }
     else if( serv.AssemblyInfo == null )
     {
         serv.AssemblyInfo = a;
         _services.Add( serv );
         _notFoundServices.Remove( serv );
     }
     return serv;
 }