**搭建osgi web 开发环境**

搭建osgi web 开发环境

  1. 开发环境
    java1.8
    myeclipse Spring2014

  2. 打开myeclipse的运行配置界面 选择 osgi framework 并在目标平台勾选以下bundle
    1.org.eclipse.osgi
    2.org.eclipse.equinox.console
    3.org.apache.felix.gogo.command
    4.org.apache.felix.gogo.runtime
    5.org.apache.felix.gogo.shell
    6.Javax.servlet
    7.Org.apache.commons.loggin
    8.Org.eclipse.equinox.http.jetty
    9.org.eclipse.equinox.http.servlet
    10.Org.eclipse.jetty.http
    11.Org.eclipse.jetty.io
    12.Org.eclipse.jetty.util
    13.Org.eclipse.jetty.server
    14.Org.eclipse.jetty.continuation
    15.Org.eclipse.jetty.servlet
    16.Org.eclipse.jetty.security

  3. 运行OSGI的开发环境
    主要: 启动时如果出现端口号被占用了
    就在 启动配置项的 arguments选项的第二个框追加以下内容 (主要要先加个空格)
    -Dorg.osgi.service.http.port=8080
  4. 首先新建一个插件项目
    在项目中配置依赖 如下

    Import-Package: javax.servlet;version="2.6.0",
     javax.servlet.http;version="2.6.0",
     org.osgi.framework;version="1.3.0",
     org.osgi.service.http;version="1.2.1"
  5. 在src下新建page目录 在其下新建一个hello.html文件
    文件内容如下:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>demo</title>
    </head>
    <body>Hello,World!</body>
    </html>
  6. 在该项目绑定的Activator下 进行服务注册
    具体方法实现如下:

    package com.hz.example.hello.web;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    import org.osgi.service.http.HttpService;
    import org.osgi.service.http.NamespaceException;
    
    import com.hz.example.consoleCommand.BundleNameCommand;
    
    public class Activator implements BundleActivator {
    
        private ServiceReference serviceReference;
        private HttpService httpService;
        private static BundleContext bundleContext;
    
        public void start(BundleContext context) throws Exception {
            System.out.println("Hello World!!");
            this.bundleContext =  context;
            registerResource();
        }
    
        /**
         * 注册web服务
         */
        private void registerResource() {
            try {
                serviceReference = bundleContext.getServiceReference(HttpService.class.getName());
                if(serviceReference!=null){
                    httpService = (HttpService)bundleContext.getService(serviceReference);
                    //页面的请求地址为http://127.0.0.1:8080/demo/hello.html
                    httpService.registerResources("/demo", "page", null);
                }
            } catch (NamespaceException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public void stop(BundleContext context) throws Exception {
            System.out.println("Goodbye World!!");
            unregisterResource();
        }
    
        private void unregisterResource() {
            try {
                httpService.unregister("/demo");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值