I have one Project (say A) which uses Spring. Project A internally depends on other project (Say B) for few Task. So B Project URL is configurable in server.properties file of A Project.
So now each time URL for B project get changed then I have to shutdown A Project, change the URL in server.properties file and then again start the server.
So I have requirement as to ask user to enter the new URL using GUI and change the URL at runtime.
I know it is possible to change the value of the property at runtime but it will be set only for that session and not an actual modification to properties file, and once the server is restarted again it will refer to old URL because actual changes are present in Session and not to actual Properties file.
my actual need is that changes should be reflected in properties file(for future restart of server it should work) as well as in beans configuration File(for current session - I am aware of how to do.)
Now my requirement is, whenever any changes comes in configuration then it should also physically change the properties file...is there any neat way to do this instead of reading File and search for that key and then replace substring.
Is there any third party api to do this nicely.
You can look at commons configuration. See file based save and reload here.
You can update properties file at runtime by using Commons Configuration API then get value by key. Here is good working example update and read properties
Hope this help!
Related
I have some questions about Wildfly deployment
1.An ear, when deployed in wildfly, is extracted within standalone/tmp/vfs/deployment/ directory. Can I place a file there manually and still access it from web. (I can check it, but as of now I do not have any machine to test it).
Can I create a file and place it there via some program. The reason I am asking this question is that I need to generate some files based on user input and provide the user with a link to that file. One way to do this is to statically link a directory in JBOSS and create the file there(access it using file handlers see this). I just want to know if it can be done at all using something like VFS.
If you need to persist to a file you'd want to create a new file handler, like the link you provided describes, and write the file to that folder. You don't want to try to use that temporary deployment directory. The content is not exploded by default so writing to it would likely fail.
If you don't need to persist to a file you can just use an output stream of some sort and the user will be able to download the file.
I'm using Felix ConfigurationAdmin 1.8.6 (which is included in OSGI enRoute) and i'm not able to get it working with files in a folder.
I downloaded the sources to see what's going on, but i still can't figure out...
First, I added the run property felix.cm.dir, specifying the folder I want to configure my services. From what i see debugging, it's working fine, but my managed service get a null properties map.
Next, i discovered my properties must have the following format to be read (with doublequotes):
key="value"
Still, i get a null properties map...
Then i discovered that after reading my properties, the FilePersistenceManager.seek() only returns the Dictionary if it DOESN'T contains service.pid property, OR it contains that property and it equals the filename. Before comparing, it replaces the dots with slashes, so it never equals... I better not specify service.pid property anyway, i don't see the point.
When the Dictionary comes back to CachingPersistenceManagerProxy, it only get cached if the Dictionary contains the service.pid ou factory.pid property; which never happens.
Am i missing something?
I don't know if enRoute brings Apache Felix FileInstall with it, but without this bundle configuration admin services doesn't pick up configuration files. FileInstall usually takes care of that. And in that case, yes you'll need to make sure your cfg file does have a name matching your PID.
So for a configuration with pid: my.conf.foo you'd need a file my.conf.foo.cfg in a folder monitored by the FileInstaller Bundle. It'll pick up this file and make sure your configuration is updated with the content of the file.
To externalize a .properties file, currently I am keeping it in a file system say
C:\test\UI\properties
and I have created a Environment variable $PROP_LOCATION which points to above file location. So whenever I need to change properties, just I go to that location edit properties and refresh the application. It works like a charm.
So is this the best way? Or is there any other way you professionals suggest me to keep properties file out of war file?
Note: The above thing works fine in both unix and windows environment as I have a "if" condition, if one of the environment variable not found I will check for another environment variable.
I agree that just packaging the properties file with your application makes the most sense. If you have some valid reason not to do that, your solution seems fine as long as in deployment the file is placed in a location that your application has access to and is not exposed to the Internet.
I am doing something somewhat analagous to you. In an application I'm running, there is a page of announcements and other messages that I want users to see, but to make it easier for me and others to edit these messages, they are loaded from another place on the server, not even in the Tomcat folder. I can then easily edit the messages without even re-deploying.
I usually reference external properties files by adding the directory to the classpath.
For example, if example.properties is in path C:\test\UI\properties directory, I call JVM as follows:
java -classpath C:\test\UI\properties MyJavaProgram
Inside the Java code, you can load the properties via the following mechanism:
Properties p = new Properties();
p.load(getClass().getClassLoader().getResourceAsStream("/example.properties"));
I suggest to use the ReloadableResourceBundleMessageSource of spring
Check it out: http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/context/support/ReloadableResourceBundleMessageSource.html
And as for placing the properties files, I don't think keeping them outside application is good idea. You should package them in war anyway, as they are logically part of your application.
And since you are reloading the application, you can use maven task to build, deploy and reload the app on server everytime the properties change.
I'm working on a small java application that needs to load/save configuration properties. At first I tried using a properties file that lived inside the jar but I was concerned if someone were to upgrade to a later version (replace the existing jar) they would loose all of their settings.
My next idea was to write the configuration file to the disk and reference it but I'm not sure what location I should save to. I don't want to use the current directory because the user could move the jar between directories and forget about the configuration file.
Is there a common location for configuration property files (specifically on windows)? If so how would I access it (I assume it would be some sort of wild card character like %appdata%\my app\config.properties)?
Thanks!
You might want to look into using the Preferences API instead. It provides a lightweight and simple way of storing application and user preferences, without directly accessing the file system.
Try the java.util.prefs Preferences API... as long as you are using Java 1.4 or newer. I think that will do what you want. I wrote a little post about them a while back: http://coffeaelectronica.com/blog/2009/07/java-preferences-api/
Hope this helps.
The user.home system property provides the path to a users home directory if you want to store on the file system.
There is also the Preferences API that was added to Java to solve this exact problem.
I'm implementing an EJB-based system in JBoss.
One of my message driven beans will be responsible for sending emails. I want the email template to be stored externally (probably as XML) so that it can easily be changed without having to change the code/redeploy the bean, etc.
Where should this file be placed and how do I reference it?
The JBoss documentation specifies that the environment variable jboss.server.data.dir is the "location available for use by services that want to store content in the file system". See here for more details.
You can get the value of the variable by
System.getProperty("jboss.server.data.dir");
And, as shown in the link, the location of the server/[config]/data directory will be returned. Store the template file there when you deploy your app, and instruct your admins to modify it there.
This question and this blog post refer to property files, but what is discussed also applies for other kind of files. One solution would be to place the XML file in a standard location and read it like this (code taken from the link above):
String path = System.getProperty("catalina.base")
+ System.getProperty("file.seperator")
+ "YOUR_FILE.properties";
FileInputStream fis = new FileInputStream(path);
Instead of catalina.base you can use user.dir or even define your own environmental variable (mypath_to_xml_file) and read it with System.getProperty.
Another solution would be to use JNDI to define the path to the file.
Or put it in a database where you can get at it via SQL client. Load it on startup or maybe poll to check for timestamp changes and reload it.
What kind of template do you mean? Is it a Velocity template that you populate in a mail merge fashion?
One thing that I like about the database design is that you can add so much more information to the schema that's over and above a mere file. You can include timestamp, user ID of the person who updated the template, version, meta data, system name, a boolean to indicate "live" or "dormant", etc. Your EJB can be really smart about which template it chooses. You might find yourself with even more options than you'll have if you simply park files on the server drive. They'll be available to other apps that way as well. Could be a nice design if you run with it a little bit.
If you add it to the WEB-INF/classes directory of an exploded deployment you can easily reference it using "getResourceAsStream()". Add a polling feature if you don't want to have to bounce the server.