Posts Tagged ‘rails’

Rails-like configuration of different environments in JAVA – YAML instead of properties file

April 21st, 2010 | By Christoph in Software-Development | No Comments »

This article shows a simple code example how you can replace a standard JAVA properties file (.ini) with a more flexible .yml (YAML) file using the JYAML library.
I used this to store configuration parameters for an application for different environments (e.g. development vs. production) in one central file. This is how the RubyOnRails guys are doing this e.g. in the database.yml.

1. See how the usual standard config files look like.

# config.ini file
# Database
dbconnectionurl=jdbc:mysql://localhost:3306/mydb
dbuser=dbuser
dbpass=dbpasswd

As you see it just has key/value pairs. There is also no notion of an “environement” which means I could use a separate file to store information for different environments. But I want everything centralized.

And here the new yaml file:

# config.yml
development:
    # Database
    dbconnectionurl: jdbc:mysql://localhost:3306/mydevelopmentdb
    dbuser: dbuserdevelopment
    dbpass: dbpasswddevelopment

production:
    # Database
    dbconnectionurl: jdbc:mysql://localhost:3306/myproductiondb
    dbuser: dbuserproduction
    dbpass: dbpasswdproduction

As you can see the config.yml contains two sections. The first section is the configuration for the development environment, which is the default per my convention. The second section contains the configuration for the production environment.

2. Now here is the JAVA code which first loads the config.ini file using the standard approach and second loads the config.yml using the JYAML library and puts them into properties as well. Using that approach we almost don’t have to change any application code, because our application still uses a Java Properties object.

public static void main(String[] args) {
 
		/***
		 * 1. Load Properties using traditional approach
		 * from config.ini
		 */
		try {
			Properties props = new Properties();
			props.load(new FileReader(new File("config.ini")));
			System.out.println(props.toString());
 
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
 
		/***
		 * 2. Try to do the same based on
		 */
		try {
			Properties props = new Properties();
 
			HashMap yamlConfig = (HashMap) Yaml.load(new File("config.yml"));
 
			// now check which environment to load (production or development)
			// development is default
			String environment = System.getProperty("MYENVIRONMENT","development");
			HashMap propertiesFromYamlForEnvironment = yamlConfig.get(environment);
			props.putAll(propertiesFromYamlForEnvironment);
			System.out.println(props.toString());
 
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

As you can see the line
String environment = System.getProperty("MYENVIRONMENT","development");
is responsible to read the environment from a system property. you can set this by setting the usual -DMYENVIRONMENT=”production” when you start you java program.

That’s it. Pretty simple but powerful.
You can read more about JYAML in their tutorial.

Resolving trouble with Ruby On Rails with MySql on XAMPP on OSX Snow Leopard

Oktober 9th, 2009 | By Christoph in Software-Development | 3 Comments »

I was trying to work through the Get Started guide of Ruby On Rails but I had issues when I was trying to run the command “rake db:migrate” the first time.

It was complaining that the mysql gem was not up to date or something and I had to do a “gem install mysql”. But that resulted in errors as decribed in the following blog posts:
http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard#comment-5544

http://boonedocks.net/mike/archives/175-MAMP-and-the-Ruby-MySQL-Gem.html

I had issues as I was using XAMPP and “gem install mysql” was complaining about missing mysql.h I had to upgrade my XAMPP and additionally download the XAMPP developer tools.

Go to http://sourceforge.net/projects/xampp/files/ and download:
xampp-macosx-1.7.2a.dmg
xampp-macosx-1.7.2a-dev.dmg

Of course first I had to remove/backup my existing XAMPP folder under /Applications/xampp Then I installed the new version and after that the xampp-macosx-1.7.2a-dev.dmg developer pack. this xampp-macosx-1.7.2a-dev.dmg contains the mysql.h header files and so on. After that dev tools installation the XAMPP folder has a new folder called “include” which has all the mysql header files like mysql.h Then the following command line worked for me:

sudo ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-dir=/Applications/XAMPP/xamppfiles/

Note I was pointing to the xamppfiles base-dir as the mysql-dir, because then all the paths are relative as if this would be the mysql root dir.

Update: But as I am writing this, I was running into new trouble. It seems I need to update to the 64bit MySQL Version because of Snow Leopard, because now I am running into new problems:

Couldn’t create database for {”username”=>”root”, “adapter”=>”mysql”, “database”=>”mydatabase”, “password”=>nil, “socket”=>”/tmp/mysql.sock”}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

According to http://www.techskater.com/ruby-on-rails/problems-with-mysql-gem-and-rake-on-snow-leopard/ this seems to be related to Snow Leopard and MySql 32bit vs. 64bit. On Snow Leopard the Gem Mysql needs to be build against a 64bit mysql or so.

I am currently reading http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard to resolve the issues and update MySQL. Let’s see…

Update 2: So I have finally installed MySQL 64Bit as suggested on the page above and I can start it. A “rake db:create” has also worked now as I could verify via MySQL Query browser. Let’s see what is the next thing which is not working…

Update 3: It really looks I am done and I am currently working through the Getting Started Guide.

Conclusion: 3 hours wasted to get it running on my OSX Snow Leopard. I did the same yesterday on my Windows machine at work and I didn’t have any such problems. It seems to be only related to Snow Leopard.  You might just read this page http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard and ignore everything above, but I leave it in this blog post for further reference. The difference is that you might not continue using the MySQL from XAMPP but a standalone mySQL. But you can still use phpMyAdmin from XAMPP or MySQL Query Browser to manage your database.