service.database.SQLDatabaseService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-service Show documentation
Show all versions of database-service Show documentation
This is a library providing an API for accessing databases via socket connections
package service.database;
import databaseconnector.api.sql.SQLDatabaseConnection;
import databaseconnector.api.sql.SQLSchema;
import org.eclipse.jetty.servlet.ServletHandler;
import service.RESTService;
import service.database.command.*;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class SQLDatabaseService implements RESTService {
private static final Object lock = new Object();
private static final AtomicReference databaseConnection = new AtomicReference<>();
private static final AtomicReference schema = new AtomicReference<>();
private static final AtomicBoolean serviceRunning = new AtomicBoolean(false);
@Override
public boolean isRunning() {
return serviceRunning();
}
@Override
public void start() {
synchronized (lock){
if (databaseConnection.get() != null && schema.get() != null){
serviceRunning.set(true);
}
}
}
@Override
public void stop() {
serviceRunning.set(false);
}
@Override
public void registerCommands(ServletHandler servletHandler){
servletHandler.addServletWithMapping(InitDatabase.class, InitDatabase.getCommand());
servletHandler.addServletWithMapping(DatabaseInitiated.class, DatabaseInitiated.getCommand());
servletHandler.addServletWithMapping(QueryDatabase.class, QueryDatabase.getCommand());
servletHandler.addServletWithMapping(InsertIntoDatabase.class, InsertIntoDatabase.getCommand());
servletHandler.addServletWithMapping(DeleteDatabaseRow.class, DeleteDatabaseRow.getCommand());
servletHandler.addServletWithMapping(UpdateDatabaseRow.class, UpdateDatabaseRow.getCommand());
}
public static void setDatabaseConnection(SQLDatabaseConnection databaseConnection){
synchronized (lock){
SQLDatabaseService.databaseConnection.set(databaseConnection);
}
}
public static void setSchema(SQLSchema schema){
synchronized (lock){
SQLDatabaseService.schema.set(schema);
}
}
public static Optional getDatabaseConnection(){
return Optional.ofNullable(databaseConnection.get());
}
public static Optional getSchema(){
return Optional.ofNullable(schema.get());
}
public static boolean serviceRunning(){
return serviceRunning.get();
}
}