Liferay7 BPM门户开发之38: OSGi Bndtools开发入门

前言

OSGi是目前动态模块系统的事实上的工业标准,它适用于任何需要模块化、面向服务、面向组件的应用程序。Eclipse如此庞大和复杂的插件体系,就是基于OSGi。Liferay也是基于OSGi。
OSGi是模块化设计的一种标准,适用于大中型项目。在一些小型项目中,开发者也可以自己设计私有的classLoader机制来实现插件开发环境,比如openfire

OSGi是在Java平台上开发模块化应用程序的一种方式。它允许你构建模块(称之为bundle),它们彼此之间是隔离的,具备明确的和可管理的依赖。
我们的关注点在于依赖,比如说特定的bundle能不能安装到我们的环境之中,是不是还要添加额外的依赖,它对其他模块的影响是什么等等。

OSGi长期以来的工具支持比较糟糕,OSGi本来是轻量级的技术,但因为难以使用而广受诟病,Bndtools是快速开发OSGi bundle和应用的IDE,用于快速的编码/运行/测试。它貌似只支持Eclipse,还不支持其他IDE,IntelliJ有Osmorc 项目,NetBeans会使用Maven Bundle Plugin

Bndtools是由Peter Kriens (OSGi联盟的技术总监)开发的,由Neil Bartlett维护。BndTools希望你可以使用OSGi声明式服务以POJO样式编写组件,而不依赖于OSGi。这样,这些组件就可以在OSGi之外进行单元测试,并在非OSGi的生产环境——像Sprint或JavaEE——中使用。

BndTools的“本地”构建系统是bnd本身,它支持Apache Ant同时构建多个项目,这也是OSGi联盟构建他们的1300多个bundle的方式。Bnd拥有插件化的库系统(pluggable repository system),这让我们可以使用任何一种后端的库技术,包括Ivy。

不贴代码了,直接看下面:

Bndtools OSGi对企业开发带来的好处:
http://www.infoq.com/cn/articles/agile-and-structural-modularity-part3?utm_source=tuicool&utm_medium=referral
案例:西门子公司通过OSGi实现结构性的模块化,由此达到敏捷的目标,与此同时也实现了社交和流程方面的敏捷开发。Bndtools是促成这一解决方案的关键。同时,西门子公司的业务需求也反过来促进和形成了Bndtools的关键功能。

Bndtools安装
http://bndtools.org/installation.html

Bndtools开发入门
http://bndtools.org/tutorial.html

如何在Liferay开发中自动把插件构建到osgi容器

这里简单举例说明,先写一个portlet:

注意,@Component是osgi的注解,用于注册组件,组件可以理解为最后颗粒度的类元素。

  • immediate元素控制是否立即激活Component configuration;
  • service元素描述了Component实现的接口,这些接口一定是对外提供服务接口;
  • property元素定义了配置参数
  • 当有依赖项时,还需要有reference元素
package 你的包名;

import java.io.IOException;
import java.io.PrintWriter;
import javax.portlet.GenericPortlet;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=YourPortlet",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class YourPortlet extends GenericPortlet {

    @Override
    protected void doView(RenderRequest request, RenderResponse response)
        throws IOException, PortletException {

        PrintWriter printWriter = response.getWriter();

        printWriter.print("Hello World!");
    }

}

自动化构建:
只需要运行XXXX.run文件:

-runremote: liferay;shell=-1

-runbundles:
  你的Porject目录;version=latest,

-runpath:   ../cnf/local/biz.aQute.remote.launcher/biz.aQute.remote.launcher-3.0.0.jar;version=file

注意:biz.aQute.remote.agent-X.X.X.jar这个必须要先存在。
在“你的Porject目录”里必须有这个文件:bnd.bnd
内容:

Bundle-Version: 1.0.0
-sources: true

Private-Package: 你的包名.YourPortlet
-buildpath:javax.portlet;version=‘2.0.0‘,org.osgi.service.component.annotations;version=‘1.3.0‘

OSGi原来复杂的构建过程,现在已自动化了。

也可以通过Maven,那是另一个主题了:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>default-bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.ant.bnd</artifactId>
<version>2.0.28</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

更多其他osgi参考资料

osgi大纲(v6.0 2015年版)
https://osgi.org/download/r6/osgi.cmpn-6.0.0.pdf

osgi学习资源:
https://www.osgi.org/developer/resources/learning-resources-tutorials/

一个非常好的例子:
http://www.vogella.com/tutorials/OSGi/article.html

