29.01.2013 Views

WebSphere Application Server V7.0: Concepts ... - IBM Redbooks

WebSphere Application Server V7.0: Concepts ... - IBM Redbooks

WebSphere Application Server V7.0: Concepts ... - IBM Redbooks

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For packaging the property files, you have two approaches. Either you include<br />

the property files within the EAR file itself or you distribute them separately. To<br />

include them within an EAR file, the easiest approach is to create a utility JAR<br />

project, add the property files to it, and then add that project as a dependent<br />

project to the projects that will read the property files. The utility JAR project is<br />

then made available on the class path for the other projects. The best practice,<br />

however, is to centralize access to the property files using a custom property<br />

manager class, so access to the properties is not scattered all over your code.<br />

For example, to load a property file using the class loader, you can use the code<br />

snippet in Example 8-1.<br />

Example 8-1 Loading a property file using the class loader code snippet<br />

Properties props = new Properties();<br />

InputStream in =<br />

MyClass.class.getClassLoader().getResourceAsStream(“my.properties”);<br />

props.load(in);<br />

in.close();<br />

Property files packaged in a JAR file in the EAR file are a good solution for<br />

property files that should not be modified after the application has been deployed<br />

(the application-specific category described in 8.10.1, “Classifying configuration<br />

settings” on page 301).<br />

If you want to make the property files easily accessible after the application has<br />

been deployed, you might want to store them in a folder outside the EAR file. To<br />

load the property files, you either make the folder available on the class path for<br />

the application (and use the code snippet in Example 8-1) or you use an absolute<br />

path name and the code snippet in Example 8-2 (assuming the file to load is<br />

/opt/apps/OrderApp/my.properties).<br />

Example 8-2 Absolute path name code snippet<br />

Properties props = new Properties();<br />

InputStream in = new<br />

FileInputStream(“/opt/apps/OrderApp/my.properties”);<br />

props.load(in);<br />

in.close();<br />

Using absolute path names is usually a bad idea because it tends to hard code<br />

strings into your code, which is not what you want to do. A better approach is to<br />

make the folder with the property files available on the class path for the<br />

application. You can do this by defining a shared library to <strong>WebSphere</strong><br />

<strong>Application</strong> <strong>Server</strong>. Instead of specifying JAR files, you specify the name of the<br />

folder that holds the property files, such as “/opt/apps/OrderApp”, in the<br />

Classpath field for the shared library.<br />

Chapter 8. <strong>Application</strong> development and deployment 303

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!