I'm using Glassfish 3.1.2.2. Is it possible to determine http port number (http-listener-1) from java (EJB) code? I don't have any connections or requests, so reading ServletRequest is not an option.
Yes, this is possible. You can use the Glassfish Admin REST API to retrieve nearly every property of the server.
For the properties of the http-listener-1
use the following url: http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1
A normal GET request will give you a HTML response, set the Accept
header to application/json
to retrieve the response in JSON.
In your EJB you just have to issue an HTTP request to the url from above with the right header. Here is an example:
String url = "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Accept", "application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
This will give you a result similar to this:
{
"message": "",
"command": "http-listener-1",
"exit_code": "SUCCESS",
"extraProperties": {
"commands": [
{
"path": "create-ssl",
"method": "POST",
"command": "create-ssl"
}
],
"methods": [
{
"name": "GET"
},
{},
{
"messageParameters": {
"address": {
"defaultValue": "0.0.0.0",
"optional": "true",
"type": "string",
"key": "false"
},
"enabled": {
"defaultValue": "true",
"optional": "true",
"type": "boolean",
"key": "false"
},
"jkConfigurationFile": {
"defaultValue": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
"optional": "true",
"type": "string",
"key": "false"
},
"jkEnabled": {
"defaultValue": "false",
"optional": "true",
"type": "boolean",
"key": "false"
},
"name": {
"optional": "false",
"type": "string",
"key": "true"
},
"port": {
"optional": "false",
"type": "int",
"key": "false"
},
"protocol": {
"optional": "false",
"type": "string",
"key": "false"
},
"threadPool": {
"optional": "true",
"type": "string",
"key": "false"
},
"transport": {
"optional": "false",
"type": "string",
"key": "false"
}
},
"name": "POST"
},
{
"messageParameters": {
"target": {
"acceptableValues": "",
"defaultValue": "server",
"optional": "true",
"type": "string"
}
},
"name": "DELETE"
}
],
"entity": {
"address": "0.0.0.0",
"enabled": "true",
"jkConfigurationFile": "${com.sun.aas.instanceRoot}/config/glassfish-jk.properties",
"jkEnabled": "false",
"name": "http-listener-1",
"port": "8080",
"protocol": "http-listener-1",
"threadPool": "http-thread-pool",
"transport": "tcp"
},
"childResources": {
"find-http-protocol": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/find-http-protocol",
"property": "http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1/property"
}
}
}
As you can see the result contains the port (8080 in this case).
You can either parse the value manually from the response string or use some JSON library to convert the response to an JSON object from which you can easily retrieve the property.
This procedure should work if your Glassfish Admin interface is unprotected, if you have enabled secure administration you may have to send authorization parameters with your HTTP request.
See also: