use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class BrokerManager method initConfigProvider.
/**
* Loads configurations during the broker start up.
* method will try to <br/>
* (1) Load the configuration file specified in 'broker.file' (e.g. -Dbroker.file=<FilePath>). <br/>
* (2) If -Dbroker.file is not specified, the broker.yaml file exists in current directory and load it. <br/>
* <p>
* <b>Note: </b> if provided configuration file cannot be read broker will not start.
*
* @param startupContext startup context of the broker
*/
private static void initConfigProvider(StartupContext startupContext) throws ConfigurationException {
Path brokerYamlFile;
String brokerFilePath = System.getProperty(BrokerConfiguration.SYSTEM_PARAM_BROKER_CONFIG_FILE);
if (brokerFilePath == null || brokerFilePath.trim().isEmpty()) {
// use current path.
brokerYamlFile = Paths.get("", BrokerConfiguration.BROKER_FILE_NAME).toAbsolutePath();
} else {
brokerYamlFile = Paths.get(brokerFilePath).toAbsolutePath();
}
ConfigProvider configProvider = ConfigProviderFactory.getConfigProvider(brokerYamlFile, null);
startupContext.registerService(BrokerConfigProvider.class, (BrokerConfigProvider) configProvider::getConfigurationObject);
}
use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class BundleActivator method start.
@Activate
protected void start(BundleContext bundleContext) {
try {
// Set default timestamp to UTC
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Etc/UTC"));
Context ctx = jndiContextManager.newInitialContext();
DataSource dataSourceAMDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMDB"));
DAOUtil.initialize(dataSourceAMDB);
boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
if (isAnalyticsEnabled) {
DataSource dataSourceStatDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMSTATSDB"));
DAOUtil.initializeAnalyticsDataSource(dataSourceStatDB);
}
WorkflowExtensionsConfigBuilder.build(configProvider);
ServiceDiscoveryConfigBuilder.build(configProvider);
ContainerBasedGatewayConfigBuilder.build(configProvider);
BrokerManager.start();
Broker broker = new BrokerImpl();
BrokerUtil.initialize(broker);
} catch (NamingException e) {
log.error("Error occurred while jndi lookup", e);
}
// deploying default policies
try {
ThrottlerUtil.addDefaultAdvancedThrottlePolicies();
if (log.isDebugEnabled()) {
log.debug("Checked default throttle policies successfully");
}
} catch (APIManagementException e) {
log.error("Error occurred while deploying default policies", e);
}
// securing files
try {
boolean fileEncryptionEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getFileEncryptionConfigurations().isEnabled();
if (fileEncryptionEnabled) {
FileEncryptionUtility fileEncryptionUtility = FileEncryptionUtility.getInstance();
fileEncryptionUtility.init();
fileEncryptionUtility.encryptFiles();
}
} catch (APIManagementException e) {
log.error("Error occurred while encrypting files", e);
}
}
use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class BrokerUtil method publishToTopic.
/**
* Publish to broker topic
*
* @param topicName publishing topic name
* @param gatewayEvent topic message data object
*/
public static void publishToTopic(String topicName, GatewayEvent gatewayEvent) throws GatewayException {
TopicSession topicSession = null;
Topic topic = null;
TopicPublisher topicPublisher = null;
TopicConnection topicConnection = null;
try {
topicConnection = getTopicConnection();
topicConnection.start();
topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = topicSession.createTopic(topicName);
topicPublisher = topicSession.createPublisher(topic);
TextMessage textMessage = topicSession.createTextMessage(new Gson().toJson(gatewayEvent));
topicPublisher.publish(textMessage);
} catch (JMSException e) {
String errorMessage = "Error occurred while publishing " + gatewayEvent.getEventType() + " event to JMS " + "topic :" + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (BrokerException e) {
String errorMessage = "Error occurred while obtaining broker topic connection for topic : " + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} finally {
if (topicPublisher != null) {
try {
topicPublisher.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic publisher for topic : " + topicName);
}
}
if (topicSession != null) {
try {
topicSession.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic session for topic : " + topicName);
}
}
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic connection for topic : " + topicName);
}
}
}
}
use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class APIGatewayPublisherImplTestCase method testDeleteAPIWhenLabelsAreNull.
@Test
public void testDeleteAPIWhenLabelsAreNull() throws GatewayException, ContainerBasedGatewayException {
APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
BrokerUtil.initialize(broker);
ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(null).build();
apiGatewayPublisher.deleteAPI(api);
Mockito.verify(containerBasedGatewayGenerator, Mockito.times(0)).removeContainerBasedGateway("label", api);
}
use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class APIGatewayPublisherImplTestCase method testDeleteAPIWhenAPIDoesNotHaveOwnGateway.
@Test
public void testDeleteAPIWhenAPIDoesNotHaveOwnGateway() throws GatewayException, ContainerBasedGatewayException {
APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
BrokerUtil.initialize(broker);
ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
List<String> labels = new ArrayList<>();
labels.add("label");
API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(false).labels(labels).build();
apiGatewayPublisher.deleteAPI(api);
Mockito.verify(containerBasedGatewayGenerator, Mockito.times(0)).removeContainerBasedGateway("label", api);
}
Aggregations