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

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 :-)

Related

Given a JAR file, allegedly contains web server, how do I deploy it?

I've been handed a JAR file, told it contains a web application, and instructed to deploy it. I'm familiar with java as a language, but I have next to no experience with its web ecosystem. Trying to run it as an executable, I find out it has no main class, so I take a look at the contents, and find that it has a META-INF/web-fragment.xml file, along with a couple of Servlet classes and some config files referenced in the xml. It looks like there is basically everything here for a complete application, but I have no idea how to actually deploy it, and google only yields tutorials on how to build these things from scratch with IDEs and deploy using maven plugins and goals.
Assuming that building from source is out of the question and I only have standard unix and java CLI tools available, based on the information provided (And I can provide more if necessary), what is the simplest way to get this up and running?
A web applicaton should be in a WAR file, not a JAR file, and you should only need to just drop it into the webapp directory of your Tomcat, or follow the instructions for whatever other container you're using.
If it's only a fragment, as appears from the XML, (a) it isn't a webapp, only part of one, and (b) you personally can't deploy it at all. You need the whole thing.
Send the assignment back for clarification.

Read file in grails

So I have a few .drlt files (normal txt files that serves me as templates for rule engine) and
I need to read them and put in to some static string variable in bootstrap of application.
I just cannot figure out where to put it in grails application and how to read them.
I've tried put them to src/java and read them from src/grails, domain, service etc. None of that worked. The only thing that worked is to put it into grails-app/conf and read them like this:
def classLoader = Thread.currentThread().contextClassLoader
def fileReader = classLoader.getResourceAsStream('my-file.drlt').newReader()
But I don't want to put my template files in config directory.
I've read many posts and 'solutions' for this problem but non of them are good solutions. Is it really possible that this is unable to do nice in such a stable framework as Grails?
Thanks for any help,
Ivan
Since Grails is a well structured Spring application you can take advantage of Resource and ResourceLoader to load your text file. The grailsResourceLocator makes using these quite simple (be sure to inject it into your bean). Using these classes you can locate your file within the src/java or src/groovy directories and treat it as any other class resource.
So for example, if your file is in src/java/com/example/textfile.txt then your can access it as a File using grailsResourceLocator.findResourceForURI('classpath:/com/example/textfile.txt').getFile()
A detailed example can be found within this blog post and the API documentation for Resource may help as well.
This is the correct way to handle resources within your Grails application.

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.

Accessing properties from application.properties in a non Controller class

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
In the above mentioned example we have hard coded TestUser(username) and TestPassword(password) in the message handler class.I want to externalize these values.
After some research I was not able to implement #Autowired and #value .Please help me out with this
If your .properties file is saved somewhere on the local hard-drive and it is one of the KPL (Key Project Locations), you can simply do this:
Properties propObj = new Properties ();
FileInputStream fis = new FileInputStream("relative path to your file");
propObj.load(fis);
Once you do this, simply use getProperty() or setProperty() to access/mutate desired properties. There is also another way to use the loadFromXML() method if your choice of file is an xml file. I hope this solves your problem because it sounds like you simply having trouble to load a file regardless of being in a controller class. If I am right, then you will find endless examples online that tells you to do merely what I wrote above :)
When using things like #Autowire remember that you have to annotate before the beginning of the target class with #ContextConfiguration("location_of_file.xml"). Getting the correct relative path may be a bit tricky when specifying an applicationContext file. See here and follow other links to find out more about this.
BTW one thing you have to remember that application properties are somewhat internal to an application and for this reason, they are set in a .properties file outside the source files so that it can be run without changing source code (correct me somebody if I am talking bollocks!).
If you are using build automation tools such as Maven or Ivy, there is a way to make sure that your application properties are set using the a) POM and b) applicationContext.xml files. In this way, the application is even more decoupled from breaking down in case a relative path doesn't work out to be the some on a target computer. If you want this, you have to be careful about setting the filters in POM. I haven't personally done this by hand, but we were working on a project a few months ago when a colleague of mine set the filer in front of me. I remember this because everything was failing due to incorrect filter setting. Sorry, but you have to find it out yourself using uncle Google!

Possible ways to abstract away hard-coded path-names for properties file in java app

I'm trying to bind in a third party app to our project but have discovered that the unix paths of their property files have been hard-coded into several classes. It is probably possible to make minimal changes to their setup, so my question is: what are the possible (and quickest) ways to achieve this? The third party app uses neither ant nor spring, but just a build script that compiles the code into a .jar that is called from the command line.
You could read the properties files as resources. Then you only have to put the directories containing these files into your classpath.
You haven't actually said what you want to achieve. Would your application determine some paths and pass them to third party app via an API? Would something in your environment (for example a command line argument) specify the location of the files?
I would first refactor their code so that I know for certain that any hard coded strings are held in one defined place. So if for example they have
readProperties("/usr/thing/propertyFileXxx.txt");
or
static final String PROPERTY_XXX = "/usr/thing/propertyFileXxx.txt";
readProperties(PROPERTY_XXX);
I would first consolidate to a single accessor for the properties
readProperties(PROPERTY_XXX_ENUM);
So now we have a well-defined single piece of code that determines where to obtain the properties of each type and a specific list of the types of properties.
Then we need some controllable way to define the set of property files to be used at run-time. I have used the idea suggested by #tangens of loading the properties as resourcees from a specific directory added to the classpath.

Categories