如何在 java 代码中获取 'CARBON_HOME' 的值

How to get value of 'CARBON_HOME' in java code

我试图实现一个 Axis2 服务,该服务接收用户请求并将它们作为事件发布到使用碳数据桥节俭的 CEP(通过 'org.wso2.carbon.databridge.agent.thrift.DataPublisher')

我遵循了 wso2cep-3.1.0/samples/producers/activity-monitor

中提供的代码示例

请看下面的代码片段

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; 

            }

还有一个实用程序 class 可以将密钥存储参数设置如下

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 调用服务

请求返回错误信息"cannot borrow client for TCP"

我调试了一下,发现问题可能出在class 'KeyStoreUtil',

其中 'filePath' 以某种方式重新调整了 'null',

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

并导致此行失败

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

我想如果我使用 "CARBON_HOME" 的值来确定密钥库的位置

可能会更好

所以我的问题是:

如何在Java代码中获取'CARBON_HOME'的值?

就是这么说的。如果你想多一点: 该服务将被多次调用;而 'setTrustStoreParams' 和 'setKeyStoreParams' 只需要在 server/service 启动时执行一次。

那么,是否有更好的方法将 'setTrustStoreParams' 和 'setKeyStoreParams' 从服务代码中移除,或者作为可配置项实施?

请指教

谢谢

so my question is :

How may I be able to get the value of 'CARBON_HOME' in the Java code?

您应该像下面这样使用 属性 carbon.home 来检索 WSO2 产品的主目录。

System.getProperty("carbon.home");