Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ bin
.gradle
build
/modules
/plugins
/plugins
logs
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

group = "igs-landstuhl"

version = "v2.0.0-SNAPSHOT-0"
version = "v2.0.0-SNAPSHOT-1"

application {
mainClass.set("de.igslandstuhl.database.Application")
Expand All @@ -25,6 +25,10 @@ dependencies {
implementation("org.jline:jline:3.30.6") // for better console input handling
implementation("org.yaml:snakeyaml:2.2") // plugin imports

// Logging
implementation("org.slf4j:slf4j-api:2.0.13")
implementation("ch.qos.logback:logback-classic:1.5.6")

testImplementation("org.junit.jupiter:junit-jupiter:5.13.4") // using JUnit 5 (latest)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/de/igslandstuhl/database/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;

import org.jline.reader.UserInterruptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.igslandstuhl.database.api.SerializationException;
import de.igslandstuhl.database.api.Subject;
Expand All @@ -30,6 +32,10 @@ public final class Application {
public static final String TITLE_DELIMITER = "¶";
public static final String TASK_TITLE_DELIMITER = "\\|";
public static final String TASK_DELIMITER = "¤";

public static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
public static final Logger LOGGER_API = LoggerFactory.getLogger("de.igslandstuhl.database.api");

private static Application instance = new Application(new String[] {"--test-environment", "true"});
public static Application getInstance() {
return instance;
Expand Down Expand Up @@ -89,7 +95,6 @@ public Topic[] readFile(String file) throws SerializationException, SQLException
}
}
} catch (Throwable t) {
t.printStackTrace();
throw new SerializationException("Failed to read file", t);
}

Expand All @@ -98,15 +103,20 @@ public Topic[] readFile(String file) throws SerializationException, SQLException
}

public static void main(String[] args) throws Exception {
LOGGER.info("Starting up student-database...");

instance = new Application(args);

PluginLoader.getInstance().preloadPlugins();

if (!getInstance().suppressCmd()) {
LOGGER.info("Setting up command line...");
Command.registerCommands();
CommandLineUtils.setup();
}


LOGGER.info("Setting up server...");

Server.getInstance().getConnection().createTables();

Holiday.setupCurrentSchoolYear();
Expand All @@ -119,14 +129,17 @@ public static void main(String[] args) throws Exception {
GetRequestHandler.getInstance().registerHandlers();

if (getInstance().runsWebServer()) {
LOGGER.info("Starting WebServer...");
Server.getInstance().getWebServer().start();
}

PluginLoader.getInstance().enablePlugins();

LOGGER.info("Adding shutdown hook for plugin cleanup...");
Runtime.getRuntime().addShutdownHook(new Thread(() -> PluginLoader.getInstance().unloadPlugins(),"Plugin cleanup thread"));

try {
LOGGER.info("Starting main loop...");
while (true) {
if (!getInstance().suppressCmd()) {
CommandLineUtils.waitForCommandAndExec();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/igslandstuhl/database/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.SQLException;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;

Expand Down Expand Up @@ -61,7 +62,7 @@ public static Admin get(String username) {
try {
return Server.getInstance().processSingleRequest(Admin::fromSQL, "get_admin_by_username", SQL_FIELDS, username);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve Admin user '{}' from database", username, e);
return null;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/igslandstuhl/database/api/SchoolClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;

Expand Down Expand Up @@ -157,7 +158,7 @@ public List<Student> getStudents() {
"get_students_by_class", new String[] {"id"}, String.valueOf(id)
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve student list from database", e);
}
return studentIds.stream().map(Student::get).toList();
}
Expand All @@ -168,7 +169,6 @@ public void delete() throws SQLException {
try {
s.delete();
} catch (SQLException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}
});
Expand Down Expand Up @@ -214,7 +214,7 @@ public static SchoolClass get(int id) {
try {
return Server.getInstance().processSingleRequest(SchoolClass::fromSQL, "get_class_by_id", SQL_FIELDS, String.valueOf(id));
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SchoolClass with id {} from database", id, e);
return null;
}
}
Expand All @@ -229,7 +229,7 @@ public static SchoolClass get(String label) {
try {
return Server.getInstance().processSingleRequest(SchoolClass::fromSQL, "get_class_by_label", SQL_FIELDS, label);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SchoolClass with label '{}' from database", label, e);
return null;
}
}
Expand All @@ -254,7 +254,7 @@ public static SchoolClass getOrCreate(String label) {
try {
schoolClass = addClass(label, grade);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to create previously not existing class with label {} and grade {}", label, grade, e);
}
}
return schoolClass;
Expand All @@ -268,7 +268,7 @@ public static List<SchoolClass> getAll() {
"get_all_classes", new String[] {"id"}
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve a list of all classes from the database", e);
}
return ids.stream()
.map(SchoolClass::get)
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/de/igslandstuhl/database/api/SchoolYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.sql.SQLException;
import java.util.*;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;

Expand Down Expand Up @@ -124,7 +126,7 @@ public static SchoolYear get(int id) {
if (year != null) years.put(id, year);
return year;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SchoolYear with id {} from database", id, e);
return null;
}
}
Expand All @@ -134,7 +136,7 @@ public static SchoolYear get(String label) {
return get(Integer.parseInt(fields[0]));
}, "get_school_year_by_label", new String[] {"id"}, label);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SchoolYear with label '{}' from database", label, e);
return null;
}
}
Expand All @@ -156,7 +158,7 @@ public static List<SchoolYear> getAll() {
"get_all_school_years", SQL_FIELDS
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve a list of all school years from the database", e);
}
return all;
}
Expand All @@ -176,7 +178,7 @@ public static SchoolYear getCurrentYear() {
if (year != null) years.put(year.getId(), year);
return year;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to load current school year from database", e);
return null;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/igslandstuhl/database/api/SpecialTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;

Expand Down Expand Up @@ -89,7 +90,7 @@ public static SpecialTask get(int id) {
specialTasks.put(id, task);
return task;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SpecialTask with id {} from database", id, e);
return null;
}
}
Expand All @@ -114,7 +115,7 @@ public static List<SpecialTask> getSpecialTasksByName(String name) {
try {
Server.getInstance().processRequest(SpecialTask::addToCache, "get_special_tasks_by_name", SQL_FIELDS, name);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get SpecialTask with name {} from database", name, e);
return new ArrayList<>();
}
return specialTasks.values().stream()
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/de/igslandstuhl/database/api/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.api.results.StudentGenerationResult;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static Student get(int id) {
student.fetchTasks();
return student;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Student with id {} from database", id, e);
return null;
}
}
Expand All @@ -190,7 +191,7 @@ public static Student getByEmail(String email) {
} catch (NullPointerException e) {
return null;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Student with email {} from database", email, e);
return null;
}
}
Expand All @@ -210,7 +211,7 @@ public static List<Student> getAll() {
"get_all_students", SQL_FIELDS
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve student list from database", e);
}
return studentIDs.stream()
.map(Student::get)
Expand Down Expand Up @@ -247,7 +248,7 @@ public static String[] generatePasswords(int count, int length) {
try {
Thread.sleep(new Random().nextInt(1,10)); // Sleep to ensure different seeds
} catch (InterruptedException e) {
e.printStackTrace();
Application.LOGGER_API.error("Thread sleep while generating passwords was interrupted", e);
}
}
return passwords;
Expand Down Expand Up @@ -581,7 +582,7 @@ private void loadCurrentTopics() {
}
}
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to load current topics for '{}'", this.email, e);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/de/igslandstuhl/database/api/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import de.igslandstuhl.database.Application;
import de.igslandstuhl.database.server.Server;
import de.igslandstuhl.database.server.sql.SQLHelper;

