Explorar o código

init from phab

xielq %!s(int64=4) %!d(string=hai) anos
pai
achega
6047862f4c
Modificáronse 31 ficheiros con 1136 adicións e 0 borrados
  1. 0 0
      README.md
  2. 44 0
      build.gradle
  3. 1 0
      gradle.properties
  4. 26 0
      gradle/dependencies.gradle
  5. 9 0
      gradle/tasks.gradle
  6. 2 0
      settings.gradle
  7. 6 0
      src/main/docker/Dockerfile
  8. 18 0
      src/main/java/com/uas/ops/diff/DiffApplication.java
  9. 43 0
      src/main/java/com/uas/ops/diff/api/v1/DiffControllerV1.java
  10. 70 0
      src/main/java/com/uas/ops/diff/api/v1/DiffServiceV1.java
  11. 43 0
      src/main/java/com/uas/ops/diff/api/v1/ManifestControllerV1.java
  12. 55 0
      src/main/java/com/uas/ops/diff/api/v1/ManifestServiceV1.java
  13. 54 0
      src/main/java/com/uas/ops/diff/api/v1/VersionControllerV1.java
  14. 51 0
      src/main/java/com/uas/ops/diff/api/v1/VersionServiceV1.java
  15. 18 0
      src/main/java/com/uas/ops/diff/config/AppConfig.java
  16. 53 0
      src/main/java/com/uas/ops/diff/domain/Manifest.java
  17. 94 0
      src/main/java/com/uas/ops/diff/entity/ProjectDiff.java
  18. 48 0
      src/main/java/com/uas/ops/diff/entity/ProjectDiffId.java
  19. 102 0
      src/main/java/com/uas/ops/diff/entity/ProjectVersion.java
  20. 43 0
      src/main/java/com/uas/ops/diff/entity/ProjectVersionId.java
  21. 22 0
      src/main/java/com/uas/ops/diff/repository/ProjectDiffRepository.java
  22. 24 0
      src/main/java/com/uas/ops/diff/repository/ProjectVersionRepository.java
  23. 45 0
      src/main/java/com/uas/ops/diff/service/DfsService.java
  24. 34 0
      src/main/java/com/uas/ops/diff/service/FileService.java
  25. 45 0
      src/main/java/com/uas/ops/diff/service/LocalFileService.java
  26. 26 0
      src/main/java/com/uas/ops/diff/util/ObjectUtils.java
  27. 13 0
      src/main/java/com/uas/ops/diff/util/StringUtils.java
  28. 68 0
      src/main/java/com/uas/ops/diff/util/ZipUtils.java
  29. 8 0
      src/main/resources/application.yml
  30. 29 0
      src/main/resources/bootstrap.yml
  31. 42 0
      src/test/java/com/uas/ops/diff/test/FileServiceTest.java

+ 0 - 0
README.md


+ 44 - 0
build.gradle

@@ -0,0 +1,44 @@
+buildscript {
+    ext {
+        springBootVersion = '1.4.4.RELEASE'
+        dockerVersion = '0.12.0'
+        dockerRegistry = "10.10.100.200:5000"
+    }
+    repositories {
+        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
+        maven { url "https://plugins.gradle.org/m2/" }
+        maven { url 'http://10.10.101.21:8081/artifactory/plugins-release' }
+        mavenCentral()
+        jcenter()
+    }
+    dependencies {
+        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
+        classpath "gradle.plugin.com.palantir.gradle.docker:gradle-docker:${dockerVersion}"
+    }
+}
+
+group 'com.uas.ops'
+version '0.0.1'
+
+apply plugin: 'java'
+apply plugin: "com.palantir.docker"
+apply plugin: "org.springframework.boot"
+
+apply from: "$rootDir/gradle/tasks.gradle"
+apply from: "$rootDir/gradle/dependencies.gradle"
+
+jar {
+    baseName = project.name
+    version = ''
+}
+
+allprojects {
+    sourceCompatibility = 1.8
+}
+
+dependencies {
+    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
+    compile 'org.springframework.boot:spring-boot-starter-web'
+    compile 'mysql:mysql-connector-java'
+    compile 'commons-io:commons-io:2.5'
+}

+ 1 - 0
gradle.properties

