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); } } }
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; } }