com.netcore.hsmart.AppConstants.java Source code

Java tutorial

Introduction

Here is the source code for com.netcore.hsmart.AppConstants.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netcore.hsmart;

import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hazelcast.core.*;
import com.hazelcast.config.*;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;

import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.api.async.RedisAsyncCommands;
import com.lambdaworks.redis.api.async.RedisSortedSetAsyncCommands;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author root
 */
public class AppConstants {

    private static String WRITE_DB_HOST = null;
    private static String WRITE_DB_USERNAME = null;
    private static String WRITE_DB_PASSWORD = null;
    private static String WRITE_DB_PORT = null;
    private static String WRITE_DB_NAME = null;
    private static String WRITE_DB_DIALECT = null;
    private static String WRITE_DB_DRIVER_CLASS = null;
    private static String WRITE_DB_POOL_MIN_SIZE = null;
    private static String WRITE_DB_POOL_MAX_SIZE = null;
    private static String WRITE_DB_TIMEOUT = null;
    private static String WRITE_DB_AUTO_COMMIT = null;
    private static String WRITE_DB_RELEASE_MODE = null;
    private static String WRITE_DB_IDLE_TESTS_PERIOD = null;
    private static String WRITE_DB_SHOW_SQL = null;
    private static String WRITE_DB_CTX_CLASS = null;
    private static String WRITE_DB_JDBC_TYPE = null;

    private static String RABBITMQ_HOST = null;
    private static String RABBITMQ_USERNAME = null;
    private static String RABBITMQ_PASSWORD = null;
    private static String RABBITMQ_PORT = null;

    private static String RABBITMQ_SMSRECV_EX = null;
    private static String RABBITMQ_DLRRECV_EX = null;
    private static String RABBITMQ_SMSDATA_EX = null;
    private static String RABBITMQ_DLRDATA_EX = null;
    private static String RABBITMQ_DLRPULLSERVICE_EX = null;

    private static String RABBITMQ_SMSRECV_Q_PREFIX = null;
    private static String RABBITMQ_DLRRECV_Q_PREFIX = null;
    private static String RABBITMQ_SMSDATA_Q_PREFIX = null;
    private static String RABBITMQ_DLRDATA_Q_PREFIX = null;
    private static String RABBITMQ_DLRPULLSERVICE_Q_PREFIX = null;

    private static String LOGGING_PATH = null;
    private static String LOOGING_ROOT_FILE = null;
    private static String LOGGING_SMSRECV_LOGFILE = null;
    private static String LOGGING_DLRRECV_LOGFILE = null;

    private static String HTTP_PROTOCOL = null;

    private static String SERVER_SMSRECV_HOST = null;
    private static String SERVER_SMSRECV_PORT = null;

    private static String SERVER_DLRRECV_HOST = null;
    private static String SERVER_DLRRECV_PORT = null;

    private static String REDIS_HOST = null;
    private static String REDIS_PASSWORD = null;
    private static String REDIS_PORT = null;

    private static String DM_DLR_API = null;
    private static List<Object> DM_GATEWAYS = new ArrayList<>();

    private static XMLConfiguration config = null;

    private static XMLConfiguration statusCodeConfig = null;

    private static RedisSortedSetAsyncCommands<String, String> redisConnection = null;

    private static ConnectionFactory rabbitMqFactory = null;
    private static Connection rabbitMqConnection = null;

    private static String PAYLOAD_GROUP_SEPARATOR = null;

    private static String PAYLOAD_KV_SEPARATOR = null;
    private static List<Object> HAZELCAST_MEMBERS = new ArrayList<>();
    private static HazelcastInstance hazelClient = null;
    private static final String hazelcastCounterString = "REF_ID";
    private static IAtomicLong hazelcastRefIdCounter = null;

    private static String RABBITMQ_DM_DLR_CALL_EX = null;
    private static String RABBITMQ_DM_DLR_CALL_Q = null;

    private AppConstants() {

    }

    private static void setSystemProperties() {
        System.setProperty("log_folder_path", AppConstants.LOGGING_PATH);
        System.setProperty("root_logfile", AppConstants.LOOGING_ROOT_FILE);
        System.setProperty("smsrecv_logfile", AppConstants.LOGGING_SMSRECV_LOGFILE);
        System.setProperty("dlrrecv_logfile", AppConstants.LOGGING_DLRRECV_LOGFILE);
        System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
        //System.setProperty("hazelcast.client.statistics.enabled", "true");

    }

