简体   繁体   English

如何在另一个项目中使用Eclipse OSGi Java插件?

[英]How do I use my Eclipse OSGi Java plug-in in another project?

This is all so new for me and a bit complex. 这对我来说太新了,有点复杂。 I created a plug-in project in Eclipse with a standard OSGi framework. 我在Eclipse中使用标准OSGi框架创建了一个插件项目。 The aim is to connect to H2 DB with this bundle. 目的是通过此捆绑包连接到H2 DB。 Here is the Activator.java: package dbservice; 这是Activator.java:包dbservice;

import java.sql.Connection;
import java.sql.DriverManager;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

private static BundleContext context;
private Connection conn=null;

static BundleContext getContext() {
    return context;
}


public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
    Class.forName("org.h2.Driver");
    conn = DriverManager.getConnection("jdbc:h2:~/test","sa","123456");
    System.out.print("Connection opened...");
}

public void stop(BundleContext bundleContext) throws Exception {
    Activator.context = null;
    conn.close();
    System.out.print("Connection closed...");
}

}

I run this project within OSGi framework. 我在OSGi框架中运行此项目。 It is working there. 它在那里工作。 But my question is, how do I use this bundle in another project? 但是我的问题是,如何在另一个项目中使用此捆绑包?

The question is what you really want to provide to other bundles. 问题是您真正想提供给其他捆绑包的内容。 If you want to provide a generic way to access a database then you can provide a DataSource. 如果要提供一种访问数据库的通用方法,则可以提供一个数据源。 In this case I recommend to use pax-jdbc-config as it does all the heavy lifting for you. 在这种情况下,我建议使用pax-jdbc-config,因为它可以为您完成所有繁重的工作。

Another options is Aries transactioncontrol which is even a bit easier to use but makes your user code depend on the transaction control API. 另一个选择是Aries transactioncontrol ,它甚至更易于使用,但使您的用户代码取决于事务控制API。

If you want to provide something higher level then a service is the best choice. 如果您想提供更高级别的服务,那么服务是最佳选择。 Create an interface for your service and implement the interface in your bundle. 为您的服务创建一个接口,并在您的捆绑软件中实现该接口。 Then simply export the object as an OSGi service. 然后只需将对象导出为OSGi服务即可。 Other bundles can then pick up the service. 然后,其他捆绑包可以使用该服务。 I recommend to use declarative services for offering and consuming services. 我建议使用声明性服务来提供和使用服务。 See this example . 请参阅此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM