|
1 | 1 | package org.bridj; |
2 | 2 |
|
3 | 3 | import java.io.*; |
| 4 | +import java.net.URL; |
4 | 5 |
|
5 | 6 | import java.lang.reflect.Method; |
6 | 7 | import java.lang.reflect.Modifier; |
|
13 | 14 | import static org.bridj.JNI.*; |
14 | 15 |
|
15 | 16 | /** |
16 | | - * Methods that give information about the execution platform (OS, architecture, native sizes...) |
| 17 | + * Information about the execution platform (OS, architecture, native sizes...) and platform-specific actions. |
| 18 | + * <ul> |
| 19 | + * <li>To know if the JVM platform is 32 bits or 64 bits, use {@link Platform#is64Bits()} |
| 20 | + * </li><li>To know if the OS is an Unix-like system, use {@link Platform#isUnix()} |
| 21 | + * </li><li>To open files and URLs in a platform-specific way, use {@link Platform#open(File)}, {@link Platform#open(URL)}, {@link Platform#show(File)} |
| 22 | + * </li></ul> |
17 | 23 | * @author ochafik |
18 | 24 | */ |
19 | 25 | public class Platform { |
@@ -103,7 +109,7 @@ static String getEmbeddedLibraryResource(String name) { |
103 | 109 | } |
104 | 110 | throw new RuntimeException("Platform not supported ! (os.name='" + osName + "', os.arch='" + System.getProperty("os.arch") + "')"); |
105 | 111 | } |
106 | | - public static File extractEmbeddedLibraryResource(String name) throws IOException { |
| 112 | + static File extractEmbeddedLibraryResource(String name) throws IOException { |
107 | 113 | String libraryResource = getEmbeddedLibraryResource(name); |
108 | 114 | int i = libraryResource.lastIndexOf('.'); |
109 | 115 | String ext = i < 0 ? "" : libraryResource.substring(i); |
@@ -131,4 +137,97 @@ public static File extractEmbeddedLibraryResource(String name) throws IOExceptio |
131 | 137 |
|
132 | 138 | return libFile; |
133 | 139 | } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * Opens an URL with the default system action. |
| 144 | + * @param url url to open |
| 145 | + * @throws NoSuchMethodException if opening an URL on the current platform is not supported |
| 146 | + */ |
| 147 | + public static final void open(URL url) throws NoSuchMethodException { |
| 148 | + if (url.getProtocol().equals("file")) { |
| 149 | + open(new File(url.getFile())); |
| 150 | + } else { |
| 151 | + if (Platform.isMacOSX()) { |
| 152 | + execArgs("open", url.toString()); |
| 153 | + } else if (Platform.isWindows()) { |
| 154 | + execArgs("rundll32", "url.dll,FileProtocolHandler", url.toString()); |
| 155 | + } else if (Platform.isUnix() && hasUnixCommand("gnome-open")) { |
| 156 | + execArgs("gnome-open", url.toString()); |
| 157 | + } else if (Platform.isUnix() && hasUnixCommand("konqueror")) { |
| 158 | + execArgs("konqueror", url.toString()); |
| 159 | + } else if (Platform.isUnix() && hasUnixCommand("mozilla")) { |
| 160 | + execArgs("mozilla", url.toString()); |
| 161 | + } else { |
| 162 | + throw new NoSuchMethodException("Cannot open urls on this platform"); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Opens a file with the default system action. |
| 169 | + * @param file file to open |
| 170 | + * @throws NoSuchMethodException if opening a file on the current platform is not supported |
| 171 | + */ |
| 172 | + public static final void open(File file) throws NoSuchMethodException { |
| 173 | + if (Platform.isMacOSX()) { |
| 174 | + execArgs("open", file.getAbsolutePath()); |
| 175 | + } else if (Platform.isWindows()) { |
| 176 | + if (file.isDirectory()) { |
| 177 | + execArgs("explorer", file.getAbsolutePath()); |
| 178 | + } else { |
| 179 | + execArgs("start", file.getAbsolutePath()); |
| 180 | + } |
| 181 | + } |
| 182 | + if (Platform.isUnix() && hasUnixCommand("gnome-open")) { |
| 183 | + execArgs("gnome-open", file.toString()); |
| 184 | + } else if (Platform.isUnix() && hasUnixCommand("konqueror")) { |
| 185 | + execArgs("konqueror", file.toString()); |
| 186 | + } else if (Platform.isSolaris() && file.isDirectory()) { |
| 187 | + execArgs("/usr/dt/bin/dtfile", "-folder", file.getAbsolutePath()); |
| 188 | + } else { |
| 189 | + throw new NoSuchMethodException("Cannot open files on this platform"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Show a file in its parent directory, if possible selecting the file (not possible on all platforms). |
| 195 | + * @param file file to show in the system's default file navigator |
| 196 | + * @throws NoSuchMethodException if showing a file on the current platform is not supported |
| 197 | + */ |
| 198 | + public static final void show(File file) throws NoSuchMethodException, IOException { |
| 199 | + if (Platform.isWindows()) { |
| 200 | + exec("explorer /e,/select,\"" + file.getCanonicalPath() + "\""); |
| 201 | + } else { |
| 202 | + open(file.getAbsoluteFile().getParentFile()); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + static final void execArgs(String... cmd) throws NoSuchMethodException { |
| 207 | + try { |
| 208 | + Runtime.getRuntime().exec(cmd); |
| 209 | + } catch (Exception ex) { |
| 210 | + ex.printStackTrace(); |
| 211 | + throw new NoSuchMethodException(ex.toString()); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + static final void exec(String cmd) throws NoSuchMethodException { |
| 216 | + try { |
| 217 | + Runtime.getRuntime().exec(cmd).waitFor(); |
| 218 | + } catch (Exception ex) { |
| 219 | + ex.printStackTrace(); |
| 220 | + throw new NoSuchMethodException(ex.toString()); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + static final boolean hasUnixCommand(String name) { |
| 225 | + try { |
| 226 | + Process p = Runtime.getRuntime().exec(new String[]{"which", name}); |
| 227 | + return p.waitFor() == 0; |
| 228 | + } catch (Exception ex) { |
| 229 | + ex.printStackTrace(); |
| 230 | + return false; |
| 231 | + } |
| 232 | + } |
134 | 233 | } |
0 commit comments