1 /* 2 * Copyright (c) 2018, 2019, 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 41 public class Hello implements OpenFilesHandler { 42 43 public static void main(String[] args) throws IOException, InterruptedException { 44 var faFiles = getFaFiles(); 45 if (faFiles != null) { 46 // Some files got opened through fa mechanizm. 47 // They are the arguments then. 48 args = faFiles.toArray(String[]::new); 49 } 50 51 var lines = printArgs(args); 52 53 Stream.of(args).forEach(arg -> System.out.println( 54 arg.codePoints() 55 .mapToObj(codePoint -> String.format("0x%04x", codePoint)) 56 .collect(Collectors.joining(",", "[", "]")))); 57 58 lines.forEach(System.out::println); 59 60 var outputFile = getOutputFile(args); 61 trace(String.format("Output file: [%s]", outputFile)); 62 Files.write(outputFile, lines); 63 } 64 65 private static List<String> printArgs(String[] args) { 66 List<String> lines = new ArrayList<>(); 67 lines.add(MSG); 68 69 lines.add("args.length: " + args.length); 70 71 lines.addAll(List.of(args)); 72 73 for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) { 74 String value = System.getProperty("param" + index); 75 if (value != null) { 76 lines.add("-Dparam" + index + "=" + value); 77 } 78 } 79 80 return lines; 81 } 82 83 private static Path getOutputFile(String[] args) { 84 Path outputFilePath = Path.of("appOutput.txt"); 85 86 // If first arg is a file (most likely from fa), then put output in the same folder as 87 // the file from fa. 88 if (args.length >= 1) { 89 Path faPath = Path.of(args[0]); 90 if (Files.exists(faPath)) { 91 return faPath.toAbsolutePath().getParent().resolve(outputFilePath); 92 } 93 } 94 95 try { 96 // Try writing in the default output file. 97 Files.write(outputFilePath, Collections.emptyList()); 98 return outputFilePath; 99 } catch (IOException ex) { 100 // Log reason of a failure. 101 StringWriter errors = new StringWriter(); 102 ex.printStackTrace(new PrintWriter(errors)); 103 Stream.of(errors.toString().split("\\R")).forEachOrdered(Hello::trace); 104 } 105 106 return Path.of(System.getProperty("user.home")).resolve(outputFilePath); 107 } 108 109 @Override 110 public void openFiles(OpenFilesEvent e) { 111 synchronized(lock) { 112 trace("openFiles"); 113 files = e.getFiles().stream() 114 .map(File::toString) 115 .collect(Collectors.toList()); 116 117 lock.notifyAll(); 118 } 119 } 120 121 private static List<String> getFaFiles() throws InterruptedException { 122 if (openFilesHandler == null) { 123 return null; 124 } 125 126 synchronized(openFilesHandler.lock) { 127 trace("getFaFiles: wait"); 128 openFilesHandler.lock.wait(1000); 129 if (openFilesHandler.files == null) { 130 trace(String.format("getFaFiles: no files")); 131 return null; 132 } 133 // Return copy of `files` to keep access to `files` field synchronized. 134 trace(String.format("getFaFiles: file count %d", 135 openFilesHandler.files.size())); 136 return new ArrayList<>(openFilesHandler.files); 137 } 138 } 139 140 private List<String> files; 141 private final Object lock = new Object(); 142 private final static Hello openFilesHandler = createInstance(); 143 144 private static Hello createInstance() { 145 if (GraphicsEnvironment.isHeadless()) { 146 return null; 147 } 148 149 trace("Environment supports a display"); 150 151 try { 152 // Disable JAB. 153 // Needed to suppress error: 154 // Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge 155 System.setProperty("javax.accessibility.assistive_technologies", ""); 156 } catch (SecurityException ex) { 157 ex.printStackTrace(); 158 } 159 160 try { 161 var desktop = Desktop.getDesktop(); 162 if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) { 163 trace("Set file handler"); 164 Hello instance = new Hello(); 165 desktop.setOpenFileHandler(instance); 166 return instance; 167 } 168 } catch (AWTError ex) { 169 trace("Set file handler failed"); 170 ex.printStackTrace(); 171 } 172 173 return null; 174 } 175 176 private static final String MSG = "jpackage test application"; 177 private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1 178 179 private static void trace(String msg) { 180 System.out.println("hello: " + msg); 181 } 182 }