Skip to content

Commit

Permalink
・ソースコードリファクタリング
Browse files Browse the repository at this point in the history
・ConnectionPool用処理を追加
・Config用メソッドをリファクタリング
  • Loading branch information
waxsd100 committed Aug 22, 2020
1 parent eff3689 commit 30c3c83
Show file tree
Hide file tree
Showing 25 changed files with 821 additions and 206 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dependencies {
compile 'org.apache.logging.log4j:log4j-core'
compile 'org.slf4j:slf4j-log4j12:1.7.30'
compile 'com.zaxxer:HikariCP:3.4.5'
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.31.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
compile group: 'org.apache.commons', name: 'commons-configuration2', version: '2.7'
compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4'
Expand All @@ -43,4 +45,8 @@ dependencyManagement {
}

compileJava.options.encoding = 'UTF-8'
tasks.withType(AbstractCompile)*.options*.encoding = tasks.withType(GroovyCompile)*.groovyOptions*.encoding = 'UTF-8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

tasks.withType(AbstractCompile)*.options*.encoding = tasks.withType(GroovyCompile)*.groovyOptions*.encoding = 'UTF-8'
9 changes: 9 additions & 0 deletions config/Database.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
connection_pool_class=jp.skyblock.Utility.db.ConnectionPool4SL
db=rolebot
#serverName=127.0.0.1
hosts=127.0.0.1
port=****
user=****
password=****
min=1
max=10
4 changes: 0 additions & 4 deletions config/database.properties

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/jp/skyblock/Command/DefaultCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package jp.skyblock.Command;

import jp.skyblock.Utility.ExceptionIf;
import jp.skyblock.Executer.CommandExecIf;

public class DefaultCommand extends Throwable implements CommandExecIf {
@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/jp/skyblock/Command/PingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

package jp.skyblock.Command;

import jp.skyblock.Core.Config;
import jp.skyblock.Core.Const.Constant;
import jp.skyblock.Core.Observer.Message.Received;
import jp.skyblock.Executer.CommandExecIf;
import jp.skyblock.Utility.EmojiUtil;
import jp.skyblock.Utility.ExceptionIf;
import jp.skyblock.Utility.RoleUtil;
import jp.skyblock.Utility.WelcomeMessage;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -35,8 +34,8 @@ public class PingCommand implements CommandExecIf {

@Override
public void execute() {
MessageReceivedEvent event = received.getEvent();
String[] cmdParam = received.getCmdParam();
MessageReceivedEvent event = Received.getEvent();
String[] cmdParam = Received.getCmdParam();
Guild guild = event.getGuild();
User author = event.getAuthor();
Member member = guild.getMember(author);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/jp/skyblock/Command/RoleCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package jp.skyblock.Command;

import jp.skyblock.Core.Config;
import jp.skyblock.Core.Const.Constant;
import jp.skyblock.Core.Observer.Message.Received;
import jp.skyblock.Executer.CommandExecIf;
import jp.skyblock.Utility.EmojiUtil;
import jp.skyblock.Utility.RoleUtil;
import jp.skyblock.Utility.WelcomeMessage;
Expand Down Expand Up @@ -101,5 +101,4 @@ public void execute() throws Exception {
public Object executeResponse(Object obj) {
return null;
}

}
65 changes: 44 additions & 21 deletions src/main/java/jp/skyblock/Core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package jp.skyblock.Core;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Configurations;
Expand All @@ -18,14 +19,21 @@
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;

import static org.apache.commons.configuration2.ConfigurationConverter.getConfiguration;

public class Config {
private static final Parameters params = new Parameters();
private static final Configurations configs = new Configurations();
@Deprecated
private static PropertiesConfiguration config;
private final Parameters params = new Parameters();
private final Configurations configs = new Configurations();

public Config() {
}
Expand All @@ -39,14 +47,32 @@ public static String getDir() {
return System.getProperty("user.dir");
}

/**
* Properties File path から Properties を取得
*
* @return Properties
*/
public static Properties loadProperties(final String basePath) {
try {
try (InputStream inputStream = new FileInputStream(basePath)) {
Properties properties = new Properties();
properties.load(inputStream);
return properties;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* プロパティ値取得
*
* @param key
* @return
*/
public String getProperty(final String key) {
return getProperty(key, "");
public String getValue(final String propFile, final String key) {
return getValue(propFile, key, "");
}

/**
Expand All @@ -57,9 +83,11 @@ public String getProperty(final String key) {
* @return キーが存在しない場合、デフォルト値
* 存在する場合、値
*/
public String getProperty(final String key, final String defaultValue) {
public String getValue(final String propFile, String key, final String defaultValue) {
Properties conf = loadProperties(propFile);
try {
return config.getString(key, defaultValue);
Configuration config = getConfiguration(conf);
return config.getString(key, defaultValue).trim();
} catch (NullPointerException e) {
return "";
}
Expand All @@ -69,15 +97,16 @@ public String getProperty(final String key, final String defaultValue) {
* @param key
* @return
*/
public ArrayList<String> getPropertyList(final String key) {
return new ArrayList<>(Arrays.asList(StringUtils.split(getProperty(key), ",")));
public ArrayList<String> getValueList(final String propFile, final String key) {
return new ArrayList<>(Arrays.asList(StringUtils.split(getValue(propFile, key), ",")));
}

/**
* config Load
*
* @param filePath 読み込むファイルプロパティPath
*/
@Deprecated
public void load(String filePath) {
try {
config = configs.properties(new File(filePath));
Expand All @@ -86,22 +115,19 @@ public void load(String filePath) {
}
}


/**
* @param propFile
* @param key
* @param value
*/
public void saveList(String propFile, String key, String value) {
load(propFile);
if ("".equals(getProperty(key))) {
if ("".equals(getValue(propFile, key, ""))) {
save(propFile, key, value);
} else {
ArrayList<String> in = getPropertyList(key);
ArrayList<String> in = getValueList(propFile, key);
in.add(value);
ArrayList<String> out = new ArrayList<>(new HashSet<>(in));
save(propFile, key, String.valueOf(StringUtils.join(out, ',')));
System.out.println(out);
}
}

Expand All @@ -112,19 +138,16 @@ public void saveList(String propFile, String key, String value) {
* @param key SaveSetting Key
* @param value SaveSetting Value
*/
public void save(String propFile, String key, String value) {
PropertiesConfiguration conf;

public synchronized void save(String propFile, String key, String value) {
FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
.configure(params.fileBased().setFileName(propFile));

try {
conf = builder.getConfiguration();
if (!conf.containsKey(key)) {
conf.addProperty(key, value);
} else {
PropertiesConfiguration conf = builder.getConfiguration();
if (conf.containsKey(key)) {
conf.setProperty(key, value);
} else {
conf.addProperty(key, value);
}
builder.save();
} catch (ConfigurationException e) {
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/jp/skyblock/Core/Const/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,20 @@

package jp.skyblock.Core.Const;

import jp.skyblock.Core.Config;
import jp.skyblock.TusbChangRoleBot;
import jp.skyblock.Utility.EmojiUtil;
import jp.skyblock.Utility.ExceptionIf;
import org.apache.log4j.Logger;

import java.io.File;

import static jp.skyblock.Core.Config.getDir;

public class Constant {
public static final EmojiUtil emj = new EmojiUtil();
public static final Config config = new Config();


public static final Logger logger = Logger.getLogger(TusbChangRoleBot.class);
public static final String DISCORD_TOKEN = new Config().getProperty("Token");
public static final String PREFIX = "!";

// Command
public static final String COMMAND_HELLO = "hello";
public static final String COMMAND_ROLE = "role";

// Emoji
public static final String EmojiSeparator = ":";
public static String DATABASE_PROP_FILE;
public static String DISCORD_PROP_FILE;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@

package jp.skyblock.Core.Const.Enums;

public enum CommandExceptionType {
public enum CommandError {
PERMISSIONS_DENIED("製作者のみ使用可能です");

//TODO ErrorMessage

String errorMessage;

CommandExceptionType() {
CommandError() {
}

CommandExceptionType(String errorMessage) {
CommandError(String errorMessage) {
this.errorMessage = errorMessage;
}
}
20 changes: 20 additions & 0 deletions src/main/java/jp/skyblock/Core/Const/Enums/DatabaseError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* *************************************************
* TusbChangRoleBot
* Copyright (c) 2020 commandExceptionType.java
*
* This software is released under the TUSB.
* see https://github.com/TUSB/TusbChangRoleBot
* ************************************************
*/

package jp.skyblock.Core.Const.Enums;

import jp.skyblock.Utility.Exception.ExceptionIf;

public enum DatabaseError implements ExceptionIf.ErrorCode {
DatabaseInit,
DatabaseState,
DatabaseTransaction,
;
}
13 changes: 6 additions & 7 deletions src/main/java/jp/skyblock/Core/Const/Enums/RoleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
package jp.skyblock.Core.Const.Enums;

public enum RoleType {
ADMIN(290015220519403520L, "管理者"), // ErrorLogを飛ばす先
CREATOR(290039249149886464L, "製作者"), // 管理者権限利用可能用ロール
FOLLOWER(691367915827757096L, "Follower"), // 制作関連の 通知を飛ばすロール
YOUTUBE_FOLLOWER(695706490371047526L, "Youtube Follow"), // Youtube 通知を飛ばすロール
TWITTER_FOLLOWER(716591043457581067L, "Twitter Follow"), // Twitter 通知を飛ばすロール
MEMBER(716604975144239115L, "メンバー"), // 認証済みメンバーロール Confirmed
EVERYONE(290014442748379137L, "everyone"); // 未認証メンバーロール (everyone)
ADMIN(290015220519403520L, "管理者"), // ErrorLogを飛ばす先
CREATOR(290039249149886464L, "製作者"), // 管理者権限利用可能用ロール
YOUTUBE_FOLLOWER(695706490371047526L, "Youtube"), // Youtube 通知を飛ばすロール
TWITTER_FOLLOWER(716591043457581067L, "Twitter"), // Twitter 通知を飛ばすロール
MEMBER(716604975144239115L, "メンバー"), // 認証済みメンバーロール Confirmed
EVERYONE(290014442748379137L, "everyone"); // 未認証メンバーロール (everyone)


private Long Id;
Expand Down
Loading

1 comment on commit 30c3c83

@waxsd100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLiteでConnectionPoolは意味ないので実質飾り

Please sign in to comment.