use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.
the class V12__MigrateUserGroupsToDB method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all user groups to the DB");
final String sFilename = PhotonSecurityManager.FactoryXML.DIRECTORY_SECURITY + PhotonSecurityManager.FactoryXML.FILENAME_USERGROUPS_XML;
final File aFile = WebFileIO.getDataIO().getFile(sFilename);
if (aFile.exists()) {
final IUserManager aUserMgr = new UserManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
final IRoleManager aRoleMgr = new RoleManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
final UserGroupManager aMgrXML = new UserGroupManager(sFilename, aUserMgr, aRoleMgr);
final ICommonsList<IUserGroup> aUserGroups = aMgrXML.getAll();
if (aUserGroups.isNotEmpty()) {
final UserGroupManagerJDBC aMgrNew = new UserGroupManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER, aUserMgr, aRoleMgr);
for (final IUserGroup aUserGroup : aUserGroups) {
// Don't run the callback here
if (aMgrNew.internalCreateNewUserGroup((UserGroup) aUserGroup, false, false) == null)
LOGGER.error("Failed to migrate user group " + aUserGroup + " to DB");
}
}
// Rename to avoid later inconsistencies
WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
LOGGER.info("Finished migrating all " + aUserGroups.size() + " user groups to the DB");
} else {
LOGGER.warn("No user group file found");
}
}
}
use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.
the class SMPManagerProviderSQL method beforeInitManagers.
@Override
public void beforeInitManagers() {
final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
// Set the special PhotonSecurityManager factory
// Must be before Flyway, so that auditing of Flyway actions (may) work
PhotonSecurityManagerFactoryJDBC.install(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
// Flyway migration is enabled by default
if (aCF.getAsBoolean(SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED, true))
FlywayMigrator.Singleton.INSTANCE.runFlyway(m_eDBType);
else
LOGGER.warn("Flyway Migration is disabled according to the configuration item " + SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED);
// Register this here, so that the SMPMetaManager is available
DBExecutor.setConnectionStatusChangeCallback((eOld, eNew) -> {
// false: don't trigger callback, because the source is DBExecutor
SMPMetaManager.getInstance().setBackendConnectionState(eNew, false);
});
// Allow communicating in the other direction as well
SMPMetaManager.getInstance().setBackendConnectionStateChangeCallback(eNew -> DBExecutor.resetConnectionEstablished());
}
use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.
the class V15__MigrateDBUsersToPhotonUsers method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all old DB users to ph-oton users");
final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
// Old JDBC user manager
final SMPUserManagerJDBC aSQLUserMgr = new SMPUserManagerJDBC(SMPDBExecutor::new);
final ICommonsList<DBUser> aSQLUsers = aSQLUserMgr.getAllUsers();
LOGGER.info("Found " + aSQLUsers.size() + " DB user to migrate");
final ICommonsOrderedMap<String, String> aCreatedMappings = new CommonsLinkedHashMap<>();
// New JDBC user manager
final IUserManager aPhotonUserMgr = PhotonSecurityManager.getUserMgr();
for (final DBUser aSQLUser : aSQLUsers) {
final DBUser aDBUser = aSQLUser;
IUser aPhotonUser = null;
int nIndex = 0;
while (true) {
final String sUserName = aDBUser.getUserName() + (nIndex > 0 ? Integer.toString(nIndex) : "");
// The suffix "@example.org" is added to make it an email-address
final String sEmailAddress = sUserName + "@example.org";
aPhotonUser = aPhotonUserMgr.createNewUser(sEmailAddress, sEmailAddress, aDBUser.getPassword(), null, sUserName, null, CSMPServer.DEFAULT_LOCALE, null, false);
if (aPhotonUser != null) {
// New user was successfully created
break;
}
// User name already taken
++nIndex;
if (nIndex > 1000) {
// Avoid endless loop
throw new IllegalStateException("Too many iterations mapping the DB user '" + aDBUser.getUserName() + "' to a ph-oton user");
}
}
aCreatedMappings.put(aDBUser.getUserName(), aPhotonUser.getID());
LOGGER.info("Mapped DB user '" + aDBUser.getUserName() + "' to ph-oton user " + aPhotonUser.getID());
}
// Update the ownership in "smp_ownership"
// Remove the table "smp_user"
aSQLUserMgr.updateOwnershipsAndKillUsers(aCreatedMappings);
if (XMLMapHandler.writeMap(aCreatedMappings, new FileSystemResource(WebFileIO.getDataIO().getFile("migrations/db-photon-user-mapping-" + eDBType.getID() + ".xml"))).isFailure())
LOGGER.error("Failed to store mapping of DB users to ph-oton users as XML");
LOGGER.info("Finished migrating all DB users to ph-oton users");
}
}
use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.
the class V5__MigrateTransportProfilesToDB method migrate.
public void migrate(@Nonnull final Context context) throws Exception {
try (final WebScoped aWS = new WebScoped()) {
LOGGER.info("Migrating all transport profiles to the DB");
final String sFilename = "transportprofiles.xml";
final File aFile = WebFileIO.getDataIO().getFile(sFilename);
if (aFile.exists()) {
final SMPTransportProfileManagerXML aMgrXML = new SMPTransportProfileManagerXML(sFilename);
final ICommonsList<ISMPTransportProfile> aTransportProfiles = aMgrXML.getAll();
if (aTransportProfiles.isNotEmpty()) {
final SMPTransportProfileManagerJDBC aMgrNew = new SMPTransportProfileManagerJDBC(SMPDBExecutor::new);
for (final ISMPTransportProfile aTransportProfile : aTransportProfiles) if (aMgrNew.createSMPTransportProfile(aTransportProfile.getID(), aTransportProfile.getName(), aTransportProfile.isDeprecated()) == null)
LOGGER.error("Failed to migrate " + aTransportProfile + " to DB");
}
// Rename to avoid later inconsistencies
WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
LOGGER.info("Finished migrating all " + aTransportProfiles.size() + " transport profiles to the DB");
} else {
LOGGER.info("No transport profile file found");
}
}
}
use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.
the class SMPIDFactoryJDBC method readAndUpdateIDCounter.
@Override
protected long readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
final MutableLong aReadValue = new MutableLong(0);
final DBExecutor aExecutor = new SMPDBExecutor();
aExecutor.performInTransaction(() -> {
// Read existing value
final String sExistingValue = SMPSettingsManagerJDBC.getSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID);
final long nRead = StringParser.parseLong(sExistingValue, m_nInitialCount);
aReadValue.set(nRead);
// Write new value
final long nNewValue = nRead + nReserveCount;
SMPSettingsManagerJDBC.setSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID, Long.toString(nNewValue));
if (LOGGER.isDebugEnabled())
LOGGER.debug("Updated SQL ID from " + sExistingValue + " to " + nNewValue);
});
return aReadValue.longValue();
}
Aggregations