]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms.jshell/src/org/argeo/internal/cms/jshell/osgi/OsgiExecutionControlProvider.java
Merge tag 'v2.3.17' into testing
[lgpl/argeo-commons.git] / org.argeo.cms.jshell / src / org / argeo / internal / cms / jshell / osgi / OsgiExecutionControlProvider.java
index f785919b2455c6d0ff7c6d00173b24603a750856..fbca085861759a1410b743555d8eb1d817db6367 100644 (file)
@@ -19,6 +19,7 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.argeo.api.cms.CmsLog;
+import org.argeo.cms.jshell.CmsExecutionControl;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -28,7 +29,6 @@ import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.framework.wiring.BundleWire;
 import org.osgi.framework.wiring.BundleWiring;
 
-import jdk.jshell.execution.DirectExecutionControl;
 import jdk.jshell.spi.ExecutionControl;
 import jdk.jshell.spi.ExecutionControlProvider;
 import jdk.jshell.spi.ExecutionEnv;
@@ -53,24 +53,15 @@ public class OsgiExecutionControlProvider implements ExecutionControlProvider {
 
        @Override
        public ExecutionControl generate(ExecutionEnv env, Map<String, String> parameters) throws Throwable {
-               // TODO find a better way to get a default bundle context
-               // NOTE: the related default bundle has to be started
-
-//             String symbolicName = parameters.get(BUNDLE_PARAMETER);
-//             Bundle fromBundle = getBundleFromSn(symbolicName);
-
                Long bundleId = Long.parseLong(parameters.get(BUNDLE_PARAMETER));
                Bundle fromBundle = getBundleFromId(bundleId);
 
                BundleWiring fromBundleWiring = fromBundle.adapt(BundleWiring.class);
                ClassLoader fromBundleClassLoader = fromBundleWiring.getClassLoader();
 
-               // use the bundle classloade as context classloader
-               Thread.currentThread().setContextClassLoader(fromBundleClassLoader);
-               
-               ExecutionControl executionControl = new DirectExecutionControl(
-                               new WrappingLoaderDelegate(fromBundleClassLoader));
-               log.debug("JShell from " + fromBundle.getSymbolicName() + "_" + fromBundle.getVersion() + " ["
+               ExecutionControl executionControl = new CmsExecutionControl(env,
+                               new WrappingLoaderDelegate(env, fromBundleClassLoader));
+               log.trace(() -> "JShell from " + fromBundle.getSymbolicName() + "_" + fromBundle.getVersion() + " ["
                                + fromBundle.getBundleId() + "]");
                return executionControl;
        }
@@ -119,6 +110,42 @@ public class OsgiExecutionControlProvider implements ExecutionControlProvider {
                        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);
                }