1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.jack.launcher;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.zip.ZipFile;
import javax.annotation.Nonnull;
/**
* Launcher protecting called main class from deletion of its classpath.
*/
public class Main {
@Nonnull
private static Logger logger = Logger.getLogger(Main.class.getName());
private static final int CMD_IDX_CLASSPATH_OPTION = 0;
private static final int CMD_IDX_CLASSPATH_VALUE = 1;
private static final int CMD_IDX_MAIN_CLASS = 2;
private static final int CMD_HANDLED_IDX = 3;
public static void main(@Nonnull String[] args) {
if (args.length < CMD_HANDLED_IDX) {
if (args.length < CMD_HANDLED_IDX) {
logUsage();
abort();
}
}
if (!args[CMD_IDX_CLASSPATH_OPTION].equals("-cp")) {
logUsage();
abort();
}
String classpath = args[CMD_IDX_CLASSPATH_VALUE];
String[] classpathEntries = classpath.split(Pattern.quote(File.pathSeparator));
ZipFile[] zips = new ZipFile[classpathEntries.length];
for (int i = 0; i < zips.length; i++) {
try {
zips[i] = new ZipFile(classpathEntries[i]);
} catch (IOException e) {
logger.log(Level.INFO, "IOEception while trying to open '" + classpathEntries[i] + "'", e);
logger.log(Level.SEVERE, "Failed to open '" + classpathEntries[i] + "'");
abort();
}
}
ClassLoader loader = new ZipLoader(zips);
Class<?> clazz;
try {
clazz = loader.loadClass(args[CMD_IDX_MAIN_CLASS]);
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, "Failed to find class '" + args[CMD_IDX_MAIN_CLASS] + "'");
abort();
return;
}
Method method;
try {
method = clazz.getMethod("main", args.getClass());
} catch (SecurityException e) {
throw new AssertionError();
} catch (NoSuchMethodException e) {
logger.log(Level.SEVERE, "Failed to find main method in class '" + args[CMD_IDX_MAIN_CLASS]
+ "'");
abort();
return;
}
String[] invokeArgs = new String[args.length - CMD_HANDLED_IDX];
System.arraycopy(args, CMD_HANDLED_IDX, invokeArgs, 0, args.length - CMD_HANDLED_IDX);
try {
method.invoke(null, (Object) invokeArgs);
} catch (IllegalArgumentException e) {
throw new AssertionError();
} catch (IllegalAccessException e) {
throw new AssertionError();
} catch (InvocationTargetException e) {
logger.log(Level.SEVERE, "An error occured during delegate execution", e.getCause());
abort();
}
}
private static void logUsage() {
logger.log(Level.SEVERE, "Usage: -cp <classpath> <main class> ...");
}
private static void abort() {
System.exit(1);
}
}
|