Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load keystore datasource #4161

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.wso2.carbon.utils.internal;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.utils.CarbonUtils;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class CarbonUtilsDataHolder {

private static final Log LOG = LogFactory.getLog(CarbonUtilsDataHolder.class);
private static ConfigurationContext configContext;
private DataSource dataSource;
private static DataSource dataSource;

private static CarbonUtilsDataHolder carbonUtilsDataHolder = new CarbonUtilsDataHolder();

Expand All @@ -28,11 +34,33 @@ public static ConfigurationContext getConfigContext() {

public DataSource getDataSource() {

return this.dataSource;
if (dataSource == null) {
LOG.warn("Key Store Data source is not initialized. Hence Initializing the data source again.");
initializeDatasource();
}
return dataSource;
}

public void setDataSource(DataSource dataSource) {
public void setDataSource() {

this.dataSource = dataSource;
initializeDatasource();
}

private static synchronized void initializeDatasource() {

String dataSourceName = CarbonUtils.getServerConfiguration().getFirstProperty(
"KeyStoreDataPersistenceManager.DataSourceName");
if (StringUtils.isNotBlank(dataSourceName)) {
try {
dataSource = InitialContext.doLookup(dataSourceName);
if (dataSource == null) {
LOG.warn("Data source for KeyStore Data Persistence not found: " + dataSourceName);
}
} catch (NamingException e) {
throw new RuntimeException("Error in looking up keystore data source.", e);
}
} else {
throw new RuntimeException("Data source name is not configured for KeyStore Data Persistence Manager.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
import org.wso2.carbon.utils.deployment.GhostMetaArtifactsLoader;
import org.wso2.carbon.utils.multitenancy.GhostServiceMetaArtifactsLoader;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

@Component(name = "org.wso2.carbon.utils.internal.CarbonUtilsServiceComponent", immediate = true)
public class CarbonUtilsServiceComponent {

Expand All @@ -25,7 +21,7 @@ protected void activate(ComponentContext ctx) {
ctx.getBundleContext().registerService(GhostMetaArtifactsLoader.class.getName(), serviceMetaArtifactsLoader, null);
// Read and set diagnostic logs config.
CarbonUtils.setDiagnosticLogMode(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
CarbonUtilsDataHolder.getInstance().setDataSource(getKeyStoreDataSource());
CarbonUtilsDataHolder.getInstance().setDataSource();
}

@Reference(name = "org.wso2.carbon.utils.ConfigurationContextService", cardinality = ReferenceCardinality.MANDATORY,
Expand All @@ -37,19 +33,4 @@ protected void setConfigurationContextService(ConfigurationContextService contex
protected void unsetConfigurationContextService(ConfigurationContextService contextService) {
CarbonUtilsDataHolder.setConfigContext(null);
}

private static synchronized DataSource getKeyStoreDataSource() {

String dataSourceName = CarbonUtils.getServerConfiguration().getFirstProperty(
"KeyStoreDataPersistenceManager.DataSourceName");
if (dataSourceName != null) {
try {
return InitialContext.doLookup(dataSourceName);
} catch (NamingException e) {
throw new RuntimeException("Error in looking up keystore data source.", e);
}
} else {
throw new RuntimeException("Data source name is not configured for KeyStore Data Persistence Manager.");
}
}
}