kz.greetgo.security.session.SessionStorageAdapterAbstract Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greetgo.security Show documentation
Show all versions of greetgo.security Show documentation
Security infrastructure used in greetgo!
package kz.greetgo.security.session;
import kz.greetgo.security.crypto.errors.SqlWrapper;
import kz.greetgo.security.session.jdbc.SelectDateOrNull;
import kz.greetgo.security.session.jdbc.SelectFirstOrNull;
import kz.greetgo.security.session.jdbc.SelectStrOrNull;
import kz.greetgo.security.session.jdbc.Update;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static kz.greetgo.security.util.ErrorUtil.extractSqlException;
public abstract class SessionStorageAdapterAbstract implements SessionStorage {
protected final SessionStorageJdbcBuilder.Names names;
public SessionStorageAdapterAbstract(SessionStorageJdbcBuilder.Names names) {
this.names = names;
init();
}
private void init() {
try {
names.jdbc.execute(new SelectStrOrNull(checkTableExistsSql()));
} catch (RuntimeException e) {
SQLException sqlException = extractSqlException(e);
if (sqlException != null) {
if (isExceptionAboutTableDoesNotExists(sqlException)) {
createSessionTable();
return;
}
throw new SqlWrapper(sqlException);
}
throw e;
}
}
private void createSessionTable() {
names.jdbc.execute(new Update(createSessionTableSql()));
}
protected abstract String createSessionTableSql();
protected abstract boolean isExceptionAboutTableDoesNotExists(SQLException sqlException);
protected abstract String checkTableExistsSql();
protected abstract String insertSessionSql(List