Java EE i18n without property files - java

normally I place my translations etc in properties - e.g. message_de.properties etc. I'm facing now a situation where I have to provide this data in a database. (Translations will be imported/edited in an other system)
Is there a common way/best practises to use a database table for messages in my webapp? One way I would thinking of was to build properties from the database but this seems not the right way to go.

A ResourceBundle doesn't need to be based on a properties file, you can write your own implementation and back them with JDBC or whatever tickles your fancy. The ResourceBundle javadoc page has an example on how to make a custom implementation.

To me it sounds like exporting the database to properties files would be the correct way to go. It's simple and I would assume the database does not change that often? The export can then be automated different ways.

Related

Spring Boot : approach to have global access to properties loaded from a custom property table

Background : I don't want to hardcode properties in a java constant file, coz every time I want to change to one of the property_value, I have to build and deploy the entire code again. I don't even want to keep them in application.yaml/application.properties file, coz my properties are huge in numbers(100s). So, I have decided to maintain properties in a properties table in oracle DB.
I can think of two approaches :
Read the property value as and when required by firing a sql query.
Load all/part of the properties at the time of starting application and have global point of access by caching them.
As I need few of the properties in the beginning itself, I want to go with approach number 2.
I wanted to go for singleton bean, but this requires me to know all of the keys(property_names) in the beginning itself, and makes the singleton look ugly having 100s of member variables. Here I was planning to fire query by using pre-construct function of spring bean. The main problem here is the bean requires to be changed every time I add/delete properties from the properties table.
Another approach I could think of was to go with a Map<String,String> as both the property_name and property_value columns of my table are of VARCHAR type. But the question is how can I get global point of access to this map ?
Any better approaches much appreciated!
Many thanks in advance
If you deploy your app on web server you can take advantage of it. Many of web server supports changing configuration on the fly (using web console) such as JBoss EAP.
The other idea is to build your own properties lib. Create a function which read the data. I'd prefer using NoSQL database than RDBMS because of lightweight, great performance and scale very well. I wouldn't use caching unless it persisted somewhere and good on availability.
I've heard about Spring Cloud Config to externalize configuration but i never deep dive into it.
Some of us uses Apache Commons Configuration that support properties reload. Take a look at this thread.

Best practice for storing config data

What is the best procedure for storing configuration data?
I have several classes that require some configuration data that is only needed in their class.
Should I load all this data from a configuration file or should I hardcode it into the classes?
Many thanks in advance
To address your question:
Should I load all this data from a configuration file or should I hardcode it into the classes?
Basically if you hard code some value into the class, you don't intend to change that value in different environments. Each such a change would require re-compilation of the project.
For example if you have a constant for PI=3.14 it doesn't make sense to use different values for different environments.
Alternatively if you go with configuration files, the update of such a file that can be supplied with a deployment script is much easier.
An example of this can be a host/port of the database. Development might use one host, production might use another.
So you should decide what works for you best.
This is common for all types of applications (not only spring boot driven).
Now its true that in spring boot you can create a configuration file (properties or yaml) and place it into the artifact (by putting it into src/resources/ or src/resources/config).
For some situations its good enough, for others you might use another way of configuration.
I don't refer managing secrets here, this is a more advanced stuff, but in general you won't want to manage things like passwords neither in the source code (hard coded) nor in the configuration file.

Which is the correct place to store master data

I am working on a spring MVC web application. There is some master data for example currency code INR which I require on jsp page to compare with data from form bean. So which should be the correct place to store master data. Should it be in property file or do I need to fetch it from database and store it in any of the scope ?
Any kind of help will be appreciated.
If you don't expect to change data often then the property file is a fine place as a start. It is the simplest in terms of development effort. But note that to change the data you will need to restart the application.
Also if the application is running on multiple machines then each machine will have separate properties file. So you will have to edit properties files on all machines.
The database is better if you want to expose functionality to update the configuration to end-users. Also, the database will act as a single source of truth, no matter how many application nodes you have.
By currency codes, do you mean data like https://www.iban.com/currency-codes.html ?
In that case, it is a small amount of data that does not change often.
So the properties file is ok. To inject properties as a map (unknown number of keys, unknown exact key values) you need to use #ConfigurationProperties as described in https://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#boot-features-external-config-loading-yaml
I would go for a database if it is used by your project for other purposes. Otherwise, I would go for the properties file.
As said in the comments, business data belong in the database, even if you change them every other year. Property files are a great place for technical properties, especially those that are required before you load the Spring context. Those might be database credentials, logging level etc.
Consider that if you start to load-balance your application on different hosts, you'd have multiple property files, too. Imagine you forgot to update the currencies in one of them, you're gonna screw up the consistency of your whole database, when each instance of your app calculate different prices.
You may consider using yml/properties files with Spring Cloud Config. It's simple enough to use and it will give you a way to update your properties in runtime, without redeploying or even restart. So don't forget to enable Spring Actuator endpoints and get familiar with #RefreshScope annotation.

Properties file for database connection GWT

Hi is it possible to use a properties file in GWT that contains database details and then just change it anytime if for example, your database details change? This file would be read by java.sql.Connection so that the system could connect to the database. Thanks
It is possible to use files to configure GWT. Every GWT "module" has a definition xml file, and you can always define constants in your Java code too.
On the server side, you can use all of the Java tools you're used to, as long as your server supports them! GWT does not place any additional constraints on your server code.
I suggest you to try with an ORM (object-relational mapping) in order to handle your database communication. In this case you will naturally use a configuration-property file.
There is a good article that provides some info about using GWT with Hibernate.

How do I save server state between webapp deployments not relying on a database?

We have a utility spring-mvc application that doesn't use a database, it is just a soap/rest wrapper. We would like to store an arbitrary message for display to users that persists between deployments. The application must be able to both read and write this data. Are there any best practices for this?
Multiple options.
Write something to the file system - Great for persistence. A little slow. Primary drawback is that it would probably have to be a shared file system, as any type of clustering wouldn't deal well with this. Then you get into file locking issues. Very easy implementation
Embedded DB - Similar benefits and pitfalls as just writing to the file system, but probably deals better with locking/transactional issues. Somewhat more difficult implementation.
Distributed Cache - Like Memcached - A bit faster than file, though not much. Deals with the clustering and locking issues. However, it's not persistent. Fairly reliable for a short webapp restart, but definitely not 100%. More difficult implementation, plus you need another server.
Why not use an embedded database? Options are:
H2
HSQL
Derby
Just include the jar file in the webapps classdir and configure the JDBC URL as normal.
Perfect for demos and easy to substitute when you want to switch to a bigger database server
I would simple store that in a file on a filesystem. It's possible to use an embedded database, or something like that, but for 1 message, a file will be fine.
I'd recommend you store the file outside of the application directory.
It might be alongside (next to) it, but don't go storing it inside your "webapps/" directory, or anything like that.
You'll probably also need to manage concurrency. A global (static) read/write lock should do fine.
I would use JNDI. Why over-complicate?

Categories

Resources