All Downloads are FREE. Search and download functionalities are using the official Maven repository.

service.database.SQLDatabaseService Maven / Gradle / Ivy

Go to download

This is a library providing an API for accessing databases via socket connections

There is a newer version: 4.0.5
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy