Repository navigation
Expand file tree
/
Copy pathPtyUtil.java
More file actions
153 lines (124 loc) · 5.24 KB
/
Copy pathPtyUtil.java
File metadata and controls
153 lines (124 loc) · 5.24 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.pty4j.util;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.pty4j.windows.WinPty;
import com.sun.jna.Platform;
import java.io.File;
import java.net.URI;
import java.security.CodeSource;
import java.util.List;
import java.util.Map;
/**
* @author traff
*/
public class PtyUtil {
public static final String OS_VERSION = System.getProperty("os.version").toLowerCase();
private final static String PTY_LIB_FOLDER = System.getenv("PTY_LIB_FOLDER") != null ? System.getenv("PTY_LIB_FOLDER") : System.getProperty("PTY_LIB_FOLDER");
public static String[] toStringArray(Map<String, String> environment) {
if (environment == null) return new String[0];
List<String> list = Lists.transform(Lists.newArrayList(environment.entrySet()), new Function<Map.Entry<String, String>, String>() {
public String apply(Map.Entry<String, String> entry) {
return entry.getKey() + "=" + entry.getValue();
}
});
return list.toArray(new String[list.size()]);
}
/**
* Returns the folder that contains a jar that contains the class
*
* @param aclass a class to find a jar
* @return
*/
public static String getJarContainingFolderPath(Class aclass) throws Exception {
CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
File jarFile;
if (codeSource.getLocation() != null) {
jarFile = new File(codeSource.getLocation().toURI());
} else {
String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
int startIndex = path.indexOf(":") + 1;
int endIndex = path.indexOf("!");
if (startIndex == -1 || endIndex == -1) {
throw new IllegalStateException("Class " + aclass.getSimpleName() + " is located not within a jar: " + path);
}
String jarFilePath = path.substring(startIndex, endIndex);
jarFilePath = new URI(jarFilePath).getPath();
jarFile = new File(jarFilePath);
}
return jarFile.getParentFile().getAbsolutePath();
}
public static String getPtyLibFolderPath() throws Exception {
if (PTY_LIB_FOLDER != null) {
return PTY_LIB_FOLDER;
}
//Class aclass = WinPty.class.getClassLoader().loadClass("com.jediterm.pty.PtyMain");
Class aclass = WinPty.class;
return getJarContainingFolderPath(aclass);
}
public static File resolveNativeLibrary() throws Exception {
String libFolderPath = getPtyLibFolderPath();
if (libFolderPath != null) {
File libFolder = new File(libFolderPath);
File lib = resolveNativeLibrary(libFolder);
lib = lib.exists() ? lib : resolveNativeLibrary(new File(libFolder, "libpty"));
if (!lib.exists()) {
throw new IllegalStateException(String.format("Couldn't find %s, jar folder %s", lib.getName(),
libFolder.getAbsolutePath()));
}
return lib;
} else {
throw new IllegalStateException("Couldn't detect lib folder");
}
}
public static File resolveNativeLibrary(File parent) {
return resolveNativeFile(parent, getNativeLibraryName());
}
public static File resolveNativeFile(String fileName) throws Exception {
File libFolder = new File(getPtyLibFolderPath());
File file = resolveNativeFile(libFolder, fileName);
return file.exists() ? file : resolveNativeFile(new File(libFolder, "libpty"), fileName);
}
public static File resolveNativeFile(File parent, String fileName) {
final File path = new File(parent, getPlatformFolder());
String arch = Platform.is64Bit() ? "x86_64" : "x86";
String prefix = isWinXp() ? "xp" : arch;
if (new File(parent, prefix).exists()) {
return new File(new File(parent, prefix), fileName);
} else {
return new File(new File(path, prefix), fileName);
}
}
private static String getPlatformFolder() {
String result;
if (Platform.isMac()) {
result = "macosx";
} else if (Platform.isWindows()) {
result = "win";
} else if (Platform.isLinux()) {
result = "linux";
} else if (Platform.isFreeBSD()) {
result = "freebsd";
} else if (Platform.isOpenBSD()) {
result = "openbsd";
} else {
throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
}
return result;
}
private static String getNativeLibraryName() {
String result;
if (Platform.isMac()) {
result = "libpty.dylib";
} else if (Platform.isWindows()) {
result = "winpty.dll";
} else if (Platform.isLinux() || Platform.isFreeBSD() || Platform.isOpenBSD()) {
result = "libpty.so";
} else {
throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
}
return result;
}
public static boolean isWinXp() {
return Platform.isWindows() && (OS_VERSION.equals("5.1") || OS_VERSION.equals("5.2"));
}
}