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