Beispiel #1
0
        public void Init(BundleImpl bundle)
        {
            this.bundle = bundle;
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            bundleAssembly = Assembly.Load(File.ReadAllBytes(bundle.GetBundleAssemblyFileName()));
        }
Beispiel #2
0
        public string ExecuteCommand(string commandLine)
        {
            String bundleIdStr = commandLine.Substring(GetCommandName().Length).Trim();

            if (String.IsNullOrEmpty(bundleIdStr))
            {
                StringBuilder sb         = new StringBuilder();
                Assembly[]    assemblies = AppDomain.CurrentDomain.GetAssemblies();
                for (int i = 0; i < assemblies.Length; i++)
                {
                    Assembly     assembly     = assemblies[i];
                    AssemblyName assemblyName = assembly.GetName();
                    sb.Append(String.Format("[{0}]: {1}, Version:{2}", i, assemblyName.Name, assemblyName.Version));
                    sb.AppendLine();
                }
                return(sb.ToString());
            }
            else
            {
                Int64  bundleId = Int64.Parse(bundleIdStr);
                Bundle bundle   = framework.getBundleContext().getBundle(bundleId);
                if (bundle == null)
                {
                    return(String.Format("未找到ID为[{0}]的插件", bundleId));
                }
                BundleImpl bundleImpl = (BundleImpl)bundle;
                return(bundleImpl.GetBundleResovler().GetAssemblies());
            }
        }
Beispiel #3
0
        public string ExecuteCommand(string commandLine)
        {
            String bundleIdStr = commandLine.Substring(GetCommandName().Length).Trim();

            if (String.IsNullOrEmpty(bundleIdStr))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine();
                sb.AppendLine("AppDomain.CurrentDomain.GetAssemblies()");
                sb.AppendLine("-----------------------");
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                for (int i = 0; i < assemblies.Length; i++)
                {
                    Assembly     assembly     = assemblies[i];
                    AssemblyName assemblyName = assembly.GetName();
                    sb.Append(String.Format("  [{0}]: {1}, Version:{2}", i, assemblyName.Name, assemblyName.Version));
                    sb.AppendLine();
                }
                return(sb.ToString());
            }
            else
            {
                Int64  bundleId = Int64.Parse(bundleIdStr);
                Bundle bundle   = framework.getBundleContext().getBundle(bundleId);
                if (bundle == null)
                {
                    return(String.Format("未找到ID为[{0}]的插件", bundleId));
                }
                if (bundle is BundleImpl)
                {
                    BundleImpl bundleImpl = (BundleImpl)bundle;
                    return(bundleImpl.GetAssemblies());
                }
                else if (bundle is golion.launch.FrameworkImpl)
                {
                    StringBuilder sb   = new StringBuilder();
                    var           list = AssemblyLoadContext.All.ToList();
                    list.Remove(AssemblyLoadContext.Default);
                    list.Insert(0, AssemblyLoadContext.Default);
                    foreach (var context in list)
                    {
                        sb.AppendLine();
                        sb.AppendLine(context.Name);
                        sb.AppendLine("-----------------------");
                        Assembly[] assemblies = context.Assemblies.ToArray();
                        for (int i = 0; i < assemblies.Length; i++)
                        {
                            Assembly     assembly     = assemblies[i];
                            AssemblyName assemblyName = assembly.GetName();
                            sb.Append(String.Format("  [{0}]: {1}, Version:{2}", i, assemblyName.Name, assemblyName.Version));
                            sb.AppendLine();
                        }
                    }
                    return(sb.ToString());
                }
                return(null);
            }
        }
Beispiel #4
0
 internal void uninstall(BundleImpl bundleImpl)
 {
     bundleList.Remove(bundleImpl);
     Directory.Delete(bundleImpl.GetBundleDirectoryPath(), true);
 }
