我是osgi的新手,我正在通过互联网学习它的基础。昨天我从 www.eclipsezone.com/files/jsig/code.zip 下载了一些代码。我可以使用 apache felix 输入 java -jar bin/felix.jar 来运行所有应用程序。但是,我尝试使用以下代码将 felix 嵌入到项目资源管理器中:
package org.example.osgi.explorer.internal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
public class Main {
public static void main(String[] argv) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"some.module.i.dont.know; version=1.0.0");
ExplorerActivator activator = new ExplorerActivator();
List<Object> list = new LinkedList<>();
list.add(activator);
map.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
Felix f = new Felix(map);
System.out.println("starting OSGI...");
f.start();
String home_dir="file:/G:/code2/JSIG_OSGi/";
Bundle b = f.getBundleContext().installBundle(home_dir+"projects/Explorer/dist/Explorer.jar");
Bundle b2 = f.getBundleContext().installBundle(home_dir+"projects/PropertiesAction/dist/PropertiesAction.jar");
// Bundle b3 = f.getBundleContext().installBundle(home_dir+"projects/ViewAction/dist/ViewAction.jar");
// Bundle b4 = f.getBundleContext().installBundle(home_dir+"bundles/org.apache.fop_0.20.5.jar");
// Bundle b5 = f.getBundleContext().installBundle(home_dir+"bundles/org.apache.fop_0.93.0.jar");
// Bundle b6 = f.getBundleContext().installBundle(home_dir+"projects/Fop20RenderAction/dist/Fop20RenderAction.jar");
// Bundle b7 = f.getBundleContext().installBundle(home_dir+"projects/Fop93RenderAction/dist/Fop93RenderAction.jar");
// String bName = b.getLocation();
//
// System.out.println("starting bundle " + bName);
b.start();
b2.start();
}
}
当我运行主应用程序时,出现以下错误:
starting OSGI...
ERROR: Bundle org.apache.felix.framework [0] EventDispatcher: Error during dispatch. (java.lang.ClassCastException: org.example.osgi.propertiesaction.PropertiesAction cannot be cast to org.example.osgi.explorer.FileSelectionListener)
java.lang.ClassCastException: org.example.osgi.propertiesaction.PropertiesAction cannot be cast to org.example.osgi.explorer.FileSelectionListener
at org.example.osgi.explorer.internal.FileSelectionService.addingService(FileSelectionService.java:35)
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:932)
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:864)
at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)
at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)
at org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:894)
at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:932)
at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:793)
at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:543)
at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4419)
at org.apache.felix.framework.Felix.registerService(Felix.java:3423)
at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:346)
at org.example.osgi.propertiesaction.Activator.start(Activator.java:22)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
at org.example.osgi.explorer.internal.Main.main(Main.java:55)
BUILD SUCCESSFUL (total time: 16 seconds)
为什么会出现这个异常。我该如何解决这个问题?
编辑:是的,它实现了!代码:
package org.example.osgi.propertiesaction;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import org.example.osgi.explorer.FileSelectionListener;
public class PropertiesAction extends AbstractAction implements FileSelectionListener {
private static final File[] EMPTY = new File[0];
private File[] selection;
public PropertiesAction() {
super("Properties");
setEnabled(false);
}
public void actionPerformed(ActionEvent event) {
Frame frame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, (Component) event.getSource());
FilePropertiesDialog dialog = new FilePropertiesDialog(selection, frame, true);
dialog.setVisible(true);
}
public void selectionChanged(File[] selection) {
this.selection = (selection == null) ? EMPTY : selection;
setEnabled(selection != null && selection.length > 0);
}
}
编辑:以下代码有效:
FrameworkFactory ff = new FrameworkFactory ();
Map<String,Object> config = new HashMap<String,Object>();
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,"packages needed,more packages needed");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
Framework fwk = ff.newFramework(config);
fwk.start();
BundleContext context = fwk.getBundleContext();
String home_dir="file:/G:/code2/JSIG_OSGi/";
Bundle bundle = context.installBundle(home_dir+"projects/Explorer/dist/Explorer.jar");
bundle.start();
Bundle bundle2 = context.installBundle(home_dir+
"projects/PropertiesAction/dist/PropertiesAction.jar");
bundle2.start();
//ExplorerActivator.isopened will be false if frame disposed.
if(ExplorerActivator.isopened) {
bundle.stop();
bundle2.stop();
bundle.uninstall();
bundle2.uninstall();
fwk.stop();
fwk.waitForStop(1000);
}