Expand Down Expand Up @@ -94,7 +95,7 @@ public static Subject get(int id) {
subjects.put(id, subject);
return subject;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Subject with id {} from database", id, e);
return null;
}
}
Expand All @@ -116,7 +117,7 @@ public static Subject get(String name) {
subjects.put(subject.getId(), subject);
return subject;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Subject with name {} from database", name, e);
return null;
}
}
Expand All @@ -137,7 +138,7 @@ public static List<Subject> getAll() {
SQL_FIELDS
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve subject list from database", e);
}
return subjectIds.stream()
.map(Subject::get)
Expand Down Expand Up @@ -199,7 +200,7 @@ public List<Topic> getTopics(int grade) {
String.valueOf(id)
);
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve topic list for subject '{}' in grade {} from database", this.name, grade, e);
}
return topicIds.stream()
.map(Topic::get)
Expand All @@ -218,7 +219,7 @@ public int[] getGrades() {
new String[] {"grade"},
String.valueOf(id));
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to retrieve list of available grades for subject '{}' from database", this.name, e);
}
return grades.stream().mapToInt(Integer::intValue).toArray();
}
Expand All @@ -242,8 +243,7 @@ public void delete() throws SQLException {
try {
t.delete();
} catch (SQLException e) {
e.printStackTrace();
throw new IllegalStateException(e);
throw new IllegalStateException("Failed to delete topic " + t.getName() + " which is necessary to delete subject " + this.name, e);
}
}));
} catch (IllegalStateException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/igslandstuhl/database/api/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static Task get(int id) {
tasks.put(id, task);
return task;
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Task with id {} from database", id, e);
return null;
}
}
Expand All @@ -194,7 +194,7 @@ public static List<Task> getByName(String name) {
String[][] table = Server.getInstance().processRequest("get_tasks_by_name", new String[] {"id"}, name);
Arrays.stream(table).map(s -> s[0]).map(Integer::parseInt).map(Task::get).forEach((t) -> t.getId()); // Do something because streams are lazy
} catch (SQLException e) {
e.printStackTrace();
Application.LOGGER_API.error("Failed to get Task with name {} from database", name, e);
return new ArrayList<>();
}
return tasks.values().stream()
Expand Down
Loading
Loading