netbeans and hibernate configuration - java

How can I load the configuration information for hibernate dynamically from a config file. Netbeans currently hard codes that information into an xml file that is then compiled into the jar. I'm a newbie to Java/Netbeans coming from PHP land and am use to a central bootstrap that pulls from a .ini or something similar, but netbeans tends to hardcode this information upon generation of the models,etc in an xml file that is then compiled in the jar. I'm looking for conventional methods of setting up configuration for various client machines using various database configurations. I don't want to have to compile the app on each machine it must be installed on.

The configuration file is read using the Configuration class. By default, it uses the hibernate.cfg.xml file found in the classpath, but you can use the configure method taking a file as parameter, and store the config file on the file system rather than in the jar.
You can also put the static mapping, which never changes between configs, in a file inside the jar, and put the varying config inside an external file. Look at the javadoc for Configuration to know how to add resources and config files to the configuration.

Related

Log4j2 read properties from an external property file

I have an application that i have to deliver in a packaged JAR which is then run by the client in a complicated environment that makes editing env variables or JVM arguments very cumbersome (additionally client is not too technical). Currently we are using some external proeprty files for configuring the database and so on and this is going well so far.
I would like to allow the client to configure some aspects of Log4j2 using these properties files, i can see that Log4j2 allows multiple ways of performing property substitutions https://logging.apache.org/log4j/log4j-2.1/manual/configuration.html#PropertySubstitution
I can also see that it is possible to load property bundles but if i understand the docs correctly these bundles need to be loaded from classpath and i have not stumbled upon a possibility of define this properties file by giving its direct path (such as "load the properties file /tmp/myconfig.properties").
So ultimately - is it possible to use variables from an external .properties file that is NOT in classpath but in a specified filesystem location? Or maybe some other way exists to load this data from external file? (i already noted that using env variables or jvm arguments is out of the question in my case).

Does bnd launcher support configuration files that are made known with system properties?

When I launch an application from bndtools (using a file launch.bndrun), I can specify -runproperties: java.util.logging.config.file=jul-debug.properties, with jul-debug.properties being a file in the working directory. Everything is fine and works as expected, i.e. the java.util.logging properties are read from my configuration file.
When I create an export (launch.jar) using this lauch.bndrun, the configuration file jul-debug.properties is not included in the launch.jar. Can I add some instruction to launch.bndrun that causes the jul-debug.properties to be included in laucn.jar in such a way that jar -jar launch.jar will cause the configuration file to be evaluated?
I looked through all the -run... instructions for bnd, but couldn't find anything.

Migrated project to Maven, where to put config folder

