The Opposite of Swagger-Core? - java

I am an experienced technical writer but new to the management of Swagger or OpenAPI.
We currently use Swagger-Core, where all the settings are in #Operation or #Parameter within Java or Python files.
However, I have seen set ups where those configuration statements are in yaml files. The biggest advantage is that the text inside a description tag can get as extensive as needed.
Somehow they are connected to the Java or Python files. The configurations get set in the yaml files and they get applied to the Java methods.
What is that system called? How does that get done?

Related

How can I write and read to and from a application.yaml at runtime using java code in a spring boot application?

I want to write to an application.yml file as well as read from it respectively, after value has been written in a spring boot application during runtime. It can be application.properties file as well. The property's key can be written dynamically or it can be entered manually by the developer(Doesn't matter).
The key-value pair must me accessible after it has been written to the yaml/properties file.
I don't have much information about the above mentioned concept. So, haven't implemented it yet. Hence, there is no source code for it. I tried to search on the web but didn't get expected information, so posting it here.
Since I don't know the way it is done, if someone knows whether it is the same way or different ways to write to both(properties/yaml) files, preferred way is using the yaml file.
Any helpful links or tutorials are welcome.
Thank You
I used #ConfigurationProperties in my project to solve this problem.

Configure Camel routes from outside JAR in Camel-Spring-Boot setup

My goal is to have a simple way of running Camel with a few routes in a dynamic fashion. Ideally, I'd like to run something like this
java -jar camel.jar routes.xml
which allows adjusting the routes in routes.xml without changing the Jar. But if the routes.xml file is read from a relative filesystem location, that would work too.
I've generated an application using camel-archetype-spring-boot using mvn archetype:generate. I put this line in resources/application.properties:
camel.springboot.xmlRoutes = classpath:routes.xml
which loads the routes defined in resources/routes.xml. So I can configure the routes but must rebuild the Jar everytime I want to adjust something. Now how can I load routes.xml from outside the Jar?
Maybe the path I've chosen is not ideal to get a Camel instance I can quickly reconfigure. If there is a better way, I'd like to hear that. I'm a bit lost with all the options.
I found Externalized Configuration in the Spring manual, but that only explains how to change properties. I also found a question that talks about how to exclude the configuration XML. Unfortunately it doesn't say how the external XML is loaded instead.
Thanks to #ClausIbsen: The file is read from outside the Jar with:
camel.springboot.xmlRoutes = file:routes.xml
Simple, once you know :-)

parsing vagrant file inside java

I need to parse the configurations defined in a Vagrantfile written in Ruby and use the settings elsewhere in my java code. Tried exploring jRubyParser but din't come across any documentation that defines it's use.
Cloned the Vagrant repo locally, but browsing through the code does not help either as I don't have prior experience with Ruby. How would Vagrant be reading the configurations defined in the file ? Any inputs ?
Vagrantfile is a regular Ruby script, i.e. it's meant to be interpreted by Ruby intepreter more than read as a configuration file.
To make things harder, some configuration options aren't declared as top level variables in Vagrantfile, but rather as properties of object in some function calls (like "config.vm.provider".
Depending on how complex your configuration is, I would consider just reading the file line by line and do regular expression matching to get variables I'd need. Not the most elegant solution, but probably way quicker too implement than alternatives.
Also, if your provider is always the same, say VirtualBox, maybe you could get some of your configuration from there. In that case, you would just need to read file located somewhere in "VirtualBox VMs" directory (on Mac, it's in "$HOME/VirtualBox VMs"). It's an XML file, so you could use one of the Java XML parsers to get what you need.

Reading configuration in Java

I have recently started working in Java. I am trying to find a good way to specify my application's configuration values in an easy to read way. I'd like the ability to create a base configuration file and then include in a derived configuration file that also allows overwriting the values in the base file.
Not sure if the java properties file provides the second feature. After searching on google I found apache commons configuration and ini4j but I'm not sure if they are actively maintained or how many people are actually using those.
An easy option is java.util.Properties, which not only supports loading configuration data from key=value and XML files, but also directly supports the hierarchical defaults you describe.
For example:
Properties base = new Properties();
base.load(new FileReader("base.cfg"));
Properties custom = new Properties(base);
custom.load(new FileReader("custom.cfg"));
// 'custom' defers to 'base' for properties that were not in "custom.cfg".
// custom.remove(key) can be used to restore a default from 'base'.
Note that Properties does not have built-in support for grouped / hierarchical properties (e.g. an XML tree or an INI file with sections), but will generally get the job done in many cases.
It is possible to load the base configuration after the custom configuration as well if, for example, the custom configuration contains a property that specifies the base file name (this may be useful given some of your comments below):
Properties base = new Properties();
Properties custom = new Properties(base);
custom.load(new FileReader("custom.cfg"));
base.load(new FileReader(custom.getProperty("basefile")));
By the way, both libraries you mentioned (Apache Commons Configuration and ini4j) are in good working condition. The Apache library has support for a wider range of formats and is more actively maintained, but ini4j is mature (been around for about 10 years and INI files haven't changed) and also works well. I've personally used both a number of times and they have always delivered as promised.
All null / exception handling left out of examples for clarity.

Best practice to create a config file for an existing software programmatically

I am working on a tool that can help me generate a configuration file for use with an existing software (Vagrant), of course, in the format which Vagrant understands.
However, I am having a tough time planning my program design that can help me with dynamic generation of the configuration file with a programmatic approach. Moreover, the config file structure will consist of many optional snippets as well which may/not be required in the final configuration file as per the requirements of the user. I can't think of some efficient approach to go about it.
Three approaches which I have thought of are :
1) Working on a readymade template and just replacing the placeholders with appropriate text.
2) Creating the config file on the fly with String append etc. (Doesn't look like a robust and future proof solution to me).
3) Bifurcation of the basic config structure into sub parts and then including each required component one by one as and when required, then replacing the placeholder values.
I am not very confident if any of these is the best professional approach to dynamically generate such a file.
Are there any constraints on the format of the config file? If not, use YAML (or similar) with two config files: default.yml and deploy.yml. Put the defaults for non-required config params in default.yml and then have Vagrant/Chef/Ansible/bash/whatever write the deploy.yml file. On launch, your app reads both config files, giving precedence to deploy.yml.

Categories