Virgo与Maven整合开发环境搭建(二)

来源:互联网 发布:sql判断是否为空 编辑:程序博客网 时间:2024/06/08 01:00

 二、开发集成。

                  配置好Maven和Virgo后,我们动手写一个demo。demo的场景是页面有一个搜索框,输入搜索条件,显示出匹配项。为了体现OSGI的特性,我们搜索内容分为图片和MP3两个bundle,他们拥有共同的接口。bundle依赖关系如下:

                     host和web两个bundle是web包,其他的是应用包。

                     说到包的拆分,或者模块的拆分,我这里稍微卖弄一下,大神勿喷!看到我的包依赖结构,有人也许会问,OSGI不就是模块化吗?不应该是一个模块一个包吗? 假如我有user和role两个模块,那就应该有两个包啊。如果这么认为那就错了,我只能说你还没有理解OSGI提出的面向服务的架构体系。页面只是表现 层,页面-controller-service-dao,这是传统的分层,将应用按功能垂直的拆分。在OSGI领域中,提出另一个概念,就是模块化,模 块化要求bundle和bundle之间隔离,一个bundle是一个物理单元,通过服务交互。那这里的服务应该怎么体现呢?通过接口。接口怎么设计?什 么地方应该放扩展点?是在做OSGI应用设计的时候要考虑的。demo中,页面输入关键字来搜索,用户不知道能搜到什么结果,结果在服务端进行控制。服务 端如何控制结果?就是留下一个扩展点。现在能搜MP3和图片,过段时间,业务扩展后,能搜人和新闻.......所以,这就是为什么MP3和图片分成不同 的bundle进行开发的原因。OSGI的另外一个特性就是动态性,插件机制,即插即用,即删即无。在程序运行的时候,前一分钟只能搜MP3和图片,服务 端动态启动一个搜新闻的实现,后一分钟就能搜到新闻了。这就是OSGI的魔力所在。刚接触的时候会感觉很麻烦,感觉带来了复杂度。等你真正熟悉后,在合适 的项目中,发挥它的最大优势。

                   

                    1.host

                             首先我们来开发host。在eclipse中创建Maven Project。Archetype选择为quickstart。host比较简单,没有java代码,最多我们放jQuery进去。一般做法是,全局的资源文件放在host中,供其他bundle调用。原则上,bundle之间是物理隔离的。

 
 

                           没有太多内容很简单,重点是pom中的打包规则和等下要介绍的bundlor插件.以及templat.mf文件的用处

                           在OSGI中,都是jar包,即使是web应用。所以将host工程打包成OSGI中的bundle,就需要在pom中需要定义打包规则: 

 

