View Javadoc
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       * Create the place from the specified config file or resource
24       *
25       * @param configInfo the config file or resource to use
26       * @param dir the name of the controlling directory to register with
27       * @param placeLoc string name of this place
28       */
29      public VersionPlace(String configInfo, String dir, String placeLoc) throws IOException {
30          super(configInfo, dir, placeLoc);
31          configurePlace();
32      }
33  
34      /**
35       * Create the place from the specified config stream data
36       *
37       * @param configInfo the config file or resource to use
38       * @param dir the name of the controlling directory to register with
39       * @param placeLoc string name of this place
40       */
41      public VersionPlace(InputStream configInfo, String dir, String placeLoc) throws IOException {
42          super(configInfo, dir, placeLoc);
43          configurePlace();
44      }
45  
46      /**
47       * Create the place from the specified config stream data
48       *
49       * @param configInfo the config file or resource to use
50       */
51      public VersionPlace(InputStream configInfo) throws IOException {
52          super(configInfo);
53          configurePlace();
54      }
55  
56      /**
57       * Create with the default configuration
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          // if a release version, return just the version, even if includeDate is true
91          if (matcher.matches()) {
92              return version;
93          }
94  
95          if (includeDate) {
96              // version with date & time information
97              // changes format of date from 2024-09-23T10:41:18-0400, to 20240923104118
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             // adds just version
104             return version;
105         }
106     }
107 
108     protected String getVersionHash(GitRepositoryState gitRepositoryState) {
109         if (useAbbrevHash) {
110             // first 7 chars of commit hash
111             return gitRepositoryState.getCommitIdAbbrev();
112         } else {
113             // full commit hash (default option)
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 }