I migrated my javafx recently to maven. It starts and works fine, but i cannot figure out where to place the config folder and how to say maven that it shell include it.
Before i converted the project to maven, the structure looked like this:
|-src
|-lib
|-config
Now the sructure looks like this:
|-src/main/java/my.super.program
|-src/main/resources/my.super.program
|-lib
|-config
I tried it with
<resource>
<directory>config</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
but that hasn't worked.
In the main.java i declare the needed config files. For example:
private final String configPath = 'config/config.xml';
private final String accountPath = 'config/account.xml';
How can I include the config folder and tell maven to use it?
By default everything that is placed inside the src/main/resources is added to the jar file that is built. Alternatively you can create your own maven module holding the configuration. Again you will have to place it under src/main/resources
If you decide to create a dedicated module holding the configuration you will have to add it to the dependencies of the previous module. Preferably it will be a compile time dependence(this is the scope).
I hope this answer doesn't confuse you, if it does, probably just seek a simpler solution.
Jars are not a good place for storing mutable configuration. You can only put a default configuration in the jar file. A jar file is not writable, so your program can't overwrite the configuration to update it if it is in the jar.
For apps that require user specific configuration, I'd be inclined to store the config in a $USER_HOME/.myapp directory. An advantage of using the file system for configuration rather than a database or the java preferences API, is that the config can be easily viewed and edited using a standard text editor.
Sample code to determine a home directory for your application's config:
String configHome = System.getProperty("myapp.home",
System.getProperty("user.home") + "/.myapp"
);
You could access the resources for the default configuration out of the jar file and copy them to files on the file system. Then a user could manually edit the files on the file system to configure your application. Or, you could provide a custom UI that you create for the user to modify the info in the files.
An example of this approach is in this OsiamHome class, which is one of the nicest ways I have seen to setup configuration for a Java application (that's a server app, but the general principle could be used for a client application). The osiam reference is a Spring application so that it benefits from some of the configuration support provided by Spring, in particular the PathMatchingResourcePatternResolver, that helps to recursively search a jar to find all default configuration resources for the application.
However, you probably don't need the additional facilities provided by the Spring based approach. Instead, you can just keep a list in code of all the files in your default configuration and place the files in a /resources/config directory in your source tree. The build process will copy the default configuration into your jar file for deployment. When the application is run, if the config directory on the filesystem does not exist, the application can read the default config resources from your jar file and copy the config resources to files on the filesystem. The config files can then be modified and changed as much as necessary.
Some other options you have for managing the configuration outside of your application jar are to:
Place the configuration information in a database OR
Use the Java preferences API OR
Use a packaging system, e.g., rpm or (perhaps) the javapackager tool or javafx maven plugin which bundles the default configuration into a native deployment package. Native OS deployment software can extract the configuration and copy it to a location you specify. Usually, this location will be a standard configuration location for the OS, such as /etc/myapp on Linux for system wide config or $USER_HOME/.myapp for user specific config.

Persistence.xml file usage without container [packaging]

I am trying to use jpa/hibernate framework with a simple java console application without a container. According to jpa documentation the persistence.xml file should be placed under the folder META-INF on the src folder.
The issue came when trying to package the application as a simple jar file then the persitence.xml file will be within the jar file generated (since it should be placed on the src folder).
In this situation the persistence.xml file is not easy accessible to modify the application configuration like the DB URL, time out,the hibernate logs...
I tried to put the META-INF/persistence.xml outside the src folder and added it to the CLASSPATH but an error saying Could not find any META-INF/persistence.xml file in the classpath is always thrown.
Is there any way to keep the persistence.xml editable and accessible once the application is packaged and deployed on production like any other classic configuration file (eg .properties files).
Thank you
I think you don't need to make persistence.xml editable since you can provide additional properties when calling Persistence.createEntityManagerFactory() (or using similar Hibernate-specific mechanisms).
So, you can use persistence.xml for static configuration only, and configure dynamic properties using your own configuration mechanism.

Config files - where to put them in Java?

I have a java desktop app and the issue of config files is vexing me.
What I want is for my distributable application folder to look like this:
MyApp/Application.jar
MyApp/SpringConfig.xml
MyApp/OtherConfig.xml
MyApp/lib
But at the moment SpringConfig.xml is inside Application.jar and I can't even find OtherConfig.xml programmatically.
I don't care how I set up the various files in my compilation path, so long as they end up looking like the above.
So..
where do i put the files in my dev setup?
and how do i access them programmatically?
thanks
the spring config file is related to the code and wiring of your application, hence it'd better be inside the jar, and should be subject to change by the users
(new File(".")).getAbsolutePath(); returns the absolute path of your jar - then you can load the OtherConfig.xml by a simple FileInputStream
if the SpringConfig.xml contains configuration data like database credentials, put them in an external application.properties and use a custom PropertyPlaceholderConfigurer to load the external file.
Answering the question "where do I put the files in my dev setup" is not possible because we don't know your environment.
Actually, if you want to be able to edit the config yourself (and not necessarily end-users), you can open the jar with any zip software (WinRAR for instance) and edit the config file from within the jar.
Update: as it seems you can't make the config files to be places out of the jar. Well, for a start, you can do it manually - whenever the .jar is complete, just remove the config file from inside and place it outside.
I typically create a structure where I have a src/ directory and then other directories exist at the same level. Some of those directories include:
lib/ - External Libraries
config/ - Configuration Files
resources/ - Various resources I use (images, etc)
At that same level, I then create an Ant script to perform my build so that the appropriate config files, resources, lib, etc are copied into my JAR file upon build. It has worked great for me up to this point and is a fairly easy to understand organizational structure.
Update: Accessing my config files is done, typically, by knowing their location and opening them up and reading them in the code. Because I use Ant to build, I make sure that my config files are in a location that I expect. So, for example, in a recent application I created, when I compile, my JAR file is in the top level directory (relative to the application release structure). Then, there is a "main" config file at that same level. And there is a "theme" config file that is in a themes folder.
To read the various files, I just open them up as I would any other file and read them in and go from there. It's nothing particularly fancy but it works well and it makes it easy to manually change configurations if I need to do so.
In dev mode, put them in source dir and they will be copied to your classes folder, you can then access them using classloader.
Example:
URL url = ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));
In Prod mode, you can make them part of your jar.

Categories