summaryrefslogtreecommitdiffstats
path: root/config-model-api/src/main/java/com/yahoo/config/application/api/ApplicationMetaData.java
blob: a8e5dd264d52281e370dd8d22e98230110fa89dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application.api;

import com.yahoo.slime.*;

import com.yahoo.text.Utf8;

import java.io.*;

/**
 * Metadata about an application package.
 *
 * @author hmusum
 * @since 5.0
 */
public class ApplicationMetaData {
    private final String deployedByUser;
    private final String deployedFromDir;
    private final long deployTimestamp;
    private final long generation;
    private final long previousActiveGeneration;
    private final String checkSum;
    private final String appName;

    public ApplicationMetaData(File appDir, String deployedByUser, String deployedFromDir, Long deployTimestamp,
                               String checkSum, Long generation, long previousActiveGeneration) {
        this(deployedByUser, deployedFromDir, deployTimestamp, appDir.getName(), checkSum, generation, previousActiveGeneration);
    }

    public ApplicationMetaData(String deployedByUser, String deployedFromDir, Long deployTimestamp, String applicationName, String checkSum, Long generation, long previousActiveGeneration) {
        this.appName = applicationName;
        this.deployedByUser = deployedByUser;
        this.deployedFromDir = deployedFromDir;
        this.deployTimestamp = deployTimestamp;
        this.checkSum = checkSum;
        this.generation = generation;
        this.previousActiveGeneration = previousActiveGeneration;
    }

    /**
     * Gets the name of the application (name of the directory from which application was deployed.
     * Will return null if a problem occurred while getting metadata
     *
     * @return application name
     */
    public String getApplicationName() {
        return appName;
    }

    /**
     * Gets the user who deployed the application.
     * Will return null if a problem occurred while getting metadata
     *
     * @return user name for the user who ran "deploy-application"
     */
    public String getDeployedByUser() {
        return deployedByUser;
    }

    /**
     * Gets the directory where the application was deployed from.
     * Will return null if a problem occurred while getting metadata
     *
     * @return path to raw deploy directory (for the original application)
     */
    public String getDeployPath() {
        return deployedFromDir;
    }

    /**
     * Gets the time the application was deployed
     * Will return null if a problem occurred while getting metadata
     *
     * @return timestamp for when "deploy-application" was run. In ms.
     */
    public Long getDeployTimestamp() {
            return deployTimestamp;
    }

    /**
     * Gets the time the application was deployed
     * Will return null if a problem occurred while getting metadata
     *
     * @return timestamp for when "deploy-application" was run. In ms.
     */
    public Long getGeneration() {
            return generation;
    }

    /**
     * Returns an md5 hash of the contents of the application package
     * @return an md5sum of the application package
     */
    public String getCheckSum() {
        return checkSum;
    }

    /**
     * Returns the previously active generation at the point when this application was created.
     * @return a generation.
     */
    public long getPreviousActiveGeneration() {
        return previousActiveGeneration;
    }

    @Override
    public String toString() {
        return deployedByUser + ", " + deployedFromDir + ", " + deployTimestamp + ", " + generation + ", " + checkSum + ", " + previousActiveGeneration;
    }

    public static ApplicationMetaData fromJsonString(String jsonString) {
        try {
            Slime data = new Slime();
            new JsonDecoder().decode(data, Utf8.toBytes(jsonString));
            Inspector root = data.get();
            Inspector deploy = root.field("deploy");
            Inspector app = root.field("application");
            return new ApplicationMetaData(deploy.field("user").asString(), deploy.field("from").asString(), deploy.field("timestamp").asLong(), app.field("name").asString(), app.field("checksum").asString(), app.field("generation").asLong(), app.field("previousActiveGeneration").asLong());
        } catch (Exception e) {
            throw new IllegalArgumentException("Error parsing json metadata", e);
        }
    }

    public Slime getSlime() {
        Slime slime = new Slime();
        Cursor meta = slime.setObject();
        Cursor deploy = meta.setObject("deploy");
        deploy.setString("user", deployedByUser);
        deploy.setString("from", deployedFromDir);
        deploy.setLong("timestamp", deployTimestamp);
        Cursor app = meta.setObject("application");
        app.setString("name", appName);
        app.setString("checksum", checkSum);
        app.setLong("generation", generation);
        app.setLong("previousActiveGeneration", previousActiveGeneration);
        return slime;
    }

    public String asJsonString() {
        Slime slime = getSlime();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new JsonFormat(false).encode(baos, slime);
            return baos.toString("UTF-8");
        } catch (IOException e) {
            throw new RuntimeException("Unable to encode metadata", e);
        }
    }
}