1

At the top of the WebSphere log file, I see a couple of lines:

WebSphere Platform 8.5 blah blah running with process name abc\xyz\pqr and process id 1234
Full server name is abc\xyz\pqr-1234

I would like to get the value pqr shown in the above two lines using Java code in my application that runs on the WebSphere server. I found that I could get the values abc and xyz by doing JNDI lookup, based on this answer to another question:

(new InitialContext()).lookup("thisNode/cell/cellname").toString();    // returns "abc"
(new InitialContext()).lookup("thisNode/nodename").toString();     // returns "xyz"

However, JNDI lookup of "servername" does not return pqr or any of the values above, but something else entirely.

How can I get the value pqr (or the entire value abc\xyz\pqr or abc\xyz\pqr-1234, whichever is possible)? I would prefer to get the value by doing a JNDI lookup rather than by using a WebSphere class like com.ibm.websphere.runtime.ServerName as mentioned here, but if that is not possible I can use any solution that works.

I realize there may be questions about why I need to get the value and perhaps even opinions that it may not be a good practice to get that value etc. However, I have a valid and unavoidable reason for doing that.

CrankyShaft
  • 111
  • 1

2 Answers2

0

Here is a link to a document about how to capture a WebSphere namespace dump, including example output, showing entries such as,

(top)/nodes/outpost/nodename 
(top)/nodes/outpost/servers/server1/servername

Have you tried a look up of the following?

thisNode/servers/thisServer/servername
njr
  • 3,399
  • 9
  • 7
  • JNDI lookup of `thisNode/servers/thisServer/servername` results in a NamingException. Ultimately I had to use the strategy described in [this answer](https://stackoverflow.com/a/10629627/14697249) to another question. – CrankyShaft Nov 28 '20 at 16:34
0

Well this answer is not a JNDI solution, however it is a solution to this problem. WebSphere provides class com.ibm.websphere.runtime.ServerName which is used for exactly this scenario. It has bunch of utility methods like:

  • getDisplayName()
  • getServerId()
  • getFullName()

So how to use this class in your project while still being able to deploy project on a non-websphere environments? By checking in runtime if you are running within WebSphere, and if you do, than invoking methods within ServerName.

In order to not pollute your project with unnecessary dependencies to was runtime create a new utility jar project and add dependencies:

  • com.ibm.ws.runtime-xxxx.jar as provided dependency (part of was or was client)
  • spring-core-xxxx.jar as runtime dependency

Rest of the solutions are in following two methods withing two classes. One which checks for presence of websphere and other which interacts with it:

import org.springframework.util.ClassUtils;

public class WasInfo {

    /** 
     * @return a map populated with relevant WebSphere names
     *         if running on WebSphere or empty one if not
     */
    public Map<String, String> about() {
        ClassLoader currentClassLoader = this.getClass().getClassLoader();
        boolean isWebsphere = ClassUtils.isPresent("com.ibm.websphere.runtime.ServerName", currentClassLoader);
        if (!isWebsphere ) {
            return new HashMap<>();
        }
        
        WebSphereConfig wc = new WebSphereConfig();
        return wc.resolveServerName();
    }       
}


import com.ibm.websphere.runtime.ServerName;

public class WebSphereConfig {      
    public Map<String, String> resolveServerName() {
        // expecting 'cell/node/server' pattern     
        String serverFullName = ServerName.getFullName();
        String serverName = ServerName.getDisplayName();        
        
        Map<String, String> map = new HashMap<>();
        map.put("serverFullName", serverFullName);
        map.put("serverName", serverName);      
        
        String[] segments = serverFullName.split("\\\\");
        if (segments.length == 3) {
            map.put("cellName", segments[0]);
            map.put("nodeName", segments[1]);
        }
    }   
}

I used Spring's ClassUtils to get rid of some boring code in this example. And for exercise one could invoke ServerName methods using reflections. That would remove a need for import ServerName statement and make code even more "simpler". But idea would remain the same.

Talijanac
  • 1,087
  • 9
  • 20