|
| 1 | +package org.red5.resource; |
| 2 | + |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.nio.file.Paths; |
| 5 | +import java.security.CodeSource; |
| 6 | +import java.security.ProtectionDomain; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.concurrent.locks.ReentrantLock; |
| 9 | + |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +/** |
| 14 | + * Utility class to resolve server root |
| 15 | + * @author Andy Shaules |
| 16 | + */ |
| 17 | +public class Red5Root { |
| 18 | + |
| 19 | + protected static Logger log = LoggerFactory.getLogger(Red5Root.class); |
| 20 | + |
| 21 | + private static final String PropertyRed5Root = "red5.root"; |
| 22 | + |
| 23 | + private static final String VarRed5Home = "RED5_HOME"; |
| 24 | + |
| 25 | + private static final String VarUnixPWD = "PWD"; |
| 26 | + |
| 27 | + private static final String PropertyUserDir = "user.dir"; |
| 28 | + |
| 29 | + private static String homeDirectory; |
| 30 | + |
| 31 | + private static ReentrantLock lookupLock = new ReentrantLock(); |
| 32 | + |
| 33 | + private static ThreadLocal<Throwable> lastError = ThreadLocal.withInitial(()->null); |
| 34 | + /** |
| 35 | + * Get Root directory of server. |
| 36 | + * @return String path or throws exception. |
| 37 | + * @throws RootResolutionException if server root cannot be resolved. |
| 38 | + */ |
| 39 | + public static String get() throws RootResolutionException { |
| 40 | + if (homeDirectory == null) { |
| 41 | + lookupLock.lock();//After acquiring the lock, ensure the condition directing this thread to the lock is still true. |
| 42 | + try { |
| 43 | + if (homeDirectory == null) { |
| 44 | + homeDirectory = resolve(); |
| 45 | + } |
| 46 | + } finally { |
| 47 | + lookupLock.unlock(); |
| 48 | + } |
| 49 | + } |
| 50 | + if (homeDirectory == null) { |
| 51 | + if(lastError.get()!=null) { |
| 52 | + try { |
| 53 | + throw new RootResolutionException("Server root path cannot be resolved from system/env properties or code location.",lastError.get()); |
| 54 | + }finally { |
| 55 | + lastError.remove(); |
| 56 | + } |
| 57 | + }else { |
| 58 | + throw new RootResolutionException("Server root path cannot be resolved from system/env properties or code location."); |
| 59 | + } |
| 60 | + } |
| 61 | + return homeDirectory; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns grandparent directory of this jar, assumed to be in lib folder of 'root/lib/red5-io-version.jar' |
| 66 | + * <p> |
| 67 | + * Uses: |
| 68 | + * <pre>{@code |
| 69 | + * sysvar : 'red5.root |
| 70 | + * envar : 'RED5_HOME' |
| 71 | + * envar : 'PWD' |
| 72 | + * sysvar : 'user.dir'} |
| 73 | + * </pre> |
| 74 | + * As a last resort , it uses {@code Class::getProtectionDomain::getCodeSource::getLocation} |
| 75 | + * |
| 76 | + * @return server root or null. |
| 77 | + */ |
| 78 | + private static String resolve() { |
| 79 | + /* |
| 80 | + We could also look up: |
| 81 | + sysvars: 'red5.config_root'='X:\red5\server\conf' |
| 82 | + sysvars: 'red5.plugins_root'='X:\red5\server/plugins' |
| 83 | + sysvars: 'red5.webapp.root'='X:\red5\server/webapps' |
| 84 | + */ |
| 85 | + String path = null; |
| 86 | + try { |
| 87 | + path = System.getProperty(PropertyRed5Root);//Exported system property set by application launch script with value of server's directory. //red5.config_root |
| 88 | + if (path == null) { |
| 89 | + path = System.getenv(VarRed5Home);//Exported system property set by application launch script with value of server's directory. |
| 90 | + if (path == null) { |
| 91 | + path = System.getenv(VarUnixPWD);//unix |
| 92 | + if (path == null) { |
| 93 | + path = System.getProperty(PropertyUserDir);// |
| 94 | + if (path == null) { |
| 95 | + //Last resort. find this jar location of lib folder, and resolve root directory. |
| 96 | + path = Optional.of(Red5Root.class).map(Class::getProtectionDomain).map(ProtectionDomain::getCodeSource).map(CodeSource::getLocation).map(location -> { |
| 97 | + try { |
| 98 | + return Paths.get(location.toURI()); |
| 99 | + } catch (Exception e) { |
| 100 | + log.warn("",e); |
| 101 | + // Wrap URI-specific issues |
| 102 | + throw new RuntimeException("Failed to convert URL to URI", e); |
| 103 | + } |
| 104 | + }).map(Path::getParent).map(Path::getParent).map(Path::toString).orElse(null); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (Throwable t) { |
| 110 | + //Catch everything possible |
| 111 | + lastError.set(t);//Bubble it to the caller gracefully. |
| 112 | + log.debug("",t); |
| 113 | + } |
| 114 | + return path; |
| 115 | + } |
| 116 | +} |
0 commit comments