Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / desktopopen / DefaultDesktopOpen.java @ 2250

History | View | Annotate | Download (6.99 KB)

1
package org.gvsig.desktopopen;
2

    
3
import java.awt.Desktop;
4
import java.io.File;
5
import java.io.IOException;
6
import java.net.URI;
7
import java.util.ArrayList;
8
import java.util.List;
9
import org.apache.commons.lang3.StringUtils;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

    
13
/*
14
 * Based on portions of code of MightyPork, http://www.ondrovo.com/
15
 * extracted from :
16
 * http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
17
 */
18
@SuppressWarnings("UseSpecificCatch")
19
public class DefaultDesktopOpen implements DesktopOpen {
20

    
21
    private static Logger LOGGER = LoggerFactory.getLogger(DefaultDesktopOpen.class);
22

    
23
    public enum EnumOS {
24

    
25
        linux, macos, solaris, unknown, windows;
26

    
27
        public boolean isLinux() {
28
            return this == linux || this == solaris;
29
        }
30

    
31
        public boolean isMac() {
32
            return this == macos;
33
        }
34

    
35
        public boolean isWindows() {
36
            return this == windows;
37
        }
38
    }
39

    
40
    @Override
41
    public boolean browse(URI uri) {
42
        if (browseDESKTOP(uri)) {
43
            return true;
44
        }
45
        return openSystemSpecific(uri.toString());
46
    }
47

    
48
    @Override
49
    public boolean open(File file) {
50
        if (openDESKTOP(file)) {
51
            return true;
52
        }
53
        return openSystemSpecific(file.getPath());
54
    }
55

    
56
    @Override
57
    public boolean edit(File file) {
58
        if (editDESKTOP(file)) {
59
            return true;
60
        }
61
        return openSystemSpecific(file.getPath());
62
    }
63

    
64
    private boolean openSystemSpecific(String what) {
65
        EnumOS os = getOs();
66
        if (os.isLinux()) {
67
//            if (runCommand("gnome-open", "%s", what)) {
68
//                return true;
69
//            }
70
//            if (runCommand("kde-open", "%s", what)) {
71
//                return true;
72
//            }
73
            if (runCommand(new String[]{"xdg-open", what})) {
74
                return true;
75
            }
76
        }
77
        if (os.isMac()) {
78
            if (runCommand(new String[]{"open", what})) {
79
                return true;
80
            }
81
        }
82
        if (os.isWindows()) {
83
            if (runCommand(new String[]{"explorer", what})) {
84
                return true;
85
            }
86
        }
87
        return false;
88
    }
89

    
90
    private boolean browseDESKTOP(URI uri) {
91
        LOGGER.info("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
92
        try {
93
            if (!Desktop.isDesktopSupported()) {
94
                LOGGER.warn("Platform is not supported.");
95
                return false;
96
            }
97
            if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
98
                LOGGER.warn("BROWSE is not supported.");
99
                return false;
100
            }
101
            Desktop.getDesktop().browse(uri);
102
            return true;
103
        } catch (Throwable t) {
104
            if (LOGGER.isDebugEnabled()) {
105
                LOGGER.warn("Error using desktop browse.", t);
106
            } else {
107
                LOGGER.warn("Error using Desktop.getDesktop().browse(). " + t.toString());
108
            }
109
            return false;
110
        }
111
    }
112

    
113
    private boolean openDESKTOP(File file) {
114
        LOGGER.info("Trying to use Desktop.getDesktop().open() with " + file.toString());
115
        try {
116
            if (!Desktop.isDesktopSupported()) {
117
                LOGGER.warn("Platform is not supported.");
118
                return false;
119
            }
120
            if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
121
                LOGGER.warn("OPEN is not supported.");
122
                return false;
123
            }
124
            Desktop.getDesktop().open(file);
125
            return true;
126
        } catch (Throwable t) {
127
            if (LOGGER.isDebugEnabled()) {
128
                LOGGER.warn("Error using desktop open.", t);
129
            } else {
130
                LOGGER.warn("Error using Desktop.getDesktop().open(). " + t.toString());
131
            }
132
            return false;
133
        }
134
    }
135

    
136
    private boolean editDESKTOP(File file) {
137
        LOGGER.info("Trying to use Desktop.getDesktop().edit() with " + file);
138
        try {
139
            if (!Desktop.isDesktopSupported()) {
140
                LOGGER.warn("Platform is not supported.");
141
                return false;
142
            }
143
            if (!Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
144
                LOGGER.warn("EDIT is not supported.");
145
                return false;
146
            }
147
            Desktop.getDesktop().edit(file);
148
            return true;
149
        } catch (Throwable t) {
150
            if (LOGGER.isDebugEnabled()) {
151
                LOGGER.warn("Error using desktop edit.", t);
152
            } else {
153
                LOGGER.warn("Error using Desktop.getDesktop().edit(). " + t.toString());
154
            }
155
            return false;
156
        }
157
    }
158

    
159
    private boolean runCommand(String command, String args, String file) {
160
        String[] parts = prepareCommand(command, args, file);
161
        return runCommand(parts);
162
    }
163

    
164
    private boolean runCommand(String[] cmdarray) {
165
//    cmdarray  array containing the command to call and its arguments.
166

    
167
        LOGGER.info("Trying to exec:\n   " + StringUtils.join(cmdarray, ","));
168
        try {
169
            Process p = Runtime.getRuntime().exec(cmdarray);
170
            if (p == null) {
171
                return false;
172
            }
173

    
174
            try {
175
                int retval = p.exitValue();
176
                if (retval == 0) {
177
                    LOGGER.warn("Process ended immediately.");
178
                    return false;
179
                } else {
180
                    LOGGER.warn("Process crashed.");
181
                    return false;
182
                }
183
            } catch (IllegalThreadStateException itse) {
184
                LOGGER.warn("Process is running.");
185
                return true;
186
            }
187
        } catch (IOException e) {
188
            if (LOGGER.isDebugEnabled()) {
189
                LOGGER.warn("Error running command.", e);
190
            } else {
191
                LOGGER.warn("Error running command. " + e.toString());
192
            }
193
            return false;
194
        }
195
    }
196

    
197
    private String[] prepareCommand(String command, String args, String file) {
198
        List<String> parts = new ArrayList<>();
199
        parts.add(command);
200
        if (args != null) {
201
            for (String s : args.split(" ")) {
202
                s = String.format(s, file); // put in the filename thing
203
                parts.add(s.trim());
204
            }
205
        }
206
        return parts.toArray(new String[parts.size()]);
207
    }
208

    
209
    public EnumOS getOs() {
210
        String s = System.getProperty("os.name").toLowerCase();
211
        if (s.contains("win")) {
212
            return EnumOS.windows;
213
        }
214
        if (s.contains("mac")) {
215
            return EnumOS.macos;
216
        }
217
        if (s.contains("solaris")) {
218
            return EnumOS.solaris;
219
        }
220
        if (s.contains("sunos")) {
221
            return EnumOS.solaris;
222
        }
223
        if (s.contains("linux")) {
224
            return EnumOS.linux;
225
        }
226
        if (s.contains("unix")) {
227
            return EnumOS.linux;
228
        } else {
229
            return EnumOS.unknown;
230
        }
231
    }
232
}