@Override
public void persist() {
// Don't use batch insert for duplications since keeping all data in memory can produce OOM
- DbSession session = mybatis.openSession(false);
- try {
+ try (DbSession session = mybatis.openSession(false)) {
MeasureMapper mapper = session.getMapper(MeasureMapper.class);
org.sonar.api.measures.Metric duplicationMetricWithId = metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY);
for (Entry<List<DuplicationGroup>> entry : duplicationCache.entries()) {
}
} catch (Exception e) {
throw new IllegalStateException("Unable to save some measures", e);
- } finally {
- MyBatis.closeQuietly(session);
}
}
@Override
public void persist() {
- DbSession session = mybatis.openSession(true);
- try {
+ try (DbSession session = mybatis.openSession(true)) {
for (BatchResource batchResource : resourceCache.all()) {
String componentKey = batchResource.resource().getEffectiveKey();
String fileHashesdata = data.getStringData(componentKey, SnapshotDataTypes.FILE_HASHES);
}
}
session.commit();
- } finally {
- MyBatis.closeQuietly(session);
}
}
}
@Override
public void persist() {
- DbSession session = mybatis.openSession(true);
- try {
+ try (DbSession session = mybatis.openSession(true)) {
MeasureMapper mapper = session.getMapper(MeasureMapper.class);
for (Entry<Measure> entry : measureCache.entries()) {
session.commit();
} catch (Exception e) {
throw new IllegalStateException("Unable to save some measures", e);
- } finally {
- MyBatis.closeQuietly(session);
}
}
@Override
public void persist() {
// Don't use batch insert for file_sources since keeping all data in memory can produce OOM for big files
- DbSession session = mybatis.openSession(false);
- try {
+ try (DbSession session = mybatis.openSession(false)) {
final Map<String, FileSourceDto> fileSourceDtoByFileUuid = new HashMap<String, FileSourceDto>();
}
} catch (Exception e) {
throw new IllegalStateException("Unable to save file sources", e);
- } finally {
- MyBatis.closeQuietly(session);
}
}
package org.sonar.batch.phases;
import com.tinkerpop.blueprints.Graph;
-import org.apache.commons.io.IOUtils;
import org.sonar.api.component.Perspective;
import org.sonar.batch.index.ScanPersister;
import org.sonar.core.component.ComponentVertex;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
+import java.io.IOException;
import java.io.StringWriter;
public class GraphPersister implements ScanPersister {
}
private void serializePerspectiveData(GraphDtoMapper mapper, ComponentVertex component, Long snapshotId,
- GraphPerspectiveBuilder builder) {
+ GraphPerspectiveBuilder builder) {
Graph subGraph = SubGraph.extract(component.element(), builder.path());
String data = write(subGraph);
mapper.insert(new GraphDto()
.setResourceId((Long) component.element().getProperty("rid"))
.setSnapshotId(snapshotId)
.setRootVertexId(component.element().getId().toString())
- );
+ );
}
private String write(Graph graph) {
- StringWriter output = new StringWriter();
- try {
+ try (StringWriter output = new StringWriter()) {
new GraphsonWriter().write(graph, output, GraphsonMode.EXTENDED);
return output.toString();
- } finally {
- IOUtils.closeQuietly(output);
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to write graph", e);
}
}
}
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
-import com.google.common.io.Closeables;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
private void dumpToFile(Properties props, String fileName) {
- FileOutputStream fos = null;
File file = new File(out, fileName);
- try {
- fos = new FileOutputStream(file);
+ try (FileOutputStream fos = new FileOutputStream(file)) {
props.store(fos, "SonarQube");
println("Profiling data stored in " + file.getAbsolutePath());
} catch (Exception e) {
throw new IllegalStateException("Unable to store profiler output: " + file, e);
- } finally {
- Closeables.closeQuietly(fos);
}
}
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
@VisibleForTesting
protected static Properties toProperties(File propertyFile) {
Properties propsFromFile = new Properties();
- FileInputStream fileInputStream = null;
- try {
- fileInputStream = new FileInputStream(propertyFile);
+ try (FileInputStream fileInputStream = new FileInputStream(propertyFile)) {
propsFromFile.load(fileInputStream);
} catch (IOException e) {
throw new IllegalStateException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e);
- } finally {
- IOUtils.closeQuietly(fileInputStream);
}
// Trim properties
for (String propKey : propsFromFile.stringPropertyNames()) {
import com.google.common.primitives.Longs;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.File;
* Maximum performance is needed.
*/
Metadata read(File file, Charset encoding) {
- Reader reader = null;
long currentOriginalOffset = 0;
List<Long> originalLineOffsets = new ArrayList<Long>();
List<Object> lineHashes = new ArrayList<Object>();
int lines = 0;
char c = (char) -1;
- try {
+ try (Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
MessageDigest globalMd5Digest = DigestUtils.getMd5Digest();
MessageDigest lineMd5Digest = DigestUtils.getMd5Digest();
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
int i = reader.read();
boolean afterCR = false;
// First offset of first line is always 0
} catch (IOException e) {
throw new IllegalStateException(String.format("Fail to read file '%s' with encoding '%s'", file.getAbsolutePath(), encoding), e);
- } finally {
- IOUtils.closeQuietly(reader);
}
}
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
-import com.google.common.io.Closeables;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.scan.filesystem.InputPathCache;
-import java.io.*;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
File exportFile = new File(fileSystem.workDir(), settings.getString("sonar.report.export.path"));
LOG.info("Export results to " + exportFile.getAbsolutePath());
- Writer output = null;
- try {
- output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), Charsets.UTF_8));
+ try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), Charsets.UTF_8))) {
writeJson(output);
} catch (IOException e) {
throw new IllegalStateException("Unable to write report results in file " + exportFile.getAbsolutePath(), e);
- } finally {
- Closeables.closeQuietly(output);
}
}
package org.sonar.batch.source;
import com.google.common.collect.Lists;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
} else {
tokenizers = format.getTokenizers();
}
- Reader reader = null;
- try {
- reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), encoding));
+ try (Reader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), encoding))) {
return new HighlightingRenderer().render(reader, tokenizers);
} catch (Exception e) {
throw new IllegalStateException("Unable to read source file for colorization", e);
- } finally {
- IOUtils.closeQuietly(reader);
}
}
}
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ScanGraph;
import org.sonar.core.component.db.ComponentMapper;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import javax.persistence.Query;
// Need to enable snapshot to make resource visible using ComponentMapper
enableSnapshot(1001);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo");
assertThat(newProject.uuid()).isNotNull();
assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid());
assertThat(newProject.moduleUuidPath()).isEmpty();
// SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table
assertThat(newProject.getCreatedAt()).isNotNull();
- } finally {
- MyBatis.closeQuietly(session);
}
}
"projects", "snapshots");
// Need to enable snapshot to make resource visible using ComponentMapper
enableSnapshot(1001);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo");
assertThat(newProject.uuid()).isNotNull();
assertThat(newProject.projectUuid()).isEqualTo(newProject.uuid());
assertThat(newProject.moduleUuid()).isNull();
assertThat(newProject.moduleUuidPath()).isEmpty();
- } finally {
- MyBatis.closeQuietly(session);
}
}
enableSnapshot(1004);
enableSnapshot(1005);
enableSnapshot(1006);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root");
assertThat(root.uuid()).isNotNull();
assertThat(root.projectUuid()).isEqualTo(root.uuid());
assertThat(fileComp.projectUuid()).isEqualTo(root.uuid());
assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid());
assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid());
- } finally {
- MyBatis.closeQuietly(session);
}
}
enableSnapshot(1004);
enableSnapshot(1005);
enableSnapshot(1006);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root");
System.out.println("Root: " + root.uuid());
assertThat(root.uuid()).isNotNull();
assertThat(fileComp.projectUuid()).isEqualTo(root.uuid());
assertThat(fileComp.moduleUuid()).isEqualTo(b1.uuid());
assertThat(fileComp.moduleUuidPath()).isEqualTo(root.uuid() + "." + b.uuid() + "." + b1.uuid());
- } finally {
- MyBatis.closeQuietly(session);
}
}
// Need to enable snapshot to make resource visible using ComponentMapper
enableSnapshot(1001);
enableSnapshot(1002);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo");
ComponentDto newDir = session.getMapper(ComponentMapper.class).selectByKey("foo:src/main/java/org/foo");
assertThat(newDir.uuid()).isNotNull();
assertThat(newDir.projectUuid()).isEqualTo(newProject.uuid());
assertThat(newDir.moduleUuid()).isEqualTo(newProject.uuid());
assertThat(newDir.moduleUuidPath()).isEqualTo(newProject.uuid());
- } finally {
- MyBatis.closeQuietly(session);
}
}
// Need to enable snapshot to make resource visible using ComponentMapper
enableSnapshot(1002);
- SqlSession session = getMyBatis().openSession(false);
- try {
+ try (SqlSession session = getMyBatis().openSession(false)) {
// FIXME selectByKey returns duplicates for libraries because of the join on snapshots table
ComponentDto newLib = session.getMapper(ComponentMapper.class).findByKeys(Arrays.asList("junit:junit")).get(0);
assertThat(newLib.uuid()).isNotNull();
assertThat(newLib.projectUuid()).isEqualTo(newLib.uuid());
assertThat(newLib.moduleUuid()).isNull();
assertThat(newLib.moduleUuidPath()).isEmpty();
- } finally {
- MyBatis.closeQuietly(session);
}
}
package org.sonar.batch.scan;
import com.google.common.collect.Maps;
-import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
private Map<String, String> loadPropsFromFile(String filePath) throws IOException {
Properties props = new Properties();
- FileInputStream fileInputStream = null;
- try {
- fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath));
+ try (FileInputStream fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath))) {
props.load(fileInputStream);
- } finally {
- IOUtils.closeQuietly(fileInputStream);
}
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {