Skip to content

Commit

Permalink
invoke registry db directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshani committed Apr 10, 2024
1 parent 0af2ed4 commit 8c9ff33
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.dao;

import org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RegistryDAO {

private static final String PATH = "PATH";
private static final String TENANT_ID = "TENANT_ID";
private static final String REG_NAME = "REG_NAME";

public String getPathId(String path, String tenantDomain) throws IdentityException {

String pathId = null;
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_PATH_ID)) {
statement.setString(PATH, path);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
pathId = resultSet.getString(1);
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry path id.", e);
}
} catch (SQLException | IdentityException e) {
throw new IdentityException("Error while retrieving registry path id.", e);
}
return pathId;
}

public Map<String, String> getCollectionProperties(int pathId, String tenantDomain) throws IdentityException {

Map<String, String> properties = new HashMap<>();
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_RESOURCE_PROPERTY_COLLECTION)) {
statement.setInt(PATH, pathId);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
String key = resultSet.getString(1);
String value = resultSet.getString(2);
properties.put(key, value);
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry resource collection properties.", e);
}
} catch (SQLException | IdentityException e) {
throw new IdentityException("Error while retrieving registry resource collection properties.", e);
}
return properties;
}

public String[] getCollectionChildren(String path, int pathId, String tenantDomain) throws IdentityException {

List<String> children = new ArrayList<>();
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_NAME_COLLECTION)) {
statement.setInt(PATH, pathId);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
children.add(path + "/" + resultSet.getString(1));
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry resource collection sub resource paths.",
e);
}

try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_PATH_COLLECTION)) {
statement.setInt(PATH, pathId);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
String childPathName = resultSet.getString(2).split("/(?!.*\\/)")[1];
children.add(path + "/" + childPathName);
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry resource collection sub paths.", e);
}
} catch (SQLException | IdentityException e) {
throw new IdentityException("Error while retrieving registry resource collection children.", e);
}
return children.toArray(new String[0]);
}

public Map<String, String> getResourceProperties(int pathId, String resourceName, String tenantDomain)
throws IdentityException {

Map<String, String> properties = new HashMap<>();
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_RESOURCE_PROPERTY_RESOURCE)) {
statement.setInt(PATH, pathId);
statement.setString(REG_NAME, resourceName);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
String key = resultSet.getString(1);
String value = resultSet.getString(2);
properties.put(key, value);
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry resource properties.", e);
}
} catch (SQLException | IdentityException e) {
throw new IdentityException("Error while retrieving registry resource properties.", e);
}
return properties;
}

public byte[] getResourceContent(int pathId, String resourceName, String tenantDomain)
throws IdentityException {

byte[] content = null;
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) {
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection,
RegistrySQLQueries.GET_REG_CONTENT)) {
statement.setInt(PATH, pathId);
statement.setString(REG_NAME, resourceName);
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
content = resultSet.getBytes(1);
}
}
} catch (SQLException e) {
throw new IdentityException("Error while retrieving registry resource content.", e);
}
} catch (SQLException | IdentityException e) {
throw new IdentityException("Error while retrieving registry resource content.", e);
}
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.core.dao;

public class RegistrySQLQueries {

private RegistrySQLQueries() {

}

public static final String GET_REG_PATH_ID =
"SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE = :PATH; AND REG_TENANT_ID = :TENANT_ID;";
public static final String GET_REG_RESOURCE_PROPERTY_COLLECTION =
"SELECT REG_NAME, REG_VALUE FROM REG_PROPERTY P, REG_RESOURCE_PROPERTY RP WHERE P.REG_ID=RP.REG_PROPERTY_ID AND RP.REG_PATH_ID = :PATH; AND RP.REG_RESOURCE_NAME IS NULL AND P.REG_TENANT_ID=RP.REG_TENANT_ID AND RP.REG_TENANT_ID = :TENANT_ID; ORDER BY P.REG_ID";
public static final String GET_REG_RESOURCE_PROPERTY_RESOURCE =
"SELECT REG_NAME, REG_VALUE FROM REG_PROPERTY P, REG_RESOURCE_PROPERTY RP WHERE P.REG_ID=RP.REG_PROPERTY_ID AND RP.REG_PATH_ID = :PATH; AND RP.REG_RESOURCE_NAME = :REG_NAME; AND P.REG_TENANT_ID=RP.REG_TENANT_ID AND RP.REG_TENANT_ID = :TENANT_ID; ORDER BY P.REG_ID";
public static final String GET_REG_NAME_COLLECTION =
"SELECT REG_NAME FROM REG_RESOURCE WHERE REG_PATH_ID = :PATH; AND REG_TENANT_ID = :TENANT_ID; AND REG_NAME IS NOT NULL;";
public static final String GET_REG_PATH_COLLECTION =
"SELECT P.REG_PATH_ID, P.REG_PATH_VALUE FROM REG_PATH P, REG_RESOURCE R WHERE P.REG_PATH_PARENT_ID = :PATH; AND P.REG_TENANT_ID = :TENANT_ID; AND R.REG_PATH_ID=P.REG_PATH_ID AND R.REG_NAME IS NULL AND R.REG_TENANT_ID = :TENANT_ID;";
public static final String GET_REG_CONTENT =
"SELECT REG_CONTENT_DATA FROM REG_CONTENT WHERE REG_CONTENT_ID = (SELECT REG_CONTENT_ID FROM REG_RESOURCE WHERE REG_PATH_ID = :PATH; AND REG_NAME = :REG_NAME; AND REG_TENANT_ID = :TENANT_ID; );";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2023, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2014-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -42,6 +42,7 @@
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.identity.core.migrate.MigrationClientStartupObserver;
import org.wso2.carbon.identity.core.persistence.JDBCPersistenceManager;
import org.wso2.carbon.identity.core.persistence.RegistryDataPersistenceManager;
import org.wso2.carbon.identity.core.persistence.UmPersistenceManager;
import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService;
import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtServiceImpl;
Expand Down Expand Up @@ -142,6 +143,9 @@ protected void activate(ComponentContext ctxt) throws MigrationClientException {
jdbcPersistenceManager.initializeDatabase();
}

// Initialize governanceDB persistence manager to retrieve governance registry datasource.
RegistryDataPersistenceManager.getInstance();

// initialize um persistence manager and retrieve the user management datasource.
UmPersistenceManager.getInstance();

Expand Down
Loading

0 comments on commit 8c9ff33

Please sign in to comment.