2014-12-13 16 views

回答

1

是的,這是可能的。您可以使用Glassfish Admin REST API來檢索服務器的幾乎所有屬性。

對於http-listener-1使用以下URL的屬性:http://localhost:4848/management/domain/configs/config/server-config/network-config/network-listeners/network-listener/http-listener-1

一個正常的GET請求會給你一個HTML響應,Accept頭設置爲application/json來檢索JSON的響應。

在您的EJB中,您只需使用正確的標題向上面的url發送HTTP請求。這裏有一個例子:

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()); 

這會給你類似這樣的結果:

{ 
    "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" 
     } 
    } 
} 

正如你所看到的結果中包含(在這種情況下,8080)的端口。

您可以從響應字符串中手動解析值,也可以使用某個JSON庫將響應轉換爲可輕鬆檢索屬性的JSON對象。

如果您的Glassfish Admin界面不受保護,如果您啓用了安全管理,您可能必須發送授權參數與您的HTTP請求,那麼此過程應該可行。

參見: