Skip to content

Commit

Permalink
service configuration using environment variables #229
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 11, 2020
1 parent 58b5b5a commit b905a46
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.cricketmsf</groupId>
<artifactId>cricket</artifactId>
<version>1.5.45</version>
<version>1.5.46</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -117,7 +117,8 @@
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>org.cricketmsf.Runner</Main-Class>
<Implementation-Version>${project.version}</Implementation-Version>
<Cricket-Version>${project.version}</Cricket-Version>
<Service-Version>${project.version}</Service-Version>
</manifestEntries>
</transformer>
</transformers>
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/cricketmsf/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ public static Kernel getInstanceWithProperties(Class c, Configuration config, Co
((Kernel) instance).setId(config.getId());
((Kernel) instance).setDescription(config.getDescription());
((Kernel) instance).setProperties(cfg.getProperties());
((Kernel) instance).setName((String) cfg.getProperties().getOrDefault("SRVC_NAME_ENV_VARIABLE", "CRICKET_NAME"));
((Kernel) instance).setSsl((String) cfg.getProperties().getOrDefault("SSL_ENV_VARIABLE", "CRICKET_SSL"));
((Kernel) instance).setName((String) cfg.getProperties().get("servicename"));
((Kernel) instance).setSsl((String) cfg.getProperties().getOrDefault("ssl", "false"));
((Kernel) instance).configureTimeFormat(cfg);
((Kernel) instance).loadAdapters(cfg);
} catch (Exception e) {
Expand Down Expand Up @@ -768,9 +768,10 @@ public void run() {
} else {
getLogger().print("# Service: " + getId());
}
getLogger().print("# UUID: " + getUuid());
getLogger().print("# NAME: " + getName());
getLogger().print("# JAVA: " + System.getProperty("java.version"));
getLogger().print("# UUID : " + getUuid());
getLogger().print("# VERSION: " + Kernel.getInstance().configSet.getServiceVersion());
getLogger().print("# NAME : " + getName());
getLogger().print("# JAVA : " + System.getProperty("java.version"));
getLogger().print("#");
if (!"jetty".equalsIgnoreCase((String) getProperties().getOrDefault("httpd", ""))) {
if (getHttpd().isSsl()) {
Expand Down Expand Up @@ -975,6 +976,10 @@ public void setInboundAdaptersLoaded(boolean inboundAdaptersLoaded) {
public String getName() {
return name;
}

public String getServiceVersion(){
return getConfigSet().getServiceVersion();
}

/**
* @param variableName system environment variable name which holds the name
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/cricketmsf/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public ConfigSet readDefaultConfig() {
String inputStreamString = new Scanner(propertyFile, "UTF-8").useDelimiter("\\A").next();
configSet = (ConfigSet) JsonReader.jsonToJava(inputStreamString, args);
configSet.setKernelVersion(getVersion());
configSet.setServiceVersion(getServiceVersion());
return configSet;
}

Expand All @@ -213,7 +214,27 @@ private String getVersion() {
Manifest manifest = new Manifest(url.openStream());
Attributes attributes = manifest.getMainAttributes();
if ("org.cricketmsf.Runner".equals(attributes.getValue("Main-Class"))) {
return attributes.getValue("Implementation-Version");
return attributes.getValue("Cricket-Version");
}
} catch (IOException ex) {
return "";
}
}
} catch (IOException e) {
return "";
}
return "";
}
private String getServiceVersion() {
try {
Enumeration<URL> resources = Runner.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
try {
Manifest manifest = new Manifest(url.openStream());
Attributes attributes = manifest.getMainAttributes();
if ("org.cricketmsf.Runner".equals(attributes.getValue("Main-Class"))) {
return attributes.getValue("Service-Version");
}
} catch (IOException ex) {
return "";
Expand Down Expand Up @@ -256,6 +277,7 @@ public ConfigSet readConfig(ArgumentParser arguments) {
configSet = (ConfigSet) JsonReader.jsonToJava(inputStreamString, args);
// read Kernel version
configSet.setKernelVersion(getVersion());
configSet.setServiceVersion(getServiceVersion());
// force property changes based on command line --force param
if (null != arguments && arguments.containsKey("force")) {
ArrayList<String> forcedProps = (ArrayList) arguments.get("force");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/cricketmsf/config/ConfigSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ConfigSet {
String description = "This is sample configuration";
ArrayList<Configuration> services;
private String kernelVersion;
private String serviceVersion;

public ConfigSet() {
services = new ArrayList<>();
Expand Down Expand Up @@ -87,13 +88,21 @@ public Configuration getConfigurationById(String id) {
public String getKernelVersion() {
return kernelVersion;
}

public String getServiceVersion() {
return serviceVersion;
}

/**
* @param kernelVersion the kernelVersion to set
*/
public void setKernelVersion(String kernelVersion) {
this.kernelVersion = kernelVersion;
}

public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void forceProperty(String definition) {
// service^property=value^adapter^property=value
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/org/cricketmsf/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

/**
*
Expand Down Expand Up @@ -115,25 +116,11 @@ public void putAdapterConfiguration(AdapterConfiguration config) {
}

public String getProperty(String name) {
String value = (String) properties.get(name);
if (null != value && value.startsWith("$")) {
String tmp = System.getenv(value.substring(1));
if (null != tmp) {
value = tmp;
}
}
return value;
return (String) properties.get(name);
}

public String getProperty(String name, String defaultValue) {
String value = (String) properties.getOrDefault(name, defaultValue);
if (null != value && value.startsWith("$")) {
String tmp = System.getenv(value.substring(1));
if (null != tmp) {
value = tmp;
}
}
return value;
return (String) properties.getOrDefault(name, defaultValue);
}

public void putProperty(String name, Object value) {
Expand Down Expand Up @@ -232,6 +219,18 @@ public void setId(String id) {
* @return the properties
*/
public HashMap<String, Object> getProperties() {
Iterator it=properties.keySet().iterator();
String key,value;
while(it.hasNext()){
key=(String)it.next();
value=""+properties.get(key);
if (value.startsWith("$")) {
String tmp = System.getenv(value.substring(1));
if (null != tmp) {
properties.put(key, tmp);
}
}
}
return properties;
}

Expand Down

0 comments on commit b905a46

Please sign in to comment.