Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a command to export the module database into a file #468

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading