Tuesday, November 24, 2020

Create Sonar Plugin - Scanner Basic - create Extractor

 Flow of our plugin is to 

  • Analyse the files using a third party app/jar which generate a xml/json violation/issues report.
  • read the xml/json report and save details as new issues along with respective Input files


  • Here we need  to bundle the third party app/jar along with out plugin jar.
  • Plugin jar will be installed to  SonarQube. (copy to "sonarqube-x.x\extensions\plugins" )  folder
  • Invoke scanner. (  "sonar-scanner" comand )
    • Plugin will be downloaded and start analysing and generate details inside work folder. 
    • Work folder will be flushed each time runs and everything will be deleted.
    • Therefore we need to copy the details to the work folder.
  • We need to do this manually.
  • Logic is handled using Extractor - extract files from plugin jar and copy back to work folder


-----------------------------------------------------------------------------------------------------------------------

package com.plugin.sonar.util;

import com.plugin.sonar.exception.FooPluginException;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import static com.plugin.sonar.util.FooConstants.*;

@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ScannerSide()
public class Extractor {
private static final Logger LOG = Loggers.get(Extractor.class);

private static String exportResource(String resourceName, String folder) throws FooPluginException {
try (InputStream stream = Extractor.class.getResourceAsStream(resourceName)) {//note that each / is a directory down in the "jar tree" been the jar the root of the tree
if (stream == null) {
throw new FooPluginException("Cannot get resource \"" + resourceName + "\" from Jar file.");
}
createResource(resourceName, folder, stream);
} catch (Exception ex) {
LOG.debug(ex.getLocalizedMessage());
}

LOG.info("folder =" + folder + " resourceName=" + resourceName);
return folder + resourceName;
}

private static void createResource(String resourceName, String folder, InputStream stream) {
LOG.debug("resourceName=" + resourceName + "folder=" + folder + "stream=" + stream);
int readBytes;
byte[] buffer = new byte[4096];
try (OutputStream resStreamOut = new FileOutputStream(folder + resourceName)) {
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
} catch (Exception ex) {
LOG.debug(ex.getLocalizedMessage());
}
}

public void extractPlsql(String workDir) throws FooPluginException {
LOG.info("Calling::extractPlsql ");
exportResource(FooSQL_JAR, workDir);
exportResource(LOGIN_CONFIGURATION, workDir);
exportResource(INIT_PROPERTIES, workDir);
exportResource(FooSQL_SONAR_RULES, workDir);
exportResource(RULE_ENGINE_PROPERTIES, workDir);
exportResource(RULES, workDir);
}

}

No comments:

Post a Comment