Apache Felix OSGi Tutorial:
http://felix.apache.org/documentation/tutorials-examples-and-presentations/apache-felix-osgi-tutorial.html
Neil Bartlett:
http://njbartlett.name/osgibook.html
http://njbartlett.name/files/osgibook_preview_20091217.pdf
Bndtools Tutorial:
http://bndtools.org/tutorial.html

Eclipse Equinox教程
http://www.eclipse.org/equinox/documents/quickstart-framework.php
Beginer Tutorial
http://www.javaworld.com/article/2077837/java-se/java-se-hello-osgi-part-1-bundles-for-beginners.html

时间: 2024-08-28 14:19:08

Liferay7 BPM门户开发之38: OSGi Bndtools开发入门的相关文章

Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发

hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为系统服务挂钩(Liferay Service Hook),还有其他类型的hook... Liferay6.2 时的hook开发比较有限,而在Liferay7则大为不同,OSGi services的彻底改进至Liferay的底层模型框架,使得Liferay可以支持更多的定制扩展!OSGi plugin

Liferay7 BPM门户开发之17: Portlet 生命周期

Portlet 生命周期 init() =〉 render() =〉 processAction() =〉 processEvent() =〉 serveResource() =〉destroy() init() 在Liferay容器部署portlet时,启动portlet实例化 init有两个写法: public void init() throws PortletException public void init(PortletConfig config) throws PortletEx

Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)

优秀的平台必然松耦合.易扩展  -- 王昕 第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd"> <hook> <portal-properties>

Liferay7 BPM门户开发之34: liferay7对外服务类生成(RestService Get Url)

在liferay7中开发不依赖Service Builder的对外服务类,非常简洁,只需要2点注解: 在类的前部定义: @ApplicationPath("/PathXXX") 方法前定义: @GET @Path("/ActionXXX") @Produces("text/plain") 例子:得到注册用户 import com.liferay.portal.kernel.model.User; import com.liferay.portal

Liferay7 BPM门户开发之39: Form表单提交的ProcessAction处理

在v6.2开始后,需要设置<requires-namespaced-parameters>false</requires-namespaced-parameters>  来避免在jsp中写<portlet:namespace/>的input前缀 在v7.0发现,注解方式是不灵的! 即  "javax.portlet.requires-namespaced-parameters=false", 无效果, 真是汗... 但我们是有办法在7.0中解决的,直

Liferay7 BPM门户开发之41: Expando API入门

Expando 是liferay的一种自定义表格扩展的方式,从5.0就已存在 , 可以在运行时新建表格\字段\行\值. 这是一种Service Builder之外的轻量级替代扩展方式,不必像Service Builder那么繁琐, 实现的具体技术实际上是列转行,其中ExpandoValue有点像Activiti的act_ru_variable 主要有4种接口,分别是表\行\列\值 ExpandoTableLocalServiceUtil ExpandoRowLocalServiceUtil Ex

Liferay7 BPM门户开发之8: Activiti实用问题集合

1.如何实现审核的上级获取(任务逐级审批) 这个是必备功能,通过Spring的注入+Activiti表达式可以很容易解决. 可参考: http://blog.csdn.net/sunxing007/article/details/8491552 http://linhongyu.blog.51cto.com/6373370/1656596 2.如何设置流程发起人,并动态在其他任务中使用 通过变量,在启动的时候设置. activiti里设置流程发起人的功能很绕. activiti 指定发起人,并作

Liferay7 BPM门户开发之44: 集成Activiti展示流程列表

集成Activiti之前,必须搞清楚其中的依赖关系,才能在Gradle里进行配置. 依赖关系: 例如,其中activiti-engine依赖于activiti-bpmn-converter,而activiti-bpmn-converter又依赖于activiti-bpmn-model 那么这以下的引用都是要设置的,缺一不可,否则portlet会无法注入进OSGi容器 org.activiti:activiti-engine:jar:5.xx.0+- org.activiti:activiti-b

Liferay7 BPM门户开发之19: 理解Service Builder体系

Service Builder是Liferay为业务开发而设计的模型驱动(model-driven)平台工具,提供一系列的实体类.数据持久化.服务相关的代码自动生成服务.支持Hibernate and Spring集成,缓存处理,动态查询等特性.令人惊讶的是,Liferay所有的持久化代码.Servie接口代码都是由Service Builder自动生成的,可见其自动化程度之高. 注意:Liferay的数据持久化开发不是必需用Service Builder,你可以直接用JDBC.JPA等任何技术