Is there an equivalent class for groovy's ConfigSlurper in java? - java

Below is a small example that I generally use in groovy, but I want something similar in java.
Configuration file
datastore{
oracle{
host="localhost"
port=1521
dbname="orcl"
}
db2{
host="localhost"
port=12807
dbname="sppd"
}
}
Groovy
public class Configuration {
public static final def cfg = new ConfigSlurper().parse(new File("configuration").toURL())
static main(args) {
println cfg.datastore.oracle.host
println cfg.datastore.db2.host
}
}

I'm guessing that you're wanting to access a property by its full path, e.g. datastore.oracle.host from Java like you can in Groovy. If so, do this:
ConfigObject config = new ConfigSlurper().parse(new File("configuration").toURL());
Map flattenedConfig = config.flatten();
String oracleHost = (String) flattenedConfig.get("datastore.oracle.host");
Better than Java properties because the type is maintained. From a Groovy User list post.

That is too much dynamic groovyness, but can be expressed in XML, properties, etc. You can try Common Configuration, which would express the same data:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
</gui-definition>
And read with:
XMLConfiguration config = new XMLConfiguration("tables.xml");
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
String linkNormal = config.getString("colors.link[#normal]");
You can also give a try to PropertyConfiguration:
# Properties definining the GUI
colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
Loaded with:
Configuration config = new PropertiesConfiguration("usergui.properties");
String backColor = config.getString("colors.background");

Related

How to read data from META/MANIFEST.MF in Spring Boot MVC Web Application?

