Esempio n. 1
0
        //加载所需的所有程序集
        private void loadAssemblys()
        {
            AssemblyDefinition     bundleAssemblyDefinition     = AssemblyDefinition.ReadAssembly(bundleAssemblyFileName);
            AssemblyNameDefinition bundleAssemblyNameDefinition = bundleAssemblyDefinition.Name;

            //修改名称
            bundleAssemblyNameDefinition.Name = getAssemblyNameWithGuid(bundleAssemblyNameDefinition.Name);
            //去掉强签名
            removeAssemblyStrongName(bundleAssemblyNameDefinition);

            foreach (ModuleDefinition moduleDefinition in bundleAssemblyDefinition.Modules)
            {
                foreach (AssemblyNameReference assemblyNameReference in moduleDefinition.AssemblyReferences)
                {
                    String     assemblyName = assemblyNameReference.Name;
                    BundleImpl bundle       = getBundleFromRequiredBundles(assemblyName);
                    if (bundle == null)
                    {
                        //没找到此名称的插件,首先,如果此程序集属于FCL
                        if (BundleUtils.IsAssemblyBelongsFCL(assemblyName))
                        {
                            continue;
                        }
                        //然后尝试从bootdelegation字典中搜索
                        else if (allBundleRefAssemblyDict.ContainsKey(assemblyName))
                        {
                            assemblyNameReference.Name = getAssemblyNameWithGuid(assemblyNameReference.Name);
                            addRefAssembly(assemblyNameReference.Name, allBundleRefAssemblyDict[assemblyName]);
                        }
                        else
                        {
                            //然后则尝试从lib目录加载
                            String newAssemblyName = loadAssemblyFromLibDir(assemblyName);
                            if (newAssemblyName == null)
                            {
                                throw new IOException(String.Format("{0}不能解析依赖的程序集[{1}]", this.ToString(), assemblyName));
                            }
                            assemblyNameReference.Name = newAssemblyName;
                        }
                    }
                    else
                    {
                        AssemblyName requiredBundleAssemblyName = bundle.bundleAssembly.GetName();
                        assemblyNameReference.Name    = requiredBundleAssemblyName.Name;
                        assemblyNameReference.Version = requiredBundleAssemblyName.Version;
                    }
                    //去掉强签名
                    removeAssemblyStrongName(assemblyNameReference);
                }
                moduleDefinition.Attributes &= ~ModuleAttributes.StrongNameSigned;
            }
            MemoryStream ms = new MemoryStream();

            bundleAssemblyDefinition.Write(ms);
            bundleAssembly = Assembly.Load(ms.ToArray());
            //加载程序集中所有的类
            bundleAssembly.GetTypes();
            addRefAssembly(bundleAssemblyNameDefinition.Name, bundleAssembly);
        }
Esempio n. 2
0
 public BundleImpl[] GetRequiredBundles()
 {
     BundleImpl[] requiredBundles = null;
     lock (requiredBundleList)
     {
         requiredBundles = new BundleImpl[requiredBundleList.Count];
         requiredBundleList.CopyTo(requiredBundles, 0);
     }
     return(requiredBundles);
 }
