Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/tools/jpackage/apps/Hello.java
66644 views
1
/*
2
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.AWTError;
25
import java.awt.Desktop;
26
import java.awt.GraphicsEnvironment;
27
import java.awt.desktop.OpenFilesEvent;
28
import java.awt.desktop.OpenFilesHandler;
29
import java.io.File;
30
import java.io.IOException;
31
import java.io.PrintWriter;
32
import java.io.StringWriter;
33
import java.nio.file.Path;
34
import java.nio.file.Files;
35
import java.util.stream.Collectors;
36
import java.util.List;
37
import java.util.ArrayList;
38
import java.util.stream.Stream;
39
import java.util.Collections;
40
import java.util.Optional;
41
42
public class Hello implements OpenFilesHandler {
43
44
public static void main(String[] args) throws IOException, InterruptedException {
45
var faFiles = getFaFiles();
46
if (faFiles != null) {
47
// Some files got opened through fa mechanizm.
48
// They are the arguments then.
49
args = faFiles.toArray(String[]::new);
50
}
51
52
var lines = printArgs(args);
53
54
Stream.of(args).forEach(arg -> System.out.println(
55
arg.codePoints()
56
.mapToObj(codePoint -> String.format("0x%04x", codePoint))
57
.collect(Collectors.joining(",", "[", "]"))));
58
59
lines.forEach(System.out::println);
60
61
var outputFile = getOutputFile(args);
62
trace(String.format("Output file: [%s]", outputFile));
63
Files.write(outputFile, lines);
64
65
if (Optional.ofNullable(System.getProperty("jpackage.test.noexit")).map(
66
Boolean::parseBoolean).orElse(false)) {
67
trace("noexit");
68
var lock = new Object();
69
synchronized (lock) {
70
lock.wait();
71
}
72
}
73
74
System.exit(Integer.getInteger("jpackage.test.exitCode", 0));
75
}
76
77
private static List<String> printArgs(String[] args) {
78
List<String> lines = new ArrayList<>();
79
lines.add(MSG);
80
81
lines.add("args.length: " + args.length);
82
83
for (String arg : args) {
84
if (arg.startsWith("jpackage.app")) {
85
lines.add(arg + "=" + System.getProperty(arg));
86
} else {
87
lines.add(arg);
88
}
89
}
90
91
for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {
92
String value = System.getProperty("param" + index);
93
if (value != null) {
94
lines.add("-Dparam" + index + "=" + value);
95
}
96
}
97
98
return lines;
99
}
100
101
private static Path getOutputFile(String[] args) {
102
Path outputFilePath = Path.of(Optional.ofNullable(System.getProperty(
103
"jpackage.test.appOutput")).orElse("appOutput.txt"));
104
105
// If first arg is a file (most likely from fa), then put output in the same folder as
106
// the file from fa.
107
if (args.length >= 1) {
108
Path faPath = Path.of(args[0]);
109
if (Files.exists(faPath)) {
110
return faPath.toAbsolutePath().getParent().resolve(outputFilePath);
111
}
112
}
113
114
try {
115
// Try writing in the default output file.
116
Files.write(outputFilePath, Collections.emptyList());
117
return outputFilePath.toAbsolutePath();
118
} catch (IOException ex) {
119
// Log reason of a failure.
120
StringWriter errors = new StringWriter();
121
ex.printStackTrace(new PrintWriter(errors));
122
Stream.of(errors.toString().split("\\R")).forEachOrdered(Hello::trace);
123
}
124
125
return Path.of(System.getProperty("user.home")).resolve(outputFilePath).toAbsolutePath();
126
}
127
128
@Override
129
public void openFiles(OpenFilesEvent e) {
130
synchronized(lock) {
131
trace("openFiles");
132
files = e.getFiles().stream()
133
.map(File::toString)
134
.collect(Collectors.toList());
135
136
lock.notifyAll();
137
}
138
}
139
140
private static List<String> getFaFiles() throws InterruptedException {
141
if (openFilesHandler == null) {
142
return null;
143
}
144
145
synchronized(openFilesHandler.lock) {
146
trace("getFaFiles: wait");
147
openFilesHandler.lock.wait(1000);
148
if (openFilesHandler.files == null) {
149
trace(String.format("getFaFiles: no files"));
150
return null;
151
}
152
// Return copy of `files` to keep access to `files` field synchronized.
153
trace(String.format("getFaFiles: file count %d",
154
openFilesHandler.files.size()));
155
return new ArrayList<>(openFilesHandler.files);
156
}
157
}
158
159
private List<String> files;
160
private final Object lock = new Object();
161
private final static Hello openFilesHandler = createInstance();
162
163
private static Hello createInstance() {
164
if (GraphicsEnvironment.isHeadless()) {
165
return null;
166
}
167
168
trace("Environment supports a display");
169
170
if (!Desktop.isDesktopSupported()) {
171
return null;
172
}
173
174
trace("Environment supports a desktop");
175
176
try {
177
// Disable JAB.
178
// Needed to suppress error:
179
// Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge
180
System.setProperty("javax.accessibility.assistive_technologies", "");
181
} catch (SecurityException ex) {
182
ex.printStackTrace();
183
}
184
185
try {
186
var desktop = Desktop.getDesktop();
187
if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) {
188
trace("Set file handler");
189
Hello instance = new Hello();
190
desktop.setOpenFileHandler(instance);
191
return instance;
192
}
193
} catch (AWTError ex) {
194
trace("Set file handler failed");
195
ex.printStackTrace();
196
}
197
198
return null;
199
}
200
201
private static final String MSG = "jpackage test application";
202
private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1
203
204
private static void trace(String msg) {
205
System.out.println("hello: " + msg);
206
}
207
}
208
209