Commit 06e51680 authored by Yiming Liu's avatar Yiming Liu

Refine biz and entity

parent 05ed035b
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -11,27 +8,26 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AppService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.AppDTO;
@RestController
@RequestMapping("/configs")
public class ConfigController {
public class AppController {
@Autowired
private AdminConfigService adminConfigService;
@Autowired
private AdminReleaseService adminReleaseService;
@RequestMapping("/release/{releaseId}")
public List<ReleaseSnapshotDTO> getRelaseSnapshot(@PathVariable long releaseId) {
return adminReleaseService.findReleaseSnapshotByReleaseId(releaseId);
private AppService appService;
@RequestMapping("/apps/{appId}")
public AppDTO findByAppId(@PathVariable("appId") String appId) {
App app = appService.findByAppId(appId);
return BeanUtils.transfrom(AppDTO.class, app);
}
@RequestMapping("/latest")
public List<ConfigItemDTO> findConfigItemsByClusters(
@RequestParam(value = "clusterIds") List<Long> clusterIds) {
return adminConfigService.findConfigItemsByClusters(clusterIds);
@RequestMapping("/apps")
public List<AppDTO> findByName(@RequestParam("name") String name) {
List<App> app = appService.findByName(name);
return BeanUtils.batchTransform(AppDTO.class, app);
}
}
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService;
import com.ctrip.apollo.core.dto.ClusterDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
@RestController
@RequestMapping("/cluster")
public class ClusterController {
@Autowired
private AdminConfigService adminConfigService;
private ViewService viewService;
@RequestMapping("/app/{appId}")
public List<ClusterDTO> findClustersByApp(@PathVariable String appId) {
return adminConfigService.findClustersByApp(appId);
@RequestMapping("/apps/{appId}/clusters")
public List<ClusterDTO> findClusters(@PathVariable("appId") String appId) {
List<Cluster> clusters = viewService.findClusters(appId);
return BeanUtils.batchTransform(ClusterDTO.class, clusters);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.service.GroupService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.GroupDTO;
@RestController
public class GroupController {
@Autowired
private ViewService viewService;
@Autowired
private GroupService groupService;
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups")
public List<GroupDTO> findGroups(@PathVariable("clusterId") Long clusterId) {
List<Group> groups = viewService.findGroups(clusterId);
return BeanUtils.batchTransform(GroupDTO.class, groups);
}
@RequestMapping("/groups/{groupId}")
public GroupDTO findOne(@PathVariable("groupId") Long groupId){
Group group = groupService.findOne(groupId);
return BeanUtils.transfrom(GroupDTO.class, group);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
@RestController
public class ItemController {
@Autowired
private ViewService viewService;
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/items")
public List<ItemDTO> findItems(
@PathVariable("groupId") Long groupId) {
List<Item> items = viewService.findItems(groupId);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
@RestController
public class ReleaseController {
@Autowired
private ViewService viewSerivce;
@Autowired
private ReleaseService releaseService;
@RequestMapping("/release/{releaseId}")
public ReleaseDTO findOne(@PathVariable("releaseId") long releaseId) {
Release release = releaseService.findOne(releaseId);
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("groupId") Long groupId){
List<Release> releases = viewSerivce.findReleases(groupId);
return BeanUtils.batchTransform(ReleaseDTO.class, releases);
}
}
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.VersionDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.VersionService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.VersionDTO;
@RestController
@RequestMapping("/version")
public class VersionController {
@Autowired
private AdminReleaseService adminReleaseService;
private ViewService viewService;
@RequestMapping("/app/{appId}")
public List<VersionDTO> versions(@PathVariable String appId) {
return adminReleaseService.findVersionsByApp(appId);
@Autowired
private VersionService versionService;
@RequestMapping("/app/{appId}/clusters/{clusterId}/versions")
public List<VersionDTO> findVersions(@PathVariable("appId") String appId, @PathVariable("clusterId") Long clusterId) {
List<Version> versions = viewService.findVersions(clusterId);
return BeanUtils.batchTransform(VersionDTO.class, versions);
}
@RequestMapping("/{versionId}")
public VersionDTO version(@PathVariable long versionId) {
return adminReleaseService.loadVersionById(versionId);
@RequestMapping("/versions/{versionId}")
public VersionDTO findOne(@PathVariable("versionId") long versionId) {
Version version = versionService.findOne(versionId);
return BeanUtils.transfrom(VersionDTO.class, version);
}
}
......@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class App extends BaseEntity{
@Id
@GeneratedValue
private long id;
public class App extends BaseEntity {
@Column(nullable = false)
private String name;
......@@ -19,37 +13,40 @@ public class App extends BaseEntity{
private String appId;
@Column(nullable = false)
private String owner;
private String ownerName;
@Column(nullable = false)
private String ownerEmail;
public String getAppId() {
return appId;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getOwner() {
return owner;
public String getOwnerEmail() {
return ownerEmail;
}
public void setAppId(String appId) {
this.appId = appId;
public String getOwnerName() {
return ownerName;
}
public void setId(long id) {
this.id = id;
public void setAppId(String appId) {
this.appId = appId;
}
public void setName(String name) {
this.name = name;
}
public void setOwner(String owner) {
this.owner = owner;
public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
}
......@@ -3,6 +3,8 @@ package com.ctrip.apollo.biz.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -11,8 +13,12 @@ import org.hibernate.annotations.Where;
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
public abstract class BaseEntity {
@Id
@GeneratedValue
private long id;
private boolean isDeleted;
@Column(name = "DataChange_CreatedBy")
private String dataChangeCreatedBy;
......@@ -24,7 +30,7 @@ public abstract class BaseEntity {
@Column(name = "DataChange_LastTime")
private Date dataChangeLastModifiedTime;
public String getDataChangeCreatedBy() {
return dataChangeCreatedBy;
}
......@@ -41,6 +47,10 @@ public abstract class BaseEntity {
return dataChangeLastModifiedTime;
}
public long getId() {
return id;
}
public boolean isDeleted() {
return isDeleted;
}
......@@ -64,4 +74,8 @@ public abstract class BaseEntity {
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public void setId(long id) {
this.id = id;
}
}
......@@ -2,18 +2,12 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
public class Cluster extends BaseEntity{
@Id
@GeneratedValue
private long id;
public class Cluster extends BaseEntity {
@Column(nullable = false)
private String name;
......@@ -21,42 +15,20 @@ public class Cluster extends BaseEntity{
@Column(nullable = false)
private String appId;
@Column
private long clusterVersionId;
public Cluster() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
public String getAppId() {
return appId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public long getClusterVersionId() {
return clusterVersionId;
}
public void setClusterVersionId(long clusterVersionId) {
this.clusterVersionId = clusterVersionId;
public void setName(String name) {
this.name = name;
}
}
......@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Group extends BaseEntity{
@Id
@GeneratedValue
private long id;
public class Group extends BaseEntity {
@Column(nullable = false)
private long clusterId;
......@@ -18,12 +12,15 @@ public class Group extends BaseEntity{
@Column(nullable = false)
private long namespaceId;
@Column(nullable = false)
private String name;
public long getClusterId() {
return clusterId;
}
public long getId() {
return id;
public String getName() {
return name;
}
public long getNamespaceId() {
......@@ -34,8 +31,8 @@ public class Group extends BaseEntity{
this.clusterId = clusterId;
}
public void setId(long id) {
this.id = id;
public void setName(String name) {
this.name = name;
}
public void setNamespaceId(long namespaceId) {
......
......@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class ConfigItem extends BaseEntity {
@Id
@GeneratedValue
private long id;
public class Item extends BaseEntity {
@Column(nullable = false)
private long groupId;
......@@ -24,14 +18,6 @@ public class ConfigItem extends BaseEntity {
@Column
private String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getGroupId() {
return groupId;
}
......
......@@ -2,16 +2,10 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Namespace extends BaseEntity{
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
......@@ -29,10 +23,6 @@ public class Namespace extends BaseEntity{
return comment;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
......@@ -45,10 +35,6 @@ public class Namespace extends BaseEntity{
this.comment = comment;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
......
package com.ctrip.apollo.biz.entity;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
public class Release extends BaseEntity{
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
......@@ -21,6 +19,9 @@ public class Release extends BaseEntity{
@Column(nullable = false)
private long groupId;
@Column
private String groupName;
@Column(nullable = false)
@Lob
private String configurations;
......@@ -28,11 +29,21 @@ public class Release extends BaseEntity{
@Column(nullable = false)
private String comment;
public Release() {
@ManyToMany
private List<Version> versions;
@Column(nullable=false)
private String appId;
@Column(nullable=false)
private String clusterName;
public String getAppId() {
return appId;
}
public long getGroupId() {
return groupId;
public String getClusterName() {
return clusterName;
}
public String getComment() {
......@@ -43,16 +54,28 @@ public class Release extends BaseEntity{
return configurations;
}
public long getId() {
return id;
public long getGroupId() {
return groupId;
}
public String getGroupName() {
return groupName;
}
public String getName() {
return name;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
public List<Version> getVersions() {
return versions;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public void setComment(String comment) {
......@@ -63,11 +86,19 @@ public class Release extends BaseEntity{
this.configurations = configurations;
}
public void setId(long id) {
this.id = id;
public void setGroupId(long groupId) {
this.groupId = groupId;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public void setName(String name) {
this.name = name;
}
public void setVersions(List<Version> versions) {
this.versions = versions;
}
}
......@@ -4,40 +4,31 @@ import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
public class Version extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String clusterId;
private long clusterId;
// parent version could be null
@Column
private Long parentVersion;
private List<Release> releases;
public Version() {}
public String getClusterId() {
@ManyToMany
private List<Release> releases;
public long getClusterId() {
return clusterId;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
......@@ -50,12 +41,9 @@ public class Version extends BaseEntity {
return releases;
}
public void setClusterId(String clusterId) {
this.clusterId = clusterId;
}
public void setId(long id) {
this.id = id;
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setName(String name) {
......
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import com.ctrip.apollo.biz.entity.App;
public interface AppRepository extends PagingAndSortingRepository<App, Long> {
@Query("SELECT a from App a WHERE a.name LIKE %':name'%")
List<App> findByName(@Param("name") String name);
App findByAppId(String appId);
}
......@@ -11,4 +11,5 @@ public interface ClusterRepository extends PagingAndSortingRepository<Cluster, L
List<Cluster> findByAppId(String appId);
Cluster findByAppIdAndName(String appId, String name);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.ConfigItem;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
import com.ctrip.apollo.biz.entity.Group;
public interface ConfigItemRepository extends PagingAndSortingRepository<ConfigItem, Long> {
public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {
List<ConfigItem> findByClusterIdIsIn(List<Long> clusterIds);
List<Group> findByClusterId(Long clusterId);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Item;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
List<Item> findByGroupIdIsIn(List<Long> groupIds);
List<Item> findByGroupId(Long groupId);
}
package com.ctrip.apollo.biz.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Namespace;
public interface NamespaceRepository extends PagingAndSortingRepository<Namespace, Long>{
}
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import com.ctrip.apollo.biz.entity.Release;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface ReleaseRepository extends PagingAndSortingRepository<Release, Long> {
@Query("SELECT r FROM VersionReleaseMapping m INNER JOIN Release r on m.releaseId = r.Id INNER JOIN Version v on m.versionId = v.id WHERE r.id = :versionId AND r.groupName = :groupName")
Release findByGroupNameAndVersionId(@Param("groupName") String groupName, @Param("versionId") long versionId);
List<Release> findByGroupId(Long groupId);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface ReleaseSnapShotRepository
extends PagingAndSortingRepository<ReleaseSnapshot, Long> {
ReleaseSnapshot findByReleaseIdAndClusterName(long releaseId, String clusterName);
List<ReleaseSnapshot> findByReleaseId(long releaseId);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Version;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
import com.ctrip.apollo.biz.entity.Version;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface VersionRepository extends PagingAndSortingRepository<Version, Long> {
Version findByAppIdAndName(String appId, String name);
Version findById(long id);
Version findByClusterIdAndName(Long id, String name);
List<Version> findByAppId(String appId);
List<Version> findByClusterId(Long clusterId);
}
package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service("adminReleaseService")
public class AdminReleaseService {
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired
private VersionRepository versionRepository;
public List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId) {
if (releaseId <= 0) {
return Collections.EMPTY_LIST;
}
List<ReleaseSnapshot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId);
if (releaseSnapShots == null || releaseSnapShots.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ReleaseSnapshotDTO.class, releaseSnapShots);
}
public List<VersionDTO> findVersionsByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
}
List<Version> versions = versionRepository.findByAppId(appId);
if (versions == null || versions.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(VersionDTO.class, versions);
}
public VersionDTO loadVersionById(long versionId) {
if (versionId <= 0) {
return null;
}
Version version = versionRepository.findById(versionId);
if (version == null) {
return null;
}
VersionDTO dto = ApolloBeanUtils.transfrom(VersionDTO.class, version);
return dto;
}
}
package com.ctrip.apollo.biz.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.repository.AppRepository;
@Service
public class AppService {
@Autowired
private AppRepository appRepository;
public App save(App entity){
return appRepository.save(entity);
}
public List<App> findAll(Pageable pageable){
Page<App> page = appRepository.findAll(pageable);
return page.getContent();
}
public List<App> findByName(String name){
return appRepository.findByName(name);
}
public App findByAppId(String appId){
return appRepository.findByAppId(appId);
}
}
package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.Map;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
import com.google.common.collect.Maps;
/**
* Config Service
*
* @author Jason Song(song_s@ctrip.com)
*/
@Service("configService")
@Service
public class ConfigService {
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private VersionRepository versionRepository;
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
private ReleaseRepository releaseRepository;
@Autowired
private ObjectMapper objectMapper;
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {
};
new TypeReference<Map<String, Object>>() {};
/**
* Load configuration from database
*/
public ApolloConfig loadConfig(String appId, String clusterName, String versionName) {
Version version = loadVersionByAppIdAndVersionName(appId, versionName);
public Release findRelease(String appId, String clusterName, String groupName, String versionName) {
Cluster cluster = clusterRepository.findByAppIdAndName(appId, clusterName);
if (cluster == null) {
return null;
}
Version version = versionRepository.findByClusterIdAndName(cluster.getId(), versionName);
if (version == null) {
return null;
}
return loadConfigByVersionAndClusterName(version, clusterName);
Release release = releaseRepository.findByGroupNameAndVersionId(groupName, version.getId());
return release;
}
/**
* Load Version by appId and versionName from database
*/
public Version loadVersionByAppIdAndVersionName(String appId, String versionName) {
return versionRepository.findByAppIdAndName(appId, versionName);
}
/**
* Load Config by version and clusterName from database
* Load configuration from database
*/
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) {
ReleaseSnapshot releaseSnapShot =
releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) {
public ApolloConfig loadConfig(Release release, String groupName, String versionName) {
if(release==null){
return null;
}
return assembleConfig(version, releaseSnapShot);
}
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId());
config.setConfigurations(transformConfigurationToMap(releaseSnapShot.getConfigurations()));
new ApolloConfig(release.getAppId(), release.getClusterName(), groupName, versionName, release.getId());
config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
return config;
}
......
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.repository.GroupRepository;
@Service
public class GroupService {
@Autowired
private GroupRepository groupRepository;
public Group findOne(Long groupId){
return groupRepository.findOne(groupId);
}
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public class ReleaseService {
@Autowired
private ReleaseRepository releaseRepository;
public Release findOne(long releaseId) {
Release release = releaseRepository.findOne(releaseId);
return release;
}
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.VersionRepository;
@Service
public class VersionService {
@Autowired
private VersionRepository versionRepository;
public Version findOne(Long versionId){
return versionRepository.findOne(versionId);
}
}
package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.GroupRepository;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.google.common.base.Strings;
/**
* config service for admin
*/
@Service("adminConfigService")
public class AdminConfigService {
@Service
public class ViewService {
@Autowired
private ClusterRepository clusterRepository;
@Autowired
private GroupRepository groupRepository;
@Autowired
private ConfigItemRepository configItemRepository;
private VersionRepository versionRepository;
@Autowired
private ItemRepository itemRepository;
@Autowired
private ReleaseRepository releaseRepository;
public List<ClusterDTO> findClustersByApp(String appId) {
public List<Cluster> findClusters(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
}
List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null || clusters.size() == 0) {
if (clusters == null) {
return Collections.EMPTY_LIST;
}
return clusters;
}
return ApolloBeanUtils.batchTransform(ClusterDTO.class, clusters);
public List<Group> findGroups(Long clusterId) {
List<Group> groups = groupRepository.findByClusterId(clusterId);
if (groups == null) {
return Collections.EMPTY_LIST;
}
return groups;
}
public List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) {
public List<Version> findVersions(Long clusterId) {
List<Version> versions = versionRepository.findByClusterId(clusterId);
if (versions == null) {
return Collections.EMPTY_LIST;
}
List<ConfigItem> configItems = configItemRepository.findByClusterIdIsIn(clusterIds);
if (configItems == null || configItems.size() == 0) {
return versions;
}
public List<Item> findItems(Long groupId) {
List<Item> items = itemRepository.findByGroupId(groupId);
if (items == null) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ConfigItemDTO.class, configItems);
return items;
}
public List<Release> findReleases(Long groupId){
List<Release> releases = releaseRepository.findByGroupId(groupId);
if(releases==null){
return Collections.EMPTY_LIST;
}
return releases;
}
}
......@@ -14,7 +14,7 @@ import java.util.Map;
import java.util.Set;
public class ApolloBeanUtils {
public class BeanUtils {
/**
* <pre>
......
package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
......@@ -38,7 +37,7 @@ public class ConfigServiceTest {
@Mock
private VersionRepository versionRepository;
@Mock
private ReleaseSnapShotRepository releaseSnapShotRepository;
private ReleaseRepository releaseRepository;
@Mock
private ObjectMapper objectMapper;
private ConfigService configService;
......@@ -48,88 +47,90 @@ public class ConfigServiceTest {
configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository);
ReflectionTestUtils
.setField(configService, "releaseSnapShotRepository", releaseSnapShotRepository);
.setField(configService, "releaseRepository", releaseRepository);
ReflectionTestUtils.setField(configService, "objectMapper", objectMapper);
}
@Test
public void testLoadConfig() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someVersionName = "someVersionName";
long someReleaseId = 1;
String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
ReleaseSnapshot
someReleaseSnapShot =
assembleReleaseSnapShot(someReleaseId, someClusterName, someValidConfiguration);
Map<String, Object> someMap = Maps.newHashMap();
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
when(releaseSnapShotRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
.thenReturn(someReleaseSnapShot);
when(objectMapper.readValue(eq(someValidConfiguration), (TypeReference) anyObject()))
.thenReturn(someMap);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertEquals(someAppId, result.getAppId());
assertEquals(someClusterName, result.getCluster());
assertEquals(someVersionName, result.getVersion());
assertEquals(someReleaseId, result.getReleaseId());
assertEquals(someMap, result.getConfigurations());
}
@Test
public void testLoadConfigWithVersionNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someVersionName = "someVersionName";
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(null);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertNull(result);
verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
}
@Test
public void testLoadConfigWithConfigNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someVersionName = "someVersionName";
long someReleaseId = 1;
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
when(releaseSnapShotRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
.thenReturn(null);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertNull(result);
verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
verify(releaseSnapShotRepository, times(1))
.findByReleaseIdAndClusterName(someReleaseId, someClusterName);
}
private Version assembleVersion(String appId, String versionName, long releaseId) {
Version version = new Version();
version.setAppId(appId);
version.setName(versionName);
version.setReleaseId(releaseId);
return version;
}
private ReleaseSnapshot assembleReleaseSnapShot(long releaseId, String clusterName,
// @Test
// public void testLoadConfig() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someGroupName = "someGroupName";
// String someVersionName = "someVersionName";
// long someReleaseId = 1;
// String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
//
// Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
// Release
// someRelease =
// assembleRelease(someReleaseId, someClusterName, someGroupName, someValidConfiguration);
// Map<String, Object> someMap = Maps.newHashMap();
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
// when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
// .thenReturn(someReleaseSnapShot);
// when(objectMapper.readValue(eq(someValidConfiguration), (TypeReference) anyObject()))
// .thenReturn(someMap);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertEquals(someAppId, result.getAppId());
// assertEquals(someClusterName, result.getCluster());
// assertEquals(someVersionName, result.getVersion());
// assertEquals(someReleaseId, result.getReleaseId());
// assertEquals(someMap, result.getConfigurations());
// }
//
// @Test
// public void testLoadConfigWithVersionNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someVersionName = "someVersionName";
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(null);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertNull(result);
// verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
// }
//
// @Test
// public void testLoadConfigWithConfigNotFound() throws Exception {
// String someAppId = "1";
// String someClusterName = "someClusterName";
// String someVersionName = "someVersionName";
// long someReleaseId = 1;
// Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
//
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
// when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
// .thenReturn(null);
//
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
//
// assertNull(result);
// verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
// verify(releaseRepository, times(1))
// .findByReleaseIdAndClusterName(someReleaseId, someClusterName);
// }
//
// private Version assembleVersion(String appId, String versionName, long releaseId) {
// Version version = new Version();
// version.setAppId(appId);
// version.setName(versionName);
// version.setReleaseId(releaseId);
// return version;
// }
private Release assembleRelease(long releaseId, String clusterName, String groupName,
String configurations) {
ReleaseSnapshot releaseSnapShot = new ReleaseSnapshot();
releaseSnapShot.setReleaseId(releaseId);
releaseSnapShot.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations);
return releaseSnapShot;
Release release = new Release();
release.setId(releaseId);
release.setClusterName(clusterName);
release.setGroupName(groupName);
release.setConfigurations(configurations);
return release;
}
......
......@@ -197,10 +197,11 @@ public class ConfigLoaderManagerTest {
ApolloConfig assembleApolloConfig(String appId, Map<String, Object> configurations) {
String someCluster = "someCluster";
String someGroup = "someGroup";
String someVersion = "someVersion";
long someReleaseId = 1;
ApolloConfig config = new ApolloConfig(appId, someCluster, someVersion, someReleaseId);
ApolloConfig config = new ApolloConfig(appId, someCluster, someGroup, someVersion, someReleaseId);
config.setConfigurations(configurations);
......
package com.ctrip.apollo.configservice.controller;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -11,9 +11,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
/**
* @author Jason Song(song_s@ctrip.com)
......@@ -24,32 +24,31 @@ public class ConfigController {
@Autowired
private ConfigService configService;
@RequestMapping(value = "/{appId}/{clusterName}/{versionName:.*}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId,
@PathVariable String clusterName,
@PathVariable String versionName,
@RequestParam(value = "releaseId", defaultValue = "-1") long clientSideReleaseId,
HttpServletResponse response) throws IOException {
Version version = configService.loadVersionByAppIdAndVersionName(appId, versionName);
if (version == null) {
@RequestMapping(value = "/{appId}/{clusterName}/{groupName}/{versionName:.*}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String groupName, @PathVariable String versionName,
@RequestParam(value = "releaseId", defaultValue = "-1") long clientSideReleaseId,
HttpServletResponse response) throws IOException {
Release release = configService.findRelease(appId, clusterName, groupName, versionName);
if (release == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND,
String.format("Could not load version with appId: %s, versionName: %s", appId,
versionName));
String.format(
"Could not load version with appId: %s, clusterName: %s, groupName: %s, versionName: %s",
appId, clusterName, groupName, versionName));
return null;
}
if (version.getReleaseId() == clientSideReleaseId) {
//Client side configuration is the same with server side, return 304
if (release.getId() == clientSideReleaseId) {
// Client side configuration is the same with server side, return 304
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return null;
}
ApolloConfig apolloConfig =
configService.loadConfigByVersionAndClusterName(version, clusterName);
ApolloConfig apolloConfig = configService.loadConfig(release, groupName, versionName);
if (apolloConfig == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND,
String.format("Could not load config with releaseId: %d, clusterName: %s",
version.getReleaseId(), clusterName));
release.getId(), clusterName));
return null;
}
......
package com.ctrip.apollo.configservice.controller;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
......@@ -44,45 +44,42 @@ public class ConfigControllerTest {
ApolloConfig someApolloConfig = mock(ApolloConfig.class);
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideNewReleaseId = 2;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Version someVersion = mock(Version.class);
Release someRelease = mock(Release.class);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName))
.thenReturn(someVersion);
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName))
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfig(someRelease, someGroupName, someVersionName))
.thenReturn(someApolloConfig);
ApolloConfig
result =
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertEquals(someApolloConfig, result);
verify(configService, times(1)).loadVersionByAppIdAndVersionName(someAppId, someVersionName);
verify(configService, times(1)).loadConfigByVersionAndClusterName(someVersion, someClusterName);
verify(configService, times(1)).findRelease(someAppId, someClusterName, someGroupName,
someVersionName);
verify(configService, times(1)).loadConfig(someRelease, someGroupName, someVersionName);
}
@Test
public void testQueryConfigWithVersionNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName))
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(null);
ApolloConfig
result =
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
......@@ -92,23 +89,20 @@ public class ConfigControllerTest {
public void testQueryConfigWithApolloConfigNotFound() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideNewReleaseId = 2;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Version someVersion = mock(Version.class);
Release someRelease = mock(Release.class);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName))
.thenReturn(someVersion);
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName))
.thenReturn(null);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfig(someRelease, someGroupName, someVersionName)).thenReturn(null);
ApolloConfig
result =
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
......@@ -118,25 +112,22 @@ public class ConfigControllerTest {
public void testQueryConfigWithApolloConfigNotModified() throws Exception {
String someAppId = "1";
String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion";
long someClientSideReleaseId = 1;
long someServerSideReleaseId = someClientSideReleaseId;
HttpServletResponse someResponse = mock(HttpServletResponse.class);
Version someVersion = mock(Version.class);
Release someRelease = mock(Release.class);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName))
.thenReturn(someVersion);
when(someVersion.getReleaseId()).thenReturn(someServerSideReleaseId);
when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(someRelease);
when(someRelease.getId()).thenReturn(someServerSideReleaseId);
ApolloConfig
result =
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
someVersionName, someClientSideReleaseId, someResponse);
assertNull(result);
verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
verify(configService, never())
.loadConfigByVersionAndClusterName(any(Version.class), anyString());
verify(configService, never()).loadConfig(any(Release.class), anyString(), anyString());
}
}
......@@ -16,6 +16,8 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
private String cluster;
private String group;
private String version;
private Map<String, Object> configurations;
......@@ -27,21 +29,26 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
@JsonCreator
public ApolloConfig(@JsonProperty("appId") String appId,
@JsonProperty("cluster") String cluster,
@JsonProperty("group") String group,
@JsonProperty("version") String version,
@JsonProperty("releaseId") long releaseId) {
super();
this.appId = appId;
this.cluster = cluster;
this.group = group;
this.version = version;
this.releaseId = releaseId;
}
public Map<String, Object> getConfigurations() {
return configurations;
}
public void setConfigurations(Map<String, Object> configurations) {
this.configurations = configurations;
@Override
public int compareTo(ApolloConfig toCompare) {
if (toCompare == null || this.getOrder() > toCompare.getOrder()) {
return 1;
}
if (toCompare.getOrder() > this.getOrder()) {
return -1;
}
return 0;
}
public String getAppId() {
......@@ -52,18 +59,34 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
return cluster;
}
public String getVersion() {
return version;
public Map<String, Object> getConfigurations() {
return configurations;
}
public long getReleaseId() {
return releaseId;
public String getGroup() {
return group;
}
public int getOrder() {
return order;
}
public long getReleaseId() {
return releaseId;
}
public String getVersion() {
return version;
}
public void setConfigurations(Map<String, Object> configurations) {
this.configurations = configurations;
}
public void setGroup(String group) {
this.group = group;
}
public void setOrder(int order) {
this.order = order;
}
......@@ -74,20 +97,10 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
.omitNullValues()
.add("appId", appId)
.add("cluster", cluster)
.add("group", group)
.add("version", version)
.add("releaseId", releaseId)
.add("configurations", configurations)
.toString();
}
@Override
public int compareTo(ApolloConfig toCompare) {
if (toCompare == null || this.getOrder() > toCompare.getOrder()) {
return 1;
}
if (toCompare.getOrder() > this.getOrder()) {
return -1;
}
return 0;
}
}
package com.ctrip.apollo.core.dto;
public class AppDTO {
private String name;
private String appId;
private String ownerName;
private String ownerEmail;
public String getAppId() {
return appId;
}
public String getName() {
return name;
}
public String getOwnerEmail() {
return ownerEmail;
}
public String getOwnerName() {
return ownerName;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setName(String name) {
this.name = name;
}
public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
}
package com.ctrip.apollo.core.dto;
public class GroupDTO {
private long id;
private long clusterId;
private long namespaceId;
private String name;
public long getClusterId() {
return clusterId;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public long getNamespaceId() {
return namespaceId;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
}
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.core.dto;
import java.util.Date;
public class ConfigItemDTO {
public class ItemDTO {
private long id;
......@@ -18,19 +18,11 @@ public class ConfigItemDTO {
private String comment;
private String dataChangeCreatedBy;
private Date dataChangeCreatedTime;
private String dataChangeLastModifiedBy;
private Date dataChangeLastModifiedTime;
public ConfigItemDTO() {
public ItemDTO() {
}
public ConfigItemDTO(String key, String value) {
public ItemDTO(String key, String value) {
this.key = key;
this.value = value;
}
......@@ -83,38 +75,6 @@ public class ConfigItemDTO {
this.value = value;
}
public String getDataChangeCreatedBy() {
return dataChangeCreatedBy;
}
public void setDataChangeCreatedBy(String dataChangeCreatedBy) {
this.dataChangeCreatedBy = dataChangeCreatedBy;
}
public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
}
public void setDataChangeCreatedTime(Date dataChangeCreatedTime) {
this.dataChangeCreatedTime = dataChangeCreatedTime;
}
public String getDataChangeLastModifiedBy() {
return dataChangeLastModifiedBy;
}
public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
}
public Date getDataChangeLastModifiedTime() {
return dataChangeLastModifiedTime;
}
public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
}
public String getComment() {
return comment;
}
......
package com.ctrip.apollo.core.dto;
public class ReleaseSnapshotDTO {
public class ReleaseDTO {
private long id;
......@@ -10,7 +10,7 @@ public class ReleaseSnapshotDTO {
private String configurations;
public ReleaseSnapshotDTO() {
public ReleaseDTO() {
}
public long getId() {
......
......@@ -4,8 +4,8 @@ import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.stereotype.Service;
......@@ -19,16 +19,16 @@ public class AdminServiceAPI {
public static class ConfigAPI extends API {
public static String CONFIG_RELEASE_API = "/configs/release/";
public ReleaseSnapshotDTO[] getConfigByReleaseId(Apollo.Env env, long releaseId) {
public ReleaseDTO[] getConfigByReleaseId(Apollo.Env env, long releaseId) {
if (releaseId <= 0) {
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + CONFIG_RELEASE_API + releaseId,
ReleaseSnapshotDTO[].class);
ReleaseDTO[].class);
}
public ConfigItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) {
public ItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) {
return null;
}
......@@ -38,7 +38,7 @@ public class AdminServiceAPI {
}
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb
.substring(0, sb.length() - 1), ConfigItemDTO[].class);
.substring(0, sb.length() - 1), ItemDTO[].class);
}
}
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.portal.entity;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import java.util.LinkedList;
import java.util.List;
......@@ -21,7 +21,7 @@ public class AppConfigVO {
/**
* default cluster and app self’s configs
*/
private List<ConfigItemDTO> defaultClusterConfigs;
private List<ItemDTO> defaultClusterConfigs;
/**
* default cluster and override other app configs
......@@ -63,7 +63,7 @@ public class AppConfigVO {
public static class OverrideAppConfig {
private String appId;
private List<ConfigItemDTO> configs;
private List<ItemDTO> configs;
public OverrideAppConfig() {
......@@ -77,15 +77,15 @@ public class AppConfigVO {
this.appId = appId;
}
public List<ConfigItemDTO> getConfigs() {
public List<ItemDTO> getConfigs() {
return configs;
}
public void setConfigs(List<ConfigItemDTO> configs) {
public void setConfigs(List<ItemDTO> configs) {
this.configs = configs;
}
public void addConfig(ConfigItemDTO config) {
public void addConfig(ItemDTO config) {
if (configs == null) {
configs = new LinkedList<>();
}
......@@ -97,7 +97,7 @@ public class AppConfigVO {
public static class OverrideClusterConfig {
private String clusterName;
private List<ConfigItemDTO> configs;
private List<ItemDTO> configs;
public OverrideClusterConfig() {
}
......@@ -110,11 +110,11 @@ public class AppConfigVO {
this.clusterName = clusterName;
}
public List<ConfigItemDTO> getConfigs() {
public List<ItemDTO> getConfigs() {
return configs;
}
public void setConfigs(List<ConfigItemDTO> configs) {
public void setConfigs(List<ItemDTO> configs) {
this.configs = configs;
}
}
......@@ -144,11 +144,11 @@ public class AppConfigVO {
this.versionId = versionId;
}
public List<ConfigItemDTO> getDefaultClusterConfigs() {
public List<ItemDTO> getDefaultClusterConfigs() {
return defaultClusterConfigs;
}
public void setDefaultClusterConfigs(List<ConfigItemDTO> defaultClusterConfigs) {
public void setDefaultClusterConfigs(List<ItemDTO> defaultClusterConfigs) {
this.defaultClusterConfigs = defaultClusterConfigs;
}
......
......@@ -16,8 +16,8 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants;
......@@ -53,14 +53,14 @@ public class ConfigService {
return null;
}
ReleaseSnapshotDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null;
}
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
for (ReleaseSnapshotDTO snapShot : releaseSnapShots) {
for (ReleaseDTO snapShot : releaseSnapShots) {
// default cluster
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
......@@ -81,17 +81,17 @@ public class ConfigService {
return version.getReleaseId();
}
private void collectDefaultClusterConfigs(String appId, ReleaseSnapshotDTO snapShot,
private void collectDefaultClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) {
Map<String, List<ConfigItemDTO>> groupedConfigs =
Map<String, List<ItemDTO>> groupedConfigs =
groupConfigsByApp(appId, snapShot.getConfigurations());
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs();
for (Map.Entry<String, List<ConfigItemDTO>> entry : groupedConfigs.entrySet()) {
for (Map.Entry<String, List<ItemDTO>> entry : groupedConfigs.entrySet()) {
String configAppId = entry.getKey();
List<ConfigItemDTO> kvs = entry.getValue();
List<ItemDTO> kvs = entry.getValue();
if (configAppId.equals(appId)) {
appConfigVO.setDefaultClusterConfigs(kvs);
......@@ -109,12 +109,12 @@ public class ConfigService {
/**
* appId -> List<KV>
*/
private Map<String, List<ConfigItemDTO>> groupConfigsByApp(String selfAppId, String configJson) {
private Map<String, List<ItemDTO>> groupConfigsByApp(String selfAppId, String configJson) {
if (configJson == null || "".equals(configJson)) {
return Maps.newHashMap();
}
Map<String, List<ConfigItemDTO>> appIdMapKVs = new HashMap<>();
Map<String, List<ItemDTO>> appIdMapKVs = new HashMap<>();
String key;
Object value;
......@@ -131,12 +131,12 @@ public class ConfigService {
value = entry.getValue();
String appId = getAppIdFromKey(key);
List<ConfigItemDTO> kvs = appIdMapKVs.get(appId);
List<ItemDTO> kvs = appIdMapKVs.get(appId);
if (kvs == null) {
kvs = new LinkedList<>();
appIdMapKVs.put(appId, kvs);
}
kvs.add(new ConfigItemDTO(key, value.toString()));
kvs.add(new ItemDTO(key, value.toString()));
}
return appIdMapKVs;
......@@ -147,7 +147,7 @@ public class ConfigService {
return key.substring(0, key.indexOf("."));
}
private void collectSpecialClusterConfigs(String appId, ReleaseSnapshotDTO snapShot,
private void collectSpecialClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) {
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs =
appConfigVO.getOverrideClusterConfigs();
......@@ -174,17 +174,17 @@ public class ConfigService {
clusterIds.add(cluster.getId());
}
ConfigItemDTO[] configItems = configAPI.getLatestConfigItemsByClusters(env, clusterIds);
ItemDTO[] configItems = configAPI.getLatestConfigItemsByClusters(env, clusterIds);
return buildAPPConfigVO(appId, Arrays.asList(configItems));
}
private AppConfigVO buildAPPConfigVO(String appId, List<ConfigItemDTO> configItems) {
private AppConfigVO buildAPPConfigVO(String appId, List<ItemDTO> configItems) {
if (configItems == null || configItems.size() == 0) {
return null;
}
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = groupConfigByCluster(configItems);
Map<String, List<ItemDTO>> groupedClusterConfigs = groupConfigByCluster(configItems);
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, PortalConstants.LASTEST_VERSION_ID);
......@@ -194,13 +194,13 @@ public class ConfigService {
}
private Map<String, List<ConfigItemDTO>> groupConfigByCluster(List<ConfigItemDTO> configItems) {
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = new HashMap<>();
private Map<String, List<ItemDTO>> groupConfigByCluster(List<ItemDTO> configItems) {
Map<String, List<ItemDTO>> groupedClusterConfigs = new HashMap<>();
String clusterName;
for (ConfigItemDTO configItem : configItems) {
for (ItemDTO configItem : configItems) {
clusterName = configItem.getClusterName();
List<ConfigItemDTO> clusterConfigs = groupedClusterConfigs.get(clusterName);
List<ItemDTO> clusterConfigs = groupedClusterConfigs.get(clusterName);
if (clusterConfigs == null) {
clusterConfigs = new LinkedList<>();
groupedClusterConfigs.put(clusterName, clusterConfigs);
......@@ -210,11 +210,11 @@ public class ConfigService {
return groupedClusterConfigs;
}
private void groupConfigByAppAndEnrichDTO(Map<String, List<ConfigItemDTO>> groupedClusterConfigs,
private void groupConfigByAppAndEnrichDTO(Map<String, List<ItemDTO>> groupedClusterConfigs,
AppConfigVO appConfigVO) {
String appId = appConfigVO.getAppId();
List<ConfigItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs();
List<ItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs();
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs();
......@@ -222,8 +222,8 @@ public class ConfigService {
appConfigVO.getOverrideClusterConfigs();
String clusterName;
List<ConfigItemDTO> clusterConfigs;
for (Map.Entry<String, List<ConfigItemDTO>> entry : groupedClusterConfigs.entrySet()) {
List<ItemDTO> clusterConfigs;
for (Map.Entry<String, List<ItemDTO>> entry : groupedClusterConfigs.entrySet()) {
clusterName = entry.getKey();
clusterConfigs = entry.getValue();
......@@ -238,13 +238,13 @@ public class ConfigService {
}
}
private void collectDefaultClusterConfigs(String appId, List<ConfigItemDTO> clusterConfigs,
List<ConfigItemDTO> defaultClusterConfigs,
private void collectDefaultClusterConfigs(String appId, List<ItemDTO> clusterConfigs,
List<ItemDTO> defaultClusterConfigs,
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) {
Map<String, AppConfigVO.OverrideAppConfig> appIdMapOverrideAppConfig = null;
for (ConfigItemDTO config : clusterConfigs) {
for (ItemDTO config : clusterConfigs) {
String targetAppId = config.getAppId();
if (appId.equals(targetAppId)) {// app self's configs
defaultClusterConfigs.add(config);
......@@ -268,7 +268,7 @@ public class ConfigService {
}
}
private void collectSpecialClusterConfigs(String clusterName, List<ConfigItemDTO> clusterConfigs,
private void collectSpecialClusterConfigs(String clusterName, List<ItemDTO> clusterConfigs,
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) {
AppConfigVO.OverrideClusterConfig overrideClusterConfig =
new AppConfigVO.OverrideClusterConfig();
......
......@@ -17,8 +17,8 @@ import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.core.exception.ServiceException;
......@@ -71,7 +71,7 @@ public class ConfigServiceTest {
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = assembleReleaseSnapShots();
ReleaseDTO[] someReleaseSnapShots = assembleReleaseSnapShots();
when(versionAPI.getVersionById(Env.DEV, versionId)).thenReturn(someVersion);
when(configAPI.getConfigByReleaseId(Env.DEV, releaseId)).thenReturn(someReleaseSnapShots);
......@@ -92,7 +92,7 @@ public class ConfigServiceTest {
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1];
ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
......@@ -114,7 +114,7 @@ public class ConfigServiceTest {
long versionId = 100;
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1];
ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}");
......@@ -136,7 +136,7 @@ public class ConfigServiceTest {
long versionId = 100;
long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[2];
ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[2];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
someReleaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1",
......@@ -158,7 +158,7 @@ public class ConfigServiceTest {
public void testLoadLastestConfig() {
String appId = "6666";
ClusterDTO[] someClusters = assembleClusters();
ConfigItemDTO[] someConfigItem = assembleConfigItems();
ItemDTO[] someConfigItem = assembleConfigItems();
when(clusterAPI.getClustersByApp(Env.DEV, appId)).thenReturn(someClusters);
when(configAPI.getLatestConfigItemsByClusters(Env.DEV, Arrays
......@@ -181,8 +181,8 @@ public class ConfigServiceTest {
return version;
}
private ReleaseSnapshotDTO[] assembleReleaseSnapShots() {
ReleaseSnapshotDTO[] releaseSnapShots = new ReleaseSnapshotDTO[3];
private ReleaseDTO[] assembleReleaseSnapShots() {
ReleaseDTO[] releaseSnapShots = new ReleaseDTO[3];
releaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\",\"3333.foo\":\"1008\",\"4444.bar\":\"99901\"}");
releaseSnapShots[1] = assembleReleaseSnapShot(11111, "cluster1", "{\"6666.foo\":\"demo1\"}");
......@@ -190,9 +190,9 @@ public class ConfigServiceTest {
return releaseSnapShots;
}
private ReleaseSnapshotDTO assembleReleaseSnapShot(long releaseId, String clusterName,
private ReleaseDTO assembleReleaseSnapShot(long releaseId, String clusterName,
String configurations) {
ReleaseSnapshotDTO releaseSnapShot = new ReleaseSnapshotDTO();
ReleaseDTO releaseSnapShot = new ReleaseDTO();
releaseSnapShot.setReleaseId(releaseId);
releaseSnapShot.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations);
......@@ -214,8 +214,8 @@ public class ConfigServiceTest {
return cluster;
}
private ConfigItemDTO[] assembleConfigItems() {
ConfigItemDTO[] configItems = new ConfigItemDTO[5];
private ItemDTO[] assembleConfigItems() {
ItemDTO[] configItems = new ItemDTO[5];
configItems[0] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k1", "6666.v1");
configItems[1] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k2", "6666.v2");
configItems[2] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k3", "6666.v3");
......@@ -224,9 +224,9 @@ public class ConfigServiceTest {
return configItems;
}
private ConfigItemDTO assembleConfigItem(long clusterId, String clusterName, String appId,
private ItemDTO assembleConfigItem(long clusterId, String clusterName, String appId,
String key, String value) {
ConfigItemDTO configItem = new ConfigItemDTO();
ItemDTO configItem = new ItemDTO();
configItem.setClusterName(clusterName);
configItem.setClusterId(clusterId);
configItem.setAppId(appId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment