19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
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 |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.BufferedOutputStream; |
24 import java.awt.AWTError; |
25 import java.io.FileOutputStream; |
25 import java.awt.Desktop; |
26 import java.io.PrintStream; |
26 import java.awt.GraphicsEnvironment; |
|
27 import java.awt.desktop.OpenFilesEvent; |
|
28 import java.awt.desktop.OpenFilesHandler; |
|
29 import java.io.File; |
27 import java.io.IOException; |
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; |
28 |
40 |
29 public class Hello { |
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 } |
30 |
170 |
31 private static final String MSG = "jpackage test application"; |
171 private static final String MSG = "jpackage test application"; |
32 private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1 |
172 private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1 |
33 |
173 |
34 public static void main(String[] args) throws IOException { |
174 private static void trace(String msg) { |
35 printArgs(args, System.out); |
175 System.out.println("hello: " + msg); |
36 |
|
37 try (PrintStream out = new PrintStream(new BufferedOutputStream( |
|
38 new FileOutputStream("appOutput.txt")))) { |
|
39 printArgs(args, out); |
|
40 } |
|
41 } |
|
42 |
|
43 private static void printArgs(String[] args, PrintStream out) { |
|
44 out.println(MSG); |
|
45 |
|
46 out.println("args.length: " + args.length); |
|
47 |
|
48 for (String arg : args) { |
|
49 out.println(arg); |
|
50 } |
|
51 |
|
52 for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) { |
|
53 String value = System.getProperty("param" + index); |
|
54 if (value != null) { |
|
55 out.println("-Dparam" + index + "=" + value); |
|
56 } |
|
57 } |
|
58 } |
176 } |
59 } |
177 } |