|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import java.io.*; |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Modifier; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import com.google.gson.*; |
| 26 | +import java.security.Permission; |
| 27 | +import java.lang.reflect.InvocationTargetException; |
| 28 | + |
| 29 | +class Launcher { |
| 30 | + |
| 31 | + private static String mainClassName = "Main"; |
| 32 | + private static String mainMethodName = "main"; |
| 33 | + private static Class mainClass = null; |
| 34 | + private static Method mainMethod = null; |
| 35 | + |
| 36 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 37 | + private static void augmentEnv(Map<String, String> newEnv) { |
| 38 | + try { |
| 39 | + for (Class cl : Collections.class.getDeclaredClasses()) { |
| 40 | + if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { |
| 41 | + Field field = cl.getDeclaredField("m"); |
| 42 | + field.setAccessible(true); |
| 43 | + Object obj = field.get(System.getenv()); |
| 44 | + Map<String, String> map = (Map<String, String>) obj; |
| 45 | + map.putAll(newEnv); |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (Exception e) {} |
| 49 | + } |
| 50 | + |
| 51 | + private static void initMain(String[] args) throws Exception { |
| 52 | + if(args.length > 0) |
| 53 | + mainClassName = args[0]; |
| 54 | + int pos = mainClassName.indexOf("#"); |
| 55 | + if(pos != -1) { |
| 56 | + if(pos + 1 != mainClassName.length()) |
| 57 | + mainMethodName = args[0].substring(pos+1); |
| 58 | + mainClassName = args[0].substring(0,pos); |
| 59 | + } |
| 60 | + |
| 61 | + mainClass = Class.forName(mainClassName); |
| 62 | + Method m = mainClass.getMethod(mainMethodName, new Class[] { JsonObject.class }); |
| 63 | + m.setAccessible(true); |
| 64 | + int modifiers = m.getModifiers(); |
| 65 | + if (m.getReturnType() != JsonObject.class || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) { |
| 66 | + throw new NoSuchMethodException(mainMethodName); |
| 67 | + } |
| 68 | + mainMethod = m; |
| 69 | + } |
| 70 | + |
| 71 | + private static JsonObject invokeMain(JsonObject arg, Map<String, String> env) throws Exception { |
| 72 | + augmentEnv(env); |
| 73 | + return (JsonObject) mainMethod.invoke(null, arg); |
| 74 | + } |
| 75 | + |
| 76 | + private static SecurityManager defaultSecurityManager = null; |
| 77 | + private static void installSecurityManager() { |
| 78 | + defaultSecurityManager = System.getSecurityManager(); |
| 79 | + System.setSecurityManager(new SecurityManager() { |
| 80 | + @Override |
| 81 | + public void checkPermission(Permission p) { |
| 82 | + // Not throwing means accepting anything. |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void checkPermission(Permission p, Object ctx) { |
| 87 | + // Not throwing means accepting anything. |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void checkExit(int status) { |
| 92 | + super.checkExit(status); |
| 93 | + throw new SecurityException("System.exit(" + status + ") called from within an action."); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private static void uninstallSecurityManager() { |
| 99 | + if(defaultSecurityManager != null) { |
| 100 | + System.setSecurityManager(defaultSecurityManager); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String[] args) throws Exception { |
| 105 | + |
| 106 | + initMain(args); |
| 107 | + |
| 108 | + // exit after main class loading if "exit" specified |
| 109 | + // used to check healthy launch after init |
| 110 | + if(args.length >1 && args[1] == "-exit") |
| 111 | + System.exit(0); |
| 112 | + |
| 113 | + // install a security manager to prevent exit |
| 114 | + installSecurityManager(); |
| 115 | + |
| 116 | + BufferedReader in = new BufferedReader( |
| 117 | + new InputStreamReader(System.in, "UTF-8")); |
| 118 | + PrintWriter out = new PrintWriter( |
| 119 | + new OutputStreamWriter( |
| 120 | + new FileOutputStream("/dev/fd/3"), "UTF-8")); |
| 121 | + JsonParser json = new JsonParser(); |
| 122 | + JsonObject empty = json.parse("{}").getAsJsonObject(); |
| 123 | + String input = ""; |
| 124 | + while (true) { |
| 125 | + try { |
| 126 | + input = in.readLine(); |
| 127 | + if (input == null) |
| 128 | + break; |
| 129 | + JsonElement element = json.parse(input); |
| 130 | + JsonObject payload = empty.deepCopy(); |
| 131 | + HashMap<String, String> env = new HashMap<String, String>(); |
| 132 | + if (element.isJsonObject()) { |
| 133 | + // collect payload and environment |
| 134 | + for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) { |
| 135 | + if (entry.getKey().equals("value")) { |
| 136 | + if (entry.getValue().isJsonObject()) |
| 137 | + payload = entry.getValue().getAsJsonObject(); |
| 138 | + } else { |
| 139 | + env.put(String.format("__OW_%s", entry.getKey().toUpperCase()), |
| 140 | + entry.getValue().getAsString()); |
| 141 | + } |
| 142 | + } |
| 143 | + augmentEnv(env); |
| 144 | + } |
| 145 | + JsonElement response = invokeMain(payload, env); |
| 146 | + out.println(response.toString()); |
| 147 | + } catch(NullPointerException npe) { |
| 148 | + System.out.println("the action returned null"); |
| 149 | + npe.printStackTrace(System.err); |
| 150 | + JsonObject error = new JsonObject(); |
| 151 | + error.addProperty("error", "the action returned null"); |
| 152 | + out.println(error.toString()); |
| 153 | + out.flush(); |
| 154 | + } catch(InvocationTargetException ite) { |
| 155 | + Throwable ex = ite; |
| 156 | + if(ite.getCause() != null) |
| 157 | + ex = ite.getCause(); |
| 158 | + ex.printStackTrace(System.err); |
| 159 | + JsonObject error = new JsonObject(); |
| 160 | + error.addProperty("error", ex.getMessage()); |
| 161 | + out.println(error.toString()); |
| 162 | + out.flush(); |
| 163 | + } catch (Exception ex) { |
| 164 | + ex.printStackTrace(System.err); |
| 165 | + JsonObject error = new JsonObject(); |
| 166 | + error.addProperty("error", ex.getMessage()); |
| 167 | + out.println(error.toString()); |
| 168 | + out.flush(); |
| 169 | + } |
| 170 | + out.flush(); |
| 171 | + System.out.flush(); |
| 172 | + System.err.flush(); |
| 173 | + } |
| 174 | + uninstallSecurityManager(); |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments