Search in sources :

Example 1 with ServerPlugin

use of org.sonar.server.plugins.ServerPlugin in project sonarqube by SonarSource.

the class GeneratePluginIndexTest method newInstalledPlugin.

private ServerPlugin newInstalledPlugin(String key, boolean supportSonarLint) throws IOException {
    FileAndMd5 jar = new FileAndMd5(temp.newFile());
    PluginInfo pluginInfo = new PluginInfo(key).setJarFile(jar.getFile()).setSonarLintSupported(supportSonarLint);
    return new ServerPlugin(pluginInfo, BUNDLED, null, jar, null, null);
}
Also used : FileAndMd5(org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5) PluginInfo(org.sonar.core.platform.PluginInfo) ServerPlugin(org.sonar.server.plugins.ServerPlugin)

Example 2 with ServerPlugin

use of org.sonar.server.plugins.ServerPlugin in project sonarqube by SonarSource.

the class GeneratePluginIndexTest method shouldWriteIndex.

@Test
public void shouldWriteIndex() throws IOException {
    ServerPlugin javaPlugin = newInstalledPlugin("java", true);
    ServerPlugin gitPlugin = newInstalledPlugin("scmgit", false);
    when(serverPluginRepository.getPlugins()).thenReturn(asList(javaPlugin, gitPlugin));
    GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, serverPluginRepository);
    underTest.start();
    List<String> lines = FileUtils.readLines(index);
    assertThat(lines).containsExactly("java,true," + javaPlugin.getJar().getFile().getName() + "|" + javaPlugin.getJar().getMd5(), "scmgit,false," + gitPlugin.getJar().getFile().getName() + "|" + gitPlugin.getJar().getMd5());
    underTest.stop();
}
Also used : ServerPlugin(org.sonar.server.plugins.ServerPlugin) Test(org.junit.Test)

Example 3 with ServerPlugin

use of org.sonar.server.plugins.ServerPlugin in project sonarqube by SonarSource.

the class GeneratePluginIndex method writeIndex.

private void writeIndex(File indexFile) {
    try {
        FileUtils.forceMkdir(indexFile.getParentFile());
        try (Writer writer = new OutputStreamWriter(new FileOutputStream(indexFile), StandardCharsets.UTF_8)) {
            for (ServerPlugin plugin : serverPluginRepository.getPlugins()) {
                writer.append(toRow(plugin));
                writer.append(CharUtils.LF);
            }
            writer.flush();
        }
    } catch (IOException e) {
        throw new IllegalStateException("Unable to generate plugin index at " + indexFile, e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ServerPlugin(org.sonar.server.plugins.ServerPlugin) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with ServerPlugin

use of org.sonar.server.plugins.ServerPlugin in project sonarqube by SonarSource.

the class RegisterPlugins method updateDB.

private void updateDB() {
    long now = system.now();
    try (DbSession dbSession = dbClient.openSession(false)) {
        Map<String, PluginDto> allPreviousPluginsByKey = dbClient.pluginDao().selectAll(dbSession).stream().collect(Collectors.toMap(PluginDto::getKee, identity()));
        for (ServerPlugin installed : serverPluginRepository.getPlugins()) {
            PluginInfo info = installed.getPluginInfo();
            PluginDto previousDto = allPreviousPluginsByKey.get(info.getKey());
            if (previousDto == null) {
                LOG.debug("Register new plugin {}", info.getKey());
                PluginDto pluginDto = new PluginDto().setUuid(uuidFactory.create()).setKee(info.getKey()).setBasePluginKey(info.getBasePlugin()).setFileHash(installed.getJar().getMd5()).setType(toTypeDto(installed.getType())).setCreatedAt(now).setUpdatedAt(now);
                dbClient.pluginDao().insert(dbSession, pluginDto);
            } else if (!previousDto.getFileHash().equals(installed.getJar().getMd5()) || !previousDto.getType().equals(toTypeDto(installed.getType()))) {
                LOG.debug("Update plugin {}", info.getKey());
                previousDto.setBasePluginKey(info.getBasePlugin()).setFileHash(installed.getJar().getMd5()).setType(toTypeDto(installed.getType())).setUpdatedAt(now);
                dbClient.pluginDao().update(dbSession, previousDto);
            }
        }
        // keep uninstalled plugins with a 'removed' flag, because corresponding rules and active rules are also not deleted
        for (PluginDto dto : allPreviousPluginsByKey.values()) {
            if (dto.isRemoved()) {
                continue;
            }
            if (serverPluginRepository.findPlugin(dto.getKee()).isEmpty()) {
                dto.setRemoved(true).setUpdatedAt(now);
                dbClient.pluginDao().update(dbSession, dto);
            }
        }
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) PluginDto(org.sonar.db.plugin.PluginDto) ServerPlugin(org.sonar.server.plugins.ServerPlugin) PluginInfo(org.sonar.core.platform.PluginInfo)

Example 5 with ServerPlugin

use of org.sonar.server.plugins.ServerPlugin in project sonarqube by SonarSource.

the class DownloadAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String pluginKey = request.mandatoryParam(PLUGIN_PARAM);
    Optional<ServerPlugin> file = pluginRepository.findPlugin(pluginKey);
    if (!file.isPresent()) {
        throw new NotFoundException("Plugin " + pluginKey + " not found");
    }
    FileAndMd5 downloadedFile;
    FileAndMd5 compressedJar = file.get().getCompressed();
    if (compressedJar != null && PACK200.equals(request.param(ACCEPT_COMPRESSIONS_PARAM))) {
        response.stream().setMediaType("application/octet-stream");
        response.setHeader("Sonar-Compression", PACK200);
        response.setHeader("Sonar-UncompressedMD5", file.get().getJar().getMd5());
        downloadedFile = compressedJar;
    } else {
        response.stream().setMediaType("application/java-archive");
        downloadedFile = file.get().getJar();
    }
    response.setHeader("Sonar-MD5", downloadedFile.getMd5());
    try (InputStream input = FileUtils.openInputStream(downloadedFile.getFile())) {
        IOUtils.copyLarge(input, response.stream().output());
    }
}
Also used : FileAndMd5(org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5) InputStream(java.io.InputStream) NotFoundException(org.sonar.server.exceptions.NotFoundException) ServerPlugin(org.sonar.server.plugins.ServerPlugin)

Aggregations

ServerPlugin (org.sonar.server.plugins.ServerPlugin)17 PluginInfo (org.sonar.core.platform.PluginInfo)9 Test (org.junit.Test)8 FileAndMd5 (org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5)6 TestResponse (org.sonar.server.ws.TestResponse)4 File (java.io.File)3 DbSession (org.sonar.db.DbSession)2 PluginDto (org.sonar.db.plugin.PluginDto)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 NotFoundException (org.sonar.server.exceptions.NotFoundException)1 PluginFilesAndMd5 (org.sonar.server.plugins.PluginFilesAndMd5)1 PluginWSCommons.buildPluginDetails (org.sonar.server.plugins.ws.PluginWSCommons.buildPluginDetails)1 Plugin (org.sonar.updatecenter.common.Plugin)1