@@ -0,0 +1 @@
+org.gradle.jvmargs=-Xmx1024m

+ 26 - 0
gradle/dependencies.gradle

@@ -0,0 +1,26 @@
+repositories {
+    mavenLocal()
+    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
+    maven { url "http://maven.springframework.org/release" }
+    maven {
+        url 'http://10.10.101.21:8081/artifactory/libs-release'
+    }
+    maven {
+        url 'http://10.10.101.21:8081/artifactory/libs-snapshot'
+    }
+    maven {
+        url 'http://10.10.101.21:8081/artifactory/plugins-snapshot'
+    }
+    mavenCentral()
+}
+
+dependencyManagement {
+    imports {
+        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
+    }
+}
+
+dependencies {
+    compile 'org.springframework.cloud:spring-cloud-starter-config'
+    testCompile 'org.springframework.boot:spring-boot-starter-test'
+}

+ 9 - 0
gradle/tasks.gradle

@@ -0,0 +1,9 @@
+bootRun {
+    addResources = true
+}
+
+docker {
+    name "${dockerRegistry}/${project.name}:${project.version}"
+    dockerfile "${projectDir}/src/main/docker/Dockerfile"
+    files "${buildDir}/libs/${project.name}.jar"
+}.dependsOn build

+ 2 - 0
settings.gradle

@@ -0,0 +1,2 @@
+rootProject.name = 'ops-diff-service'
+

+ 6 - 0
src/main/docker/Dockerfile

@@ -0,0 +1,6 @@
+FROM hub.c.163.com/library/java:8-jre-alpine
+VOLUME /tmp # reate a temporary file on my host under "/var/lib/docker" and link it to the container under "/tmp".
+ADD ops-diff-service.jar app.jar
+RUN sh -c "touch /app.jar" #  "touch" the jar file so that it has a file modification time (Docker creates all container files in an "unmodified" state by default). This actually isn’t important for the simple app that we wrote, but any static content (e.g. "index.html") would require the file to have a modification time.
+ENV JAVA_OPTS=""
+ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Duser.timezone=GMT+08 -Djava.security.egd=file:/dev/./urandom -jar /app.jar --spring.profiles.active=prod"] # To reduce Tomcat startup time we added a system property pointing to "/dev/urandom" as a source of entropy.

+ 18 - 0
src/main/java/com/uas/ops/diff/DiffApplication.java

@@ -0,0 +1,18 @@
+package com.uas.ops.diff;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@SpringBootApplication
+@EnableJpaAuditing
+public class DiffApplication {
+
+    public static void main(String[] args) {
+        new SpringApplicationBuilder(DiffApplication.class).web(true).run(args);
+    }
+
+}

+ 43 - 0
src/main/java/com/uas/ops/diff/api/v1/DiffControllerV1.java

@@ -0,0 +1,43 @@
+package com.uas.ops.diff.api.v1;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@RestController
+@RequestMapping(path = "/v1/diff")
+public class DiffControllerV1 {
+
+    @Autowired
+    private DiffServiceV1 diffService;
+
+    @PostMapping
+    public ResponseEntity uploadDiff(@RequestParam("file") MultipartFile file, @RequestParam("groupId") String groupId,
+                                         @RequestParam("artifactId") String artifactId, @RequestParam("fromVersion") String fromVersion,
+                                            @RequestParam("toVersion") String toVersion) {
+        if (!file.isEmpty()) {
+            try {
+                diffService.save(groupId, artifactId, fromVersion, toVersion, file.getBytes());
+            } catch (Exception e) {
+                return ResponseEntity.badRequest().body("文件错误");
+            }
+        }
+        return ResponseEntity.ok("上传成功");
+    }
+
+    @GetMapping
+    public ResponseEntity downloadDiff(@RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId,
+                                       @RequestParam("fromVersion") String fromVersion, @RequestParam("toVersion") String toVersion) {
+        try {
+            return ResponseEntity.ok().header("Content-Disposition", "attachment;filename=.diff")
+                    .body(diffService.getDiffFile(groupId, artifactId, fromVersion, toVersion));
+        } catch (Exception e) {
+            return ResponseEntity.badRequest().body("未找到文件");
+        }
+    }
+
+}

+ 70 - 0
src/main/java/com/uas/ops/diff/api/v1/DiffServiceV1.java

