Path: blob/master/test/jdk/tools/jpackage/apps/image/Hello.java
41153 views
/*1* Copyright (c) 2018, 2019, 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;3940public class Hello implements OpenFilesHandler {4142public static void main(String[] args) throws IOException, InterruptedException {43var faFiles = getFaFiles();44if (faFiles != null) {45// Some files got opened through fa mechanizm.46// They are the arguments then.47args = faFiles.toArray(String[]::new);48}4950var lines = printArgs(args);5152Stream.of(args).forEach(arg -> System.out.println(53arg.codePoints()54.mapToObj(codePoint -> String.format("0x%04x", codePoint))55.collect(Collectors.joining(",", "[", "]"))));5657lines.forEach(System.out::println);5859var outputFile = getOutputFile(args);60trace(String.format("Output file: [%s]", outputFile));61Files.write(outputFile, lines);62}6364private static List<String> printArgs(String[] args) {65List<String> lines = new ArrayList<>();66lines.add(MSG);6768lines.add("args.length: " + args.length);6970for (String arg : args) {71if (arg.startsWith("jpackage.app")) {72lines.add(arg + "=" + System.getProperty(arg));73} else {74lines.add(arg);75}76}7778for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {79String value = System.getProperty("param" + index);80if (value != null) {81lines.add("-Dparam" + index + "=" + value);82}83}8485return lines;86}8788private static Path getOutputFile(String[] args) {89Path outputFilePath = Path.of("appOutput.txt");9091// If first arg is a file (most likely from fa), then put output in the same folder as92// the file from fa.93if (args.length >= 1) {94Path faPath = Path.of(args[0]);95if (Files.exists(faPath)) {96return faPath.toAbsolutePath().getParent().resolve(outputFilePath);97}98}99100try {101// Try writing in the default output file.102Files.write(outputFilePath, Collections.emptyList());103return outputFilePath;104} catch (IOException ex) {105// Log reason of a failure.106StringWriter errors = new StringWriter();107ex.printStackTrace(new PrintWriter(errors));108Stream.of(errors.toString().split("\\R")).forEachOrdered(Hello::trace);109}110111return Path.of(System.getProperty("user.home")).resolve(outputFilePath);112}113114@Override115public void openFiles(OpenFilesEvent e) {116synchronized(lock) {117trace("openFiles");118files = e.getFiles().stream()119.map(File::toString)120.collect(Collectors.toList());121122lock.notifyAll();123}124}125126private static List<String> getFaFiles() throws InterruptedException {127if (openFilesHandler == null) {128return null;129}130131synchronized(openFilesHandler.lock) {132trace("getFaFiles: wait");133openFilesHandler.lock.wait(1000);134if (openFilesHandler.files == null) {135trace(String.format("getFaFiles: no files"));136return null;137}138// Return copy of `files` to keep access to `files` field synchronized.139trace(String.format("getFaFiles: file count %d",140openFilesHandler.files.size()));141return new ArrayList<>(openFilesHandler.files);142}143}144145private List<String> files;146private final Object lock = new Object();147private final static Hello openFilesHandler = createInstance();148149private static Hello createInstance() {150if (GraphicsEnvironment.isHeadless()) {151return null;152}153154trace("Environment supports a display");155156if (!Desktop.isDesktopSupported()) {157return null;158}159160trace("Environment supports a desktop");161162try {163// Disable JAB.164// Needed to suppress error:165// Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge166System.setProperty("javax.accessibility.assistive_technologies", "");167} catch (SecurityException ex) {168ex.printStackTrace();169}170171try {172var desktop = Desktop.getDesktop();173if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) {174trace("Set file handler");175Hello instance = new Hello();176desktop.setOpenFileHandler(instance);177return instance;178}179} catch (AWTError ex) {180trace("Set file handler failed");181ex.printStackTrace();182}183184return null;185}186187private static final String MSG = "jpackage test application";188private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1189190private static void trace(String msg) {191System.out.println("hello: " + msg);192}193}194195196