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()))) {
+ insertNewPluginDto(dbSession, installed, info);
+ continue;
+ }
+ if (pluginTypeOrJarHashChanged(installed, previousDto)) {
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);
+ updatePluginDto(dbSession, installed, info, previousDto);
+ }
+ if (previousDto.isRemoved()) {
+ LOG.debug("Previously removed plugin {} was re-installed", info.getKey());
+ previousDto.setRemoved(false);
+ updatePluginDto(dbSession, installed, info, previousDto);
}
}
}
}
+ private void insertNewPluginDto(DbSession dbSession, ServerPlugin installed, PluginInfo info) {
+ long now = system.now();
+ 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);
+ }
+
+ private void updatePluginDto(DbSession dbSession, ServerPlugin installed, PluginInfo info, PluginDto previousDto) {
+ long now = system.now();
+ previousDto
+ .setBasePluginKey(info.getBasePlugin())
+ .setFileHash(installed.getJar().getMd5())
+ .setType(toTypeDto(installed.getType()))
+ .setUpdatedAt(now);
+ dbClient.pluginDao().update(dbSession, previousDto);
+ }
+
+ private static boolean pluginTypeOrJarHashChanged(ServerPlugin installed, PluginDto previousDto) {
+ return !previousDto.getFileHash().equals(installed.getJar().getMd5()) || !previousDto.getType().equals(toTypeDto(installed.getType()));
+ }
+
private static PluginDto.Type toTypeDto(PluginType type) {
switch (type) {
case EXTERNAL:
@Before
public void setUp() {
- when(system2.now()).thenReturn(now).thenThrow(new IllegalStateException("Should be called only once"));
+ when(system2.now()).thenReturn(now);
}
/**
verify(pluginsByKey.get("csharp"), Type.EXTERNAL, null, "a20d785dbacb8f41a3b7392aa7d03b78", false, 1L, 1L);
}
+ @Test
+ public void re_add_previously_removed_plugin() throws IOException {
+ dbClient.pluginDao().insert(dbTester.getSession(), new PluginDto()
+ .setUuid("c")
+ .setKee("csharp")
+ .setBasePluginKey(null)
+ .setFileHash("a20d785dbacb8f41a3b7392aa7d03b78")
+ .setType(Type.EXTERNAL)
+ .setRemoved(true)
+ .setCreatedAt(1L)
+ .setUpdatedAt(1L));
+ dbTester.commit();
+
+ addPlugin("csharp", PluginType.EXTERNAL, null);
+ register.start();
+
+ Map<String, PluginDto> pluginsByKey = selectAllPlugins();
+ assertThat(pluginsByKey).hasSize(1);
+ verify(pluginsByKey.get("csharp"), Type.EXTERNAL, null, "a20d785dbacb8f41a3b7392aa7d03b78", false, 1L, now);
+ }
+
/**
* Update existing plugins, only when checksum is different and don't remove uninstalled plugins
*/