I have a requirement to read information that is available in the META/MANIFEST.MF file of Spring Boot MVC web application and use this info to perform some business logic. I'm using gradle to build the application as war file and deploying it into the external tomcat.
I have tried the following:
#Configuration
public class AppConfig
{
#Bean("manifest")
public java.util.jar.Manifest getManifest() throws IOException
{
InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
Manifest manifestObj = new Manifest(inputFile);
return manifestObj;
}
}
AppService.java
#Service
public class AppService
{
#Autowired
#Qualifier("manifest")
private Manifest manifest;
#PostConstruct
public String init()
{
Attributes mainAttributes = manifest.getMainAttributes();
String buildNum = mainAttributes.getValue("Build-Number");
String customPropInfo= mainAttributes.getValue("customPropInfo");
String systemPrp1= buildNum + "_" + "SomeBusinessInfoLogic1";
String systemPrp2= customPropInfo+ "_" + "SomeBusinessInfoLogic2";
//Some Business Logic with these attributes systemPrp, systemPrp2
logger.info("System Props are updated");
}
}
I'm getting null for both buildNum and customPropInfo.
Note: I have tried creating the Manifest bean something like this which was created by me. As per the #M.Deinum suggestion I'm creating this new question here. I also tried the solutions here which didn't work for me.
#M.Deinum suggested to make use of Spring Boot's Actuator Info endpoint. But this endpoint is useful when we want to access the info outside of the application but my requirement is different as I need the data that is available in MANIFEST.MF file to perform some business operations within the application.
I get the following error when I tried this solution "/META-INF/MANIFEST.MF".
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'manifest' defined in class path resource [com/abc/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.jar.Manifest]: Factory method 'getManifest' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read(byte[], int, int)" because "this.in" is null
Can someone please help me to read information from META/MANIFEST.MF of the Spring Boot MVC Web Application?.
UPDATE1: I get the following MainAttributes when I try to print MainAttributes. But the problem is when I try to deploy the war into external tomcat.
System.out.println("Manifest MainAttributes = " +manifestObj.getMainAttributes().keySet());
Output:
Manifest MainAttributes = [Manifest-Version, Implementation-Title, Automatic-Module-Name, Implementation-Version, Built-By, Spring-Boot-Jar-Type, Build-Jdk-Spec]
UPDATE2:
I have updated to AppService.java to print the info available in autowired Manifest object. Something like below:
#Configuration
public class AppConfig
{
#Bean("manifest")
public java.util.jar.Manifest getManifest() throws IOException
{
InputStream inputFile = new ClassPathResource("/META-INF/MANIFEST.MF").getInputStream();
Manifest manifestObj = new Manifest(inputFile);
System.out.println("Manifest Manifest-Version = " +manifestObj.getMainAttributes().getValue("Manifest-Version"));
System.out.println("Manifest KeySet = " +manifestObj.getMainAttributes().keySet());
return manifestObj;
}
}
#Service
public class AppService
{
#Autowired
#Qualifier("manifest")
private Manifest manifest;
#PostConstruct
public String init()
{
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.forEach((k,v) -> {
System.out.println("AppService.init(): Key = "+k+", Value = "+v);
});
String buildNum = mainAttributes.getValue("Build-Number");
String customPropInfo= mainAttributes.getValue("customPropInfo");
String systemPrp1= buildNum + "_" + "SomeBusinessInfoLogic1";
String systemPrp2= customPropInfo+ "_" + "SomeBusinessInfoLogic2";
//Some Business Logic with these attributes systemPrp, systemPrp2
logger.info("System Props are updated");
}
}
I see the following output on the console:
AppService.init(): Key = Implementation-Title, Value = Apache Tomcat Bootstrap
AppService.init(): Key = Implementation-Version, Value = 9.0.12
AppService.init(): Key = Specification-Vendor, Value = Apache Software Foundation
AppService.init(): Key = Specification-Title, Value = Apache Tomcat Bootstrap
AppService.init(): Key = Class-Path, Value = commons-daemon.jar
AppService.init(): Key = Manifest-Version, Value = 1.0
AppService.init(): Key = Main-Class, Value = org.apache.catalina.startup.Bootstrap
AppService.init(): Key = Implementation-Vendor, Value = Apache Software Foundation
AppService.init(): Key = Ant-Version, Value = Apache Ant 1.9.9
AppService.init(): Key = X-Compile-Target-JDK, Value = 1.8
AppService.init(): Key = X-Compile-Source-JDK, Value = 1.8
AppService.init(): Key = Created-By, Value = some xyz
AppService.init(): Key = Specification-Version, Value = 9.0
So just by the above output, I think MANIFEST.MF is not application specific but is from commons-daemon.jar.
OK - the problem isn't that you "can't read data from META/MANIFEST.MF in Spring Boot MVC Web Application". Rather, the problem is that your code happens to be reading the WRONG MANIFEST.MF from some other, random .jar in the classpath.
One solution might be to use JarClassLoader.
Another solution, as M. Deinum suggested, might be to store the properties you wish to retrieve in application.properties (or some other "global" properties file) instead of MANIFEST.MF.
ALSO:
I assume you're probably using an IDE to develop your app (Eclipse, Netbeans, etc). If you haven't already, I would STRONGLY encourage you to familiarize yourself with your IDE's debugger: the ability to set breakpoints, display variables, single-step through method calls, etc.

ph-schematron validation error message