@@ -0,0 +1,70 @@
+package com.uas.ops.diff.api.v1;
+
+import com.uas.ops.diff.entity.ProjectDiff;
+import com.uas.ops.diff.entity.ProjectDiffId;
+import com.uas.ops.diff.repository.ProjectDiffRepository;
+import com.uas.ops.diff.service.FileService;
+import com.uas.ops.diff.util.StringUtils;
+import com.uas.ops.diff.util.ZipUtils;
+import org.apache.commons.io.FileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.FileCopyUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Service
+public class DiffServiceV1 {
+
+    @Autowired
+    private ProjectDiffRepository projectDiffRepository;
+
+    @Autowired
+    private FileService fileService;
+
+    private String tmpdir = System.getProperty("java.io.tmpdir");
+
+    public void save(String groupId, String artifactId, String fromVersion, String toVersion, byte[] data) throws IOException {
+        ProjectDiffId projectDiffId = new ProjectDiffId(groupId, artifactId, fromVersion, toVersion);
+        ProjectDiff projectDiff = projectDiffRepository.findOne(projectDiffId);
+        if (null != projectDiff) {
+            fileService.delete(projectDiff.getDiffPath());
+        } else {
+            projectDiff = new ProjectDiff(groupId, artifactId, fromVersion, toVersion);
+        }
+        projectDiff.setCreateTime(new Date());
+        String path = fileService.save(data, ".diff");
+        projectDiff.setDiffPath(path);
+        projectDiffRepository.save(projectDiff);
+    }
+
+    public byte[] getDiffFile(String groupId, String artifactId, String fromVersion, String toVersion) throws IOException{
+        File mergedZipFile = new File(tmpdir, String.format("%s:%s:%s:%s.diff", groupId, artifactId, fromVersion, toVersion));
+        if (!mergedZipFile.exists()) {
+            List<ProjectDiff> diffs = projectDiffRepository.findByFromVersionAndToVersion(groupId, artifactId, fromVersion, toVersion);
+            if (!CollectionUtils.isEmpty(diffs)) {
+                if (diffs.size() == 1) {
+                    return  fileService.get(diffs.get(0).getDiffPath());
+                }
+                String targetDir = tmpdir + File.separator + StringUtils.uuid() + File.separator;
+                for (ProjectDiff diff:diffs) {
+                    byte[] zipData = fileService.get(diff.getDiffPath());
+                    ZipUtils.unzip(zipData, targetDir);
+                }
+                ZipUtils.zip(targetDir, mergedZipFile);
+                FileUtils.deleteDirectory(new File(targetDir));
+            } else {
+                return null;
+            }
+        }
+        return FileCopyUtils.copyToByteArray(mergedZipFile);
+    }
+
+}

+ 43 - 0
src/main/java/com/uas/ops/diff/api/v1/ManifestControllerV1.java

@@ -0,0 +1,43 @@
+package com.uas.ops.diff.api.v1;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@RestController
+@RequestMapping(path = "/v1/manifest")
+public class ManifestControllerV1 {
+
+    @Autowired
+    private ManifestServiceV1 manifestService;
+
+    @PostMapping
+    public ResponseEntity uploadManifest(@RequestParam("file") MultipartFile file, @RequestParam("groupId") String groupId,
+                                         @RequestParam("artifactId") String artifactId) {
+        if (!file.isEmpty()) {
+            try {
+                manifestService.save(groupId, artifactId, file.getBytes());
+            } catch (Exception e) {
+                e.printStackTrace();
+                return ResponseEntity.badRequest().body("文件错误");
+            }
+        }
+        return ResponseEntity.ok("上传成功");
+    }
+
+    @GetMapping
+    public ResponseEntity downloadManifest(@RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId,
+                                           @RequestParam("version") String version) {
+        try {
+            return ResponseEntity.ok().header("Content-Disposition", "attachment;filename=.manifest")
+                .body(manifestService.getManifestFile(groupId, artifactId, version));
+        } catch (Exception e) {
+            return ResponseEntity.badRequest().body("未找到文件");
+        }
+    }
+
+}

+ 55 - 0
src/main/java/com/uas/ops/diff/api/v1/ManifestServiceV1.java