    @SuppressWarnings("deprecation")
    public static void loadAppConfig() throws ConfigurationException {

        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(
                XMLConfiguration.class)
                        .configure(params.xml().setBasePath(System.getProperty("hsmart-config-path"))
                                .setFileName("application.xml").setExpressionEngine(new XPathExpressionEngine()));

        AppConstants.config = builder.getConfiguration();

        AppConstants.HTTP_PROTOCOL = AppConstants.config.getString("appservers/protocol");
        AppConstants.SERVER_SMSRECV_HOST = AppConstants.config.getString("appservers/server[@type='SMSRECV']/host");
        AppConstants.SERVER_SMSRECV_PORT = AppConstants.config.getString("appservers/server[@type='SMSRECV']/port");

        AppConstants.SERVER_DLRRECV_HOST = AppConstants.config.getString("appservers/server[@type='DLRRECV']/host");
        AppConstants.SERVER_DLRRECV_PORT = AppConstants.config.getString("appservers/server[@type='DLRRECV']/port");

        AppConstants.WRITE_DB_JDBC_TYPE = AppConstants.config
                .getString("databases/database[@type='WRITE']/jdbc_type");
        AppConstants.WRITE_DB_HOST = AppConstants.config.getString("databases/database[@type='WRITE']/host");
        AppConstants.WRITE_DB_USERNAME = AppConstants.config
                .getString("databases/database[@type='WRITE']/username");
        AppConstants.WRITE_DB_PASSWORD = AppConstants.config
                .getString("databases/database[@type='WRITE']/password");
        AppConstants.WRITE_DB_PORT = AppConstants.config.getString("databases/database[@type='WRITE']/port");
        AppConstants.WRITE_DB_NAME = AppConstants.config.getString("databases/database[@type='WRITE']/dbname");
        AppConstants.WRITE_DB_DIALECT = AppConstants.config.getString("databases/database[@type='WRITE']/dialect");
        AppConstants.WRITE_DB_DRIVER_CLASS = AppConstants.config
                .getString("databases/database[@type='WRITE']/driver_class");
        AppConstants.WRITE_DB_POOL_MIN_SIZE = AppConstants.config
                .getString("databases/database[@type='WRITE']/c3p0_min_size");
        AppConstants.WRITE_DB_POOL_MAX_SIZE = AppConstants.config
                .getString("databases/database[@type='WRITE']/c3p0_max_size");
        AppConstants.WRITE_DB_AUTO_COMMIT = AppConstants.config
                .getString("databases/database[@type='WRITE']/autocommit");
        AppConstants.WRITE_DB_TIMEOUT = AppConstants.config
                .getString("databases/database[@type='WRITE']/c3p0_timeout");
        AppConstants.WRITE_DB_RELEASE_MODE = AppConstants.config
                .getString("databases/database[@type='WRITE']/release_mode");
        AppConstants.WRITE_DB_IDLE_TESTS_PERIOD = AppConstants.config
                .getString("databases/database[@type='WRITE']/idle_test_periods");

        AppConstants.WRITE_DB_SHOW_SQL = AppConstants.config
                .getString("databases/database[@type='WRITE']/show_sql");
        AppConstants.WRITE_DB_CTX_CLASS = AppConstants.config
                .getString("databases/database[@type='WRITE']/current_session_context_class");

        AppConstants.RABBITMQ_HOST = AppConstants.config.getString("rabbitmq/host");
        AppConstants.RABBITMQ_USERNAME = AppConstants.config.getString("rabbitmq/username");
        AppConstants.RABBITMQ_PASSWORD = AppConstants.config.getString("rabbitmq/password");
        AppConstants.RABBITMQ_PORT = AppConstants.config.getString("rabbitmq/port");

        AppConstants.RABBITMQ_SMSRECV_EX = AppConstants.config.getString("rabbitmq/exchanges/smsrecv_ex");
        AppConstants.RABBITMQ_DLRRECV_EX = AppConstants.config.getString("rabbitmq/exchanges/dlrrecv_ex");
        AppConstants.RABBITMQ_SMSDATA_EX = AppConstants.config.getString("rabbitmq/exchanges/smsdata_ex");
        AppConstants.RABBITMQ_DLRDATA_EX = AppConstants.config.getString("rabbitmq/exchanges/dlrdata_ex");
        AppConstants.RABBITMQ_DLRPULLSERVICE_EX = AppConstants.config
                .getString("rabbitmq/exchanges/dlrpullservice_ex");

        AppConstants.RABBITMQ_SMSRECV_Q_PREFIX = AppConstants.config.getString("rabbitmq/queues/smsrecv_q_prefix");
        AppConstants.RABBITMQ_DLRRECV_Q_PREFIX = AppConstants.config.getString("rabbitmq/queues/dlrrecv_q_prefix");
        AppConstants.RABBITMQ_SMSDATA_Q_PREFIX = AppConstants.config.getString("rabbitmq/queues/smsdata_q_prefix");
        AppConstants.RABBITMQ_DLRDATA_Q_PREFIX = AppConstants.config.getString("rabbitmq/queues/dlrdata_q_prefix");
        AppConstants.RABBITMQ_DLRPULLSERVICE_Q_PREFIX = AppConstants.config
                .getString("rabbitmq/queues/dlrpullservice_q_prefix");

        AppConstants.LOGGING_PATH = AppConstants.config.getString("logging/path");
        AppConstants.LOOGING_ROOT_FILE = AppConstants.config.getString("logging/root_log");
        AppConstants.LOGGING_SMSRECV_LOGFILE = AppConstants.config.getString("logging/smsrecv_logfile");
        AppConstants.LOGGING_DLRRECV_LOGFILE = AppConstants.config.getString("logging/dlrrecv_logfile");

        AppConstants.REDIS_HOST = AppConstants.config.getString("redis/host");
        AppConstants.REDIS_PASSWORD = AppConstants.config.getString("redis/password");
        AppConstants.REDIS_PORT = AppConstants.config.getString("redis/port");

        AppConstants.DM_DLR_API = AppConstants.config.getString("dm/return_api");
        AppConstants.DM_GATEWAYS = AppConstants.config.getList("dm/gateways/id");

        AppConstants.PAYLOAD_GROUP_SEPARATOR = AppConstants.config
                .getString("misc/separators/payload/group_separator");
        AppConstants.PAYLOAD_KV_SEPARATOR = AppConstants.config
                .getString("misc/separators/payload/key_val_separator");

        AppConstants.HAZELCAST_MEMBERS = AppConstants.config.getList("hazelcast/members/member");

        AppConstants.RABBITMQ_DM_DLR_CALL_EX = AppConstants.config.getString("rabbitmq/exchanges/dmdlrcall_ex");
        AppConstants.RABBITMQ_DM_DLR_CALL_Q = AppConstants.config.getString("rabbitmq/queues/dmdlrcall_q");

        AppConstants.setSystemProperties();

        final Logger logger = LoggerFactory.getLogger(AppConstants.class);

        ClientConfig clientConfig = new ClientConfig();
        for (int i = 0; i < HAZELCAST_MEMBERS.size(); i++) {
            clientConfig.addAddress(HAZELCAST_MEMBERS.get(i).toString());
        }

        hazelClient = HazelcastClient.newHazelcastClient(clientConfig);
        hazelcastRefIdCounter = hazelClient.getAtomicLong(AppConstants.hazelcastCounterString);

        AppConstants.loadStatusCodeConfig();

        try {

            // initializeDBConnection();
            // initializeRedisConnection();
            initializeRabbitMqConnection();
            logger.info(AppConstants.getApplicationCodeMessage("HSMART_GEN_1006"));

        } catch (Exception cex) {
            logger.error("HSMART application initialization error");
            logger.error("Error:" + cex.getMessage());

        }

    }

