|
| 1 | +/** |
| 2 | + * Original work: copyright 1999-2004 The Apache Software Foundation |
| 3 | + * (http://www.apache.org/) |
| 4 | + * |
| 5 | + * This project is based on the work licensed to the Apache Software |
| 6 | + * Foundation (ASF) under one or more contributor license agreements. |
| 7 | + * See the NOTICE file distributed with this work for additional |
| 8 | + * information regarding copyright ownership. |
| 9 | + * |
| 10 | + * Modified work: copyright 2013-2019 Valery Silaev (http://vsilaev.com) |
| 11 | + * |
| 12 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | + * you may not use this file except in compliance with the License. |
| 14 | + * You may obtain a copy of the License at |
| 15 | + * |
| 16 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | + * |
| 18 | + * Unless required by applicable law or agreed to in writing, software |
| 19 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | + * See the License for the specific language governing permissions and |
| 22 | + * limitations under the License. |
| 23 | + */ |
| 24 | +package org.apache.commons.javaflow.tools.runtime; |
| 25 | + |
| 26 | +import java.lang.annotation.Annotation; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.Collections; |
| 31 | + |
| 32 | +import org.apache.commons.javaflow.spi.ResourceTransformationFactory; |
| 33 | + |
| 34 | +public final class ApplicationWeaver { |
| 35 | + private ApplicationWeaver() {} |
| 36 | + |
| 37 | + public static boolean bootstrap(ResourceTransformationFactory factory, String...args) { |
| 38 | + return _trampoline(factory, null, null, null, args); |
| 39 | + } |
| 40 | + |
| 41 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 42 | + boolean isContinuablePackageRoot, |
| 43 | + String...args) { |
| 44 | + return _trampoline(factory, null, null, asPackageRoots(isContinuablePackageRoot), args); |
| 45 | + } |
| 46 | + |
| 47 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 48 | + String[] continuablePackageRoots, |
| 49 | + String...args) { |
| 50 | + return _trampoline(factory, null, null, Arrays.asList(continuablePackageRoots), args); |
| 51 | + } |
| 52 | + |
| 53 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 54 | + Collection<String> continuablePackageRoots, |
| 55 | + String...args) { |
| 56 | + return _trampoline(factory, null, null, continuablePackageRoots, args); |
| 57 | + } |
| 58 | + |
| 59 | + public static boolean bootstrap(ResourceTransformationFactory factory, ClassLoader loader, String...args) { |
| 60 | + return _trampoline(factory, null, loader, null, args); |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 64 | + ClassLoader loader, |
| 65 | + boolean isContinuablePackageRoot, |
| 66 | + String...args) { |
| 67 | + return _trampoline(factory, null, loader, asPackageRoots(isContinuablePackageRoot), args); |
| 68 | + } |
| 69 | + |
| 70 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 71 | + ClassLoader loader, |
| 72 | + String[] continuablePackageRoots, |
| 73 | + String...args) { |
| 74 | + return _trampoline(factory, null, loader, Arrays.asList(continuablePackageRoots), args); |
| 75 | + } |
| 76 | + |
| 77 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 78 | + ClassLoader loader, |
| 79 | + Collection<String> continuablePackageRoots, |
| 80 | + String...args) { |
| 81 | + return _trampoline(factory, null, null, continuablePackageRoots, args); |
| 82 | + } |
| 83 | + |
| 84 | + public static boolean bootstrap(ResourceTransformationFactory factory, Class<?> source, String...args) { |
| 85 | + return _trampoline(factory, source, source.getClassLoader(), null, args); |
| 86 | + } |
| 87 | + |
| 88 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 89 | + Class<?> source, |
| 90 | + boolean isContinuablePackageRoot, |
| 91 | + String...args) { |
| 92 | + return _trampoline(factory, source, source.getClassLoader(), asPackageRoots(isContinuablePackageRoot), args); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 97 | + Class<?> source, |
| 98 | + String[] continuablePackageRoots, |
| 99 | + String...args) { |
| 100 | + return _trampoline(factory, source, source.getClassLoader(), Arrays.asList(continuablePackageRoots), args); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public static boolean bootstrap(ResourceTransformationFactory factory, |
| 105 | + Class<?> source, |
| 106 | + Collection<String> continuablePackageRoots, |
| 107 | + String...args) { |
| 108 | + return _trampoline(factory, source, source.getClassLoader(), continuablePackageRoots, args); |
| 109 | + } |
| 110 | + |
| 111 | + private static boolean _trampoline(ResourceTransformationFactory factory, |
| 112 | + Class<?> originalClass, |
| 113 | + ClassLoader originalLoader, |
| 114 | + Collection<String> continuablePackageRoots, |
| 115 | + String...args) { |
| 116 | + if (null == originalLoader) { |
| 117 | + if (null != originalClass) { |
| 118 | + originalLoader = originalClass.getClassLoader(); |
| 119 | + } else { |
| 120 | + originalLoader = CURRENT_INITIATOR.get(); |
| 121 | + if (null == originalLoader) { |
| 122 | + originalLoader = Thread.currentThread().getContextClassLoader(); |
| 123 | + } |
| 124 | + if (null == originalLoader) { |
| 125 | + originalLoader = ClassLoader.getSystemClassLoader(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (null == originalClass) { |
| 131 | + StackTraceElement ste = new Exception().getStackTrace()[2]; |
| 132 | + try { |
| 133 | + originalClass = originalLoader.loadClass(ste.getClassName()); |
| 134 | + } catch (ClassNotFoundException ex) { |
| 135 | + throw new RuntimeException( |
| 136 | + "Unable to load requesting class " + ste.getClassName() + " using class loader " + originalLoader, |
| 137 | + ex |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if ((originalLoader instanceof ContinuableClassLoader) && |
| 143 | + isProcessed(originalClass)) { |
| 144 | + // Correct loader and annotations applied |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + try { |
| 149 | + ContinuableClassLoader.Builder builder = new ContinuableClassLoader.Builder(factory); |
| 150 | + if (null == continuablePackageRoots || continuablePackageRoots.isEmpty()) { |
| 151 | + builder.parentFirst(false); |
| 152 | + } else { |
| 153 | + for (String s : continuablePackageRoots) { |
| 154 | + if ("*".equals(s)) { |
| 155 | + s = originalClass.getPackage().getName(); |
| 156 | + } |
| 157 | + builder.addLoaderPackageRoot(s); |
| 158 | + } |
| 159 | + } |
| 160 | + ContinuableClassLoader loader = builder.parent(originalLoader).create(); |
| 161 | + |
| 162 | + ClassLoader prev = CURRENT_INITIATOR.get(); |
| 163 | + CURRENT_INITIATOR.set(loader); |
| 164 | + try { |
| 165 | + run(loader, originalClass.getName(), "main", args); |
| 166 | + } finally { |
| 167 | + if (null == prev) { |
| 168 | + CURRENT_INITIATOR.remove(); |
| 169 | + } else { |
| 170 | + CURRENT_INITIATOR.set(prev); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + catch (RuntimeException ex) { |
| 175 | + throw ex; |
| 176 | + } |
| 177 | + catch (Exception ex) { |
| 178 | + throw new RuntimeException(ex); |
| 179 | + } |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + private static void run(ContinuableClassLoader classLoader, String className, String method, String... args) throws Exception { |
| 184 | + Class<?> mainClass = classLoader.forceLoadClass(className); |
| 185 | + if (!isProcessed(mainClass)) { |
| 186 | + throw new IllegalStateException("Class " + className + " has no continuable methods"); |
| 187 | + } |
| 188 | + Method mainMethod = mainClass.getMethod(method, String[].class); |
| 189 | + mainMethod.invoke(null, new Object[] {args}); |
| 190 | + } |
| 191 | + |
| 192 | + private static boolean isProcessed(Class<?> clazz) { |
| 193 | + for (Annotation a : clazz.getAnnotations()) { |
| 194 | + if ("org.apache.commons.javaflow.core.Skip".equals(a.annotationType().getName())) { |
| 195 | + return true; |
| 196 | + } |
| 197 | + } |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + private static Collection<String> asPackageRoots(boolean isContinuablePackageRoot) { |
| 202 | + return isContinuablePackageRoot ? Collections.singleton("*") : null; |
| 203 | + } |
| 204 | + |
| 205 | + private static final ThreadLocal<ClassLoader> CURRENT_INITIATOR = new ThreadLocal<ClassLoader>(); |
| 206 | +} |
0 commit comments