diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-02-01 14:34:15 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-02-01 14:35:13 +0100 |
commit | a9203f4791002a127872f6e09a4f20de44502aa8 (patch) | |
tree | 16a06f0a6938fd9897455565606577a5231faef4 /sonar-server | |
parent | 43fe02be87a3f22cdb2b61ce17eb6eb6b8afb856 (diff) | |
download | sonarqube-a9203f4791002a127872f6e09a4f20de44502aa8.tar.gz sonarqube-a9203f4791002a127872f6e09a4f20de44502aa8.zip |
SONAR-2291 Implement cache for JDBC driver
Diffstat (limited to 'sonar-server')
3 files changed, 37 insertions, 10 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java b/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java index faf605a190f..53d17dc6960 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerFileSystem.java @@ -111,8 +111,8 @@ public class DefaultServerFileSystem implements ServerFileSystem { return deployDir; } - public File getDeployedJdbcDriver() { - return new File(deployDir, "jdbc-driver.jar"); + public File getDeployedJdbcDriverIndex() { + return new File(deployDir, "jdbc-driver.txt"); } public File getDeployedPluginsDir() { diff --git a/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java b/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java index 8bf17a700bb..1e1a8a8b52b 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/JdbcDriverDeployer.java @@ -19,14 +19,21 @@ */ package org.sonar.server.startup; +import com.google.common.io.Closeables; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.server.platform.DefaultServerFileSystem; import java.io.File; import java.io.IOException; +import java.io.InputStream; public class JdbcDriverDeployer { + private static final Logger LOG = LoggerFactory.getLogger(JdbcDriverDeployer.class); + private final DefaultServerFileSystem fileSystem; public JdbcDriverDeployer(DefaultServerFileSystem fileSystem) { @@ -35,7 +42,7 @@ public class JdbcDriverDeployer { public void start() { File driver = fileSystem.getJdbcDriver(); - File deployedDriver = fileSystem.getDeployedJdbcDriver(); + File deployedDriver = new File(fileSystem.getDeployDir(), driver.getName()); if (deployedDriver == null || !deployedDriver.exists() || deployedDriver.length() != driver.length()) { try { FileUtils.copyFile(driver, deployedDriver); @@ -44,5 +51,17 @@ public class JdbcDriverDeployer { throw new RuntimeException("Can not copy the JDBC driver from " + driver + " to " + deployedDriver, e); } } + File deployedDriverIndex = fileSystem.getDeployedJdbcDriverIndex(); + // Compute MD5 + InputStream is = null; + try { + is = FileUtils.openInputStream(deployedDriver); + String md5 = DigestUtils.md5Hex(is); + FileUtils.writeStringToFile(deployedDriverIndex, deployedDriver.getName() + "|" + md5); + } catch (IOException e) { + throw new RuntimeException("Can not generate index of JDBC driver", e); + } finally { + Closeables.closeQuietly(is); + } } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java index bdf11002336..4cac7112870 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/JdbcDriverDeployerTest.java @@ -19,12 +19,15 @@ */ package org.sonar.server.startup; +import org.apache.commons.io.FileUtils; import org.junit.Test; import org.sonar.server.platform.DefaultServerFileSystem; import org.sonar.test.TestUtils; import java.io.File; +import java.io.IOException; +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; @@ -33,20 +36,25 @@ import static org.mockito.Mockito.when; public class JdbcDriverDeployerTest { @Test - public void testDeploy() { + public void testDeploy() throws IOException { DefaultServerFileSystem fs = mock(DefaultServerFileSystem.class); File initialDriver = TestUtils.getResource(getClass(), "deploy/my-driver.jar"); when(fs.getJdbcDriver()).thenReturn(initialDriver); - - File deployed = new File(TestUtils.getTestTempDir(getClass(), "deploy", true), "copy.jar"); - assertThat(deployed.exists(), is(false)); - when(fs.getDeployedJdbcDriver()).thenReturn(deployed); + File deployDir = TestUtils.getTestTempDir(getClass(), "deploy", true); + when(fs.getDeployDir()).thenReturn(deployDir); + File deployedIndex = new File(deployDir, "jdbc-driver.txt"); + File deployedFile = new File(deployDir, "my-driver.jar"); + assertThat(deployedIndex).doesNotExist(); + assertThat(deployedFile).doesNotExist(); + when(fs.getDeployedJdbcDriverIndex()).thenReturn(deployedIndex); JdbcDriverDeployer deployer = new JdbcDriverDeployer(fs); deployer.start(); - assertThat(deployed.exists(), is(true)); - assertThat(deployed.length(), is(initialDriver.length())); + assertThat(deployedIndex).exists(); + assertThat(deployedFile).exists(); + assertThat(deployedFile.length(), is(initialDriver.length())); + assertThat(FileUtils.readFileToString(deployedIndex)).isEqualTo("my-driver.jar|02b97f7bc37b2b68fc847fcc3fc1c156"); } } |