I'm using ph-schematron to validate my XML files. I'm able to validate the files correctly but I couldn't find how to generate reports about failing assertions.
This is my context(point-of-interest):
<bpmn:extensionElements>
<activiti:in sourceExpression="RESERVATION" target="OPERATION_CODE"/>
<activiti:in sourceExpression="true" target="IS_SYNC"/>
</bpmn:extensionElements>
This is my Schematron schema:
<iso:schema
xmlns="http://purl.oclc.org/dsdl/schematron"
xmlns:iso="http://purl.oclc.org/dsdl/schematron"
queryBinding='xslt2'
schemaVersion='ISO19757-3'>
<iso:title>Test ISO schematron file. Introduction mode</iso:title>
<iso:ns prefix='bpmn' uri='http://www.omg.org/spec/BPMN/20100524/MODEL'/>
<iso:ns prefix='activiti' uri='http://activiti.org/bpmn'/>
<iso:let name="callActivity" value="bpmn:definitions/bpmn:process/bpmn:callActivity"/>
<iso:let name="inSourceExpression" value="child::activiti:in/sourceExpression"/>
<iso:let name="outSourceExpression" value="child::activiti:out/sourceExpression"/>
<iso:let name="inTarget" value="child::activiti:in/target"/>
<iso:let name="outTarget" value="child::activiti:out/target"/>
<!-- Your constraints go here -->
<iso:pattern id="RESERVATION">
<iso:p>
This pattern validates call activities with RESERVATION operation code.
</iso:p>
<iso:rule context="$callActivity[bpmn:extensionElements/activiti:in[(#target='OPERATION_CODE') and (#sourceExpression='RESERVATION')]]/bpmn:extensionElements">
<iso:assert test="count(($inSourceExpression='RESERVATION') and ($inTarget='OPERATION_CODE')) = 0">err1</iso:assert>
<iso:assert test="count(($inSourceExpression='true') and ($inTarget='IS_SYNC')) = 1">err2</iso:assert>
</iso:rule>
</iso:pattern>
</iso:schema>
This is my Java code:
public static boolean validateXMLViaPureSchematron(#Nonnull final String aSchematronFilePath, #Nonnull final File aXMLFile) throws Exception {
final SchematronResourcePure schematronResourcePure = SchematronResourcePure.fromClassPath(aSchematronFilePath);
IPSErrorHandler errorHandler = new CollectingPSErrorHandler();
schematronResourcePure.setErrorHandler(errorHandler);
final boolean validSchematron = schematronResourcePure.isValidSchematron();
if (!validSchematron) {
throw new IllegalArgumentException("Invalid Schematron!");
}
final Source streamSource = new StreamSource(aXMLFile);
final EValidity schematronValidity = schematronResourcePure.getSchematronValidity(streamSource);
return schematronValidity.isValid();
}
I can see the result of the validation by calling schematronResourcePure.getSchematronValidity(streamSource) but I want to see (a report would be sufficient) which rules are failed(err1 or err2). I've read about SVRL but I don't know how to generate report.
Thank you.
Simply call applySchematronValidationToSVRL to get the full SVRL (Schematron Validation Result List) document. You can query it for failed asserts or reports.
Code example where only failed asserts are printed:
SchematronOutputType schematronOutputType = schematronResourcePure.applySchematronValidationToSVRL(streamSource);
List<Object> failedAsserts = schematronOutputType.getActivePatternAndFiredRuleAndFailedAssert();
for (Object object : failedAsserts) {
if (object instanceof FailedAssert) {
FailedAssert failedAssert = (FailedAssert) object;
System.out.println(failedAssert.getText());
System.out.println(failedAssert.getTest());
}
}

Converting Typesafe Config type to java.util.Properties

