From bd2a24edb13d64a2cf8b29d15689d8e568fa0aaf Mon Sep 17 00:00:00 2001 From: emeroad Date: Thu, 20 Apr 2023 14:59:10 +0900 Subject: [PATCH] [#noissue] Apply ConditionalOnProperty to InstallModule --- .../pinpoint/web/install/InstallModule.java | 27 +++++--- .../dao/AgentDownloadInfoDaoFactoryBean.java | 65 ------------------- .../dao/GithubAgentDownloadInfoDao.java | 2 +- web/src/main/resources/application.yml | 5 +- .../web/install/AgentDownloadInfoTest.java | 32 ++++----- 5 files changed, 33 insertions(+), 98 deletions(-) delete mode 100644 web/src/main/java/com/navercorp/pinpoint/web/install/dao/AgentDownloadInfoDaoFactoryBean.java diff --git a/web/src/main/java/com/navercorp/pinpoint/web/install/InstallModule.java b/web/src/main/java/com/navercorp/pinpoint/web/install/InstallModule.java index ac6fd3d1ec16..f02ccc10799c 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/install/InstallModule.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/install/InstallModule.java @@ -1,14 +1,16 @@ package com.navercorp.pinpoint.web.install; import com.navercorp.pinpoint.web.install.dao.AgentDownloadInfoDao; -import com.navercorp.pinpoint.web.install.dao.AgentDownloadInfoDaoFactoryBean; +import com.navercorp.pinpoint.web.install.dao.GithubAgentDownloadInfoDao; +import com.navercorp.pinpoint.web.install.dao.MemoryAgentDownloadInfoDao; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; import org.springframework.web.client.RestTemplate; /** @@ -27,14 +29,19 @@ public InstallModule() { } @Bean - public FactoryBean agentDownloadInfoDao( + @ConditionalOnProperty(value = "pinpoint.modules.web.install.type", havingValue = "url") + public AgentDownloadInfoDao urlAgentDownloadInfoDao( @Value("${web.installation.pinpointVersion:}") String version, - @Value("${web.installation.downloadUrl:}") String downloadUrl, - RestTemplate restTemplate) { - AgentDownloadInfoDaoFactoryBean factoryBean = new AgentDownloadInfoDaoFactoryBean(); - factoryBean.setVersion(version); - factoryBean.setDownloadUrl(downloadUrl); - factoryBean.setRestTemplate(restTemplate); - return factoryBean; + @Value("${web.installation.downloadUrl:}") String downloadUrl) { + Assert.hasLength(version, "version"); + Assert.hasLength(downloadUrl, "downloadUrl"); + return new MemoryAgentDownloadInfoDao(version, downloadUrl); + + } + + @Bean + @ConditionalOnProperty(value = "pinpoint.modules.web.install.type", havingValue = "github", matchIfMissing = true) + public AgentDownloadInfoDao githubAgentDownloadInfoDao(RestTemplate restTemplate) { + return new GithubAgentDownloadInfoDao(restTemplate); } } diff --git a/web/src/main/java/com/navercorp/pinpoint/web/install/dao/AgentDownloadInfoDaoFactoryBean.java b/web/src/main/java/com/navercorp/pinpoint/web/install/dao/AgentDownloadInfoDaoFactoryBean.java deleted file mode 100644 index e787f3f73a73..000000000000 --- a/web/src/main/java/com/navercorp/pinpoint/web/install/dao/AgentDownloadInfoDaoFactoryBean.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2017 NAVER Corp. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.navercorp.pinpoint.web.install.dao; - -import com.navercorp.pinpoint.common.util.StringUtils; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.web.client.RestTemplate; - -/** - * @author Taejin Koo - */ -public class AgentDownloadInfoDaoFactoryBean implements FactoryBean { - - private String version; - private String downloadUrl; - private RestTemplate restTemplate; - - - public AgentDownloadInfoDaoFactoryBean() { - } - - public void setVersion(String version) { - this.version = version; - } - - public void setDownloadUrl(String downloadUrl) { - this.downloadUrl = downloadUrl; - } - - public void setRestTemplate(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - } - - @Override - public AgentDownloadInfoDao getObject() throws Exception { - if (StringUtils.hasText(version) && StringUtils.hasText(downloadUrl)) { - return new MemoryAgentDownloadInfoDao(version, downloadUrl); - } - return new GithubAgentDownloadInfoDao(restTemplate); - } - - @Override - public Class getObjectType() { - return AgentDownloadInfoDao.class; - } - - @Override - public boolean isSingleton() { - return true; - } -} diff --git a/web/src/main/java/com/navercorp/pinpoint/web/install/dao/GithubAgentDownloadInfoDao.java b/web/src/main/java/com/navercorp/pinpoint/web/install/dao/GithubAgentDownloadInfoDao.java index 2e529fb4be78..c66b21762807 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/install/dao/GithubAgentDownloadInfoDao.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/install/dao/GithubAgentDownloadInfoDao.java @@ -42,7 +42,7 @@ public class GithubAgentDownloadInfoDao implements AgentDownloadInfoDao { private static final Pattern STABLE_VERSION_PATTERN = Pattern.compile(IdValidateUtils.STABLE_VERSION_PATTERN_VALUE); private static final ParameterizedTypeReference> responseType - = new ParameterizedTypeReference>() {}; + = new ParameterizedTypeReference<>() {}; private final RestTemplate restTemplate; diff --git a/web/src/main/resources/application.yml b/web/src/main/resources/application.yml index 44b92eac1521..d018cb78ca34 100644 --- a/web/src/main/resources/application.yml +++ b/web/src/main/resources/application.yml @@ -12,4 +12,7 @@ server: include-binding-errors: always include-stacktrace: always whitelabel: - enabled: true \ No newline at end of file + enabled: true + +# github or url +pinpoint.modules.web.install.type: github \ No newline at end of file diff --git a/web/src/test/java/com/navercorp/pinpoint/web/install/AgentDownloadInfoTest.java b/web/src/test/java/com/navercorp/pinpoint/web/install/AgentDownloadInfoTest.java index 5fbd64a57677..bf041581b22f 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/install/AgentDownloadInfoTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/install/AgentDownloadInfoTest.java @@ -21,7 +21,6 @@ import com.navercorp.pinpoint.common.util.BytesUtils; import com.navercorp.pinpoint.common.util.IOUtils; import com.navercorp.pinpoint.web.install.dao.AgentDownloadInfoDao; -import com.navercorp.pinpoint.web.install.dao.AgentDownloadInfoDaoFactoryBean; import com.navercorp.pinpoint.web.install.dao.GithubAgentDownloadInfoDao; import com.navercorp.pinpoint.web.install.dao.MemoryAgentDownloadInfoDao; import com.navercorp.pinpoint.web.install.model.GithubAgentDownloadInfo; @@ -34,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * @author Taejin Koo @@ -45,41 +45,31 @@ public class AgentDownloadInfoTest { RestTemplate restTemplate = new RestTemplate(); @Test - void factoryTest1() throws Exception { - AgentDownloadInfoDaoFactoryBean factoryBean = new AgentDownloadInfoDaoFactoryBean(); - factoryBean.setVersion(version); - factoryBean.setDownloadUrl(downloadUrl); - factoryBean.setRestTemplate(restTemplate); + void factoryTest1() { + InstallModule module = new InstallModule(); - AgentDownloadInfoDao dao = factoryBean.getObject(); + AgentDownloadInfoDao dao = module.urlAgentDownloadInfoDao(version, downloadUrl); assertThat(dao).isInstanceOf(MemoryAgentDownloadInfoDao.class); assertEquals(version, dao.getDownloadInfoList().get(0).getVersion()); assertEquals(downloadUrl, dao.getDownloadInfoList().get(0).getDownloadUrl()); } @Test - void factoryTest2() throws Exception { - AgentDownloadInfoDaoFactoryBean factoryBean = new AgentDownloadInfoDaoFactoryBean(); - factoryBean.setVersion(version); - factoryBean.setDownloadUrl(""); - factoryBean.setRestTemplate(restTemplate); + void factoryTest2() { + InstallModule module = new InstallModule(); - AgentDownloadInfoDao dao = factoryBean.getObject(); - assertThat(dao).isInstanceOf(GithubAgentDownloadInfoDao.class); + assertThrows(IllegalArgumentException.class, + () -> module.urlAgentDownloadInfoDao(version, "")); } @Test - void factoryTest3() throws Exception { - AgentDownloadInfoDaoFactoryBean factoryBean = new AgentDownloadInfoDaoFactoryBean(); - factoryBean.setVersion(" "); - factoryBean.setDownloadUrl(downloadUrl); - factoryBean.setRestTemplate(restTemplate); + void factoryTest3() { + InstallModule module = new InstallModule(); - AgentDownloadInfoDao dao = factoryBean.getObject(); + AgentDownloadInfoDao dao = module.githubAgentDownloadInfoDao(restTemplate); assertThat(dao).isInstanceOf(GithubAgentDownloadInfoDao.class); } - @Test void defaultTest() throws Exception { String mockResponseString = getMockJsonString();