Skip to content

Commit

Permalink
Merge pull request #173 from SOBotics/hotfix/170-too-many-open-files
Browse files Browse the repository at this point in the history
Fix "Too many open files"
  • Loading branch information
FelixSFD authored Apr 25, 2018
2 parents 587672f + d99fb7a commit 4749f01
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.felixsfd.stackoverflow</groupId>
<artifactId>guttenberg</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>

<dependencies>
<dependency>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/sobotics/guttenberg/clients/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.sobotics.guttenberg.roomdata.SOBoticsWorkshopChatRoom;
import org.sobotics.guttenberg.services.RunnerService;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.StatusUtils;

import fr.tunaki.stackoverflow.chat.StackExchangeClient;
Expand All @@ -40,7 +41,7 @@ private Client(){
public static void main(String[] args) {
Properties loggerProperties = new Properties();
try{
loggerProperties.load(new FileInputStream(FilePathUtils.loggerPropertiesFile));
loggerProperties = FileUtils.getPropertiesFromFile(FilePathUtils.loggerPropertiesFile);

String levelStr = loggerProperties.getProperty("level");
Level newLevel = Level.toLevel(levelStr, Level.ERROR);
Expand All @@ -58,7 +59,7 @@ public static void main(String[] args) {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.sobotics.guttenberg.finders.RelatedAnswersFinder;
import org.sobotics.guttenberg.printers.SoBoticsPostPrinter;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.StatusUtils;

import fr.tunaki.stackoverflow.chat.Room;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void execute() throws Throwable {
LOGGER.info("Starting Guttenberg.execute() ...");

try {
props.load(new FileInputStream(FilePathUtils.generalPropertiesFile));
props = FileUtils.getPropertiesFromFile(FilePathUtils.generalPropertiesFile);
} catch (IOException e) {
LOGGER.warn("Could not load general properties", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void globalCommand(Room room, MessagePostedEvent event, RunnerService ins
Properties prop = new Properties();

try {
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
username = prop.getProperty("username").substring(0, 3).toLowerCase();
} catch (IOException e) {
LOGGER.error("Could not load login.properties", e);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sobotics/guttenberg/commands/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;

import com.google.gson.JsonObject;

Expand Down Expand Up @@ -51,7 +52,7 @@ public void execute(Room room, RunnerService instance) {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("Could not read login.properties", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.PostUtils;
import org.sobotics.guttenberg.utils.PrintUtils;

Expand Down Expand Up @@ -278,7 +279,7 @@ public static void main(String[] args) throws Exception {
Properties prop = new Properties();

try {
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
} catch (IOException e) {
LOGGER.error("Could not read login.properties", e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.JsonUtils;
import org.sobotics.guttenberg.utils.PostUtils;

Expand Down Expand Up @@ -217,7 +218,7 @@ public static void main(String[] args) throws Exception {
Properties prop = new Properties();

try {
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
} catch (IOException e) {
LOGGER.error("Could not read login.properties", e);
}
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/org/sobotics/guttenberg/commands/LogLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Level;
Expand All @@ -11,6 +12,7 @@
import org.sobotics.guttenberg.services.RunnerService;
import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.redunda.PingService;

import fr.tunaki.stackoverflow.chat.Message;
Expand Down Expand Up @@ -83,15 +85,30 @@ public void execute(Room room, RunnerService instance) {
//the logging-level for this instance should be changed
Level newLevel = Level.toLevel(levelString);
LogManager.getRootLogger().setLevel(newLevel);
FileOutputStream fOut = null;

try {
Properties loggingProp = new Properties();
loggingProp.load(new FileInputStream(FilePathUtils.loggerPropertiesFile));
loggingProp = FileUtils.getPropertiesFromFile(FilePathUtils.loggerPropertiesFile);
loggingProp.setProperty("level", levelString.toLowerCase());
loggingProp.store(new FileOutputStream(FilePathUtils.loggerPropertiesFile), null);

fOut = new FileOutputStream(FilePathUtils.loggerPropertiesFile);

loggingProp.store(fOut, null);

fOut.close();
} catch (Exception e) {
LOGGER.error("Error while saving the new log-level to the logging.properties", e);
}
finally {
if (fOut != null) {
try {
fOut.close();
} catch (IOException e) {
LOGGER.error("Could not close FileOutputStream!", e);
}
}
}

LOGGER.info("Logging-level successfully changed to " + levelString);
room.send("Instance *" + PingService.location + "* is now using the logging-level **" + levelString.toUpperCase() + "**");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sobotics/guttenberg/commands/Quota.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.StatusUtils;
import org.sobotics.redunda.PingService;

Expand Down Expand Up @@ -36,7 +37,7 @@ public void execute(Room room, RunnerService instance) {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("Error: ", e);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/sobotics/guttenberg/commands/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.utils.CommandUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.StatusUtils;
import org.sobotics.redunda.PingService;
import org.sobotics.guttenberg.services.RunnerService;
Expand Down Expand Up @@ -37,9 +38,10 @@ public void execute(Room room, RunnerService instance) {
Properties prop2 = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
InputStream is = Status.class.getResourceAsStream("/guttenberg.properties");
prop2.load(is);
is.close();
}
catch (IOException e){
LOGGER.error("Could not load properties", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;

/**
* Represents a plagiarized post
Expand Down Expand Up @@ -91,7 +92,7 @@ public boolean isValidMatch() {
int minimumLength = 200;
Properties quantifiers = new Properties();
try {
quantifiers.load(new FileInputStream(FilePathUtils.generalPropertiesFile));
quantifiers = FileUtils.getPropertiesFromFile(FilePathUtils.generalPropertiesFile);
minimumLength = new Integer(quantifiers.getProperty("minimumPostLength", "200"));
} catch (Throwable e) {
LOGGER.warn("Could not load quantifiers from general.properties. Using hardcoded", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.sobotics.guttenberg.entities.Post;
import org.sobotics.guttenberg.services.ApiService;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.PostUtils;
import org.sobotics.guttenberg.utils.StatusUtils;

Expand All @@ -38,7 +39,7 @@ public static List<Post> findRecentAnswers() {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("Could not load login.properties", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.sobotics.guttenberg.services.ApiService;
import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.PostUtils;

import com.google.gson.JsonElement;
Expand Down Expand Up @@ -69,7 +70,7 @@ private void fetchRelatedAnswers() {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("Could not load login.properties", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.sobotics.guttenberg.entities.Post;
import org.sobotics.guttenberg.services.ApiService;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.PostUtils;

import com.google.gson.JsonElement;
Expand Down Expand Up @@ -49,7 +50,7 @@ public List<Post> fetchRelatedAnswers() {
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("Could not load login.properties", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.entities.Post;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.PostUtils;

import info.debatty.java.stringsimilarity.JaroWinkler;
Expand Down Expand Up @@ -39,7 +40,7 @@ public boolean check() {
LOGGER.trace("Checking for " + LABEL);
Properties prop = new Properties();
try {
prop.load(new FileInputStream(FilePathUtils.generalPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.generalPropertiesFile);
} catch (IOException e) {
LOGGER.warn("Could not load general.properties. Using hardcoded value", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.entities.Post;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;

import info.debatty.java.stringsimilarity.JaroWinkler;

Expand Down Expand Up @@ -49,7 +50,7 @@ public static double similarityOf(Post targetPost, Post originalPost) {

Properties quantifiers = new Properties();
try {
quantifiers.load(new FileInputStream(FilePathUtils.generalPropertiesFile));
quantifiers = FileUtils.getPropertiesFromFile(FilePathUtils.generalPropertiesFile);
} catch (IOException e) {
LOGGER.warn("Could not load quantifiers from general.properties. Using hardcoded", e);
}
Expand Down Expand Up @@ -117,7 +118,7 @@ public boolean check() {
//get the report threshold
Properties prop = new Properties();
try {
prop.load(new FileInputStream(FilePathUtils.generalPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.generalPropertiesFile);
} catch (IOException e) {
LOGGER.warn("Could not load general.properties. Using hardcoded value", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.sobotics.guttenberg.utils.ApiUtils;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.JsonUtils;

import com.google.gson.JsonObject;
Expand All @@ -35,7 +36,7 @@ public ApiService(String site){
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.sobotics.guttenberg.clients.Guttenberg;
import org.sobotics.guttenberg.roomdata.BotRoom;
import org.sobotics.guttenberg.utils.FilePathUtils;
import org.sobotics.guttenberg.utils.FileUtils;
import org.sobotics.guttenberg.utils.StatusUtils;
import org.sobotics.redunda.PingService;
import org.sobotics.redunda.PingServiceDelegate;
Expand Down Expand Up @@ -43,7 +44,7 @@ public RunnerService(StackExchangeClient client, List<BotRoom> rooms) {
public void start() {
Properties prop = new Properties();
try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
}
catch (IOException e){
LOGGER.error("login.properties could not be loaded!", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/sobotics/guttenberg/utils/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static boolean checkForCommand(String message, String command){
Properties prop = new Properties();

try{
prop.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
prop = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);
username = prop.getProperty("username").substring(0,3).toLowerCase();
}
catch (IOException e){
Expand Down Expand Up @@ -87,7 +87,7 @@ public static int getPostIdFromUrl(String input) throws NumberFormatException, I
Properties props = new Properties();

try {
props.load(new FileInputStream(FilePathUtils.loginPropertiesFile));
props = FileUtils.getPropertiesFromFile(FilePathUtils.loginPropertiesFile);

String cpUrl = props.getProperty("copypastor_url");

Expand Down
Loading

0 comments on commit 4749f01

Please sign in to comment.