Xml代码  收藏代码
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>               
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-compiler-plugin</artifactId>  
  7.                 <version>2.3.2</version>  
  8.                 <configuration>  
  9.                     <source>1.6</source>  
  10.                     <target>1.6</target>  
  11.                 </configuration>  
  12.             </plugin>  
  13.   
  14.             <plugin>  
  15.                 <groupId>org.apache.maven.plugins</groupId>  
  16.                 <artifactId>maven-resources-plugin</artifactId>  
  17.                 <version>2.5</version>  
  18.                 <executions>  
  19.                     <execution>  
  20.                         <id>copy-resources</id>  
  21.                         <phase>prepare-package</phase>  
  22.                         <goals>  
  23.                             <goal>copy-resources</goal>  
  24.                         </goals>  
  25.                         <configuration>  
  26.                             <overwrite>true</overwrite>  
  27.                             <outputDirectory>${project.build.outputDirectory}/WEB-INF/classes</outputDirectory>  
  28.                         <resources>  
  29.                             <resource>  
  30.                                 <directory>${project.build.outputDirectory}</directory>  
  31.                                 <includes>  
  32.                                     <include>**/*.class</include>  
  33.                                 </includes>  
  34.                             </resource>  
  35.                         </resources>  
  36.                     </configuration>  
  37.             </execution>  
  38.     </executions>  
  39.      </plugin>  
  40.   
  41.               
  42.             <plugin>  
  43.                 <groupId>org.apache.maven.plugins</groupId>  
  44.                 <artifactId>maven-surefire-plugin</artifactId>  
  45.                 <version>2.9</version>  
  46.             </plugin>  
  47.               
  48.         <plugin>  
  49.             <groupId>org.eclipse.virgo.bundlor</groupId>  
  50.             <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>  
  51.             <version>1.1.1.RELEASE</version>  
  52.             <executions>  
  53.                 <execution>  
  54.                     <id>bundlor</id>  
  55.                     <goals>  
  56.                         <goal>bundlor</goal>  
  57.                     </goals>  
  58.                     <configuration>  
  59.                         <OSGiProfilePath>./virgo.profile</OSGiProfilePath>  
  60.                     </configuration>  
  61.                 </execution>  
  62.             </executions>  
  63.         </plugin>  
  64.           
  65.         <plugin>  
  66.             <groupId>org.apache.maven.plugins</groupId>  
  67.             <artifactId>maven-source-plugin</artifactId>  
  68.             <version>2.1.2</version>  
  69.             <executions>  
  70.                     <execution>  
  71.                     <id>attach-sources</id>  
  72.                     <phase>verify</phase>  
  73.                     <goals>  
  74.                             <goal>jar-no-fork</goal>  
  75.                     </goals>  
  76.                     </execution>  
  77.             </executions>  
  78.             </plugin>  
  79.               
  80.             <plugin>  
  81.             <groupId>org.apache.maven.plugins</groupId>  
  82.             <artifactId>maven-jar-plugin</artifactId>  
  83.             <configuration>  
  84.                 <includes>  
  85.                     <include>WEB-INF/**/*</include>  
  86.                     <include>META-INF/**/*</include>  
  87.                     <include>resource*/**/*</include>  
  88.                     <include>**/*.html</include>  
  89.                     <include>**/*.js</include>  
  90.                     <include>**/*.css</include>  
  91.                     <include>**/*image*/**/*</include>  
  92.                 </includes>  
  93.                 <archive>  
  94.                     <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>  
  95.                     </archive>  
  96.             </configuration>  
  97.             <version>2.4</version>  
  98.         </plugin>  
  99.         </plugins>  
  100.     </pluginManagement>  
  101.       
  102.     <plugins>  
  103.             <plugin>  
  104.             <groupId>org.apache.maven.plugins</groupId>  
  105.             <artifactId>maven-jar-plugin</artifactId>  
  106.         </plugin>  
  107.         <plugin>  
  108.             <groupId>org.eclipse.virgo.bundlor</groupId>  
  109.             <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>  
  110.         </plugin>  
  111.         <plugin>  
  112.             <groupId>org.apache.maven.plugins</groupId>  
  113.             <artifactId>maven-compiler-plugin</artifactId>  
  114.         </plugin>  
  115.         <plugin>  
  116.             <groupId>org.apache.maven.plugins</groupId>  
  117.             <artifactId>maven-surefire-plugin</artifactId>  
  118.         </plugin>  
  119.         <plugin>  
  120.             <groupId>org.apache.maven.plugins</groupId>  
  121.             <artifactId>maven-source-plugin</artifactId>  
  122.             </plugin>  
  123.         <plugin>  
  124.             <groupId>org.apache.maven.plugins</groupId>  
  125.             <artifactId>maven-resources-plugin</artifactId>  
  126.         </plugin>  
  127.     </plugins>  
  128. </build>  

                    可以将这个规则做成一个maven父工程,其他用作web的bundle都可以继承这个parent。

 

                    然后来看一下template.mf:

 