    /**
     * Redis initiation
     */
    public static void initializeRedisConnection() {
        final Logger logger = LoggerFactory.getLogger(AppConstants.class);

        RedisClient client = RedisClient.create("redis://" + AppConstants.REDIS_PASSWORD + "@"
                + AppConstants.REDIS_HOST + ":" + AppConstants.REDIS_PORT + "/0");

        try {
            AppConstants.redisConnection = client.connect().async();

        } catch (Exception e) {
            logger.debug(e.getMessage());

        }
    }

    /**
     * RabbitMQ
     */
    public static void initializeRabbitMqConnection() {
        final Logger logger = LoggerFactory.getLogger(AppConstants.class);

        AppConstants.rabbitMqFactory = new ConnectionFactory();
        rabbitMqFactory.setHost(AppConstants.RABBITMQ_HOST);
        rabbitMqFactory.setPort(Integer.parseInt(AppConstants.RABBITMQ_PORT));
        rabbitMqFactory.setUsername(AppConstants.RABBITMQ_USERNAME);
        rabbitMqFactory.setPassword(AppConstants.RABBITMQ_PASSWORD);
        rabbitMqFactory.setNetworkRecoveryInterval(5000);
        rabbitMqFactory.setAutomaticRecoveryEnabled(true);
        try {
            rabbitMqConnection = rabbitMqFactory.newConnection();
        } catch (IOException | TimeoutException e) {
            // TODO Auto-generated catch block
            logger.error("RABBITMQ_CONN_ERROR:" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * This will load the code constants from application_status_codes.xml and
     * will push in redis key name: xml_node key value: xml node object
     */
    private static void loadStatusCodeConfig() {
        final Logger logger = LoggerFactory.getLogger(AppConstants.class);
        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(
                XMLConfiguration.class)
                        .configure(params.xml().setBasePath(System.getProperty("hsmart-config-path"))
                                .setFileName("application_status_codes.xml")
                                .setExpressionEngine(new XPathExpressionEngine()));

        try {
            AppConstants.statusCodeConfig = builder.getConfiguration();

            logger.info(AppConstants.getApplicationCodeMessage("HSMART_GEN_1007"));
            // AppConstants.GEN_STATUS_CODES =
            // AppConstants.statusCodeConfig.getList("general/data");

            // for (Object codes : AppConstants.GEN_STATUS_CODES) {
            // System.out.println(codes);
            // }
            // AppConstants.APP_ERR_CODES =
            // AppConstants.statusCodeConfig.getList("general/internal_app_error_codes/data");
            // AppConstants.DLR_STATUS_CODES =
            // AppConstants.statusCodeConfig.getList("general/dlr_status_codes/data");
        } catch (ConfigurationException cex) {
            // loading of the configuration file failed
        }

    }

    /**
     *
     * @param code
     * @return String
     */
    public static String getApplicationCodeMessage(String code) {

        if (code == null || code.equals("")) {
            return AppConstants.statusCodeConfig.getString("data[@code='HSMART_GEN_8888']");
        }

        return AppConstants.statusCodeConfig.getString("data[@code='" + code + "']");

    }

    public static Integer getApplicationCodeHttpStatus(String code) {

        return Integer.parseInt(AppConstants.statusCodeConfig.getString("data[@code='" + code + "']/@http_status"));

    }

    /**
     * Returns the SMS receiver exchange name
     *
     * @return String
     */
    public static String getSmsReceiverExchangeName() {
        return AppConstants.RABBITMQ_SMSRECV_EX;
    }

    /**
     * Returns the DLR receiver exchange name
     *
     * @return String
     */
    public static String getDlrReceiverExchangeName() {
        return AppConstants.RABBITMQ_DLRRECV_EX;
    }

    /**
     * Returns the SMS receiver queue prefix string
     *
     * @return String
     */
    public static String getSmsReceiverQueuePrefix() {
        return AppConstants.RABBITMQ_SMSRECV_Q_PREFIX;
    }

    /**
     * Returns the DLR receiver queue prefix string
     *
     * @return String
     */
    public static String getDlrReceiverQueuePrefix() {
        return AppConstants.RABBITMQ_DLRRECV_Q_PREFIX;
    }

    /**
     * Returns the host of HSMART SMS receiver server
     *
     * @return String
     */
    public static String getSmsrecvServerHost() {
        return AppConstants.SERVER_SMSRECV_HOST;
    }

    /**
     * Returns the port number of HSMART SMS receiver server
     *
     * @return String
     */
    public static String getSmsrecvServerPort() {
        return AppConstants.SERVER_SMSRECV_PORT;
    }

    /**
     * Returns the host of HSMART SMS receiver server
     *
     * @return String
     */
    public static String getDlrrecvServerHost() {
        return AppConstants.SERVER_DLRRECV_HOST;
    }

    /**
     * Returns the port number of HSMART SMS receiver server
     *
     * @return String
     */
    public static String getDlrrecvServerPort() {
        return AppConstants.SERVER_DLRRECV_PORT;
    }

    /**
     * Returns the HTTP protocol used to the servers
     *
     * @return String
     */
    public static String getHttpProtocol() {
        return AppConstants.HTTP_PROTOCOL;
    }

    public static RedisSortedSetAsyncCommands<String, String> getRedisConnObject() {
        return AppConstants.redisConnection;
    }

    public static ConnectionFactory getRabbitMqObject() {

        return rabbitMqFactory;
    }

    /**
     * Returns the DLR API to return DLR data from vendors
     *
     * @return String
     */
    public static List<Object> getGateways() {
        return AppConstants.DM_GATEWAYS;
    }

    public static String getPayloadGroupSeparator() {
        return AppConstants.PAYLOAD_GROUP_SEPARATOR;
    }

    public static String getPayloadKVSeparator() {
        return AppConstants.PAYLOAD_KV_SEPARATOR;
    }

    public static void test(String gatewayId) {
        String ss1 = AppConstants.statusCodeConfig.getString("data[@code='" + gatewayId + "']");
        String ss2 = AppConstants.statusCodeConfig.getString("data[@code='" + gatewayId + "']/@http_status");
        // List<String> myList = IteratorUtils.toList(ss);
        System.out.println("properties:" + ss1 + "---" + ss2);
        // String[] dd = new String[2];
        //
        // if (ss.hasNext()) {
        // String d = "";
        // while (ss.hasNext()) {
        // Object a = ss.next();
        // d = AppConstants.config.getString(a.toString());
        // System.out.println(d);
        // }
        // System.out.println(d);
        // } else {
        // System.out.println("Invalid");
        // }
    }

    public static String getConfigString(String s) {
        return AppConstants.config.getString(s);
    }

    public static String getDmDlrReturnApi() {
        return AppConstants.DM_DLR_API;
    }

    public static String getWriteDbHost() {
        return AppConstants.WRITE_DB_HOST;
    }

    public static String getWriteDbUsername() {
        return AppConstants.WRITE_DB_USERNAME;
    }

    public static String getWriteDbPassword() {
        return AppConstants.WRITE_DB_PASSWORD;
    }

    public static String getWriteDbPort() {
        return AppConstants.WRITE_DB_PORT;
    }

    public static String getWriteDbName() {
        return AppConstants.WRITE_DB_NAME;
    }

    public static String getWriteDbDialect() {
        return AppConstants.WRITE_DB_DIALECT;
    }

    public static String getWriteDbDriverClass() {
        return AppConstants.WRITE_DB_DRIVER_CLASS;
    }

    public static String getWriteDbPoolMinSize() {
        return AppConstants.WRITE_DB_POOL_MIN_SIZE;
    }

    public static String getWriteDbPoolMaxSize() {
        return AppConstants.WRITE_DB_POOL_MAX_SIZE;
    }

    public static String getWriteDbShowSql() {
        return AppConstants.WRITE_DB_SHOW_SQL;
    }

    public static String getWriteDbCtxClass() {
        return AppConstants.WRITE_DB_CTX_CLASS;
    }

    public static String getWriteDbJdbcType() {
        return AppConstants.WRITE_DB_JDBC_TYPE;
    }

    public static String getWriteDbTimeout() {
        return AppConstants.WRITE_DB_TIMEOUT;
    }

    public static String getWriteDbAutoCommit() {
        return AppConstants.WRITE_DB_AUTO_COMMIT;
    }

    public static String getWriteDbReleaseMode() {
        return AppConstants.WRITE_DB_RELEASE_MODE;
    }

    public static String getWriteDbIdleTestsPeriod() {
        return AppConstants.WRITE_DB_IDLE_TESTS_PERIOD;
    }

    public static String getSmsDataExchangeName() {
        return AppConstants.RABBITMQ_SMSDATA_EX;
    }

    public static String getDlrDataExchangeName() {
        return AppConstants.RABBITMQ_DLRDATA_EX;
    }

    public static String getSmsDataQueuePrefix() {
        return AppConstants.RABBITMQ_SMSDATA_Q_PREFIX;
    }

    public static String getDlrDataQueuePrefix() {
        return AppConstants.RABBITMQ_DLRDATA_Q_PREFIX;
    }

    public static IAtomicLong refIdCounter() {
        return AppConstants.hazelcastRefIdCounter;
    }

    public static HazelcastInstance getHazelcastClient() {
        return AppConstants.hazelClient;
    }

    public static String getGatewayCode(String gatewayId) {
        return AppConstants.config.getString("dm/gateways/id[@name='" + gatewayId + "']/@encoded");
    }

    public static String getGatewayIdByCode(String enc) {
        return AppConstants.config.getString("dm/gateways/id[@encoded='" + enc + "']/@name");
    }

    public static Connection getRabbitMqConnection() {
        return rabbitMqConnection;
    }

    public static String getDlrPullServiceExchangeName() {
        return AppConstants.RABBITMQ_DLRPULLSERVICE_EX;
    }

    public static String getDlrPullServiceQueuePrefix() {
        return AppConstants.RABBITMQ_DLRPULLSERVICE_Q_PREFIX;
    }

    public static String getDlrCallBackHost(String gatewayId) {
        return AppConstants.config.getString("dlr_callback/id[@name='" + gatewayId + "']/@host");
    }

    public static String getDmDlrExchangeName() {
        return AppConstants.RABBITMQ_DM_DLR_CALL_EX;
    }

    public static String getDmDlrQueueName() {
        return AppConstants.RABBITMQ_DM_DLR_CALL_Q;
    }

}