private static StoragesManager createCacheOnTemp() { Map<String, String> props = ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, temp.getRoot().getAbsolutePath(), CoreProperties.GLOBAL_WORKING_DIRECTORY, temp.getRoot().getAbsolutePath()); return new StoragesManager(new GlobalTempFolderProvider().provide(new ScannerProperties(props))); }
public TempFolder provide(ScannerProperties scannerProps) { if (tempFolder == null) { String workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE); Path workingPath = Paths.get(workingPathName); if (!workingPath.isAbsolute()) { Path home = findSonarHome(scannerProps); workingPath = home.resolve(workingPath).normalize(); } try { cleanTempFolders(workingPath); } catch (IOException e) { LOG.error(String.format("failed to clean global working directory: %s", workingPath), e); } Path tempDir = createTempFolder(workingPath); tempFolder = new DefaultTempFolder(tempDir.toFile(), true); } return tempFolder; }
private void addBootstrapComponents() { Version apiVersion = ApiVersion.load(System2.INSTANCE); add( // plugins ScannerPluginRepository.class, PluginLoader.class, PluginClassloaderFactory.class, ScannerPluginJarExploder.class, ScannerPluginPredicate.class, ExtensionInstaller.class, new SonarQubeVersion(apiVersion), SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SCANNER), StoragesManager.class, new GlobalServerSettingsProvider(), new GlobalConfigurationProvider(), new ScannerWsClientProvider(), DefaultServer.class, new GlobalTempFolderProvider(), DefaultHttpDownloader.class, UriReader.class, PluginFiles.class, System2.INSTANCE, Clock.systemDefaultZone(), new MetricsRepositoryProvider(), UuidFactoryImpl.INSTANCE); addIfMissing(ScannerPluginInstaller.class, PluginInstaller.class); add(CoreExtensionRepositoryImpl.class, CoreExtensionsLoader.class, ScannerCoreExtensionsInstaller.class); addIfMissing(DefaultSettingsLoader.class, SettingsLoader.class); addIfMissing(DefaultMetricsRepositoryLoader.class, MetricsRepositoryLoader.class); }
@Test public void dotWorkingDir() throws IOException { File sonarHome = temp.getRoot(); String globalWorkDir = "."; ScannerProperties globalProperties = new ScannerProperties(ImmutableMap.of("sonar.userHome", sonarHome.getAbsolutePath(), CoreProperties.GLOBAL_WORKING_DIRECTORY, globalWorkDir)); TempFolder tempFolder = tempFolderProvider.provide(globalProperties); File newFile = tempFolder.newFile(); assertThat(newFile.getParentFile().getParentFile().getAbsolutePath()).isEqualTo(sonarHome.getAbsolutePath()); assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_"); }
private void addBootstrapComponents() { Version apiVersion = ApiVersion.load(System2.INSTANCE); add( // plugins ScannerPluginRepository.class, PluginLoader.class, PluginClassloaderFactory.class, ScannerPluginJarExploder.class, ScannerPluginPredicate.class, ExtensionInstaller.class, new SonarQubeVersion(apiVersion), SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SCANNER), StoragesManager.class, new GlobalServerSettingsProvider(), new GlobalConfigurationProvider(), new ScannerWsClientProvider(), DefaultServer.class, new GlobalTempFolderProvider(), DefaultHttpDownloader.class, UriReader.class, PluginFiles.class, System2.INSTANCE, Clock.systemDefaultZone(), new MetricsRepositoryProvider(), UuidFactoryImpl.INSTANCE); addIfMissing(ScannerPluginInstaller.class, PluginInstaller.class); add(CoreExtensionRepositoryImpl.class, CoreExtensionsLoader.class, ScannerCoreExtensionsInstaller.class); addIfMissing(DefaultSettingsLoader.class, SettingsLoader.class); addIfMissing(DefaultMetricsRepositoryLoader.class, MetricsRepositoryLoader.class); }
@Test public void homeIsSymbolicLink() throws IOException { assumeTrue(!System2.INSTANCE.isOsWindows()); File realSonarHome = temp.newFolder(); File symlink = temp.newFolder(); symlink.delete(); Files.createSymbolicLink(symlink.toPath(), realSonarHome.toPath()); ScannerProperties globalProperties = new ScannerProperties(ImmutableMap.of("sonar.userHome", symlink.getAbsolutePath())); TempFolder tempFolder = tempFolderProvider.provide(globalProperties); File newFile = tempFolder.newFile(); assertThat(newFile.getParentFile().getParentFile().getAbsolutePath()).isEqualTo(symlink.getAbsolutePath()); assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_"); }
public TempFolder provide(ScannerProperties scannerProps) { if (tempFolder == null) { String workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE); Path workingPath = Paths.get(workingPathName); if (!workingPath.isAbsolute()) { Path home = findSonarHome(scannerProps); workingPath = home.resolve(workingPath).normalize(); } try { cleanTempFolders(workingPath); } catch (IOException e) { LOG.error(String.format("failed to clean global working directory: %s", workingPath), e); } Path tempDir = createTempFolder(workingPath); tempFolder = new DefaultTempFolder(tempDir.toFile(), true); } return tempFolder; }
@Test public void createTempFolderDefault() throws Exception { System2 system = mock(System2.class); tempFolderProvider = new GlobalTempFolderProvider(system); File userHome = temp.newFolder(); when(system.envVariable("SONAR_USER_HOME")).thenReturn(null); when(system.property("user.home")).thenReturn(userHome.getAbsolutePath().toString()); // if nothing is defined, it will be in {user.home}/.sonar/.sonartmp File defaultSonarHome = new File(userHome.getAbsolutePath(), ".sonar"); File workingDir = new File(defaultSonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE).getAbsoluteFile(); try { TempFolder tempFolder = tempFolderProvider.provide(new ScannerProperties(Collections.<String, String>emptyMap())); tempFolder.newDir(); tempFolder.newFile(); assertThat(getCreatedTempDir(workingDir)).exists(); assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); } finally { FileUtils.deleteQuietly(workingDir); } }
@Test public void cleanUpOld() throws IOException { long creationTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(100); File workingDir = temp.newFolder(); for (int i = 0; i < 3; i++) { File tmp = new File(workingDir, ".sonartmp_" + i); tmp.mkdirs(); setFileCreationDate(tmp, creationTime); } tempFolderProvider.provide(new ScannerProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.getAbsolutePath()))); // this also checks that all other temps were deleted assertThat(getCreatedTempDir(workingDir)).exists(); FileUtils.deleteQuietly(workingDir); }
@Test public void createTempFolderSonarHome() throws Exception { // with sonar home, it will be in {sonar.home}/.sonartmp File sonarHome = temp.newFolder(); File workingDir = new File(sonarHome, CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE).getAbsoluteFile(); TempFolder tempFolder = tempFolderProvider.provide(new ScannerProperties(ImmutableMap.of("sonar.userHome", sonarHome.getAbsolutePath()))); tempFolder.newDir(); tempFolder.newFile(); assertThat(getCreatedTempDir(workingDir)).exists(); assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); FileUtils.deleteQuietly(sonarHome); }
@Test public void createTempFolderProps() throws Exception { File workingDir = temp.newFolder(); workingDir.delete(); TempFolder tempFolder = tempFolderProvider.provide(new ScannerProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.getAbsolutePath()))); tempFolder.newDir(); tempFolder.newFile(); assertThat(getCreatedTempDir(workingDir)).exists(); assertThat(getCreatedTempDir(workingDir).list()).hasSize(2); FileUtils.deleteQuietly(workingDir); }