@@ -0,0 +1,55 @@
+package com.uas.ops.diff.api.v1;
+
+import com.uas.ops.diff.domain.Manifest;
+import com.uas.ops.diff.entity.ProjectVersion;
+import com.uas.ops.diff.entity.ProjectVersionId;
+import com.uas.ops.diff.repository.ProjectVersionRepository;
+import com.uas.ops.diff.service.FileService;
+import com.uas.ops.diff.util.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.util.Date;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Service
+public class ManifestServiceV1 {
+
+    @Autowired
+    private ProjectVersionRepository projectVersionRepository;
+
+    @Autowired
+    private FileService fileService;
+
+    public void save(String groupId, String artifactId, byte[] data) throws IOException, ClassNotFoundException{
+        Manifest manifest = ObjectUtils.read(data);
+        String version = manifest.getVersion();
+        ProjectVersionId projectVersionId = new ProjectVersionId(groupId, artifactId, version);
+        ProjectVersion projectVersion = projectVersionRepository.findOne(projectVersionId);
+        if (null != projectVersion) {
+            fileService.delete(projectVersion.getManifestPath());
+        } else {
+            projectVersion = new ProjectVersion(groupId, artifactId, version);
+        }
+        projectVersion.setCreateTime(new Date());
+        projectVersion.setVersionTime(new Date(manifest.getLastChanged()));
+        // 默认有效
+        projectVersion.setEnabled((short) 1);
+        String path = fileService.save(data, ".manifest");
+        projectVersion.setManifestPath(path);
+        projectVersionRepository.save(projectVersion);
+    }
+
+    public byte[] getManifestFile(String groupId, String artifactId, String version) throws IOException{
+        ProjectVersionId projectVersionId = new ProjectVersionId(groupId, artifactId, version);
+        ProjectVersion projectVersion = projectVersionRepository.findOne(projectVersionId);
+        if (null != projectVersion) {
+            return fileService.get(projectVersion.getManifestPath());
+        }
+        return null;
+    }
+
+}

+ 54 - 0
src/main/java/com/uas/ops/diff/api/v1/VersionControllerV1.java

@@ -0,0 +1,54 @@
+package com.uas.ops.diff.api.v1;
+
+import com.uas.ops.diff.entity.ProjectVersion;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+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;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@RestController
+@RequestMapping(path = "/v1/version")
+public class VersionControllerV1 {
+
+    @Autowired
+    private VersionServiceV1 versionService;
+
+    @GetMapping
+    public ResponseEntity<String> getVersion(@RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId) {
+        return ResponseEntity.ok(versionService.getLastVersion(groupId, artifactId));
+    }
+
+    @GetMapping(path = "/list")
+    public ResponseEntity<Page<ProjectVersion>> getVersions(@PageableDefault(size = 15, page = 0, sort = { "version" }, direction = Sort.Direction.DESC) Pageable pageable,
+                                                            @RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId) {
+        return ResponseEntity.ok(versionService.findAll(groupId, artifactId, pageable));
+    }
+
+    @RequestMapping(path = "/enable")
+    public ResponseEntity enableVersion(@RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId,
+                                        @RequestParam("version") String version) {
+        versionService.changeVersionStatus(groupId, artifactId, version, (short) 1);
+        return ResponseEntity.ok().build();
+    }
+
+    @RequestMapping(path = "/disable")
+    public ResponseEntity disableVersion(@RequestParam("groupId") String groupId, @RequestParam("artifactId") String artifactId,
+                                        @RequestParam("version") String version) {
+        versionService.changeVersionStatus(groupId, artifactId, version, (short) 0);
+        return ResponseEntity.ok().build();
+    }
+
+}

+ 51 - 0
src/main/java/com/uas/ops/diff/api/v1/VersionServiceV1.java

