Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Utils.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  5. *
  6. * This program 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. * This program 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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.api;
  21. import java.io.Closeable;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.nio.file.FileVisitResult;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.nio.file.SimpleFileVisitor;
  30. import java.nio.file.attribute.BasicFileAttributes;
  31. import java.util.Arrays;
  32. import java.util.Iterator;
  33. import java.util.Properties;
  34. import javax.annotation.Nullable;
  35. class Utils {
  36. private Utils() {
  37. // only util static methods
  38. }
  39. /**
  40. * Similar to org.apache.commons.lang.StringUtils#join()
  41. */
  42. static String join(String[] array, String delimiter) {
  43. StringBuilder sb = new StringBuilder();
  44. Iterator<String> it = Arrays.asList(array).iterator();
  45. while (it.hasNext()) {
  46. sb.append(it.next());
  47. if (!it.hasNext()) {
  48. break;
  49. }
  50. sb.append(delimiter);
  51. }
  52. return sb.toString();
  53. }
  54. static boolean taskRequiresProject(Properties props) {
  55. Object task = props.get(RunnerProperties.TASK);
  56. return task == null || ScanProperties.SCAN_TASK.equals(task);
  57. }
  58. static void writeProperties(File outputFile, Properties p) {
  59. try (OutputStream output = new FileOutputStream(outputFile)) {
  60. p.store(output, "Generated by sonar-runner");
  61. } catch (Exception e) {
  62. throw new IllegalStateException("Fail to export sonar-runner properties", e);
  63. }
  64. }
  65. static void deleteQuietly(File f) {
  66. try {
  67. Files.walkFileTree(f.toPath(), new DeleteQuietlyFileVisitor());
  68. } catch (IOException e) {
  69. // ignore
  70. }
  71. }
  72. private static class DeleteQuietlyFileVisitor extends SimpleFileVisitor<Path> {
  73. @Override
  74. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
  75. try {
  76. Files.delete(file);
  77. } catch (IOException e) {
  78. // ignore
  79. }
  80. return FileVisitResult.CONTINUE;
  81. }
  82. @Override
  83. public FileVisitResult visitFileFailed(Path file, IOException exc) {
  84. try {
  85. Files.delete(file);
  86. } catch (IOException e) {
  87. // ignore
  88. }
  89. return FileVisitResult.CONTINUE;
  90. }
  91. @Override
  92. public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
  93. try {
  94. Files.delete(dir);
  95. } catch (IOException e) {
  96. // ignore
  97. }
  98. return FileVisitResult.CONTINUE;
  99. }
  100. }
  101. static void closeQuietly(@Nullable Closeable c) {
  102. if (c == null) {
  103. return;
  104. }
  105. try {
  106. c.close();
  107. } catch (IOException e) {
  108. // ignore
  109. }
  110. }
  111. }