Search code examples
wso2wso2-cepwso2-as

How to get value of 'CARBON_HOME' in java code


I was trying to implement an Axis2 service that receives user requests and publishes them as events to a CEP using carbon databridge thrift (via 'org.wso2.carbon.databridge.agent.thrift.DataPublisher')

I followed the code sample provided in wso2cep-3.1.0/samples/producers/activity-monitor

please see the following code snippet

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; 

            }

there is also a utility class that set the key store parameters as following

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

    }
}

I uploaded the service into a wso2as-5.2.1, and called the service using SOAPUI

the request returned an error message "cannot borrow client for TCP"

I debug, and found out the problem might lies with the class 'KeyStoreUtil',

where the 'filePath' somehow retuned a 'null',

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

and caused the failure on this line

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

I guess it could be a better idea if I use the value of "CARBON_HOME" to figure out the location of Key Store

so my question is :

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

that said. If you think a bit more: the service will be called numerous time; whileas the 'setTrustStoreParams' and the 'setKeyStoreParams' will only be needed to executed once at the server/service initiate.

So, are there any even better ways to remove 'setTrustStoreParams' and 'setKeyStoreParams' out of the service code, or implement as configurable items?

Please advise

thanks


Solution

  • so my question is :

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

    You should use the property carbon.home like following which will retrieve the WSO2 product's home directory.

    System.getProperty("carbon.home");