2727import java .net .InetSocketAddress ;
2828import java .nio .charset .StandardCharsets ;
2929import java .nio .file .Path ;
30+ import java .nio .file .Paths ;
3031import java .util .HashMap ;
3132import java .util .Map ;
3233import java .util .Set ;
@@ -42,6 +43,8 @@ public class Proxy {
4243 private HttpServer server ;
4344 private JarLoader loader = null ;
4445 private boolean allowMultipleInits = false ;
46+ private static final String OW_AUTO_INIT = "OW_AUTO_INIT" ;
47+ private static final String OW_AUTO_INIT_MAIN = "OW_AUTO_INIT_MAIN" ;
4548
4649 public Proxy (int port ) throws IOException {
4750 long startTime = Debug .start ();
@@ -158,8 +161,25 @@ private class RunHandler implements HttpHandler {
158161 public void handle (HttpExchange t ) throws IOException {
159162 long startTime = Debug .start ();
160163 if (loader == null ) {
161- Proxy .writeError (t , "Cannot invoke an uninitialized action." );
162- return ;
164+ // check if the Jar file contents are set in the enviorment
165+ // OW_AUTO_INIT: Jar file with absolute/relative path
166+ // OW_AUTO_INIT_MAIN: name of the function in the "OW_AUTO_INIT" to call as the action handler
167+ String ow_auto_init = System .getenv (OW_AUTO_INIT );
168+ String ow_auto_init_main = System .getenv (OW_AUTO_INIT_MAIN );
169+ if (ow_auto_init == null || ow_auto_init .isEmpty ()) {
170+ Proxy .writeError (t , "Cannot invoke an uninitialized action." );
171+ return ;
172+ } else {
173+ try {
174+ Path jarPath = Paths .get (ow_auto_init );
175+ loader = new JarLoader (jarPath , ow_auto_init_main );
176+ } catch (Exception e ) {
177+ e .printStackTrace (System .err );
178+ writeLogMarkers ();
179+ Proxy .writeError (t , "An error has occurred (see logs for details): " + e );
180+ return ;
181+ }
182+ }
163183 }
164184
165185 ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
0 commit comments