我正在构建一个简单的 OSGi 演示应用程序来了解该框架。我想从另一个包中更新活动包,或者从嵌入了 OSGi 框架的应用程序更新(如Neil Bartlett 的 How To Embed OSGi中所述)。
我的应用程序被分成这些包(我已将代码放在帖子末尾以便于阅读):
- com.dc.sszostek.interfaces - 包含一个带有 draw() 方法的 Shape 接口
- com.dc.sszostek.implementations - 有 2 个带有此 SymbolicName 的包,每个包都实现了 Shape 接口:println 是一个 Line,另一个是 Square。它们的清单文件都是相同的,捆绑包仅在实现上有所不同。
- com.dc.sszostek.programs - 包含一个 Painter 程序;它使用 Shape 接口来绘制()(我使用了OSGi 服务 - Lars Vogel 的教程来编写它)。
- com.dc.sszostek.xmpp - 包含使用 SmackAPI 实现的 Jabber 客户端,等待文件传输并在收到文件时尝试更新 com.dc.sszostek.implementations 包。
我的问题是,当我向我的应用程序发送不同的实现时,文件被写入,但捆绑包没有得到更新。
bundle.update()
被调用时,它不会抛出异常,但我的程序一直在画一条线(或一个正方形,取决于我首先放入的包)。当我从 OSGi 控制台更新包时,它被正确替换,并且我的演示开始绘制不同的形状。
谁能告诉我我犯的错误在哪里,或者指出一个可行的例子?
先感谢您。
com.dc.sszostek.interfaces
清单文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Provider
Bundle-SymbolicName: com.dc.sszostek.interfaces
Bundle-Version: 1.0.0
Export-Package: com.dc.sszostek.interfaces
形状.java
package com.dc.sszostek.interfaces;
public interface Shape {
void draw();
}
com.dc.sszostek.implementations
清单文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Impl
Bundle-SymbolicName: com.dc.sszostek.implementations
Bundle-Version: 1.0.0
Bundle-Activator: com.dc.sszostek.implementations.Activator
Export-Package: com.dc.sszostek.implementations
Import-Package: org.osgi.framework, com.dc.sszostek.interfaces
激活器.java
package com.dc.sszostek.implementations;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.dc.sszostek.interfaces.Shape;
public class Activator implements BundleActivator {
public void start(BundleContext ctx) throws Exception {
ctx.registerService(Shape.class.getName(), new Line(), null);
}
public void stop(BundleContext ctx) throws Exception {}
}
线.java
package com.dc.sszostek.implementations;
import com.dc.sszostek.interfaces.Shape;
public class Line implements Shape {
public void draw() {
System.out.println("*********");
}
}
com.dc.sszostek.programs
清单文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Prog
Bundle-SymbolicName: com.dc.sszostek.programs
Bundle-Version: 1.0.0
Bundle-Activator: com.dc.sszostek.programs.Activator
Export-Package: com.dc.sszostek.programs
Import-Package: org.osgi.framework, com.dc.sszostek.interfaces
激活器.java
package com.dc.sszostek.programs;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.dc.sszostek.interfaces.Shape;
public class Activator implements BundleActivator {
private MyThread thread;
public void start(BundleContext ctx) throws Exception {
ServiceReference ref = getServiceReference(ctx);
thread = new MyThread((Shape)ctx.getService(ref));
thread.start();
}
public void stop(BundleContext ctx) throws Exception {
ServiceReference ref = getServiceReference(ctx);
ctx.ungetService(ref);
thread.stopThread();
}
private ServiceReference getServiceReference(BundleContext ctx) {
ServiceReference ref = ctx.getServiceReference(Shape.class.getName());
return ref;
}
public static class MyThread extends Thread {
private volatile boolean active = true;
private final Shape service;
public MyThread(Shape service) {
this.service = service;
}
public void run() {
while (active) {
service.draw();
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
}
}
public void stopThread() {
active = false;
}
}
}
com.dc.sszostek.programs
清单文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: FileReceiver
Bundle-SymbolicName: com.dc.sszostek.xmpp
Bundle-Version: 1.0.0
Bundle-Activator: com.dc.sszostek.xmpp.Activator
Bundle-ClassPath: ., lib/smack-3.2.1.jar, lib/smackx-3.2.1.jar
Import-Package: org.osgi.framework, javax.net, javax.security.auth.callback, javax.net.ssl, javax.security.sasl,
javax.naming.directory, javax.naming
激活器.java
package com.dc.sszostek.xmpp;
import org.jivesoftware.smack.*;
import org.jivesoftware.smackx.filetransfer.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import java.io.File;
import java.io.IOException;
public class Activator implements BundleActivator {
private Connection connection;
public void start(BundleContext bundleContext) throws Exception {
final BundleContext ctx = bundleContext;
try {
connection = new XMPPConnection("JABBER_SERVER");
connection.connect();
connection.login("USER", "PASS");
final FileTransferManager manager = new FileTransferManager(connection);
FileTransferNegotiator.getInstanceFor(connection);
FileTransferNegotiator.setServiceEnabled(connection, true);
manager.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(FileTransferRequest request) {
IncomingFileTransfer transfer = request.accept();
File file = new File("D:\\bundles\\" + transfer.getFileName());
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {
transfer.recieveFile(file);
} catch (XMPPException e) {
System.out.println(e.getMessage());
}
Bundle bundle = ctx.getBundle(2); //com.dc.sszostek.implementations is bundle number 2
try {
bundle.update();
} catch (BundleException e) {
System.out.println(e.getMessage());
}
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void stop(BundleContext bundleContext) throws Exception {
connection.disconnect();
}
}