cool hit counter
Cafe4Java
Cafe4Java
 
Configuration issues with Deployment of Java projects on different servers
< Previous    Page 1    2    3    4    Next >

PART 1 : Role of Java Application Developer

1) Create the Properties file : application.properties
A '.properties' file is a text file, which contain variable-value pairs.

Complete name and path of file 'application.properties' : C:\cafe4javaProjectContext\src\com\application.properties
(A '.property' file is text file without the extension .txt)
The following are the complete contents of file 'application.properties':

com.cafe4java.mail.smtp.host=@CAFE4JAVA_SMTPHOST@
com.cafe4java.mail.smtp.username=@CAFE4JAVA_SMTPAUTHUSER@
com.cafe4java.mail.smtp.password=@CAFE4JAVA_SMTPAUTHUSERPASSWD@

2) Ant Properties file (build.properties)
build.properties file defines the actual values that need to be passed to an application. These values normally vary for an application, when it is deployed on separate servers.

build.properties is read by build.xml(Ant build file: discussed later in this article)

Complete name and path of file 'build.xml' : C:\cafe4javaProjectContext\build.properties
The following are the complete contents of file 'build.properties':

cafe4java.mailserver=smtp.cafe4javamailserver.com
cafe4java.mailserver.user=usercafe4java@cafe4java.com
cafe4java.mailserver.user.password=pwdcafe4java
com.cafe4java.testapp.src=c:/cafe4javaProjectContext/src
com.cafe4java.testapp.build=c:/cafe4javaProjectContext/build
The last 2 values in the above mentioned build.properties file specify the exact location of the code files on the server. A 'properies' file is a text file, which can be easily edited by an application deployer to pass on server specific values to an application.

3) Define a Java Class to read the properties file
The class Cafe4JavaPropertiesUtil will read the (above mentioned) application.properties file, and load its contents into its class variable.

Imp: Before the class Cafe4JavaPropertiesUtil reads the file 'application.properties' file, the following text will be replaced by the actual values from file buiild.properties. The transfer of values take place, when we build/ compile our code:

  • @CAFE4JAVA_SMTPHOST@
  • @CAFE4JAVA_SMTPAUTHUSER@
  • @CAFE4JAVA_SMTPAUTHUSERPASSWD@
Complete name and path of file 'Cafe4JavaPropertiesUtil.java' : C:\cafe4javaProjectContext\src\com\cafe4java\Cafe4JavaPropertiesUtil.java
The following are the complete contents of file 'Cafe4JavaPropertiesUtil.java':

// Cafe4JavaPropertiesUtil.java

package com.cafe4java;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

class Cafe4JavaPropertiesUtil {
	private static Properties properties = new Properties();
	private static final String PROPERTY_FILE_NAME = "com/application.properties";

	static
	{
		// Initialise the InputStream
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(
					PROPERTY_FILE_NAME);
		try	{
			properties.load(in);
		}
		catch (IOException e) {
			// Print out the error message to the console for this example
			// However it is a good programming practice to maintain a log
			// of these messages for debugging
			System.out.println ("Error loading properties file :" + e );
		}
	}

	// accessor method to access a retrieve a property
	public static String getProperty(String propertyName) {
		return properties.getProperty(propertyName);
	}

	// set the value of a property
	public static void setProperty(String propertyName, String propertyValue) {
		properties.setProperty(propertyName, propertyValue);
	}
}

Test Code to verify the new values that will be transferred from build.properties to application.properties
Complete name and path of file 'TestExample.java' : C:\cafe4javaProjectContext\com\cafe4java\TestExample.java
The following are the complete contents of file 'TestExample.java':

package com.cafe4java;

/**
 * This class reads and prints values from a property file.
 * The values are written to the properties file through the
 *
 */
class TestExample {
	public static void main (String args[]) {
		System.out.println (Cafe4JavaPropertiesUtil.
				getProperty ("com.cafe4java.mail.smtp.host"));

		System.out.println (Cafe4JavaPropertiesUtil.
				getProperty ("com.cafe4java.mail.smtp.username"));

		System.out.println (Cafe4JavaPropertiesUtil.
				getProperty ("com.cafe4java.mail.smtp.password"));

	}
}

4) Create the Ant Build file (build.xml)
Apart from building/ compiling code, the following Ant file (build.xml) reads values from build.properties file and passes it on to the application.properties file.

Complete name and path of file 'build.xml' : C:\cafe4javaProjectContext\build.xml
The following are the complete contents of file 'build.xml':

<project name="cafe4java" default="build" basedir=".">
<description> Cafe 4 Java: Using Properties file with Ant</description>

    <target name="build" >
        <property file="build.properties"/>

        <property name="src" location="${com.cafe4java.testapp.src}"/>
        <property name="build" location="${com.cafe4java.testapp.build}"/>

        <tstamp/>

        <filter token="CAFE4JAVA_SMTPHOST" value="${cafe4java.mailserver}"/>
        <filter token="CAFE4JAVA_SMTPAUTHUSER" value="${cafe4java.mailserver.user}"/>
        <filter token="CAFE4JAVA_SMTPAUTHUSERPASSWD" value="${cafe4java.mailserver.user.password}"/>

        <delete dir="${build}" />
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>

	<copy 	file="${com.cafe4java.testapp.src}/com/application.properties"
			todir="${com.cafe4java.testapp.build}/classes/com"
			filtering="true"
			overwrite="true" />

        <javac srcdir="${src}" destdir="${build}/classes"/>

    </target>

</project>

< Previous    Page 1    2    3    4    Next >

Submit your feedback on this article


 
 
Custom Search
     
Cafe4Java