Esempio n. 3
0
        public void update(System.IO.Stream input)
        {
            Int32 preState = state;

            //如果input不为空,则开始安装插件
            if (input != null)
            {
                //验证要安装的插件与当前插件是否匹配
                String tmpBundleDirectoryPath = Path.Combine(Path.GetDirectoryName(bundleDirectoryPath), Int64.MaxValue.ToString());
                framework.extractBundleFile("Stream:", input, tmpBundleDirectoryPath, false);

                try
                {
                    BundleImpl tmpBundleImpl = new BundleImpl(framework, tmpBundleDirectoryPath);
                    if (!this.getSymbolicName().Equals(tmpBundleImpl.getSymbolicName()))
                    {
                        throw new ArgumentException(String.Format("要更新的插件[{0}]与输入流中的插件[{1}]不匹配!", this.getSymbolicName(), tmpBundleImpl.getSymbolicName()));
                    }
                }
                catch (Exception ex)
                {
                    Directory.Delete(tmpBundleDirectoryPath, true);
                    throw ex;
                }

                // 注意,如果有要保存的文件,那么在删除代码之前将文件移动到临时插件目录中
                //先删除自身
                Directory.Delete(bundleDirectoryPath, true);
                //临时目录移动到正式目录
                Directory.Move(tmpBundleDirectoryPath, bundleDirectoryPath);
            }

            if (preState == Bundle_Const.INSTALLED)
            {
                this.init();
            }
            else if (preState == Bundle_Const.RESOLVED)
            {
                this.init();
                this.resolve();
            }
            else if (preState == Bundle_Const.ACTIVE)
            {
                this.stop();
                this.init();
                this.resolve();
                this.start();
            }
        }
Esempio n. 4
0
        private void loadRequiredBundle()
        {
            //加载Required-Bundle中的所有程序集
            foreach (String tmpStr in this.requireBundles.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                String requireBundleString = tmpStr.Trim();
                if (String.IsNullOrEmpty(requireBundleString))
                {
                    continue;
                }
                String requireBundle = null;
                String requireBundleVersionString = null;

                String[] requireBundleStringArray = requireBundleString.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                requireBundle = requireBundleStringArray[0].Trim();
                IDictionary <String, String> otherDict = new Dictionary <String, String>();
                for (int i = 1; i < requireBundleStringArray.Length; i++)
                {
                    String   requireBundleStringPart            = requireBundleStringArray[i];
                    String[] requireBundleStringPartStringArray = requireBundleStringPart.Split(new Char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    otherDict.Add(requireBundleStringPartStringArray[0].Trim(), requireBundleStringPartStringArray[1].Trim());
                }
                //得到需要的版本信息
                if (otherDict.ContainsKey(BUNDLE_MANIFEST_REQUIRED_BUNDLE_VERSION))
                {
                    requireBundleVersionString = otherDict[BUNDLE_MANIFEST_REQUIRED_BUNDLE_VERSION];
                    requireBundleVersionString = requireBundleVersionString.Replace("\"", "");
                }

                BundleImpl        matchBundle     = null;
                List <BundleImpl> matchBundleList = new List <BundleImpl>();
                foreach (Bundle bundle in this.getBundleContext().getBundles())
                {
                    if (bundle.getBundleId() == 0 || bundle.getBundleId() == this.getBundleId())
                    {
                        continue;
                    }
                    BundleImpl tmpBundle = bundle as BundleImpl;

                    if (requireBundle.Equals(tmpBundle.getSymbolicName()))
                    {
                        if (String.IsNullOrEmpty(requireBundleVersionString))
                        {
                            matchBundle = tmpBundle;
                            break;
                        }
                        else
                        {
                            matchBundleList.Add(tmpBundle);
                        }
                    }
                }
                if (matchBundle == null && !String.IsNullOrEmpty(requireBundleVersionString))
                {
                    Version requireBundleVersion = new Version(requireBundleVersionString);

                    matchBundleList.Sort(new Comparison <BundleImpl>(delegate(BundleImpl x, BundleImpl y)
                    {
                        return(x.getVersion().CompareTo(y.getVersion()));
                    }));
                    foreach (BundleImpl tmpBundle in matchBundleList)
                    {
                        if (tmpBundle.getVersion().CompareTo(requireBundleVersion) >= 0)
                        {
                            matchBundle = tmpBundle;
                            break;
                        }
                    }
                }
                if (matchBundle == null)
                {
                    continue;
                }
                //如果此Bundle没有解析,则解析此Bundle
                if (matchBundle.getState() != Bundle_Const.RESOLVED &&
                    matchBundle.getState() != Bundle_Const.ACTIVE)
                {
                    matchBundle.resolve();
                }
                requiredBundleList.Add(matchBundle);
            }
        }