The title talks by itself, I have a Config object (from https://github.com/typesafehub/config) and I want to pass it the a constructor which only supports java.util.Properties as argument.
Is there an easy way to convert a Config to a Properties object ?
Here is a way to convert a typesafe Config object into a Properties java object. I have only tested it in a simple case for creating Kafka properties.
Given this configuration in application.conf
kafka-topics {
my-topic {
zookeeper.connect = "localhost:2181",
group.id = "testgroup",
zookeeper.session.timeout.ms = "500",
zookeeper.sync.time.ms = "250",
auto.commit.interval.ms = "1000"
}
}
You can create the corresponding Properties object like that:
import com.typesafe.config.{Config, ConfigFactory}
import java.util.Properties
import kafka.consumer.ConsumerConfig
object Application extends App {
def propsFromConfig(config: Config): Properties = {
import scala.collection.JavaConversions._
val props = new Properties()
val map: Map[String, Object] = config.entrySet().map({ entry =>
entry.getKey -> entry.getValue.unwrapped()
})(collection.breakOut)
props.putAll(map)
props
}
val config = ConfigFactory.load()
val consumerConfig = {
val topicConfig = config.getConfig("kafka-topics.my-topic")
val props = propsFromConfig(topicConfig)
new ConsumerConfig(props)
}
// ...
}
The function propsFromConfig is what you are mainly interested in, and the key points are the use of entrySet to get a flatten list of properties, and the unwrapped of the entry value, that gives an Object which type depends on the configuration value.
You can try my scala wrapper https://github.com/andr83/scalaconfig. Using it convert config object to java Properties is simple:
val properties = config.as[Properties]
As typesafe config/hocon supports a much richer structure than java.util.propeties it will be hard to get a safe conversion.
Or spoken otherwise as properties can only express a subset of hocon the conversion is not clear, as it will have a possible information loss.
So if you configuration is rather flat and does not contain utf-8 then you could transform hocon to json and then extract the values.
A better solution would be to implement a ConfigClass and populate the values with values from hocon and passing this to the class you want to configure.
It is not possible directly through typesafe config. Even rending the entire hocon file into json does provide a true valid json:
ex:
"play" : {
"filters" : {
"disabled" : ${?play.filters.disabled}[
"play.filters.hosts.AllowedHostsFilter"
],
"disabled" : ${?play.filters.disabled}[
"play.filters.csrf.CSRFFilter"
]
}
}
That format is directly from Config.render
as you can see, disabled is represented twice with hocon style syntax.
I have also had problems with rendering hocon -> json -> hocon
Example hocon:
http {
port = "9000"
port = ${?HTTP_PORT}
}
typesafe config would parse this to
{
"http": {
"port": "9000,${?HTTP_PORT}"
}
}
However if you try to parse that in hocon - it throws a syntax error. the , cannot be there.
The hocon correct parsing would be 9000${?HTTP_PORT} - with no comma between the values. I believe this is true for all array concatenation and substitution

How do I load up configuration params defined in a XML file?

I have a project A that I want to use from a project B, and I would like to pass some configuration parameters into the project A using a XML configuration file.
In other words, suppose I am implementing something like my own spring-data-mongo (for instance) and I want to configure my Mongo instance from a configuration file in which I have defined as follows:
<mongo: mongo host = "$ {mongo.hostname}" port = "$ {mongo.port}">
<mongo: options connections-per-host = "8"
threads-allowed-to-block-for-connection-multiplier = "4"
connect-timeout = "15000"
max-wait-time = "1500"
auto-connect-retry = "true"
socket-keep-alive = "true"
socket-timeout = "60000"
slave-ok = "true"
write-number = "1"
write-timeout = "0"
write-fsync = "false" />
How do I achieve something like this?

list of vaadin's special path variables #JavaScript(value = { "vaadin://...", "???://..." }

I try to add js file to output html code in vaadin7.4.13+maven project.
I know that it is possible to use vaadin://... protocol which is translated to /VAADIN/ directory.
Now I know that this isn't the only one trick it can be used there because I've seen some other protocols being used in #JavaScript annotation but I cannot recall them. Please help me with this.
vaadin://
???://
???://
...
Looking at vaadin's source code of com.vaadin.shared.ApplicationConstants I found out these:
APP_PATH = "APP";
UIDL_PATH = "UIDL";
HEARTBEAT_PATH = "HEARTBEAT";
PUSH_PATH = "PUSH";
PUBLISHED_FILE_PATH = APP_PATH + '/' + "PUBLISHED";
APP_PROTOCOL_PREFIX = "app://";
VAADIN_PROTOCOL_PREFIX = "vaadin://";
FONTICON_PROTOCOL_PREFIX = "fonticon://";
PUBLISHED_PROTOCOL_NAME = "published";
PUBLISHED_PROTOCOL_PREFIX = PUBLISHED_PROTOCOL_NAME + "://";

Categories

Resources