From c1ce2d113fb54bdc241b3c363317214fd7045596 Mon Sep 17 00:00:00 2001 From: Isuru Wijesiri Date: Thu, 31 Aug 2023 00:29:00 +0530 Subject: [PATCH] Revamp configuration handling mechanism --- .../gateway/APIMgtGatewayConstants.java | 27 ---- .../transaction/TransactionCountHandler.java | 27 ++-- .../TransactionCounterConstants.java | 47 +++++++ .../transaction/config/APIMConfigFetcher.java | 124 ++++++++++++++++++ .../transaction/config/ConfigFetcher.java | 6 + .../transaction/config/MIConfigFetcher.java | 27 ++++ .../config/TransactionCounterConfig.java | 80 +++++++++++ .../consumer/TransactionRecordConsumer.java | 6 +- ...sactionCounterInitializationException.java | 19 +++ .../producer/TransactionRecordProducer.java | 6 +- .../transaction/record/TransactionRecord.java | 4 +- .../store/TransactionRecordStoreImpl.java | 8 +- .../util/TransactionCountConfig.java | 101 -------------- .../impl/internal/APIManagerComponent.java | 1 + 14 files changed, 333 insertions(+), 150 deletions(-) create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/ConfigFetcher.java create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/MIConfigFetcher.java create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java create mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/exception/TransactionCounterInitializationException.java delete mode 100644 components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/util/TransactionCountConfig.java diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/APIMgtGatewayConstants.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/APIMgtGatewayConstants.java index f796516dd43e..030a0c3f62cb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/APIMgtGatewayConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/APIMgtGatewayConstants.java @@ -188,32 +188,5 @@ public class APIMgtGatewayConstants { //This will be a reserved name for the synapse message context properties. public static final String ADDITIONAL_ANALYTICS_PROPS = "ADDITIONAL_ANALYTICS_PROPS_TO_PUBLISH"; - /** - * Constants for transaction counting - */ - public static final String IS_THERE_ASSOCIATED_INCOMING_REQUEST = "is_there_incoming_request"; - public static final String TRANSPORT_WS = "ws"; - public static final String TRANSPORT_WSS = "wss"; - public static final String TRANSACTION_COUNTER_CONFIG_ROOT = "APIGateway.TransactionCounter"; - public static final String TRANSACTION_COUNTER_PRODUCER_THREAD_POOL_SIZE = TRANSACTION_COUNTER_CONFIG_ROOT + - ".ProducerThreadPoolSize"; - public static final String TRANSACTION_COUNTER_QUEUE_SIZE = TRANSACTION_COUNTER_CONFIG_ROOT + ".QueueSize"; - public static final String TRANSACTION_COUNTER_STORE_CLASS = TRANSACTION_COUNTER_CONFIG_ROOT + ".StoreClass"; - public static final String TRANSACTION_COUNTER_MAX_TRANSACTION_COUNT = TRANSACTION_COUNTER_CONFIG_ROOT + - ".MaxTransactionCount"; - public static final String TRANSACTION_COUNTER_RECORD_INTERVAL = TRANSACTION_COUNTER_CONFIG_ROOT - + ".ProducerScheduledInterval"; - public static final String TRANSACTION_COUNTER_MAX_RETRY_COUNT = TRANSACTION_COUNTER_CONFIG_ROOT + ".MaxRetryCount"; - public static final String TRANSACTION_COUNTER_MIN_RETRY_COUNT = TRANSACTION_COUNTER_CONFIG_ROOT + ".MinRetryCount"; - public static final String TRANSACTION_COUNTER_MAX_TRANSACTION_RECORDS_PER_COMMIT = TRANSACTION_COUNTER_CONFIG_ROOT - + ".MaxBatchSize"; - public static final String TRANSACTION_COUNTER_CONSUMER_COMMIT_INTERVAL = TRANSACTION_COUNTER_CONFIG_ROOT - + ".PublisherScheduledInterval"; - public static final String TRANSACTION_COUNTER_SERVER_ID = TRANSACTION_COUNTER_CONFIG_ROOT + ".ServerID"; - public static final String TRANSACTION_COUNTER_SERVICE = TRANSACTION_COUNTER_CONFIG_ROOT + ".ServiceURL"; - public static final String TRANSACTION_COUNTER_SERVICE_USERNAME = TRANSACTION_COUNTER_CONFIG_ROOT - + ".ServiceUsername"; - public static final String TRANSACTION_COUNTER_SERVICE_PASSWORD = TRANSACTION_COUNTER_CONFIG_ROOT - + ".ServicePassword"; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java index 20bf7b4de99a..21899fc3c2bf 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java @@ -5,12 +5,12 @@ import org.apache.synapse.AbstractExtendedSynapseHandler; import org.apache.synapse.MessageContext; import org.apache.synapse.core.axis2.Axis2MessageContext; -import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; import org.wso2.carbon.apimgt.gateway.handlers.transaction.consumer.TransactionRecordConsumer; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.exception.TransactionCounterInitializationException; import org.wso2.carbon.apimgt.gateway.handlers.transaction.producer.TransactionRecordProducer; import org.wso2.carbon.apimgt.gateway.handlers.transaction.queue.TransactionRecordQueue; import org.wso2.carbon.apimgt.gateway.handlers.transaction.store.TransactionRecordStore; -import org.wso2.carbon.apimgt.gateway.handlers.transaction.util.TransactionCountConfig; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import java.lang.reflect.Constructor; import java.util.concurrent.*; @@ -30,11 +30,17 @@ public class TransactionCountHandler extends AbstractExtendedSynapseHandler { public TransactionCountHandler() { + try { + TransactionCounterConfig.init(); + } catch (TransactionCounterInitializationException e) { + throw new RuntimeException(e); + } + // Obtain config values - PRODUCER_THREAD_POOL_SIZE = TransactionCountConfig.getProducerThreadPoolSize(); - CONSUMER_COMMIT_INTERVAL = TransactionCountConfig.getConsumerCommitInterval(); - TRANSACTION_RECORD_QUEUE_SIZE = TransactionCountConfig.getTransactionRecordQueueSize(); - TRANSACTION_COUNT_STORE_CLASS = TransactionCountConfig.getTransactionCountStoreClass(); + PRODUCER_THREAD_POOL_SIZE = TransactionCounterConfig.getProducerThreadPoolSize(); + CONSUMER_COMMIT_INTERVAL = TransactionCounterConfig.getConsumerCommitInterval(); + TRANSACTION_RECORD_QUEUE_SIZE = TransactionCounterConfig.getTransactionRecordQueueSize(); + TRANSACTION_COUNT_STORE_CLASS = TransactionCounterConfig.getTransactionCountStoreClass(); this.transactionRecordQueue = TransactionRecordQueue.getInstance(TRANSACTION_RECORD_QUEUE_SIZE); // Load the transaction count store @@ -62,13 +68,14 @@ public boolean handleRequestInFlow(MessageContext messageContext) { ((Axis2MessageContext) messageContext).getAxis2MessageContext(); // Setting this property to identify request-response pairs - messageContext.setProperty(APIMgtGatewayConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST, true); + messageContext.setProperty(TransactionCounterConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST, true); LOG.info("Recieved an incoming request"); // Counting message received via an open WebSocket String transport = axis2MessageContext.getIncomingTransportName(); - if (transport.equals(APIMgtGatewayConstants.TRANSPORT_WS) || transport.equals(APIMgtGatewayConstants.TRANSPORT_WSS)){ + if (transport.equals(TransactionCounterConstants.TRANSPORT_WS) || + transport.equals(TransactionCounterConstants.TRANSPORT_WSS)){ LOG.info("Counting WebSocket message"); this.transactionRecordProducer.addTransaction(); } @@ -82,7 +89,7 @@ public boolean handleRequestInFlow(MessageContext messageContext) { public boolean handleRequestOutFlow(MessageContext messageContext) { try { Object isThereAnAssociatedIncomingRequest = messageContext.getProperty( - APIMgtGatewayConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST); + TransactionCounterConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST); // Counting outgoing messages that are not related to any request-response pair if (isThereAnAssociatedIncomingRequest == null) { @@ -103,7 +110,7 @@ public boolean handleResponseInFlow(MessageContext messageContext) { @Override public boolean handleResponseOutFlow(MessageContext messageContext) { Object isThereAnAssociatedIncomingRequest = messageContext.getProperty( - APIMgtGatewayConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST); + TransactionCounterConstants.IS_THERE_ASSOCIATED_INCOMING_REQUEST); // Counting request-response pairs if (isThereAnAssociatedIncomingRequest instanceof Boolean) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java new file mode 100644 index 000000000000..7f290f98d4f4 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java @@ -0,0 +1,47 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction; + +public class TransactionCounterConstants { + + public static final String IS_THERE_ASSOCIATED_INCOMING_REQUEST = "is_there_incoming_request"; + public static final String TRANSPORT_WS = "ws"; + public static final String TRANSPORT_WSS = "wss"; + + public static final String SERVER_ID = "serverId"; + public static final String TRANSACTION_COUNT_STORE_CLASS = "transactionCountStoreClass"; + public static final String TRANSACTION_RECORD_QUEUE_SIZE = "transactionRecordQueueSize"; + public static final String PRODUCER_THREAD_POOL_SIZE = "producerThreadPoolSize"; + public static final String TRANSACTION_COUNT_RECORD_INTERVAL = "transactionCountRecordInterval"; + public static final String MAX_TRANSACTION_COUNT = "maxTransactionCount"; + public static final String CONSUMER_COMMIT_INTERVAL = "consumerCommitInterval"; + public static final String MAX_TRANSACTION_RECORDS_PER_COMMIT = "maxTransactionRecordsPerCommit"; + public static final String MAX_RETRY_COUNT = "maxRetryCount"; + public static final String TRANSACTION_COUNT_SERVICE = "transactionCountService"; + public static final String TRANSACTION_COUNT_SERVICE_USERNAME = "transactionCountServiceUsername"; + public static final String TRANSACTION_COUNT_SERVICE_PASSWORD = "transactionCountServicePassword"; + + // APIM Gateway related constants + public static final String APIM_CONFIG_CLASS = "org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder"; + public static final String GATEWAY_CONFIG_ROOT = "APIGateway.TransactionCounter"; + public static final String GATEWAY_PRODUCER_THREAD_POOL_SIZE = GATEWAY_CONFIG_ROOT + + ".ProducerThreadPoolSize"; + public static final String GATEWAY_QUEUE_SIZE = GATEWAY_CONFIG_ROOT + ".QueueSize"; + public static final String GATEWAY_STORE_CLASS = GATEWAY_CONFIG_ROOT + ".StoreClass"; + public static final String GATEWAY_MAX_TRANSACTION_COUNT = GATEWAY_CONFIG_ROOT + + ".MaxTransactionCount"; + public static final String GATEWAY_RECORD_INTERVAL = GATEWAY_CONFIG_ROOT + + ".ProducerScheduledInterval"; + public static final String GATEWAY_MAX_RETRY_COUNT = GATEWAY_CONFIG_ROOT + ".MaxRetryCount"; + public static final String GATEWAY_MAX_TRANSACTION_RECORDS_PER_COMMIT = GATEWAY_CONFIG_ROOT + + ".MaxBatchSize"; + public static final String GATEWAY_CONSUMER_COMMIT_INTERVAL = GATEWAY_CONFIG_ROOT + + ".PublisherScheduledInterval"; + public static final String GATEWAY_SERVER_ID = GATEWAY_CONFIG_ROOT + ".ServerID"; + public static final String GATEWAY_SERVICE = GATEWAY_CONFIG_ROOT + ".ServiceURL"; + public static final String GATEWAY_SERVICE_USERNAME = GATEWAY_CONFIG_ROOT + + ".ServiceUsername"; + public static final String GATEWAY_SERVICE_PASSWORD = GATEWAY_CONFIG_ROOT + + ".ServicePassword"; + + // MI related constants + public static final String MI_CONFIG_CLASS = "org.wso2.config.mapper.ConfigParser"; +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java new file mode 100644 index 000000000000..ac5d87772d56 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java @@ -0,0 +1,124 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction.config; + +import org.wso2.carbon.apimgt.gateway.handlers.transaction.TransactionCounterConstants; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.exception.TransactionCounterInitializationException; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Objects; + +public class APIMConfigFetcher implements ConfigFetcher { + + private static APIMConfigFetcher instance = null; + private static HashMap configMap = new HashMap<>(); + + private APIMConfigFetcher() throws TransactionCounterInitializationException { + try { + Class configClass = Class.forName(TransactionCounterConstants.APIM_CONFIG_CLASS); + + Object serviceReferenceHolder = configClass.getMethod("getInstance").invoke(null); + Object apiManagerConfigurationService = configClass.getMethod("getAPIManagerConfigurationService") + .invoke(serviceReferenceHolder); + Object apiManagerConfiguration = apiManagerConfigurationService.getClass() + .getMethod("getAPIManagerConfiguration").invoke(apiManagerConfigurationService); + Method getFirstProperty = apiManagerConfiguration.getClass().getMethod("getFirstProperty", + String.class); + + // Reading the config values + String temp; + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_SERVER_ID); + String SERVER_ID = Objects.requireNonNull( temp, "Server ID cannot be null"); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_STORE_CLASS); + String TRANSACTION_COUNT_STORE_CLASS = Objects.requireNonNull( + temp, "Transaction count store class cannot be null"); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_QUEUE_SIZE); + temp = Objects.requireNonNull(temp, "Transaction record queue size cannot be null"); + Integer TRANSACTION_RECORD_QUEUE_SIZE = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_PRODUCER_THREAD_POOL_SIZE); + temp = Objects.requireNonNull(temp, "Producer thread pool size cannot be null"); + Integer PRODUCER_THREAD_POOL_SIZE = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_RECORD_INTERVAL); + temp = Objects.requireNonNull(temp, "Transaction count record interval cannot be null"); + Integer TRANSACTION_COUNT_RECORD_INTERVAL = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_MAX_TRANSACTION_COUNT); + temp = Objects.requireNonNull(temp, "Max transaction count cannot be null"); + Double MAX_TRANSACTION_COUNT = Double.parseDouble(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_CONSUMER_COMMIT_INTERVAL); + temp = Objects.requireNonNull(temp, "Consumer commit interval cannot be null"); + Integer CONSUMER_COMMIT_INTERVAL = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_MAX_TRANSACTION_RECORDS_PER_COMMIT); + temp = Objects.requireNonNull(temp, "Max transaction records per commit cannot be null"); + Integer MAX_TRANSACTION_RECORDS_PER_COMMIT = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_MAX_RETRY_COUNT); + temp = Objects.requireNonNull(temp, "Max retry count cannot be null"); + Integer MAX_RETRY_COUNT = Integer.parseInt(temp); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_SERVICE); + String TRANSACTION_COUNT_SERVICE = Objects.requireNonNull(temp, + "Transaction count service cannot be null"); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_SERVICE_USERNAME); + String TRANSACTION_COUNT_SERVICE_USERNAME = Objects.requireNonNull(temp, + "Transaction count service username cannot be null"); + + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_SERVICE_PASSWORD); + String TRANSACTION_COUNT_SERVICE_PASSWORD = Objects.requireNonNull(temp, + "Transaction count service password cannot be null"); + + configMap.put(TransactionCounterConstants.SERVER_ID, SERVER_ID); + configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_STORE_CLASS, TRANSACTION_COUNT_STORE_CLASS); + configMap.put(TransactionCounterConstants.TRANSACTION_RECORD_QUEUE_SIZE, TRANSACTION_RECORD_QUEUE_SIZE); + configMap.put(TransactionCounterConstants.PRODUCER_THREAD_POOL_SIZE, PRODUCER_THREAD_POOL_SIZE); + configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_RECORD_INTERVAL , TRANSACTION_COUNT_RECORD_INTERVAL); + configMap.put(TransactionCounterConstants.MAX_TRANSACTION_COUNT, MAX_TRANSACTION_COUNT); + configMap.put(TransactionCounterConstants.CONSUMER_COMMIT_INTERVAL, CONSUMER_COMMIT_INTERVAL); + configMap.put(TransactionCounterConstants.MAX_TRANSACTION_RECORDS_PER_COMMIT, MAX_TRANSACTION_RECORDS_PER_COMMIT); + configMap.put(TransactionCounterConstants.MAX_RETRY_COUNT, MAX_RETRY_COUNT); + configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE, TRANSACTION_COUNT_SERVICE); + configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE_USERNAME, TRANSACTION_COUNT_SERVICE_USERNAME); + configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE_PASSWORD, TRANSACTION_COUNT_SERVICE_PASSWORD); + + } catch (ClassNotFoundException e) { + // This error won't be thrown here because it is already checked in TransactionCountConfig + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new TransactionCounterInitializationException(); + } catch (NullPointerException | NumberFormatException e) { + throw new TransactionCounterInitializationException("Error while reading the config values", e); + } + } + + public static ConfigFetcher getInstance() throws TransactionCounterInitializationException { + if (instance == null) { + instance = new APIMConfigFetcher(); + } + return instance; + } + + @Override + public String getConfigValue(String key) { + return configMap.get(key).toString(); + } + +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/ConfigFetcher.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/ConfigFetcher.java new file mode 100644 index 000000000000..8522b2347549 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/ConfigFetcher.java @@ -0,0 +1,6 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction.config; + +public interface ConfigFetcher { + String getConfigValue(String key); + +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/MIConfigFetcher.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/MIConfigFetcher.java new file mode 100644 index 000000000000..5c06bcf598d4 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/MIConfigFetcher.java @@ -0,0 +1,27 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction.config; + +import org.wso2.carbon.apimgt.gateway.handlers.transaction.exception.TransactionCounterInitializationException; + +import java.util.HashMap; + +public class MIConfigFetcher implements ConfigFetcher { + + private static MIConfigFetcher instance = null; + private static HashMap configMap = new HashMap<>(); + + private MIConfigFetcher() throws TransactionCounterInitializationException { + // To be implemented + } + + public static MIConfigFetcher getInstance() throws TransactionCounterInitializationException{ + if(instance == null) { + instance = new MIConfigFetcher(); + } + return instance; + } + + @Override + public String getConfigValue(String key) { + return null; + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java new file mode 100644 index 000000000000..7a40d614fdf0 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java @@ -0,0 +1,80 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction.config; + +import org.wso2.carbon.apimgt.gateway.handlers.transaction.TransactionCounterConstants; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.exception.TransactionCounterInitializationException; + +public class TransactionCounterConfig { + + private static ConfigFetcher configFetcher; + + public static void init() throws TransactionCounterInitializationException { + try { + // Check whether the APIM Config class is available + Class.forName(TransactionCounterConstants.APIM_CONFIG_CLASS); + configFetcher = APIMConfigFetcher.getInstance(); + } catch (ClassNotFoundException e) { + try { + // Check whether the MI Config class is available + Class.forName(TransactionCounterConstants.MI_CONFIG_CLASS); + configFetcher = MIConfigFetcher.getInstance(); + } catch (ClassNotFoundException ex) { + throw new TransactionCounterInitializationException(ex); + } + } + } + + public static String getServerID() { + return configFetcher.getConfigValue(TransactionCounterConstants.GATEWAY_SERVER_ID); + } + + public static String getTransactionCountStoreClass() { + return configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_STORE_CLASS); + } + + public static int getProducerThreadPoolSize() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.PRODUCER_THREAD_POOL_SIZE)); + } + + public static double getMaxTransactionCount() { + return Double.parseDouble( + configFetcher.getConfigValue(TransactionCounterConstants.MAX_TRANSACTION_COUNT)); + } + + public static int getTransactionCountRecordInterval() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_RECORD_INTERVAL)); + } + + public static int getMaxRetryCount() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.MAX_RETRY_COUNT)); + } + + public static int getMaxTransactionRecordsPerCommit() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.MAX_TRANSACTION_RECORDS_PER_COMMIT)); + } + + public static int getTransactionRecordQueueSize() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_RECORD_QUEUE_SIZE)); + } + + public static String getTransactionCountService() { + return configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE); + } + + public static String getTransactionCountServiceUsername() { + return configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE_USERNAME); + } + + public static String getTransactionCountServicePassword() { + return configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_SERVICE_PASSWORD); + } + + public static int getConsumerCommitInterval() { + return Integer.parseInt( + configFetcher.getConfigValue(TransactionCounterConstants.CONSUMER_COMMIT_INTERVAL)); + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/consumer/TransactionRecordConsumer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/consumer/TransactionRecordConsumer.java index 29b865489ff5..2ac0fcbd2d31 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/consumer/TransactionRecordConsumer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/consumer/TransactionRecordConsumer.java @@ -2,7 +2,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.apimgt.gateway.handlers.transaction.util.TransactionCountConfig; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import org.wso2.carbon.apimgt.gateway.handlers.transaction.record.TransactionRecord; import org.wso2.carbon.apimgt.gateway.handlers.transaction.queue.TransactionRecordQueue; import org.wso2.carbon.apimgt.gateway.handlers.transaction.store.TransactionRecordStore; @@ -27,8 +27,8 @@ private TransactionRecordConsumer(TransactionRecordStore transactionRecordStore, TransactionRecordQueue transactionRecordQueue, int commitInterval) { // Obtain config values - MAX_RETRY_COUNT = TransactionCountConfig.getMaxRetryCount(); - MAX_TRANSACTION_RECORDS_PER_COMMIT = TransactionCountConfig.getMaxTransactionRecordsPerCommit(); + MAX_RETRY_COUNT = TransactionCounterConfig.getMaxRetryCount(); + MAX_TRANSACTION_RECORDS_PER_COMMIT = TransactionCounterConfig.getMaxTransactionRecordsPerCommit(); this.transactionRecordStore = transactionRecordStore; this.transactionRecordQueue = transactionRecordQueue; diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/exception/TransactionCounterInitializationException.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/exception/TransactionCounterInitializationException.java new file mode 100644 index 000000000000..9ab274fe7434 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/exception/TransactionCounterInitializationException.java @@ -0,0 +1,19 @@ +package org.wso2.carbon.apimgt.gateway.handlers.transaction.exception; + +public class TransactionCounterInitializationException extends Exception { + + public TransactionCounterInitializationException() { + super("Error while initializing the transaction counter"); + } + + public TransactionCounterInitializationException(Exception e) { + super("Error while initializing the transaction counter", e); + } + public TransactionCounterInitializationException(String msg) { + super(msg); + } + + public TransactionCounterInitializationException(String msg, Exception e) { + super(msg, e); + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java index 96dd6589d538..cf339395d851 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java @@ -2,7 +2,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.apimgt.gateway.handlers.transaction.util.TransactionCountConfig; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import org.wso2.carbon.apimgt.gateway.handlers.transaction.record.TransactionRecord; import org.wso2.carbon.apimgt.gateway.handlers.transaction.queue.TransactionRecordQueue; @@ -27,8 +27,8 @@ public class TransactionRecordProducer { private TransactionRecordProducer(TransactionRecordQueue transactionRecordQueue, int threadPoolSize) { // Obtain config values - MAX_TRANSACTION_COUNT = TransactionCountConfig.getMaxTransactionCount(); - TRANSACTION_COUNT_RECORD_INTERVAL = TransactionCountConfig.getTransactionCountRecordInterval(); + MAX_TRANSACTION_COUNT = TransactionCounterConfig.getMaxTransactionCount(); + TRANSACTION_COUNT_RECORD_INTERVAL = TransactionCounterConfig.getTransactionCountRecordInterval(); this.transactionRecordQueue = transactionRecordQueue; this.executorService = Executors.newFixedThreadPool(threadPoolSize); diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/record/TransactionRecord.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/record/TransactionRecord.java index 02eae23a8c4e..fb7277946e35 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/record/TransactionRecord.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/record/TransactionRecord.java @@ -1,6 +1,6 @@ package org.wso2.carbon.apimgt.gateway.handlers.transaction.record; -import org.wso2.carbon.apimgt.gateway.handlers.transaction.util.TransactionCountConfig; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import java.net.InetAddress; import java.net.UnknownHostException; @@ -12,7 +12,7 @@ public class TransactionRecord { private static String server; static { - server = TransactionCountConfig.getServerID(); + server = TransactionCounterConfig.getServerID(); try { localhost = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/store/TransactionRecordStoreImpl.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/store/TransactionRecordStoreImpl.java index 9622fdc10fe2..168c21ccce95 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/store/TransactionRecordStoreImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/store/TransactionRecordStoreImpl.java @@ -9,7 +9,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; -import org.wso2.carbon.apimgt.gateway.handlers.transaction.util.TransactionCountConfig; +import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import org.wso2.carbon.apimgt.gateway.handlers.transaction.record.TransactionRecord; import org.wso2.carbon.apimgt.impl.utils.APIUtil; @@ -30,9 +30,9 @@ public class TransactionRecordStoreImpl implements TransactionRecordStore { private static String password; public TransactionRecordStoreImpl() { - endpoint = TransactionCountConfig.getTransactionCountService(); - username = TransactionCountConfig.getTransactionCountServiceUsername(); - password = TransactionCountConfig.getTransactionCountServicePassword(); + endpoint = TransactionCounterConfig.getTransactionCountService(); + username = TransactionCounterConfig.getTransactionCountServiceUsername(); + password = TransactionCounterConfig.getTransactionCountServicePassword(); URL url = null; try { diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/util/TransactionCountConfig.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/util/TransactionCountConfig.java deleted file mode 100644 index c78b8fb0eeb3..000000000000 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/util/TransactionCountConfig.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.wso2.carbon.apimgt.gateway.handlers.transaction.util; - -import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; -import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; -import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; - -public class TransactionCountConfig { - - private static int PRODUCER_THREAD_POOL_SIZE; - private static int CONSUMER_COMMIT_INTERVAL; - private static int TRANSACTION_RECORD_QUEUE_SIZE; - private static String TRANSACTION_COUNT_STORE_CLASS; - private static double MAX_TRANSACTION_COUNT; - private static int TRANSACTION_COUNT_RECORD_INTERVAL; - private static int MAX_RETRY_COUNT; - private static int MAX_TRANSACTION_RECORDS_PER_COMMIT; - private static String SERVER_ID; - private static String TRANSACTION_COUNT_SERVICE; - private static String TRANSACTION_COUNT_SERVICE_USERNAME; - private static String TRANSACTION_COUNT_SERVICE_PASSWORD; - - static { - APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance() - .getAPIManagerConfigurationService().getAPIManagerConfiguration(); - - if (apiManagerConfiguration != null) { - SERVER_ID = apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_SERVER_ID); - TRANSACTION_COUNT_STORE_CLASS = apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_STORE_CLASS); - TRANSACTION_RECORD_QUEUE_SIZE = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_QUEUE_SIZE)); - PRODUCER_THREAD_POOL_SIZE = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_PRODUCER_THREAD_POOL_SIZE)); - TRANSACTION_COUNT_RECORD_INTERVAL = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_RECORD_INTERVAL)); - MAX_TRANSACTION_COUNT = Double.parseDouble(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_MAX_TRANSACTION_COUNT)); - CONSUMER_COMMIT_INTERVAL = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_CONSUMER_COMMIT_INTERVAL)); - MAX_TRANSACTION_RECORDS_PER_COMMIT = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_MAX_TRANSACTION_RECORDS_PER_COMMIT)); - MAX_RETRY_COUNT = Integer.parseInt(apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_MAX_RETRY_COUNT)); - TRANSACTION_COUNT_SERVICE = apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_SERVICE); - TRANSACTION_COUNT_SERVICE_USERNAME = apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_SERVICE_USERNAME); - TRANSACTION_COUNT_SERVICE_PASSWORD = apiManagerConfiguration.getFirstProperty( - APIMgtGatewayConstants.TRANSACTION_COUNTER_SERVICE_PASSWORD); - } - } - - public static int getProducerThreadPoolSize() { - return PRODUCER_THREAD_POOL_SIZE; - } - - public static double getMaxTransactionCount() { - return MAX_TRANSACTION_COUNT; - } - - public static int getTransactionCountRecordInterval() { - return TRANSACTION_COUNT_RECORD_INTERVAL; - } - - public static int getMaxRetryCount() { - return MAX_RETRY_COUNT; - } - - public static int getMaxTransactionRecordsPerCommit() { - return MAX_TRANSACTION_RECORDS_PER_COMMIT; - } - - public static int getTransactionRecordQueueSize() { - return TRANSACTION_RECORD_QUEUE_SIZE; - } - - public static String getTransactionCountStoreClass() { - return TRANSACTION_COUNT_STORE_CLASS; - } - - public static String getServerID() { - return SERVER_ID; - } - - public static String getTransactionCountService() { - return TRANSACTION_COUNT_SERVICE; - } - - public static String getTransactionCountServiceUsername() { - return TRANSACTION_COUNT_SERVICE_USERNAME; - } - - public static String getTransactionCountServicePassword() { - return TRANSACTION_COUNT_SERVICE_PASSWORD; - } - - public static int getConsumerCommitInterval() { - return CONSUMER_COMMIT_INTERVAL; - } -} diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java index d43fc8a39596..27576f53c44f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java @@ -214,6 +214,7 @@ protected void activate(ComponentContext componentContext) throws Exception { bundleContext.registerService(Notifier.class.getName(),new ExternallyDeployedApiNotifier(),null); bundleContext.registerService(Notifier.class.getName(),new KeyTemplateNotifier(), null); bundleContext.registerService(Notifier.class.getName(), new CorrelationConfigNotifier(), null); + // temp APIManagerConfigurationServiceImpl configurationService = new APIManagerConfigurationServiceImpl(configuration); ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(configurationService); APIMgtDBUtil.initialize();