@@ -0,0 +1,51 @@
+package com.uas.ops.diff.api.v1;
+
+import com.uas.ops.diff.entity.ProjectVersion;
+import com.uas.ops.diff.entity.ProjectVersionId;
+import com.uas.ops.diff.repository.ProjectVersionRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import java.util.List;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Service
+public class VersionServiceV1 {
+
+    @Autowired
+    private ProjectVersionRepository projectVersionRepository;
+
+    public String getLastVersion(String groupId, String artifactId) {
+        return projectVersionRepository.findMaxEnabledVersion( groupId, artifactId );
+    }
+
+    public Page<ProjectVersion> findAll(final String groupId, final String artifactId, Pageable pageable) {
+        return projectVersionRepository.findAll(new Specification<ProjectVersion>() {
+            @Override
+            public Predicate toPredicate(Root<ProjectVersion> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
+                criteriaQuery.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("groupId"), groupId),
+                        criteriaBuilder.equal(root.get("artifactId"), artifactId)));
+                return null;
+            }
+        }, pageable);
+    }
+
+    public void changeVersionStatus(String groupId, String artifactId, String version, short status) {
+        ProjectVersionId projectVersionId = new ProjectVersionId(groupId, artifactId, version);
+        ProjectVersion projectVersion = projectVersionRepository.findOne(projectVersionId);
+        if (null != projectVersion) {
+            projectVersion.setEnabled((short) (0 == status ? 0 : 1));
+        }
+        projectVersionRepository.save(projectVersion);
+    }
+
+}

+ 18 - 0
src/main/java/com/uas/ops/diff/config/AppConfig.java

@@ -0,0 +1,18 @@
+package com.uas.ops.diff.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Created by Pro1 on 2017/6/16.
+ */
+@Configuration
+public class AppConfig {
+
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+
+}

+ 53 - 0
src/main/java/com/uas/ops/diff/domain/Manifest.java

@@ -0,0 +1,53 @@
+package com.uas.ops.diff.domain;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class Manifest implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private String path;
+    private String version;
+    private long lastChanged;
+    private List<Manifest> children;
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    /**
+     * 上次更新时间
+     */
+    public long getLastChanged() {
+        return lastChanged;
+    }
+
+    public void setLastChanged(long lastChanged) {
+        this.lastChanged = lastChanged;
+    }
+
+    public List<Manifest> getChildren() {
+        return children;
+    }
+
+    public void setChildren(List<Manifest> children) {
+        this.children = children;
+    }
+
+}

+ 94 - 0
src/main/java/com/uas/ops/diff/entity/ProjectDiff.java

@@ -0,0 +1,94 @@
+package com.uas.ops.diff.entity;
+
+import javax.persistence.*;
+import java.util.Date;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Entity
+@IdClass(ProjectDiffId.class)
+@Table(name = "project_diff")
+public class ProjectDiff {
+
+    @Id
+    @Column(name = "group_id")
+    private String groupId;
+
+    @Id
+    @Column(name = "artifact_id")
+    private String artifactId;
+
+    @Id
+    @Column(name = "from_version")
+    private String fromVersion;
+
+    @Id
+    @Column(name = "to_version")
+    private String toVersion;
+
+    @Column(name="create_time")
+    private Date createTime;
+
+    @Column(name = "diff_path")
+    private String diffPath;
+
+    public ProjectDiff() {
+
+    }
+
+    public ProjectDiff(String groupId, String artifactId, String fromVersion, String toVersion) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.fromVersion = fromVersion;
+        this.toVersion = toVersion;
+    }
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public String getFromVersion() {
+        return fromVersion;
+    }
+
+    public void setFromVersion(String fromVersion) {
+        this.fromVersion = fromVersion;
+    }
+
+    public String getToVersion() {
+        return toVersion;
+    }
+
+    public void setToVersion(String toVersion) {
+        this.toVersion = toVersion;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public String getDiffPath() {
+        return diffPath;
+    }
+
+    public void setDiffPath(String diffPath) {
+        this.diffPath = diffPath;
+    }
+}

+ 48 - 0
src/main/java/com/uas/ops/diff/entity/ProjectDiffId.java

