LocatorImpl.java
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.commons.lang.SystemUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.handler.ffmpeg;
import com.day.cq.dam.handler.ffmpeg.ExecutableLocator;
import java.io.File;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Day CQ Executable Locator", description="Locates command line executables")
@Service
@Properties(value={@Property(name="service.description", value={"CQ Executable Locator"})})
public class LocatorImpl
implements ExecutableLocator {
private static final Logger log = LoggerFactory.getLogger(LocatorImpl.class);
@Property(value={"/opt/local/bin", "/usr/local/bin", "PATH"})
public static final String PROP_SEARCH_PATH = "executable.searchpath";
private static final String PATH_ENV = "PATH";
private static final String[] FALLBACK_SEARCH_PATH = new String[]{"PATH"};
private List<String> searchPath;
private File last;
public LocatorImpl() {
}
public LocatorImpl(String[] searchPath) {
this.setSearchPath(searchPath);
}
protected void activate(ComponentContext componentContext) {
Dictionary properties = componentContext.getProperties();
String[] sp = OsgiUtil.toStringArray(properties.get("executable.searchpath"));
if (sp == null || sp.length == 0) {
this.setSearchPath(FALLBACK_SEARCH_PATH);
} else {
this.setSearchPath(sp);
}
}
protected void setSearchPath(String[] sp) {
this.searchPath = new ArrayList<String>();
for (int i = 0; i < sp.length; ++i) {
if ("PATH".equals(sp[i])) {
String[] paths = LocatorImpl.getPathEnv();
for (int j = 0; j < paths.length; ++j) {
this.searchPath.add(paths[j]);
}
continue;
}
this.searchPath.add(sp[i]);
}
log.info("Search path: {}", (Object)StringUtils.join((Object[])this.searchPath.toArray(), (String)", "));
}
@Override
public synchronized String getPath(String cmd) {
if (SystemUtils.IS_OS_WINDOWS && cmd != null && !cmd.endsWith(".exe")) {
cmd = cmd + ".exe";
}
if (this.last != null && this.last.getName().equals(cmd) && this.last.exists()) {
log.debug("Found executable '{}' (cached): {}", (Object)cmd, (Object)this.last.getAbsolutePath());
return this.last.getAbsolutePath();
}
if (this.searchPath != null) {
for (String path : this.searchPath) {
File file = new File(path, cmd);
if (!file.exists()) continue;
this.last = file;
log.debug("Found executable '{}': {}", (Object)cmd, (Object)file.getAbsolutePath());
return file.getAbsolutePath();
}
}
log.warn("Could not find executable '{}'!", (Object)cmd);
return null;
}
public static String[] getPathEnv() {
String path = System.getenv("PATH");
if (path == null) {
path = System.getenv("Path");
}
if (path == null) {
return new String[0];
}
return StringUtils.split((String)path, (char)File.pathSeparatorChar);
}
}