KevSheedy
KevSheedy

Reputation: 3265

Websphere Application Level Environment Variables

Is it possible to set environment variables at the application level in websphere?

Websphere servers can host multiple applications. Any environment variable applied at the server level applies to all the applications on that server. Is it possible to create variables that only apply to individual applications?

For example: Lets say we have a SpreadsheetApp and a DocsApp running on the same server. They both share some common code that can be configured via an environment variable called DocStorageLocation. Is it possible to set the DocStorageLocation differently for both applications?

Alternatively, is there another way of configuring multiple applications running on the same server?

Upvotes: 5

Views: 5609

Answers (1)

user2015707
user2015707

Reputation:

QUESTION 1

Is it possible to set the DocStorageLocation differently for both applications?

I don't think it is possible. Websphere's environment variables are meant to be used by the server itself. A variable has only three possible scopes, which are Server, Cluster and Node.

For instance, an ORACLE_JDBC_DRIVER_PATH environment variable on server1, node1 scope could be used for the JDBC provider on node1 (classPath = ${ORACLE_JDBC_DRIVER_PATH}/ojdbc14.jar).

The question is : "why can't I set a different value for my application only" ? But my guess is that as long as the server, clusters and nodes are started, it does not make sense to override this value for a deployed application.

Although I think it is not possible, I still tried. But I did not manage to override an environment variable set for the websphere server.

QUESTION 2

Alternatively, is there another way of configuring multiple applications running on the same server?

Environment entry

You could add an environment entry to your web.xml deployment descriptor, a variable you can look up for.

<env-entry>
    <env-entry-name>DocStorageLocation</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>C:/DocStorage</env-entry-value>
</env-entry>

Then test, look up for this variable in the java class :

//TEST
Object l_test = null;
try {
    Context l_ctx = new InitialContext();
    l_test = l_ctx.lookup("java:comp/env/DocStorageLocation");
} catch (NamingException e1) {
    // TODO
    e1.printStackTrace();
}

URL ressource for .properties file

You can create an URL ressource. It would link to a .properties file set on a local host or any server, so each property could be set to a different value depending on the environment.

For instance, JNDI name url/environmentJndiName with value file:///server1/environment.properties on server1, and file:///server2/environment.properties on server2.

Then on server 1, you could set docStorageLocation=value1 in the environment.properties file, docStorageLocation=value2 on server2.

In your deployment descriptor web.xml, the ressource's reference would be the same. You wouldn't have to change this reference in the java source :

   <resource-ref>
         <res-ref-name>url/environment</res-ref-name>
         <res-type>java.net.URL</res-type>
         <res-auth>Application</res-auth>
         <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>

Then use this ressource to read the properties.

try {
    Context l_ctx = new InitialContext();
    URL l_url = (URL) l_ctx.lookup("java:comp/env/url/environment");
    // New properties
    Properties l_properties = new Properties();
    // Load properties
    this.loadProps(l_properties, l_url.getPath());

} catch (NamingException e1) {
    // TODO
    e1.printStackTrace();
} catch (IOException e) {
    // TODO Bloc catch auto-généré
    e.printStackTrace();
}

    ...

private void loadProps(final Properties p_properties, final String p_fileLocation)
    throws IOException
{
    // Open stream
    BufferedInputStream l_is = new BufferedInputStream(
            new FileInputStream(
                    new File(p_fileLocation)));

    p_properties.load(l_is);
    // Close stream
    l_is.close();
}

You will need to bind the ressource reference url/environment of the web.xml to the JNDI name url/environmentJndiName set for this ressource on the websphere server. Modify the ibm-web-bnd.xml file with websphere, the sun-web.xml file with glassfish, etc.

THANKS

If there is a better solution, or if it does not answer the question, let me know. I am still learning but I have been working with websphere for a while - even if I prefer other solutions. Thanks, @+.

Upvotes: 7

Related Questions