@@ -0,0 +1,48 @@
+package com.uas.ops.diff.entity;
+
+import java.io.Serializable;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class ProjectDiffId implements Serializable{
+
+    private String groupId;
+    private String artifactId;
+    private String fromVersion;
+    private String toVersion;
+
+    public ProjectDiffId() {
+
+    }
+
+    public ProjectDiffId(String groupId, String artifactId, String fromVersion, String toVersion) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.fromVersion = fromVersion;
+        this.toVersion = toVersion;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ProjectDiffId that = (ProjectDiffId) o;
+
+        if (groupId != null ? !groupId.equals(that.groupId) : that.groupId != null) return false;
+        if (artifactId != null ? !artifactId.equals(that.artifactId) : that.artifactId != null) return false;
+        if (fromVersion != null ? !fromVersion.equals(that.fromVersion) : that.fromVersion != null) return false;
+        return toVersion != null ? toVersion.equals(that.toVersion) : that.toVersion == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+        int result = groupId != null ? groupId.hashCode() : 0;
+        result = 31 * result + (artifactId != null ? artifactId.hashCode() : 0);
+        result = 31 * result + (fromVersion != null ? fromVersion.hashCode() : 0);
+        result = 31 * result + (toVersion != null ? toVersion.hashCode() : 0);
+        return result;
+    }
+}

+ 102 - 0
src/main/java/com/uas/ops/diff/entity/ProjectVersion.java

@@ -0,0 +1,102 @@
+package com.uas.ops.diff.entity;
+
+import javax.persistence.*;
+import java.util.Date;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Entity
+@IdClass(ProjectVersionId.class)
+@Table(name = "project_version")
+public class ProjectVersion {
+
+    @Id
+    @Column(name = "group_id")
+    private String groupId;
+
+    @Id
+    @Column(name = "artifact_id")
+    private String artifactId;
+
+    @Id
+    private String version;
+
+    @Column(name="version_time")
+    private Date versionTime;
+
+    @Column(name="create_time")
+    private Date createTime;
+
+    @Column
+    private short enabled;
+
+    @Column(name = "manifest_path")
+    private String manifestPath;
+
+    public ProjectVersion() {
+
+    }
+
+    public ProjectVersion(String groupId, String artifactId, String version) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.version = version;
+    }
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public Date getVersionTime() {
+        return versionTime;
+    }
+
+    public void setVersionTime(Date versionTime) {
+        this.versionTime = versionTime;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public short getEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(short enabled) {
+        this.enabled = enabled;
+    }
+
+    public String getManifestPath() {
+        return manifestPath;
+    }
+
+    public void setManifestPath(String manifestPath) {
+        this.manifestPath = manifestPath;
+    }
+}

+ 43 - 0
src/main/java/com/uas/ops/diff/entity/ProjectVersionId.java

@@ -0,0 +1,43 @@
+package com.uas.ops.diff.entity;
+
+import java.io.Serializable;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class ProjectVersionId implements Serializable {
+
+    private String groupId;
+    private String artifactId;
+    private String version;
+
+    public ProjectVersionId() {
+    }
+
+    public ProjectVersionId(String groupId, String artifactId, String version) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.version = version;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ProjectVersionId that = (ProjectVersionId) o;
+
+        if (groupId != null ? !groupId.equals(that.groupId) : that.groupId != null) return false;
+        if (artifactId != null ? !artifactId.equals(that.artifactId) : that.artifactId != null) return false;
+        return version != null ? version.equals(that.version) : that.version == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+        int result = groupId != null ? groupId.hashCode() : 0;
+        result = 31 * result + (artifactId != null ? artifactId.hashCode() : 0);
+        result = 31 * result + (version != null ? version.hashCode() : 0);
+        return result;
+    }
+}

+ 22 - 0
src/main/java/com/uas/ops/diff/repository/ProjectDiffRepository.java

@@ -0,0 +1,22 @@
+package com.uas.ops.diff.repository;
+
+import com.uas.ops.diff.entity.ProjectDiff;
+import com.uas.ops.diff.entity.ProjectDiffId;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Repository
+public interface ProjectDiffRepository extends JpaRepository<ProjectDiff, ProjectDiffId>{
+
+    @Query("select p from ProjectDiff p where p.groupId = :groupId and p.artifactId = :artifactId and p.fromVersion>=:fromVersion and p.toVersion<=:toVersion order by p.toVersion")
+    List<ProjectDiff> findByFromVersionAndToVersion(@Param("groupId") String groupId, @Param("artifactId") String artifactId,
+                                                    @Param("fromVersion") String fromVersion, @Param("toVersion") String toVersion);
+
+}

+ 24 - 0
src/main/java/com/uas/ops/diff/repository/ProjectVersionRepository.java

