views:

158

answers:

3

Hi all,

I would like a web application to learn things about its environment so that it can describe various details when an exception occurs.

How would I get the ip address(es) that glassfish is running on within the web application?

I would like to also take a snapshot of the configuration, JDBC connections, JMS, JNDI, etc. Is this possible?

I guess this would be more related to the API of glassfish and injecting a bean or mbean.

Walter

A: 

How would I get the ip address(es) that glassfish is running on within the web application?

You can get such information in ServletRequest, e.g. the port.

I would like to also take a snapshot of the configuration, JDBC connections, JMS, JNDI, etc. Is this possible?

You will need to rely on the metadata of the connection or the connection factory. If fear this is not entirely portable, but you should be able to get some information, e.g. the JDBC metadata.

You can also connect the JMX beans of Glassfish. I did that once from a web app to monitor the connection that were allocated over time. If you know the name of the JMX beans to look up (e.g. if you know the name of the data source) you can get a wagon on information there. Basically everything that the native Glassfish console display is there.

You could maybe gather all the information at startup-up withing a ServletContextListener and then store them somewhere to spit them out in case of an exception.

Note that you can disclose potential sensitive information to the end-user, which is not a recommended practice.

ewernli
True, but what if the server is multi-homed? Can I get what it is configured to listen on?
You mean you don't know in which Glassfish domain your app is deployed?
ewernli
Right, I am deploying an application to a cluster and want the web application to know where it is. How can I tell it where it is, or have it figure that out?
I guess the IP can be obtained as usual with `InetAddress`, and all the other information can be looked up as described on each node of the cluster. I don't understand exactly what you mean by "where it is"?
ewernli
A: 

How would I get the ip address(es) that glassfish is running on within the web application?

Use ServletRequest#getLocalPort() for that (not ServletRequest#getServerPort() which actually returns the port number to which the request was sent, most often 80, and might be different from the port the container is listening to due to routing/load balancing).

I would like to also take a snapshot of the configuration, JDBC connections, JMS, JNDI, etc. Is this possible?

Well, you can maybe browse the JNDI tree to see what is in there but your application is not supposed to be aware of all the details (understand: I don't think you'll be able to access all metadata, at least not this way).

Another option would be to connect to GlassFish's exposed administrative MBeans via JMX. But this will require the admin credentials (and might thus not be possible).

Maybe you should clarify your needs and constraints to get more precise answers.

Pascal Thivent
A: 

I do the following:

  1. find all network interfaces on the machine
  2. record JVM information

This gives me enough information to determine where a problem came from.

Walter