Beispiel #5
0
        internal Bundle installBundle(string location, Stream input)
        {
            Boolean   isInputStreamOwner;
            Exception installException = null;

            if (input == null)
            {
                if (File.Exists(location))
                {
                    input = new FileStream(location, FileMode.Open);
                }
                else
                {
                    throw new ArgumentException(String.Format("无法找到路径[{0}]对应的文件!", location));
                }
                isInputStreamOwner = true;
            }
            else
            {
                location           = "Stream:";
                isInputStreamOwner = false;
            }

            String newBundleDirectoryName = Path.Combine(bundlesDirectoryPath, getNextBundleId().ToString());

            try
            {
                if (!Directory.Exists(newBundleDirectoryName))
                {
                    Directory.CreateDirectory(newBundleDirectoryName);
                }
                //解压插件文件到插件目录
                extractBundleFile(location, input, newBundleDirectoryName, isInputStreamOwner);
                //验证并加载插件
                Bundle bundle = new BundleImpl(this, newBundleDirectoryName);
                foreach (Bundle installedBundle in bundleList)
                {
                    //如果此插件的相同版本已经安装
                    if (installedBundle.getSymbolicName().Equals(bundle.getSymbolicName()) &&
                        installedBundle.getVersion().Equals(bundle.getVersion()))
                    {
                        throw new Exception(String.Format("名称为[{0}],版本为[{1}]的插件已经存在,安装失败!", installedBundle.getSymbolicName(), installedBundle.getVersion()));
                    }
                }
                bundleList.Add(bundle);
                IncraseNextBundleId();
                return(bundle);
            }
            catch (Exception ex)
            {
                installException = ex;
                throw ex;
            }
            finally
            {
                if (installException != null && Directory.Exists(newBundleDirectoryName))
                {
                    Directory.Delete(newBundleDirectoryName, true);
                }
            }
        }
Beispiel #6
0
        public void init()
        {
            Assembly selfAssembly = typeof(FrameworkImpl).Assembly;

            BundleImpl.AddBootDelegationAssembly(selfAssembly.GetName().Name, selfAssembly);

            // 初始化配置参数
            if (configuration != null)
            {
                //设置Bundle缓存位置
                if (configuration.ContainsKey(ORG_OSGI_FRAMEWORK_STORAGE))
                {
                    bundlesDirectoryPath = configuration[ORG_OSGI_FRAMEWORK_STORAGE];
                }
                //设置boot delegation
                if (configuration.ContainsKey(ORG_OSGI_FRAMEWORK_BOOTDELEGATION))
                {
                    String[] bootDelegationNames = configuration[ORG_OSGI_FRAMEWORK_BOOTDELEGATION].Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        String assemblyName = assembly.GetName().Name;
                        foreach (String bootDelegationName in bootDelegationNames)
                        {
                            if (assemblyName.Equals(bootDelegationName))
                            {
                                BundleImpl.AddBootDelegationAssembly(assemblyName, assembly);
                            }
                        }
                    }
                }
                //设置是否使用AppDomain隔离各Bundle
                if (configuration.ContainsKey(GOLION_USE_APPDOMAIN))
                {
                    _IsUseAppDomain = Boolean.Parse(configuration[GOLION_USE_APPDOMAIN]);
                }
            }
            // 设置插件目录
            if (String.IsNullOrEmpty(bundlesDirectoryPath))
            {
                bundlesDirectoryPath = System.IO.Path.Combine(Environment.CurrentDirectory, DEFAULT_BUNDLE_DIRECTORY_NAME);
            }
            //先将自己添加上去
            bundleList.Add(this);

            IList <String> installedBundleNameVersionList = new List <String>();

            if (Directory.Exists(bundlesDirectoryPath))
            {
                //读取已经安装的Bundle
                DirectoryInfo di = new DirectoryInfo(bundlesDirectoryPath);
                List <Int64>  installedBundleIdList = new List <Int64>();
                foreach (DirectoryInfo subDi in di.GetDirectories())
                {
                    Int64 tmpInt64;
                    if (!Int64.TryParse(subDi.Name, out tmpInt64))
                    {
                        continue;
                    }
                    installedBundleIdList.Add(tmpInt64);
                }
                installedBundleIdList.Sort();
                foreach (Int64 bundleId in installedBundleIdList)
                {
                    String bundleDirectoryName = Path.Combine(bundlesDirectoryPath, bundleId.ToString());
                    try
                    {
                        Bundle bundle = new BundleImpl(this, bundleDirectoryName);
                        String bundleNameVersionString = String.Format("{0}({1}))", bundle.getSymbolicName(), bundle.getVersion());
                        if (installedBundleNameVersionList.Contains(bundleNameVersionString))
                        {
                            log.Warn(String.Format("插件[{0})]存在多重安装,只加载第一个!", bundleNameVersionString));
                            continue;
                        }
                        bundleList.Add(bundle);
                        installedBundleNameVersionList.Add(bundleNameVersionString);
                    }
                    catch (Exception ex)
                    {
                        log.Error("加载Bundle时出现异常!", ex);
                    }
                }
                nextBundleId = bundleList[bundleList.Count - 1].getBundleId() + 1;
            }
            else
            {
                Directory.CreateDirectory(bundlesDirectoryPath);
                nextBundleId = 1;
            }
        }