+ try (Writer writer = Files.newBufferedWriter(bundleStartupScript, StandardCharsets.UTF_8)) {
+ for (String p : packagesToImport) {
+ writer.write("import " + p + ".*;\n");
+ }
+
+ String std = """
+ /open DEFAULT
+ import jdk.jshell.spi.ExecutionEnv;
+ import java.util.function.*;
+
+ /** Redirected standard IO. */
+ public class Std {
+ final static InputStream in = new Supplier<InputStream>() {
+
+ @Override
+ public InputStream get() {
+ return ((ExecutionEnv) getClass().getClassLoader()).userIn();
+ }
+
+ }.get();
+ final static PrintStream out = new Supplier<PrintStream>() {
+
+ @Override
+ public PrintStream get() {
+ return ((ExecutionEnv) getClass().getClassLoader()).userOut();
+ }
+
+ }.get();
+ final static PrintStream err = new Supplier<PrintStream>() {
+
+ @Override
+ public PrintStream get() {
+ return ((ExecutionEnv) getClass().getClassLoader()).userErr();
+ }
+
+ }.get();
+
+ }
+ """;
+ writer.write(std);
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot writer bundle startup script to " + bundleStartupScript, e);
+ }
+
+ return bundleStartupScript;
+ }
+
+ public static String getBundleClasspath(Long bundleId) throws IOException {
+ String framework = System.getProperty("osgi.framework");
+ Path frameworkLocation = Paths.get(URI.create(framework)).toAbsolutePath();
+ BundleContext bc = FrameworkUtil.getBundle(OsgiExecutionControlProvider.class).getBundleContext();
+ Bundle fromBundle = bc.getBundle(bundleId);
+
+ BundleWiring fromBundleWiring = fromBundle.adapt(BundleWiring.class);
+
+ Set<Bundle> bundlesToAddToCompileClasspath = new TreeSet<>();
+
+ // from bundle
+ bundlesToAddToCompileClasspath.add(fromBundle);
+
+ List<BundleWire> bundleWires = fromBundleWiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
+ for (BundleWire bw : bundleWires) {
+ bundlesToAddToCompileClasspath.add(bw.getProviderWiring().getBundle());
+ }
+
+ StringJoiner classpath = new StringJoiner(File.pathSeparator);
+ bundles: for (Bundle b : bundlesToAddToCompileClasspath) {
+ if (b.getBundleId() == 0) {// system bundle
+ classpath.add(frameworkLocation.toString());
+ continue bundles;
+ }
+ Path p = bundleToPath(frameworkLocation, b);
+ classpath.add(p.toString());
+ }
+
+ return classpath.toString();