Path: blob/master/test/jdk/tools/jpackage/apps/Hello.java
66644 views
/*1* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.AWTError;24import java.awt.Desktop;25import java.awt.GraphicsEnvironment;26import java.awt.desktop.OpenFilesEvent;27import java.awt.desktop.OpenFilesHandler;28import java.io.File;29import java.io.IOException;30import java.io.PrintWriter;31import java.io.StringWriter;32import java.nio.file.Path;33import java.nio.file.Files;34import java.util.stream.Collectors;35import java.util.List;36import java.util.ArrayList;37import java.util.stream.Stream;38import java.util.Collections;39import java.util.Optional;4041public class Hello implements OpenFilesHandler {4243public static void main(String[] args) throws IOException, InterruptedException {44var faFiles = getFaFiles();45if (faFiles != null) {46// Some files got opened through fa mechanizm.47// They are the arguments then.48args = faFiles.toArray(String[]::new);49}5051var lines = printArgs(args);5253Stream.of(args).forEach(arg -> System.out.println(54arg.codePoints()55.mapToObj(codePoint -> String.format("0x%04x", codePoint))56.collect(Collectors.joining(",", "[", "]"))));5758lines.forEach(System.out::println);5960var outputFile = getOutputFile(args);61trace(String.format("Output file: [%s]", outputFile));62Files.write(outputFile, lines);6364if (Optional.ofNullable(System.getProperty("jpackage.test.noexit")).map(65Boolean::parseBoolean).orElse(false)) {66trace("noexit");67var lock = new Object();68synchronized (lock) {69lock.wait();70}71}7273System.exit(Integer.getInteger("jpackage.test.exitCode", 0));74}7576private static List<String> printArgs(String[] args) {77List<String> lines = new ArrayList<>();78lines.add(MSG);7980lines.add("args.length: " + args.length);8182for (String arg : args) {83if (arg.startsWith("jpackage.app")) {84lines.add(arg + "=" + System.getProperty(arg));85} else {86lines.add(arg);87}88}8990for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {91String value = System.getProperty("param" + index);92if (value != null) {93lines.add("-Dparam" + index + "=" + value);94}95}9697return lines;98}99100private static Path getOutputFile(String[] args) {101Path outputFilePath = Path.of(Optional.ofNullable(System.getProperty(102"jpackage.test.appOutput")).orElse("appOutput.txt"));103104// If first arg is a file (most likely from fa), then put output in the same folder as105// the file from fa.106if (args.length >= 1) {107Path faPath = Path.of(args[0]);108if (Files.exists(faPath)) {109return faPath.toAbsolutePath().getParent().resolve(outputFilePath);110}111}112113try {114// Try writing in the default output file.115Files.write(outputFilePath, Collections.emptyList());116return outputFilePath.toAbsolutePath();117} catch (IOException ex) {118// Log reason of a failure.119StringWriter errors = new StringWriter();120ex.printStackTrace(new PrintWriter(errors));121Stream.of(errors.toString().split("\\R")).forEachOrdered(Hello::trace);122}123124return Path.of(System.getProperty("user.home")).resolve(outputFilePath).toAbsolutePath();125}126127@Override128public void openFiles(OpenFilesEvent e) {129synchronized(lock) {130trace("openFiles");131files = e.getFiles().stream()132.map(File::toString)133.collect(Collectors.toList());134135lock.notifyAll();136}137}138139private static List<String> getFaFiles() throws InterruptedException {140if (openFilesHandler == null) {141return null;142}143144synchronized(openFilesHandler.lock) {145trace("getFaFiles: wait");146openFilesHandler.lock.wait(1000);147if (openFilesHandler.files == null) {148trace(String.format("getFaFiles: no files"));149return null;150}151// Return copy of `files` to keep access to `files` field synchronized.152trace(String.format("getFaFiles: file count %d",153openFilesHandler.files.size()));154return new ArrayList<>(openFilesHandler.files);155}156}157158private List<String> files;159private final Object lock = new Object();160private final static Hello openFilesHandler = createInstance();161162private static Hello createInstance() {163if (GraphicsEnvironment.isHeadless()) {164return null;165}166167trace("Environment supports a display");168169if (!Desktop.isDesktopSupported()) {170return null;171}172173trace("Environment supports a desktop");174175try {176// Disable JAB.177// Needed to suppress error:178// Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge179System.setProperty("javax.accessibility.assistive_technologies", "");180} catch (SecurityException ex) {181ex.printStackTrace();182}183184try {185var desktop = Desktop.getDesktop();186if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) {187trace("Set file handler");188Hello instance = new Hello();189desktop.setOpenFileHandler(instance);190return instance;191}192} catch (AWTError ex) {193trace("Set file handler failed");194ex.printStackTrace();195}196197return null;198}199200private static final String MSG = "jpackage test application";201private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1202203private static void trace(String msg) {204System.out.println("hello: " + msg);205}206}207208209