私は osgi が初めてで、インターネットを通じてその基礎を学んでいます。昨日、www.eclipsezone.com/files/jsig/code.zip からいくつかのコードをダウンロードしました。Java -jar bin/felix.jar と入力する apache 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);
}