首页IT科技osgi原理(OSGi Bundle的构建策略及实践)

osgi原理(OSGi Bundle的构建策略及实践)

时间2025-06-17 03:35:31分类IT科技浏览5607
导读:STARTED...

STARTED

框架已经启动

ERROR

某个Bundle启动过程中引发错误

WARNING

某一Bundle引发一个警告

INFO

某一Bundle引发一个INFO类型的事件

PACKAGES_REFRESHED

PackageAdmin.refreshPackage操作执行完成

STARTLEVEL_CHANGED StartLevel.setStartLevel操作执行完成

INSTALLED

Bundle被安装到OSGi环境后系统发布该事件

RESOLVED

Bundle被成功解析

LAZY_ACTIVATION

Bundle将被延迟激活

STARTING

Bundle正在被激活

STARTED

Bundle被成功激活

STOPPING

Bundle被停止

STOPPED

Bundle正在被停止

UPDATED

Bundle被更新

UNRESOLVED

Bundle被UNRESOLVED

UNINSTALLED Bundle被卸载

REGISTERED

服务被注册

MODIFIED

服务被修改

UNREGISTERING 服务正在被注销

package com.example;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.BundleEvent;

import org.osgi.framework.BundleListener;

import org.osgi.framework.FrameworkEvent;

import org.osgi.framework.FrameworkListener;

import org.osgi.framework.ServiceEvent;

import org.osgi.framework.ServiceListener;

public class Activator implements BundleActivator, FrameworkListener,

BundleListener, ServiceListener {

public void start(BundleContext context) throws Exception {

//注册监听

context.addFrameworkListener(this);

context.addBundleListener(this);

context.addServiceListener(this);

}

public void stop(BundleContext context) throws Exception {

}

//处理框架事件

public void frameworkEvent(FrameworkEvent event) {

if ((event.getType() & FrameworkEvent.ERROR) != 0) {

System.err.println("Framework ERROR: " + event.getBundle());

}

}

//处理Bundle事件

public void bundleChanged(BundleEvent event) {

if ((event.getType() & BundleEvent.STARTED) != 0) {

System.err.println("Bundle STARTED: " + event.getBundle());

} else if ((event.getType() & BundleEvent.STOPPED) != 0) {

System.err.println("Bundle STOPPED: " + event.getBundle());

}

}

//处理服务事件

public void serviceChanged(ServiceEvent event) {

if ((event.getType() & ServiceEvent.REGISTERED) != 0) {

System.err.println("Service REGISTERED: "

+ event.getServiceReference());

}

}

}

public void start(BundleContext context) throws Exception {

// 获取OSGi环境中的所有安装的bundle

for (Bundle bundle : context.getBundles()) {

System.out.println("Bundle Symbolic Name: "

+ bundle.getSymbolicName());

}

// 获取OSGi运行环境中的属性(查找范围包括系统属性)

System.out.println("osgi.framework = "

+ context.getProperty("osgi.framework"));

//注册其他Bundle

context.installBundle("file:C://test_bundle_1.0.0.jar");

//在该Bundle的数据存储区中构建datacache.file文件

context.getDataFile("datacache.file");

//注册监听

context.addFrameworkListener(this);

context.addBundleListener(this);

context.addServiceListener(this);

}

public void start(BundleContext context) throws Exception {

//获取OSGi Log服务的服务引用

ServiceReference logSr = context.getServiceReference(LogService.class.getName());

//根据Log服务引用获取LogService服务实例

log = (LogService) context.getService(logSr);

//创建StringService接口服务实例

StringService ss = new StringServiceImpl(log);

//设定StringService的属性

Hashtable

//使用BundleContext注册发布StringService服务

context.registerService(StringService.class.getName(), ss, properties);

}

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-Name: Apache Log4j

Bundle-SymbolicName: org.apache.log4j

Bundle-Version: 1.2.13

Bundle-ClassPath: .;log4j.1.2.13.jar

Export-Package: org.apache.log4j;version="1.2.13",

org.apache.log4j.config;version="1.2.13",

org.apache.log4j.helpers;version="1.2.13",

org.apache.log4j.jdbc;version="1.2.13",

org.apache.log4j.jmx;version="1.2.13",

org.apache.log4j.net;version="1.2.13",

org.apache.log4j.or;version="1.2.13",

org.apache.log4j.or.jms;version="1.2.13",

org.apache.log4j.or.sax;version="1.2.13",

org.apache.log4j.spi;version="1.2.13",

org.apache.log4j.varia;version="1.2.13",

org.apache.log4j.xml;version="1.2.13"

package com.example;

public interface StringService {

String concat(String str1, String str2);

}

package com.example.internal;

import org.osgi.service.log.LogService;

import com.example.StringService;

public class StringServiceImpl implements StringService {

private LogService log;

public StringServiceImpl(LogService log) {

this.log = log;

}

public String concat(String str1, String str2) {

String str = str1 + str2;

if (log != null)

log.log(LogService.LOG_INFO, "Concat String Result is " + str);

return str;

}

}

package com.example.internal;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.Constants;

import org.osgi.framework.ServiceReference;

import org.osgi.service.log.LogService;

import com.example.StringService;

public class Activator implements BundleActivator {

private LogService log;

public void start(BundleContext context) throws Exception {

//获取OSGi Log服务的服务引用

ServiceReference logSr = context.getServiceReference(LogService.class.getName());

//根据Log服务引用获取LogService服务实例

log = (LogService) context.getService(logSr);

//创建StringService接口服务实例

StringService ss = new StringServiceImpl(log);

//设定StringService的属性

Hashtable

//使用BundleContext注册发布StringService服务

context.registerService(StringService.class.getName(), ss, properties);

}

public void stop(BundleContext context) throws Exception {

}

}

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-Name: Example Plug-in

Bundle-SymbolicName: com.example

Bundle-Version: 1.0.0

Bundle-Activator: com.example.internal.Activator

Bundle-Vendor: EGRID

Eclipse-LazyStart: true

Import-Package: org.osgi.framework;version="1.3.0",

org.osgi.service.log;version="1.3.0"

Export-Package: com.example

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
新网站SEO优化的技巧与方法(百度SEO内部优化到排名提升) php微信授权登录开发(php(ThinkPHP)实现微信小程序的登录过程)