diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java index f809029e41c..6924371de15 100644 --- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java +++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java @@ -32,6 +32,7 @@ import org.eclipse.equinox.console.commands.CommandsTracker; import org.eclipse.equinox.console.commands.DisconnectCommand; import org.eclipse.equinox.console.commands.EquinoxCommandProvider; +import org.eclipse.equinox.console.commands.ExportStateCommand; import org.eclipse.equinox.console.commands.HelpCommand; import org.eclipse.equinox.console.commands.ManCommand; import org.eclipse.equinox.console.commands.WireCommand; @@ -329,6 +330,9 @@ public void start(BundleContext context) throws Exception { WireCommand wireCommand = new WireCommand(context); wireCommand.startService(); + ExportStateCommand exportResourcesCommand = new ExportStateCommand(context); + exportResourcesCommand.startService(); + GOGO.RUNTIME.start(frameworkWiring); GOGO.SHELL.start(frameworkWiring); GOGO.COMMAND.start(frameworkWiring); diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ExportStateCommand.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ExportStateCommand.java new file mode 100644 index 00000000000..b4129a88efb --- /dev/null +++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ExportStateCommand.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2024 Christoph Läubrich and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.console.commands; + +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Dictionary; +import java.util.Hashtable; + +import org.apache.felix.service.command.CommandProcessor; +import org.apache.felix.service.command.CommandSession; +import org.apache.felix.service.command.Descriptor; +import org.eclipse.osgi.container.Module; +import org.osgi.framework.BundleContext; + +public class ExportStateCommand { + + private BundleContext context; + + public ExportStateCommand(BundleContext context) { + this.context = context; + } + + public void startService() { + Dictionary dict = new Hashtable<>(); + dict.put(CommandProcessor.COMMAND_SCOPE, "export"); + dict.put(CommandProcessor.COMMAND_FUNCTION, new String[] { "exportFrameworkState" }); + context.registerService(ExportStateCommand.class, this, dict); + } + + @Descriptor("Exports the current framework state including the wiring information") + public void exportFrameworkState(CommandSession session, String path) throws IOException { + exportFrameworkState(session, path, true); + } + + @Descriptor("Exports the current framework state, with or without the wiring information") + public void exportFrameworkState(CommandSession session, String path, boolean persistWirings) throws IOException { + Module module = context.getBundle(0).adapt(Module.class); + PrintStream console = session.getConsole(); + if (module != null) { + File file = new File(path); + console.println("Exporting ModuleDatabase to " + file.getAbsolutePath() + "..."); + try (DataOutputStream stream = new DataOutputStream(new FileOutputStream(file))) { + module.getContainer().store(stream, persistWirings); + } + } else { + console.println("Can't determine ModuleDatabase!"); + } + } + +} diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/container/ModuleContainer.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/container/ModuleContainer.java index 4d7c0957f70..42178c67707 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/container/ModuleContainer.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/container/ModuleContainer.java @@ -14,6 +14,8 @@ package org.eclipse.osgi.container; import java.io.Closeable; +import java.io.DataOutputStream; +import java.io.IOException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -2156,4 +2158,17 @@ String toString(Module m) { return b != null ? b.toString() : m.toString(); } } + + /** + * Stores the module into the given stream + * + * @param stream the stream to use + * @param persistWirings controls if wirings should be persisted or not + * @throws IOException if there is any problem storing to the stream + * + * @since 3.19 + */ + public void store(DataOutputStream stream, boolean persistWirings) throws IOException { + moduleDatabase.store(stream, persistWirings); + } } \ No newline at end of file