OSGI(二)加载Bundle

11 篇文章 1 订阅
           

   上篇博客,我们简单介绍了OSGI的理论,下面我们看看如何使用。

     第一个问题,我们为什么使用OSGIOSGI带来了什么?

     需求实现方面,OSGI为动态扩充、修改系统功能和改变系统行为提供了支撑,而在传统的开发方式下,要实现系统的动态扩充、修改以及改变是一件很麻烦的事。

      从技术角度方面,OSGI带来了规范化的模块组织以及统一的开发方式,这为传统的模块的组织、模块开发以及模块积累提供了一种全新的指导以及支撑,面向服务的组件模型设计思想、高效系统设计思想。

 

      第二个问题,如何使用?

       首先安装框架SDK,安装SDK后创建新项目时会出现iopenWork模板,创建iopenworks下的应用程序:


        Program类中,写开始代码,初始化窗体,程序由此开始;

 static void Main()
        {
           // UpdateCore();
            RunApplication();
        }
  
 private static void RunApplication()
        {
            
            //显示开放工厂为您提供。。。。。。
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            const string PAGE_NAME_LOGIN = "LoginPage";
            using (BundleRuntime bundleRuntime = new BundleRuntime())
            {
                //运行时启动
                bundleRuntime.Start();

                // 开启服务
                IPageFlowService pageFlowService = bundleRuntime.GetFirstOrDefaultService<IPageFlowService>();
                //判断服务是否启动
                if (pageFlowService == null)
                {
                    throw new Exception(Resources.IPageFlowServiceServiceNotFound);
                }

                // Assert the first page node.
                if (string.IsNullOrEmpty(pageFlowService.FirstPageNodeValue) ||
                    string.IsNullOrEmpty(pageFlowService.FirstPageNodeName) ||
                    pageFlowService.FirstPageNodeOwner == null)
                {
                    throw new Exception(Resources.CanNotFindAnAvailablePageNode);
                }

                // Load the form type of first page node.
                Type formType = pageFlowService.FirstPageNodeOwner.LoadClass(pageFlowService.FirstPageNodeValue);
                if (formType == null)
                {
                    throw new Exception(string.Format(Resources.CanNotLoadTypeFromBundle, pageFlowService.FirstPageNodeValue, pageFlowService.FirstPageNodeOwner.SymbolicName));
                }

                // Create the form instance of first page node and show it.
                Form formInstance = System.Activator.CreateInstance(formType) as Form;

                if (formInstance == null)
                {
                    throw new Exception(string.Format(Resources.TypeIsNotWindowsFormType, pageFlowService.FirstPageNodeValue, pageFlowService.FirstPageNodeOwner.SymbolicName));
                }

                // If first page node name is 'Login' then, show the dialog and then run application.
                if (pageFlowService.FirstPageNodeName.Equals(PAGE_NAME_LOGIN))
                {
                    DialogResult result = formInstance.ShowDialog();
                    if (result == DialogResult.OK || result == DialogResult.Yes)
                    {
                        PageNode nextNode = pageFlowService.GetNextPageNode(pageFlowService.FirstPageNodeName);
                        if (nextNode != null)
                        {
                            Type mainPageFormType = nextNode.Bundle.LoadClass(nextNode.Value);
                            if (mainPageFormType == null)
                            {
                                throw new Exception(string.Format(Resources.CanNotLoadTypeFromBundle, nextNode.Value, nextNode.Bundle.SymbolicName));
                            }
                            Form mainPageForm = System.Activator.CreateInstance(mainPageFormType) as Form;
                            Application.Run(mainPageForm);
                        }
                    }
                }
                else // Run the application directly.
                {
                    //执行,显示界面
                    Application.Run(formInstance);
                }
            }
        }

           添加必要的引用和 resource 文件;程序即可运行,界面如下:

    

    如果需要扩充功能,我们只需要将插件放在主程序的bin-plugins下,重新运行软件即可,如下:

    

运行效果如下:


   

          我们添加的Plugin就进去了,是不是很灵活呢?如果我们又很多的plugins,我们可以像搭积木一样,将这些累加使用,效率是不是高了很多呢?

        那编辑插件难不难呢?如何编辑自己的插件呢?不用担心,iopenwork做的很好,自带了发布插件功能,我们只要将自己做好的插件发布即可。有了这样的功能给我们带来了很多的好处,比如:

       可插拔的系统,基于OSGI的系统,可通过安装新的Bundle 、更新或停止现有的Bundle来实现系统功能的插拔。高效,可以将别人写好的插件拿过来直接用,省去了开发,节省了时间和成本,而且即插即用;如果有大量的插件的积累,那么会更高效;

       基于OSGI的系统采用的是微核机制,微核机制保证了系统的稳定性,微核机制的系统只要微核是稳定运行的,那么系统就不会崩溃,也就是说基于OSGI的系统不会受到运行在其中的Bundle的影响,不会因为 Bundle的崩溃而导致整个系统的崩溃。即使某一个插件崩溃了,不会影响整个系统的使用,系统其他部分仍然可以正常运行;

        

        下篇博客中,我们将介绍如何加载插件,加载需要注意的地方,不足之处敬请指正!

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值