2

I'm googling but I find difficulties in finding translate for the strings to pass to the config.getServletContext().getAttribute() method in the next two lines. These lines are from an application deployed on WAS 8.5.

String applicationName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.enterprise.application.name");
String serverName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.application.host");

I'm working/studying now on WAS Liberty.

Maybe would be even better a link to a reference for all the possible attributes for Liberty (if exists...)

EDIT

Sorry, I could not test it before because other errors in application blocking the execution, but it turns that those strings to pass to getAttribute method are valid even on my local Liberty test server, so:

String applicationName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.enterprise.application.name");
String serverName = (String)config.getServletContext().getAttribute("com.ibm.websphere.servlet.application.host");

is valid on WebSphere 8.5 and ALSO on Liberty.

@Andy Guibert methods are also ok to retrieve AppName and HostName, even in more general manner.

Anyway I'd like to go deeper in this topic, and I'd like to find documentation on which are all the possible strings I could pass to (String)config.getServletContext().getAttribute("") in order to retrieve information in a IBM environment, especially Liberty (if there is any difference against classic Websphere)...

But I can't find it...

Falco
  • 1,458
  • 3
  • 19
  • 47
  • java doesn't have the concept of global variables. You can create public static final variables, though. – Stultuske Feb 10 '20 at 13:50
  • 1
    @Falco for your follow up question on what values you can pass to `(String)config.getServletContext().getAttribute("")`, you can check for yourself by calling `config.getServletContext().getAttributeNames()` – Andy Guibert Feb 13 '20 at 20:05

1 Answers1

1

An easy/standard way to obtain the application name in a JavaEE application is using this builtin JNDI name:

import javax.naming.InitialContext;
// ...

String appName = InitialContext.doLookup("java:app/AppName");

To obtain the hostname, you can probably just use a JavaSE API for this:

InetAddress.getLocalHost().getHostName()

You can also defined and lookup any arbitrary variables in server.xml and then look them up with MicroProfile config like so:

@Inject
@ConfigProperty(name = "foo", defaultValue = "bar")
String fooProperty;

Also, here is a guide for MicroProfile Config for Liberty: https://openliberty.io/guides/microprofile-config-intro.html

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61