Xml代码  收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: search demo  
  4. Bundle-SymbolicName: org.phantom.demo.host  
  5. Bundle-Version: 1.0.0.SNAPSHOT  
  6. Web-ContextPath: /demo  
  7. Import-Template: org.eclipse.virgo.*;version="[3.5.0,4)"  
  8. Excluded-Exports: resources.*  

                    这里要说明的是Import-Template,Excluded-Exports,Excluded-Import这些东西。这些不是OSGI原生的东西,是Virgo特有的描述符。说到这里不得不介绍介绍一下org.eclipse.virgo.bundlor.maven这个插件。这个插件在打包时,会扫描所有java类、spring配置文件、web.xml的其他一些特定文件。然后结合template.mf文件,最终生成META- INF/MANIFEST.MF。假如你在java类中import了其他bundle的东西:import org.apache.ibatis.session.SqlSession;而你不用再去template.mf中手动维护Import- Package,bundlor会扫描到,在生成的时候,会在Import-Package中写入:Import-Package: org.apache.ibatis.session。另外还有个特性,bundlor会检查Import-Template,Excluded- Exports,Excluded-Import,根据描述项,对扫描到的依赖项进行填充。假设,你在template.mf中写到Import- Template: org.springframework.*;version="[3.5.0,4)",你在开发时中使用了spring的东西比如 DispatcherServlet,那么在最终生成的MANIFEST.MF中,就会有Import-Package: org.springframework.web.servlet;version=3.5.0。

                    我们知道,在进行OSGI开发时,最麻烦也是最容易出问题的地方,一个是ClassLoad的问题,还有一个就是bundle之间的依赖关系。所以,bundlor插件结合template.mf文件给我们帮了很大忙,我们不需要很费时费力的手动维护bundle之间的依赖关系,有时候甚至都不用去关心,因为bundlor插件会去扫描-生成。
                    有一句配置是Web-ContextPath: /demo.这就是我们整个应用的上下文.

                    最后看一下web.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     version="2.5">  
  6.   
  7.     <display-name>Search Host</display-name>  
  8.   
  9.     <filter>  
  10.         <filter-name>host-filter</filter-name>  
  11.         <filter-class>org.eclipse.virgo.snaps.core.SnapHostFilter</filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>host-filter</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.         <dispatcher>INCLUDE</dispatcher>  
  18.         <dispatcher>FORWARD</dispatcher>  
  19.         <dispatcher>REQUEST</dispatcher>  
  20.     </filter-mapping>  
  21.   
  22.     <filter>  
  23.         <filter-name>encodingFilter</filter-name>  
  24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  25.         <init-param>  
  26.             <param-name>encoding</param-name>  
  27.             <param-value>UTF-8</param-value>  
  28.         </init-param>  
  29.     </filter>  
  30.     <filter-mapping>  
  31.         <filter-name>encodingFilter</filter-name>  
  32.         <url-pattern>/*</url-pattern>  
  33.     </filter-mapping>  
  34. </web-app>  

                        这里需要介绍一下SnapHostFilter.这是Virgo对WAB(web application bundle)做的支持.我们在做OSGI-Web开始时,一般有很多模块.比如用户(user),角色(role).那么我们要请求用户或者角色下的资源时,我们的URL就是http://localhost:8080/demo/user/xxx.html或者http://localhost:8080/demo/role/xxx.html。这里的demo就上前面的配置“Web-ContextPath: /demo”,/demo是第一级路径,/user第二级路径是,即我们的"Snap-ContextPath: /search",这个后续的篇章会介绍./demo由web容器去分发,当tomcat接受到请求后,根据第一级路径决定分发到哪个应用。当进到某个应用后,SnapHostFilter会根据第二级路径决定分发到哪个bundle中。这就是SnapHostFilter的作用了。

                   

                   2.api

                       OK,host已经完成,我们来开发api这个包.api中放两个接口,先来看一下结构.


                        SearchBean做为标识接口,没有任何代码.因为我们的接口不知道具体返回的MP3或者图片中包含什么属性.所以这里不定义任何属性.

                        SearchHandler只有简单的一个方法:

Java代码  收藏代码
  1. package org.phantom.demo.api;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface SearchHandler {  
  6.     List<? extends SearchBean> doSearch(String key);  
  7. }  

                      然后来看应用类型的bundle应该如何打包.它也有自己的打包规则,而且跟web类的bundle不太一样,因为没有页面文件哪些东西。

Xml代码  收藏代码
  1. <build>  
  2.     <pluginManagement>  
  3.         <plugins>               
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-compiler-plugin</artifactId>  
  7.                 <version>2.3.2</version>  
  8.                 <configuration>  
  9.                     <source>1.6</source>  
  10.                     <target>1.6</target>  
  11.                 </configuration>  
  12.             </plugin>  
  13.               
  14.             <plugin>  
  15.                 <groupId>org.apache.maven.plugins</groupId>  
  16.                 <artifactId>maven-surefire-plugin</artifactId>  
  17.                 <version>2.9</version>  
  18.             </plugin>  
  19.               
  20.         <plugin>  
  21.             <groupId>org.eclipse.virgo.bundlor</groupId>  
  22.             <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>  
  23.             <version>1.1.1.RELEASE</version>  
  24.             <executions>  
  25.                 <execution>  
  26.                     <id>bundlor</id>  
  27.                     <goals>  
  28.                         <goal>bundlor</goal>  
  29.                     </goals>  
  30.                     <configuration>  
  31.                         <OSGiProfilePath>./virgo.profile</OSGiProfilePath>  
  32.                     </configuration>  
  33.                 </execution>  
  34.             </executions>  
  35.         </plugin>  
  36.           
  37.         <plugin>  
  38.             <groupId>org.apache.maven.plugins</groupId>  
  39.             <artifactId>maven-source-plugin</artifactId>  
  40.             <version>2.1.2</version>  
  41.             <executions>  
  42.                     <execution>  
  43.                     <id>attach-sources</id>  
  44.                     <phase>verify</phase>  
  45.                     <goals>  
  46.                             <goal>jar-no-fork</goal>  
  47.                     </goals>  
  48.                     </execution>  
  49.             </executions>  
  50.             </plugin>  
  51.               
  52.             <plugin>  
  53.             <groupId>org.apache.maven.plugins</groupId>  
  54.             <artifactId>maven-jar-plugin</artifactId>  
  55.             <configuration>  
  56.                 <archive>  
  57.                     <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>  
  58.                     </archive>  
  59.             </configuration>  
  60.             <version>2.4</version>  
  61.         </plugin>  
  62.         </plugins>  
  63.     </pluginManagement>  
  64.       
  65.     <plugins>  
  66.             <plugin>  
  67.             <groupId>org.apache.maven.plugins</groupId>  
  68.             <artifactId>maven-jar-plugin</artifactId>  
  69.         </plugin>  
  70.         <plugin>  
  71.             <groupId>org.eclipse.virgo.bundlor</groupId>  
  72.             <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>  
  73.         </plugin>  
  74.         <plugin>  
  75.             <groupId>org.apache.maven.plugins</groupId>  
  76.             <artifactId>maven-compiler-plugin</artifactId>  
  77.         </plugin>  
  78.         <plugin>  
  79.             <groupId>org.apache.maven.plugins</groupId>  
  80.             <artifactId>maven-surefire-plugin</artifactId>  
  81.         </plugin>  
  82.         <plugin>  
  83.             <groupId>org.apache.maven.plugins</groupId>  
  84.             <artifactId>maven-source-plugin</artifactId>  
  85.             </plugin>  
  86.         <plugin>  
  87.             <groupId>org.apache.maven.plugins</groupId>  
  88.             <artifactId>maven-resources-plugin</artifactId>  
  89.         </plugin>  
  90.     </plugins>  
  91. </build>  

                          相比较web-bundle的打包要简单一些。仍然可以做成一个parent项目,所有的应用类bundle去继承就OK了。

0 0
原创粉丝点击