2015-09-01 12 views
1

我試圖實現一個Axis2服務,它接收用戶請求並將它們作爲事件發佈到使用碳數據橋節點的CEP(通過'org.wso2.carbon.databridge.agent .thrift.DataPublisher')如何在java代碼中獲取'CARBON_HOME'的值

我遵循wso2cep-3.1.0 /樣品/生產者/活動監視器提供的代碼示例

請參閱下面的代碼段

public class GatewayServiceSkeleton{ 

     private static Logger logger = Logger.getLogger(GatewayServiceSkeleton.class); 


     public RequestResponse request(Request request)throws AgentException, 
        MalformedStreamDefinitionException,StreamDefinitionException, 
        DifferentStreamDefinitionAlreadyDefinedException, 
        MalformedURLException,AuthenticationException,DataBridgeException, 
        NoStreamDefinitionExistException,TransportException, SocketException, 
        org.wso2.carbon.databridge.commons.exception.AuthenticationException 
      { 

       final String GATEWAY_SERVICE_STREAM = "gateway.cep"; 
       final String VERSION = "1.0.0"; 
       final String PROTOCOL = "tcp://"; 
       final String CEPHOST = "cep.gubnoi.com"; 
       final String CEPPORT = "7611"; 
       final String CEPUSERNAME = "admin"; 
       final String CEPPASSWORD = "admin"; 

       Object[] metadata = { request.getDeviceID(), request.getViewID()}; 
       Object[] correlationdata = { request.getSessionID()}; 
       Object[] payloaddata = {request.getBucket()}; 


       KeyStoreUtil.setTrustStoreParams(); 

       KeyStoreUtil.setKeyStoreParams(); 


       DataPublisher dataPublisher = new DataPublisher(PROTOCOL + CEPHOST + ":" + CEPPORT, CEPUSERNAME, CEPPASSWORD); 

       //create event 
       Event event = new Event (GATEWAY_SERVICE_STREAM + ":" + VERSION, System.currentTimeMillis(), metadata, correlationdata, payloaddata); 

       //Publish event for a valid stream 

       dataPublisher.publish(event); 
       //stop 
       dataPublisher.stop(); 


       RequestResponse response = new RequestResponse(); 
       response.setSessionID(request.getSessionID()); 
       response.setDeviceID(request.getDeviceID()); 
       response.setViewID(request.getViewID()); 
       response.setBucket(request.getBucket()); 
       return response; 

      } 

也有一個實用程序類,它將密鑰存儲參數設置爲如下

public class KeyStoreUtil { 

    static File filePath = new File("../../../repository/resources/security"); 

    public static void setTrustStoreParams() { 
     String trustStore = filePath.getAbsolutePath(); 
     System.setProperty("javax.net.ssl.trustStore", trustStore + "/client-truststore.jks"); 
     System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); 

    } 

    public static void setKeyStoreParams() { 
     String keyStore = filePath.getAbsolutePath(); 
     System.setProperty("Security.KeyStore.Location", keyStore + "/wso2carbon.jks"); 
     System.setProperty("Security.KeyStore.Password", "wso2carbon"); 

    } 
} 

我上傳的服務到wso2as-5.2.1,和稱爲使用SOAPUI

請求返回錯誤消息服務「不能借客戶端TCP」

本人調試,並發現了問題可能出在類的KeyStoreUtil',

在「文件路徑」不知何故重新調整的一個「空」,

static File filePath = new File("../../../repository/resources/security"); 

,並導致失敗在這條線

DataPublisher dataPublisher = new DataPublisher(PROTOCOL + CEPHOST + ":" + CEPPORT, CEPUSERNAME, CEPPASSWORD); 

我想這可能是一個更好的主意,如果我用「CARBON_HOME」的價值要弄清楚的密鑰存儲

所以我的問題是位置:

我怎麼能夠在Java代碼中獲得'CARBON_HOME'的值?

那說的。如果你想多一點: 該服務將被稱爲無數次;而'setTrustStoreParams'和'setKeyStoreParams'只需要在服務器/服務啓動時執行一次。

那麼,有沒有更好的方法從服務代碼中刪除'setTrustStoreParams'和'setKeyStoreParams',或者將其實現爲可配置項?

請告知

感謝

回答

1

所以我的問題是:

我怎麼可能能夠在Java代碼來獲得 'CARBON_HOME' 的價值?

您應該使用屬性carbon.home,如下所示,它將檢索WSO2產品的主目錄。

System.getProperty("carbon.home"); 
相關問題