@@ -0,0 +1,24 @@
+package com.uas.ops.diff.repository;
+
+import com.uas.ops.diff.entity.ProjectVersion;
+import com.uas.ops.diff.entity.ProjectVersionId;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+@Repository
+public interface ProjectVersionRepository extends JpaRepository<ProjectVersion, ProjectVersionId>,JpaSpecificationExecutor<ProjectVersion> {
+
+    @Query("select max(p.version) from ProjectVersion p where p.groupId = :groupId and p.artifactId = :artifactId and p.enabled=1")
+    String findMaxEnabledVersion(@Param("groupId") String groupId, @Param("artifactId") String artifactId);
+
+    List<ProjectVersion> findByGroupIdAndArtifactIdOrderByVersionDesc(String groupId, String artifactId);
+
+}

+ 45 - 0
src/main/java/com/uas/ops/diff/service/DfsService.java

@@ -0,0 +1,45 @@
+package com.uas.ops.diff.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.http.*;
+import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Map;
+
+/**
+ * Created by Pro1 on 2017/6/16.
+ */
+@Service
+public class DfsService implements FileService{
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Override
+    public String save(byte[] data, final String fileName) throws IOException {
+        LinkedMultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
+        parts.add("file", new ByteArrayResource(data) {
+            @Override
+            public String getFilename() {
+                return fileName;
+            }
+        });
+        Map<String, String> resData = restTemplate.postForObject("http://10.10.100.200:9999/file/upload", parts, Map.class);
+        return resData.get("path");
+    }
+
+    @Override
+    public byte[] get(String path) throws IOException {
+        return restTemplate.getForObject("http://10.10.100.200:9999/file/download?path={path}", byte[].class, path);
+    }
+
+    @Override
+    public void delete(String path) {
+        restTemplate.delete("http://10.10.100.200:9999/file/delete?path={path}", path);
+    }
+}

+ 34 - 0
src/main/java/com/uas/ops/diff/service/FileService.java

@@ -0,0 +1,34 @@
+package com.uas.ops.diff.service;
+
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public interface FileService {
+
+    /**
+     * 保存文件
+     * @param data
+     * @param  fileName
+     * @return
+     */
+    String save(byte[] data, String fileName) throws IOException;
+
+    /**
+     * 获取文件
+     * @param path 文件路径、文件下载路径
+     * @return
+     */
+    byte[] get(String path) throws IOException;
+
+    /**
+     * 删除文件
+     * @param path
+     */
+    void delete(String path);
+
+}

+ 45 - 0
src/main/java/com/uas/ops/diff/service/LocalFileService.java

@@ -0,0 +1,45 @@
+package com.uas.ops.diff.service;
+
+import com.uas.ops.diff.util.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.util.FileCopyUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.UUID;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+//@Service
+@Deprecated
+public class LocalFileService implements FileService{
+
+    @Value("${diff.home}")
+    private String appHome;
+
+    @Override
+    public String save(byte[] data, String fileName) throws IOException{
+        String name = StringUtils.uuid();
+        FileCopyUtils.copy(data, new File(appHome, name));
+        return name;
+    }
+
+    @Override
+    public byte[] get(String path) throws IOException {
+        File file = new File(appHome, path);
+        if (file.exists()) {
+            return FileCopyUtils.copyToByteArray(file);
+        }
+        return null;
+    }
+
+    @Override
+    public void delete(String path) {
+        File file = new File(appHome, path);
+        if (file.exists()) {
+           file.delete();
+        }
+    }
+}

+ 26 - 0
src/main/java/com/uas/ops/diff/util/ObjectUtils.java

@@ -0,0 +1,26 @@
+package com.uas.ops.diff.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class ObjectUtils {
+
+    /**
+     * 读对象
+     *
+     * @param data
+     * @return
+     * @throws Exception
+     */
+    public static <T> T read(byte[] data) throws IOException, ClassNotFoundException {
+        ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(data));
+        T obj = (T) is.readObject();
+        is.close();
+        return obj;
+    }
+
+}

+ 13 - 0
src/main/java/com/uas/ops/diff/util/StringUtils.java

@@ -0,0 +1,13 @@
+package com.uas.ops.diff.util;
+
+import java.util.UUID;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class StringUtils {
+
+    public static String uuid() {
+        return UUID.randomUUID().toString();
+    }
+}

+ 68 - 0
src/main/java/com/uas/ops/diff/util/ZipUtils.java

@@ -0,0 +1,68 @@
+package com.uas.ops.diff.util;
+
+import org.apache.commons.io.IOUtils;
+import org.springframework.util.StreamUtils;
+
+import java.io.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * Created by Pro1 on 2017/6/13.
+ */
+public class ZipUtils {
+
+    public static void unzip(byte[] zipData, String targetPath) throws IOException{
+        ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zipData));
+        ZipEntry entry;
+        File file;
+        BufferedOutputStream out = null;
+        while((entry = in.getNextEntry())!=null && !entry.isDirectory()){
+            file = new File(targetPath, entry.getName());
+            if (!file.getParentFile().exists()) {
+               file.getParentFile().mkdirs();
+            }
+            out = new BufferedOutputStream(new FileOutputStream(file));
+            StreamUtils.copy(in, out);
+        }
+        in.closeEntry();
+        IOUtils.closeQuietly(in);
+        IOUtils.closeQuietly(out);
+    }
+
+    public static void zip(String sourcePath, File zipFile) throws IOException{
+        File source = new File(sourcePath);
+        if (source.exists()) {
+            FileOutputStream fileOut = new FileOutputStream(zipFile);
+            ZipOutputStream zipOut = new ZipOutputStream(fileOut);
+            try {
+                addEntry(null, source, zipOut);
+                // 如果希望保留sourcePath文件夹,使用addEntry("", source, zipOut);
+            } finally {
+                IOUtils.closeQuietly(zipOut);
+                IOUtils.closeQuietly(fileOut);
+            }
+        }
+    }
+
+    private static void addEntry(String entryPath, File source, ZipOutputStream zipOut) throws IOException {
+        String entry = null == entryPath ? "" : (entryPath + source.getName());
+        if (source.isDirectory()) {
+            entry = entry.isEmpty() ? entry : (entry + File.separator);
+            for (File file : source.listFiles()) {
+                addEntry(entry, file, zipOut);
+            }
+        } else {
+            FileInputStream fileIn = new FileInputStream(source);
+            try {
+                zipOut.putNextEntry(new ZipEntry(entry));
+                StreamUtils.copy(fileIn, zipOut);
+                zipOut.closeEntry();
+            } finally {
+                IOUtils.closeQuietly(fileIn);
+            }
+        }
+    }
+
+}

+ 8 - 0
src/main/resources/application.yml

@@ -0,0 +1,8 @@
+server:
+  port: 23000
+spring:
+  http:
+    multipart:
+      enabled: true
+      max-file-size: 500Mb
+      max-request-size: 500Mb

+ 29 - 0
src/main/resources/bootstrap.yml

@@ -0,0 +1,29 @@
+spring:
+  application:
+    name: ops-diff-service
+  profiles:
+    active: dev
+---
+spring:
+  profiles: dev
+  cloud:
+    config:
+      profile: dev
+      label: master
+      uri: http://10.10.100.24:28001/
+---
+spring:
+  profiles: test
+  cloud:
+    config:
+      profile: test
+      label: master
+      uri: http://configserver:28001/
+---
+spring:
+  profiles: prod
+  cloud:
+    config:
+      profile: prod
+      label: master
+      uri: http://configserver:28001/

+ 42 - 0
src/test/java/com/uas/ops/diff/test/FileServiceTest.java

@@ -0,0 +1,42 @@
+package com.uas.ops.diff.test;
+
+import com.uas.ops.diff.DiffApplication;
+import com.uas.ops.diff.service.FileService;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.util.FileCopyUtils;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Created by Pro1 on 2017/6/16.
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = DiffApplication.class)
+public class FileServiceTest {
+
+    @Autowired
+    private FileService fileService;
+
+    private String filePath;
+
+    @Before
+    public void save() throws IOException{
+        File file = File.createTempFile("test", "txt");
+        filePath = fileService.save(FileCopyUtils.copyToByteArray(file), file.getName());
+        file.delete();
+    }
+
+    @Test
+    public void getAndDelete() throws IOException {
+        fileService.get(filePath);
+        fileService.delete(filePath);
+    }
+
+}