欢迎使用CSDN-mrkdown编辑器a

来源:互联网 发布:php get和post的区别 编辑:程序博客网 时间:2024/06/18 00:20

搭建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
原创粉丝点击