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.

DefaultServerFileSystem.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform;
  21. import org.apache.commons.io.FileUtils;
  22. import org.apache.commons.io.filefilter.FileFilterUtils;
  23. import org.picocontainer.Startable;
  24. import org.sonar.api.config.Settings;
  25. import org.sonar.api.platform.Server;
  26. import org.sonar.api.platform.ServerFileSystem;
  27. import org.sonar.api.utils.log.Logger;
  28. import org.sonar.api.utils.log.Loggers;
  29. import org.sonar.process.ProcessProperties;
  30. import javax.annotation.CheckForNull;
  31. import java.io.File;
  32. import java.io.FileFilter;
  33. import java.io.IOException;
  34. import java.util.ArrayList;
  35. import java.util.Collections;
  36. import java.util.List;
  37. /**
  38. * Introspect the filesystem and the classloader to get extension files at startup.
  39. *
  40. * @since 2.2
  41. */
  42. public class DefaultServerFileSystem implements ServerFileSystem, Startable {
  43. private static final Logger LOGGER = Loggers.get(DefaultServerFileSystem.class);
  44. private final Server server;
  45. private final File homeDir, tempDir;
  46. public DefaultServerFileSystem(Settings settings, Server server) {
  47. this.server = server;
  48. this.homeDir = new File(settings.getString(ProcessProperties.PATH_HOME));
  49. this.tempDir = new File(settings.getString(ProcessProperties.PATH_TEMP));
  50. }
  51. /**
  52. * for unit tests
  53. */
  54. public DefaultServerFileSystem(File homeDir, File tempDir, Server server) {
  55. this.homeDir = homeDir;
  56. this.tempDir = tempDir;
  57. this.server = server;
  58. }
  59. @Override
  60. public void start() {
  61. LOGGER.info("SonarQube home: " + homeDir.getAbsolutePath());
  62. File deployDir = getDeployDir();
  63. if (deployDir == null) {
  64. throw new IllegalArgumentException("Web app directory does not exist");
  65. }
  66. try {
  67. FileUtils.forceMkdir(deployDir);
  68. FileFilter fileFilter = FileFilterUtils.directoryFileFilter();
  69. File[] files = deployDir.listFiles(fileFilter);
  70. if (files != null) {
  71. for (File subDirectory : files) {
  72. FileUtils.cleanDirectory(subDirectory);
  73. }
  74. }
  75. } catch (IOException e) {
  76. throw new IllegalStateException("The following directory can not be created: " + deployDir.getAbsolutePath(), e);
  77. }
  78. File deprecated = getDeprecatedPluginsDir();
  79. try {
  80. FileUtils.forceMkdir(deprecated);
  81. FileUtils.cleanDirectory(deprecated);
  82. } catch (IOException e) {
  83. throw new IllegalStateException("The following directory can not be created: " + deprecated.getAbsolutePath(), e);
  84. }
  85. }
  86. @Override
  87. public void stop() {
  88. // do nothing
  89. }
  90. @Override
  91. public File getHomeDir() {
  92. return homeDir;
  93. }
  94. @Override
  95. public File getTempDir() {
  96. return tempDir;
  97. }
  98. @CheckForNull
  99. public File getDeployDir() {
  100. return server.getDeployDir();
  101. }
  102. public File getDeployedJdbcDriverIndex() {
  103. return new File(getDeployDir(), "jdbc-driver.txt");
  104. }
  105. public File getDeployedPluginsDir() {
  106. return new File(getDeployDir(), "plugins");
  107. }
  108. public File getDownloadedPluginsDir() {
  109. return new File(getHomeDir(), "extensions/downloads");
  110. }
  111. public File getInstalledPluginsDir() {
  112. return new File(getHomeDir(), "extensions/plugins");
  113. }
  114. public File getBundledPluginsDir() {
  115. return new File(getHomeDir(), "lib/bundled-plugins");
  116. }
  117. public File getCorePluginsDir() {
  118. return new File(getHomeDir(), "lib/core-plugins");
  119. }
  120. public File getDeprecatedPluginsDir() {
  121. return new File(getHomeDir(), "extensions/deprecated");
  122. }
  123. public File getPluginIndex() {
  124. return new File(getDeployDir(), "plugins/index.txt");
  125. }
  126. /**
  127. * @deprecated since 4.1
  128. */
  129. @Override
  130. @Deprecated
  131. public List<File> getExtensions(String dirName, String... suffixes) {
  132. File dir = new File(getHomeDir(), "extensions/rules/" + dirName);
  133. if (dir.exists() && dir.isDirectory()) {
  134. return getFiles(dir, suffixes);
  135. }
  136. return Collections.emptyList();
  137. }
  138. private List<File> getFiles(File dir, String... fileSuffixes) {
  139. List<File> files = new ArrayList<>();
  140. if (dir != null && dir.exists()) {
  141. if (fileSuffixes != null && fileSuffixes.length > 0) {
  142. files.addAll(FileUtils.listFiles(dir, fileSuffixes, false));
  143. } else {
  144. files.addAll(FileUtils.listFiles(dir, null, false));
  145. }
  146. }
  147. return files;
  148. }
  149. }