You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DMDircResourceManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.resourcemanager;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.Logger;
  26. import java.io.File;
  27. import java.io.UnsupportedEncodingException;
  28. import java.net.MalformedURLException;
  29. import java.net.URI;
  30. import java.net.URISyntaxException;
  31. import java.net.URL;
  32. import java.net.URLDecoder;
  33. import java.security.CodeSource;
  34. /**
  35. * Provides utility methods for working with resources from the DMDirc distribution.
  36. */
  37. public final class DMDircResourceManager {
  38. private DMDircResourceManager() {
  39. //Prevent instantiation
  40. }
  41. /**
  42. * Returns the working directory for the application.
  43. *
  44. * @return Current working directory
  45. */
  46. public static synchronized String getWorkingDirectory() {
  47. final CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
  48. try {
  49. final File jarFile = new File(codeSource.getLocation().toURI().getPath());
  50. String jarDir;
  51. if (isRunningFromJar()) {
  52. jarDir = jarFile.getParentFile().getPath();
  53. } else {
  54. jarDir = jarFile.getPath();
  55. }
  56. return URLDecoder.decode(jarDir, "UTF-8");
  57. } catch (URISyntaxException | UnsupportedEncodingException ex) {
  58. Logger.appError(ErrorLevel.FATAL, "Unable to find working directory", ex);
  59. return null;
  60. }
  61. }
  62. /**
  63. * Returns the working directory for the application or the jar name.
  64. *
  65. * @return Current working directory
  66. */
  67. public static synchronized String getApplicationDirectory() {
  68. return getWorkingDirectoryString(Thread.currentThread()
  69. .getContextClassLoader().getResource("com/dmdirc/Main.class"));
  70. }
  71. /**
  72. * Returns the working directory for the application
  73. *
  74. * @param mainClassURL Main class of the application
  75. *
  76. * @return location or null
  77. */
  78. static String getWorkingDirectoryString(final URL mainClassURL) {
  79. try {
  80. return URLDecoder.decode(getWorkingDirectoryURL(mainClassURL).getPath(), "UTF-8");
  81. } catch (UnsupportedEncodingException ex) {
  82. Logger.userError(ErrorLevel.MEDIUM, "Unable to decode path");
  83. }
  84. return "";
  85. }
  86. /**
  87. * Returns the working directory for the application
  88. *
  89. * @param mainClassURL Main class of the application
  90. *
  91. * @return URL location or null
  92. */
  93. static URL getWorkingDirectoryURL(final URL mainClassURL) {
  94. if (isRunningFromJar(mainClassURL)) {
  95. return getJarURL(mainClassURL);
  96. } else {
  97. return getFileURL();
  98. }
  99. }
  100. /**
  101. * Returns the URL of the folder the client is loaded from.
  102. *
  103. * @return URL of client folder file or null
  104. */
  105. protected static URL getFileURL() {
  106. return Thread.currentThread().getContextClassLoader().getResource("");
  107. }
  108. /**
  109. * Returns the URL of the jar the class is loaded from.
  110. *
  111. * @param mainClassURL Class to get Jar file from.
  112. *
  113. * @return URL of jar file or null
  114. */
  115. protected static URL getJarURL(final URL mainClassURL) {
  116. try {
  117. return new URI(mainClassURL.getPath().substring(0, mainClassURL.getPath()
  118. .indexOf("!/"))).toURL();
  119. } catch (MalformedURLException | URISyntaxException ex) {
  120. return null;
  121. }
  122. }
  123. /**
  124. * Determines if this instance of DMDirc is running from a jar or not.
  125. *
  126. * @return True if this instance is running from a JAR, false otherwise
  127. */
  128. public static boolean isRunningFromJar() {
  129. return isRunningFromJar(Thread.currentThread().getContextClassLoader().
  130. getResource("com/dmdirc/Main.class"));
  131. }
  132. /**
  133. * Determines if this instance of DMDirc is running from a jar or not.
  134. *
  135. * @param mainClassURL URL of main class
  136. *
  137. * @return True if this instance is running from a JAR, false otherwise
  138. */
  139. protected static boolean isRunningFromJar(final URL mainClassURL) {
  140. return "jar".equals(mainClassURL.getProtocol());
  141. }
  142. }