1 package emissary.place;
2
3 import emissary.core.IBaseDataObject;
4 import emissary.core.ResourceException;
5 import emissary.util.GitRepositoryState;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 public class VersionPlace extends ServiceProviderPlace {
13
14 private static final String EMISSARY_VERSION = "EMISSARY_VERSION";
15 private static final String EMISSARY_VERSION_HASH = "EMISSARY_VERSION_HASH";
16 private boolean includeDate;
17 private boolean useAbbrevHash;
18 private String formattedVersion;
19 private String versionHash;
20 protected Pattern regexPatternForVersion;
21
22
23
24
25
26
27
28
29 public VersionPlace(String configInfo, String dir, String placeLoc) throws IOException {
30 super(configInfo, dir, placeLoc);
31 configurePlace();
32 }
33
34
35
36
37
38
39
40
41 public VersionPlace(InputStream configInfo, String dir, String placeLoc) throws IOException {
42 super(configInfo, dir, placeLoc);
43 configurePlace();
44 }
45
46
47
48
49
50
51 public VersionPlace(InputStream configInfo) throws IOException {
52 super(configInfo);
53 configurePlace();
54 }
55
56
57
58
59 public VersionPlace() throws IOException {
60 super();
61 configurePlace();
62 }
63
64 private void configurePlace() {
65 GitRepositoryState gitRepositoryState = initGitRepositoryState();
66
67 includeDate = configG.findBooleanEntry("INCLUDE_DATE", true);
68 useAbbrevHash = configG.findBooleanEntry("USE_ABBREV_HASH", true);
69
70 String cfgRegex = configG.findStringEntry("VERSION_REGEX_FOR_NO_DATE", "^(\\d+\\.)?(\\d+\\.)?(\\d+)$");
71 regexPatternForVersion = Pattern.compile(cfgRegex);
72
73 formattedVersion = getVersion(gitRepositoryState);
74 versionHash = getVersionHash(gitRepositoryState);
75 }
76
77 @Override
78 public void process(IBaseDataObject payload) throws ResourceException {
79 payload.putParameter(EMISSARY_VERSION, formattedVersion);
80 payload.putParameter(EMISSARY_VERSION_HASH, versionHash);
81 }
82
83 GitRepositoryState initGitRepositoryState() {
84 return GitRepositoryState.getRepositoryState();
85 }
86
87 protected String getVersion(GitRepositoryState gitRepositoryState) {
88 String version = gitRepositoryState.getBuildVersion();
89 Matcher matcher = regexPatternForVersion.matcher(version);
90
91 if (matcher.matches()) {
92 return version;
93 }
94
95 if (includeDate) {
96
97
98 String buildTime = gitRepositoryState.getBuildTime();
99 int cutEndMark = buildTime.lastIndexOf(":") + 3;
100 String formattedDate = buildTime.substring(0, cutEndMark).replaceAll("\\D", "");
101 return version + "-" + formattedDate;
102 } else {
103
104 return version;
105 }
106 }
107
108 protected String getVersionHash(GitRepositoryState gitRepositoryState) {
109 if (useAbbrevHash) {
110
111 return gitRepositoryState.getCommitIdAbbrev();
112 } else {
113
114 return gitRepositoryState.getCommitId();
115 }
116 }
117
118 public void setIncludeDate(Boolean includeDate) {
119 this.includeDate = includeDate;
120 }
121
122 public void setAbbrevHash(Boolean useAbbrevHash) {
123 this.useAbbrevHash = useAbbrevHash;
124 }
125
126 public boolean isIncludeDate() {
127 return includeDate;
128 }
129
130 public boolean isUseAbbrevHash() {
131 return useAbbrevHash;
132 }
133 }