Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.cloud.dataflow.core.DefaultStreamDefinitionService;
import org.springframework.cloud.dataflow.core.StreamDefinitionService;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.service.AppRegistryService;
import org.springframework.cloud.dataflow.registry.service.DefaultAppRegistryService;
Expand Down Expand Up @@ -128,7 +129,7 @@ public AppRegistryService appRegistry() {

return new DefaultAppRegistryService(mock(AppRegistrationRepository.class),
new AppResourceCommon(new MavenProperties(), new FileSystemResourceLoader()),
mock(DefaultAuditRecordService.class)) {
mock(DefaultAuditRecordService.class), mock(AppRegistrationDao.class)) {

@Override
public boolean appExist(String name, ApplicationType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.cloud.dataflow.core.DefaultStreamDefinitionService;
import org.springframework.cloud.dataflow.core.StreamDefinitionService;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.service.AppRegistryService;
import org.springframework.cloud.dataflow.registry.service.DefaultAppRegistryService;
Expand Down Expand Up @@ -74,7 +75,7 @@ public AppRegistryService appRegistry() {

return new DefaultAppRegistryService(mock(AppRegistrationRepository.class),
new AppResourceCommon(new MavenProperties(), new FileSystemResourceLoader()),
mock(DefaultAuditRecordService.class)) {
mock(DefaultAuditRecordService.class), mock(AppRegistrationDao.class)) {

@Override
public boolean appExist(String name, ApplicationType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.dataflow.registry.repository;

import org.springframework.cloud.dataflow.core.AppRegistration;
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.Nullable;

/**
* DAO to access {@link org.springframework.cloud.dataflow.core.AppRegistration}. Contains
* predicate specific operations to make filtering based on optional parameters more
* efficient.
*
* @author Siddhant Sorann
*/
public interface AppRegistrationDao {

/**
* Function to find all app registrations based on various optional parameters using
* predicates.
* @param type application type.
* @param name application name.
* @param version application version.
* @param defaultVersion default version.
* @param pageable enumerates the data to be returned.
* @return paginated list of filtered app registrations.
*/
Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersion(
@Nullable ApplicationType type, @Nullable String name, @Nullable String version, boolean defaultVersion,
Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public interface AppRegistrationRepository extends KeyValueRepository<AppRegistr

Page<AppRegistration> findAllByNameContainingIgnoreCaseAndDefaultVersionIsTrue(String name, Pageable pageable);

List<AppRegistration> findAllByName(String name);

@Override
<S extends AppRegistration> S save(S s);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2021 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.dataflow.registry.repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.cloud.dataflow.core.AppRegistration;
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.query.QueryUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* DAO to access {@link org.springframework.cloud.dataflow.core.AppRegistration}. Contains
* predicate specific operations to make filtering based on optional parameters more
* efficient. Implements
* {@link org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao}
*
* @author Siddhant Sorann
*/
public class JdbcAppRegistrationDao implements AppRegistrationDao {

private final EntityManager entityManager;

private final AppRegistrationRepository appRegistrationRepository;

public JdbcAppRegistrationDao(EntityManager entityManager, AppRegistrationRepository appRegistrationRepository) {
Assert.notNull(entityManager, "Entity manager cannot be null");
Assert.notNull(appRegistrationRepository, "AppRegistrationRepository cannot be null");
this.entityManager = entityManager;
this.appRegistrationRepository = appRegistrationRepository;
}

@Override
public Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersion(ApplicationType type,
String name, String version, boolean defaultVersion, Pageable pageable) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<AppRegistration> cq = cb.createQuery(AppRegistration.class);
Root<AppRegistration> appRegistrationRoot = cq.from(AppRegistration.class);
final List<Predicate> predicates = new ArrayList<>();
if (type != null) {
predicates.add(cb.equal(appRegistrationRoot.get("type"), type));
}
if (StringUtils.hasText(name)) {
predicates.add(cb.like(cb.lower(appRegistrationRoot.get("name")), "%" + name.toLowerCase() + "%"));
}
if (StringUtils.hasText(version)) {
predicates.add(cb.equal(cb.lower(appRegistrationRoot.get("version")), version.toLowerCase()));
}
if (defaultVersion) {
predicates.add(cb.isTrue(appRegistrationRoot.get("defaultVersion")));
}
cq.where(predicates.toArray(new Predicate[0]));
cq.orderBy(QueryUtils.toOrders(pageable.getSort(), appRegistrationRoot, cb));
TypedQuery<AppRegistration> query = entityManager.createQuery(cq);
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
final List<AppRegistration> resultList = query.getResultList();
if (defaultVersion) {
resultList.forEach(appRegistration -> {
HashSet<String> versions = appRegistrationRepository.findAllByName(appRegistration.getName()).stream()
.map(AppRegistration::getVersion).collect(Collectors.toCollection(HashSet::new));
appRegistration.setVersions(versions);
});
}
return new PageImpl<>(resultList, pageable, getTotalCount(cb, predicates.toArray(new Predicate[0])));
}

private Long getTotalCount(CriteriaBuilder criteriaBuilder, Predicate[] predicateArray) {
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<AppRegistration> root = criteriaQuery.from(AppRegistration.class);

criteriaQuery.select(criteriaBuilder.count(root));
criteriaQuery.where(predicateArray);

return entityManager.createQuery(criteriaQuery).getSingleResult();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.Nullable;

/**
* @author Christian Tzolov
Expand Down Expand Up @@ -206,4 +207,17 @@ default AppRegistration find(String name, ApplicationType type, String version)
* @return the resource version
*/
String getResourceVersion(String uriString);

/**
* Returns all app registrations based on various optional parameters.
* @param type application type
* @param name application name
* @param version application version
* @param defaultVersion application default version
* @param pageable Pagination information
* @return returns all {@link AppRegistration} versions for given name and type. Uses the
* pagination.
*/
Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersion(@Nullable ApplicationType type,
@Nullable String name, @Nullable String version, boolean defaultVersion, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.cloud.dataflow.core.AuditActionType;
import org.springframework.cloud.dataflow.core.AuditOperationType;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.support.AppResourceCommon;
import org.springframework.cloud.dataflow.registry.support.NoSuchAppRegistrationException;
Expand Down Expand Up @@ -81,21 +82,25 @@ public class DefaultAppRegistryService implements AppRegistryService {

private final AppRegistrationRepository appRegistrationRepository;

private final AppRegistrationDao appRegistrationDao;

private AppResourceCommon appResourceCommon;

protected final AuditRecordService auditRecordService;

protected final AuditServiceUtils auditServiceUtils;

public DefaultAppRegistryService(AppRegistrationRepository appRegistrationRepository,
AppResourceCommon appResourceCommon, AuditRecordService auditRecordService) {
AppResourceCommon appResourceCommon, AuditRecordService auditRecordService,
AppRegistrationDao appRegistrationDao) {
Assert.notNull(appResourceCommon, "'appResourceCommon' must not be null");
Assert.notNull(appRegistrationRepository, "'appRegistrationRepository' must not be null");
Assert.notNull(auditRecordService, "'auditRecordService' must not be null");
this.appResourceCommon = appResourceCommon;
this.appRegistrationRepository = appRegistrationRepository;
this.auditRecordService = auditRecordService;
this.auditServiceUtils = new AuditServiceUtils();
this.appRegistrationDao = appRegistrationDao;
}

@Override
Expand Down Expand Up @@ -183,22 +188,25 @@ else if (StringUtils.hasText(name)) {
}

@Override
public Page<AppRegistration> findAllByTypeAndNameIsLikeAndDefaultVersionIsTrue(ApplicationType type, String name, Pageable pageable) {
public Page<AppRegistration> findAllByTypeAndNameIsLikeAndDefaultVersionIsTrue(ApplicationType type, String name,
Pageable pageable) {
Page<AppRegistration> result = null;
if (!StringUtils.hasText(name) && type == null) {
result = this.appRegistrationRepository.findAllByDefaultVersionIsTrue(pageable);
}
else if (StringUtils.hasText(name) && type == null) {
result = this.appRegistrationRepository.findAllByNameContainingIgnoreCaseAndDefaultVersionIsTrue(name, pageable);
result = this.appRegistrationRepository.findAllByNameContainingIgnoreCaseAndDefaultVersionIsTrue(name,
pageable);
}
else if (StringUtils.hasText(name)) {
result = this.appRegistrationRepository.findAllByTypeAndNameContainingIgnoreCaseAndDefaultVersionIsTrue(type, name, pageable);
result = this.appRegistrationRepository
.findAllByTypeAndNameContainingIgnoreCaseAndDefaultVersionIsTrue(type, name, pageable);
}
else {
result = this.appRegistrationRepository.findAllByTypeAndDefaultVersionIsTrue(type, pageable);
}
for (AppRegistration pagedAppRegistration: result.getContent()) {
for (AppRegistration appRegistration: this.findAll()) {
for (AppRegistration pagedAppRegistration : result.getContent()) {
for (AppRegistration appRegistration : this.findAll()) {
if (pagedAppRegistration.getName().equals(appRegistration.getName()) &&
pagedAppRegistration.getType().equals(appRegistration.getType())) {
if (pagedAppRegistration.getVersions() == null) {
Expand Down Expand Up @@ -272,7 +280,8 @@ private void populateAuditData(AuditActionType auditActionType, AppRegistration
public void delete(String name, ApplicationType type, String version) {
this.appRegistrationRepository.deleteAppRegistrationByNameAndTypeAndVersion(name, type, version);

populateAuditData(AuditActionType.DELETE, new AppRegistration(name, type, version, URI.create(""), URI.create("")));
populateAuditData(AuditActionType.DELETE,
new AppRegistration(name, type, version, URI.create(""), URI.create("")));
}

@Override
Expand Down Expand Up @@ -326,6 +335,13 @@ public String getResourceVersion(String uriString) {
return this.getResourceVersion(this.appResourceCommon.getResource(uriString));
}

@Override
public Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersion(ApplicationType type,
String name, String version, boolean defaultVersion, Pageable pageable) {
return appRegistrationDao.findAllByTypeAndNameIsLikeAndVersionAndDefaultVersion(type, name, version,
defaultVersion, pageable);
}

protected Properties loadProperties(Resource resource) {
try {
return PropertiesLoaderUtils.loadProperties(resource);
Expand Down Expand Up @@ -383,9 +399,7 @@ public List<AppRegistration> importAll(boolean overwrite, Resource... resources)
return registrations;
}

private BiFunction<HashMap<String, AppRegistration>,
? super String[],
HashMap<String, AppRegistration>> reduceToAppRegistrations() {
private BiFunction<HashMap<String, AppRegistration>, ? super String[], HashMap<String, AppRegistration>> reduceToAppRegistrations() {
return (map, lineSplit) -> {
String[] typeName = lineSplit[0].split("\\.");
if (typeName.length < 2 || typeName.length > 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.cloud.dataflow.audit.service.DefaultAuditRecordService;
import org.springframework.cloud.dataflow.core.AppRegistration;
import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.support.AppResourceCommon;
import org.springframework.cloud.deployer.resource.maven.MavenProperties;
Expand Down Expand Up @@ -72,7 +73,8 @@ public class DefaultAppRegistryServiceTests {
private ResourceLoader resourceLoader = new DefaultResourceLoader();

private AppRegistryService appRegistryService = new DefaultAppRegistryService(appRegistrationRepository,
new AppResourceCommon(new MavenProperties(), resourceLoader), mock(DefaultAuditRecordService.class));
new AppResourceCommon(new MavenProperties(), resourceLoader), mock(DefaultAuditRecordService.class), mock(
AppRegistrationDao.class));

@Test
public void testNotFound() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.cloud.dataflow.completion.TaskCompletionProvider;
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
import org.springframework.cloud.dataflow.core.StreamDefinitionService;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.service.AppRegistryService;
import org.springframework.cloud.dataflow.registry.service.DefaultAppRegistryService;
Expand Down Expand Up @@ -227,8 +228,10 @@ public AppResourceCommon appResourceCommon(@Nullable MavenProperties mavenProper

@Bean
public AppRegistryService appRegistryService(AppRegistrationRepository appRegistrationRepository,
AppResourceCommon appResourceCommon, AuditRecordService auditRecordService) {
return new DefaultAppRegistryService(appRegistrationRepository, appResourceCommon, auditRecordService);
AppResourceCommon appResourceCommon, AuditRecordService auditRecordService,
AppRegistrationDao appRegistrationDao) {
return new DefaultAppRegistryService(appRegistrationRepository, appResourceCommon, auditRecordService,
appRegistrationDao);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.dataflow.server.config;

import javax.persistence.EntityManager;
import javax.servlet.Filter;
import javax.sql.DataSource;

Expand All @@ -26,6 +27,9 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.dataflow.audit.service.AuditRecordService;
import org.springframework.cloud.dataflow.completion.CompletionConfiguration;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationDao;
import org.springframework.cloud.dataflow.registry.repository.AppRegistrationRepository;
import org.springframework.cloud.dataflow.registry.repository.JdbcAppRegistrationDao;
import org.springframework.cloud.dataflow.server.config.apps.CommonApplicationProperties;
import org.springframework.cloud.dataflow.server.config.features.FeaturesConfiguration;
import org.springframework.cloud.dataflow.server.config.web.WebConfiguration;
Expand Down Expand Up @@ -113,4 +117,10 @@ public AuthenticationSuccessEventListener authenticationSuccessEventListener(
AuditRecordService auditRecordService) {
return new AuthenticationSuccessEventListener(auditRecordService);
}

@Bean
AppRegistrationDao appRegistrationDao(
EntityManager entityManager, AppRegistrationRepository appRegistrationRepository) {
return new JdbcAppRegistrationDao(entityManager, appRegistrationRepository);
}
}
Loading