---
title: "osgi gemini blueprint环境"
author: "conkeyn"
date: "2014-09-05T20:46:17Z"
tags: []
categories: ["编程语言"]
slug: "osgi-gemini-blueprint"
draft: false
lastmod: "2014-09-11T09:54:09Z"
id: 2113824
url: "https://conkeyn.iteye.com/blog/2113824"
---

**Gemini Blueprint应用**

 

 

# 1.  Gemini Blueprint环境搭建

 

## []()1.0.  Gemini blueprint环境下载

个人建议下载virgo环境，而不是下载Gemini web开发包。

 [http://www.eclipse.org/downloads/download.php?file=/virgo/release/VP/3.6.3.RELEASE/virgo-tomcat-server-3.6.3.RELEASE.zip&mirror\_id=1071](http://www.eclipse.org/downloads/download.php?file=%2Fvirgo%2Frelease%2FVP%2F3.6.3.RELEASE%2Fvirgo-tomcat-server-3.6.3.RELEASE.zip&mirror_id=1071)

 

## []()1.1.  搭建Gemini Server

步骤1：

![](http://dl2.iteye.com/upload/attachment/0101/0206/4339611f-199c-3782-95fc-64c55f7a1271.png)

步骤2：

 ![](http://dl2.iteye.com/upload/attachment/0101/0208/75b7720b-387e-3fcc-ace6-fd561bb1f8f7.png)

 

步骤3：

![](http://dl2.iteye.com/upload/attachment/0101/0210/bda2a846-7ac5-343f-b5f0-df1b17b959ec.png)

 

步骤4：

![](http://dl2.iteye.com/upload/attachment/0101/0214/ba7f33f7-49b0-377f-9202-e7c2dd1334f8.png)

步骤5：

![](http://dl2.iteye.com/upload/attachment/0101/0214/ba7f33f7-49b0-377f-9202-e7c2dd1334f8.png)

步骤6，添加运行环境的目录

![](http://dl2.iteye.com/upload/attachment/0101/0216/a3c2a3f6-102a-3cef-8ca9-1ebeef50841a.png)

步骤7：

![](http://dl2.iteye.com/upload/attachment/0101/0218/b169efb5-c29c-315f-9f7d-ce27c3c4ce6e.png)

步骤8：下面这几个包需要手动选择添加。

![](http://dl2.iteye.com/upload/attachment/0101/0220/b74fc3da-5e9c-3042-a2c0-72591c1f808f.png)

 

## 1.2.  配置plug-in development

激活目标平台。

步骤1：打开window-&gt;preferences-&gt;Plugin-in Development-&gt;Target Platform

![](http://dl2.iteye.com/upload/attachment/0101/0222/3684237b-f5be-358a-a818-dd206c983c50.png)

步骤2：选中目标平台，然后按下“Reload”,最后点击“Apply”和”OK”

![](http://dl2.iteye.com/upload/attachment/0101/0224/a95ec05a-8eb4-3e36-85ea-eb4ae18140e2.png)

# 2.  Gemini HelloWorld

 

## 2.1.  HelloWorld Service

**步骤1：**创建两个Plug-in项目，项目名称分别为：springdm\_helloworld和springdm\_client。

![](http://dl2.iteye.com/upload/attachment/0101/0226/f7530e48-fefb-3d3c-978a-a870715334c9.png)

**步骤2：**展开两个项目树如下图

![](http://dl2.iteye.com/upload/attachment/0101/0228/7b1c0592-64e5-3468-b7d8-7dadb9881328.png)

 

**步骤3：**添加HelloWorld.java代码

**package** springdm.helloworld;

 

**publicinterface** HelloWorld

{

    String sayHello(String name);

}

 

 

**步骤4：**添加HelloWorld的实现代码

**package** springdm.helloworld;

 

**publicclass** HelloWorldImpl **implements** HelloWorld

{

 

    @Override

    **public** String sayHello(String name)

    {

       **return**"Hello "+name;

    }

 

}

 

**步骤5：**

![](http://dl2.iteye.com/upload/attachment/0101/0230/3c56b01f-752e-39c6-8265-b83dea652416.png)

**步骤6：**

![](http://dl2.iteye.com/upload/attachment/0101/0232/f63b495f-ec57-37d8-adb2-66ca49f2d70c.png)

**步骤7：**在springdm\_helloworld/META-INF目录下创建spring目录（如步骤2展示的项目树结构），并在spring目录下创建两个XML文件，分别是：helloworld-spring-osgi.xml和helloworld-spring.xml。内容分别为：

 

**helloworld-spring-osgi.xml**

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;beans xmlns=*"http://www.springframework.org/schema/beans"*

    xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"* xmlns:osgi=*"http://www.springframework.org/schema/osgi"*

    xsi:schemaLocation=*"*

  *http://www.springframework.org/schema/beans*

  *http://www.springframework.org/schema/beans/spring-beans.xsd*

  *http://www.springframework.org/schema/osgi*

  *http://www.springframework.org/schema/osgi/spring-osgi.xsd"*&gt;

    &lt;osgi:service interface=*"springdm.helloworld.HelloWorld"* ref=*"helloWordService"*  /&gt;

&lt;/beans&gt;

 

**helloworld-spring.xml**

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;beans xmlns=*"http://www.springframework.org/schema/beans"*

    xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"* xmlns:osgi=*"http://www.springframework.org/schema/osgi"*

    xsi:schemaLocation=*"*

  *http://www.springframework.org/schema/beans*

  *http://www.springframework.org/schema/beans/spring-beans.xsd*

  *http://www.springframework.org/schema/osgi*

  *http://www.springframework.org/schema/osgi/spring-osgi.xsd"*&gt;

    &lt;bean id=*"helloWordService"* class=*"springdm.helloworld.HelloWorldImpl"* /&gt;

&lt;/beans&gt;

 

**步骤8：**运行

![](http://dl2.iteye.com/upload/attachment/0101/0234/3691d4eb-63d6-338c-aa0c-15c0c5f5fb90.png)

 

**步骤9：**配置Plugin-in运行环境，此步骤与[1.1搭建Gemini Server](#_%E6%90%AD%E5%BB%BAGemini_Server)环境类似。

**步骤10**：在osgi&gt;命令环境输入“ss”，列表出当前OSGi运行环境中的plug-in包。可以看到plug-in id为56和57的已经是ACTIVE状态。

**osgi&gt; ss**

"Framework is launched."

 

 

id  State       Bundle

0   ACTIVE      org.eclipse.osgi\_3.9.1.v20140110-1610

                Fragments=23, 28

………

 

55  ACTIVE      javax.servlet.jsp\_2.2.0.v201112011158

56  ACTIVE      springdm\_client\_1.0.0.qualifier

57  ACTIVE      springdm\_helloworld\_1.0.0.qualifier

 

**步骤11：**继续输入 osgi&gt; b 57和osgi&gt; b 56，显示plug-in的详细信息。要能看到如下图显示出来的结果。通过gemini blueprint注册的服务。

![](http://dl2.iteye.com/upload/attachment/0101/0236/9621c2dc-654d-3baa-bb63-5deca9fa11d3.png)

## 2.2.  HelloWorld Client

前面某些步骤是相似的，就不再赘述了。下列步骤列出一些不同点。

**步骤1：**添加依赖springdm.helloworld

![](http://dl2.iteye.com/upload/attachment/0101/0238/0e554211-19e3-3a56-bed3-322e068f793b.png)

**步骤2：**添加Client代码

**package** springdm\_client;

 

**import** org.osgi.framework.Bundle;

 

**import** springdm.helloworld.HelloWorld;

 

**publicclass** Client

{

    **private** HelloWorld helloWorld;

   

    **private** Bundle hw;

   

    **publicvoid** init(){

       System.*out*.println(helloWorld.sayHello("client"));

       System.*out*.println(hw.getHeaders().toString());

    }

 

    **public** HelloWorld getHelloWorld()

    {

       **return**helloWorld;

    }

 

    **publicvoid** setHelloWorld(HelloWorld helloWorld)

    {

       **this**.helloWorld = helloWorld;

    }

 

    **publicvoid** setHw(Bundle hw)

    {

       **this**.hw = hw;

    }

}

 

**步骤3：**META-INF/spring目录下的文件内容

 

**client-spring-osgi.xml**

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;beans xmlns=*"http://www.springframework.org/schema/beans"*

    xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"*

    xmlns:osgi=*"http://www.springframework.org/schema/osgi"*

    xsi:schemaLocation=*"*

  *http://www.springframework.org/schema/beans*

  *http://www.springframework.org/schema/beans/spring-beans.xsd*

  *http://www.springframework.org/schema/osgi*

  *http://www.springframework.org/schema/osgi/spring-osgi.xsd"*&gt;

    &lt;osgi:reference id=*"hello"* interface=*"springdm.helloworld.HelloWorld"* /&gt;

    &lt;osgi:bundle id=*"hw"* symbolic-name=*"org.eclipse.gemini.blueprint.extender"*&gt;&lt;/osgi:bundle&gt;

&lt;/beans&gt;

 

**client-spring.xml**

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;beans xmlns=*"http://www.springframework.org/schema/beans"*

    xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"*

    xmlns:osgi=*"http://www.springframework.org/schema/osgi"*

    xsi:schemaLocation=*"*

  *http://www.springframework.org/schema/beans*

  *http://www.springframework.org/schema/beans/spring-beans.xsd*

  *http://www.springframework.org/schema/osgi*

  *http://www.springframework.org/schema/osgi/spring-osgi.xsd"*&gt;

    &lt;bean id=*"client"* class=*"springdm\_client.Client"* init-method=*"init"*&gt;

        &lt;property name=*"helloWorld"* ref=*"hello"* /&gt;

        &lt;property name=*"hw"* ref=*"hw"* /&gt;

    &lt;/bean&gt;

&lt;/beans&gt;

 

**步骤4：**需要将两个包勾起来，在同一个环境中运行。

![](http://dl2.iteye.com/upload/attachment/0101/0240/1cdddaec-70de-3c65-9ac4-0285b1cfcaa5.png)

 

**控制台结果：**

osgi&gt; Hello client

[Ljava.lang.Object;@1b32627

 

# 3.  Gemini WEB 应用