Skip to content

Commit

Permalink
Release v3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Mar 13, 2023
2 parents 70ce630 + b4fdede commit bc0e2db
Show file tree
Hide file tree
Showing 132 changed files with 1,232 additions and 659 deletions.
8 changes: 4 additions & 4 deletions KPMAlias/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.kunlab.kpm</groupId>
<artifactId>TeamKunPluginManager</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -22,19 +22,19 @@
<dependency>
<groupId>org.kunlab.kpm</groupId>
<artifactId>KPMCommon</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kunlab.kpm</groupId>
<artifactId>KPMModels</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kunlab.kpm</groupId>
<artifactId>KPMResolver</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion KPMCommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.kunlab.kpm</groupId>
<artifactId>TeamKunPluginManager</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
34 changes: 27 additions & 7 deletions KPMCommon/src/main/java/org/kunlab/kpm/DebugConstants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.kunlab.kpm;

import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* KPM のデバッグ状態の定数です。
Expand All @@ -13,51 +15,48 @@ public class DebugConstants
* VM プロパティの {@code kpm.enable-debug} が {@code true} である場合に有効化されます。
*/
public static final boolean DEBUG_MODE;

/**
* 不必要な KPM のアップグレードを許可するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.debug.allow-unneeded-upgrade} が {@code true} である場合に有効化されます。
*/
public static final boolean ALLOW_UNNEEDED_UPGRADE;

/**
* データベースのコネクションを出力するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.kpm.debug.db.trace-connection} が {@code true} である場合に有効化されます。
*/
public static final boolean DB_CONNECTION_TRACE;

/**
* HTTP リクエストの詳細を出力するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.debug.http.trace-request} が {@code true} である場合に有効化されます。
*/
public static final boolean HTTP_REQUEST_TRACE;

/**
* HTTP リクエストのリダイレクトを出力するかどうかを示します。
* デバッグモードが有効か、{@link #HTTP_REQUEST_TRACE} が有効か、VM プロパティの {@code kpm.debug.http.trace-redirect} が {@code true} である場合に有効化されます。
*/
public static final boolean HTTP_REDIRECT_TRACE;

/**
* プラグインメタデータの操作を出力するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.debug.plugin-meta.trace-operation} が {@code true} である場合に有効化されます。
*/
public static final boolean PLUGIN_META_OPERATION_TRACE;

/**
* 依存関係ツリーの操作を出力するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.debug.plugin-meta.trace-dependency-tree} が {@code true} である場合に有効化されます。
*/
public static final boolean PLUGIN_META_DEPENDENCY_TREE_TRACE;

/**
* メッセージの出力時に、翻訳キーを出力するかどうかを示します。
* デバッグモードが有効か、VM プロパティの {@code kpm.debug.raw-message} が {@code true} である場合に有効化されます。
*/
public static final boolean RAW_MESSAGE;

private static final Logger LOGGER;

static
{
LOGGER = LoggerFactory.getLogger(DebugConstants.class);

DEBUG_MODE = Boolean.getBoolean("kpm.enable-debug");

ALLOW_UNNEEDED_UPGRADE = DEBUG_MODE || Boolean.getBoolean("kpm.debug.allow-unneeded-upgrade");
Expand All @@ -73,4 +72,25 @@ public class DebugConstants
RAW_MESSAGE = DEBUG_MODE || Boolean.getBoolean("kpm.debug.raw-message");

}

public static Logger getLogger()
{
return LOGGER;
}

public static void debugLog(String message, boolean flag)
{
if (flag)
LOGGER.info(message);
}

public static void debugLog(String message)
{
LOGGER.info(message);
}

public static void onException(Exception e)
{
LOGGER.error("An exception occurred", e);
}
}
14 changes: 14 additions & 0 deletions KPMCommon/src/main/java/org/kunlab/kpm/ExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.kunlab.kpm;

/**
* KPM 内部で発生した例外を全てキャッチする機能のインターフェースです。
*/
public interface ExceptionHandler
{
/**
* 例外をキャッチします。
*
* @param e 例外
*/
void report(Throwable e);
}
23 changes: 13 additions & 10 deletions KPMCommon/src/main/java/org/kunlab/kpm/TokenStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
Expand All @@ -33,29 +32,34 @@ public class TokenStore
private static final String SALT = "TeamKunPluginManager>114514";
private static final String SECONDARY_KEY = "KPM>Origin>114514";

private static final int ALGO_KEY_SIZE = 256;
private static final int ALGO_KEY_ITERATION = 65536;

private final Path tokenPath;

private final byte[] key;
private final ExceptionHandler exceptionHandler;
private final SecretKeySpec SECONDARY_KEY_SPEC;

private String tokenCache;

@SneakyThrows(IOException.class)
public TokenStore(@NotNull Path tokenPath, @NotNull Path keyPath)
public TokenStore(@NotNull Path tokenPath, @NotNull Path keyPath, @NotNull ExceptionHandler exceptionHandler)
{
if (Files.isDirectory(tokenPath) || Files.isDirectory(keyPath))
throw new IllegalArgumentException("Path must be a file");

this.tokenPath = tokenPath;
this.key = this.getkey(keyPath);
this.exceptionHandler = exceptionHandler;
this.SECONDARY_KEY_SPEC = getKeySpec(SECONDARY_KEY);
}

@SneakyThrows({NoSuchAlgorithmException.class, InvalidKeySpecException.class})
private static SecretKeySpec getKeySpec(String key)
{
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(key.toCharArray(), SALT.getBytes(), 65536, 256);
KeySpec spec = new PBEKeySpec(key.toCharArray(), SALT.getBytes(), ALGO_KEY_ITERATION, ALGO_KEY_SIZE);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
}

Expand All @@ -73,7 +77,7 @@ private static void setPermission(Path path)
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rw-------"));
}

private static boolean isTokenAlive(String token)
private boolean isTokenAlive(String token)
{
try (HTTPResponse apiResponse = Requests.request(RequestContext.builder()
.url("https://api.github.com/rate_limit")
Expand All @@ -85,7 +89,7 @@ private static boolean isTokenAlive(String token)
}
catch (IOException e)
{
e.printStackTrace();
this.exceptionHandler.report(e);
return false;
}
}
Expand Down Expand Up @@ -135,7 +139,7 @@ private byte[] getkey(Path keyPath) throws IOException
try
{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
keyGenerator.init(ALGO_KEY_SIZE);
SecretKey key = keyGenerator.generateKey();
byte[] keyBytes = key.getEncoded();
Files.write(keyPath, keyBytes);
Expand All @@ -158,7 +162,7 @@ private byte[] getkey(Path keyPath) throws IOException
*/
public void storeToken(String token, boolean checkLiving) throws IOException
{
if (checkLiving && !isTokenAlive(token))
if (checkLiving && !this.isTokenAlive(token))
throw new IllegalStateException("Token is not available.");

byte[] tokenBytes = this.encryptToken(token);
Expand Down Expand Up @@ -247,8 +251,7 @@ public String getToken()
}
catch (IOException e)
{
System.out.println("Failed to load token.");
throw new UncheckedIOException(e);
this.exceptionHandler.report(e);
}
}
return this.tokenCache;
Expand All @@ -272,6 +275,6 @@ public boolean isTokenAlive()
if (this.tokenCache == null)
return false;

return isTokenAlive(this.tokenCache);
return this.isTokenAlive(this.tokenCache);
}
}
3 changes: 2 additions & 1 deletion KPMCommon/src/main/java/org/kunlab/kpm/db/ResultRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.kunlab.kpm.DebugConstants;

import java.sql.Date;
import java.sql.ResultSet;
Expand Down Expand Up @@ -29,7 +30,7 @@ private void handleException()
}
catch (SQLException e)
{
e.printStackTrace();
DebugConstants.onException(e);
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions KPMCommon/src/main/java/org/kunlab/kpm/db/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -28,6 +29,9 @@
*/
public class Transaction implements AutoCloseable
{
private static final int MAX_POOL_SIZE = 20;
private static final long CONN_LEAK_DETECT_THRESHOLD = 300000L;

/**
* Dbのコネクションです。
*/
Expand Down Expand Up @@ -134,8 +138,8 @@ public static HikariDataSource createDataSource(@NotNull Path databasePath)
config.setDriverClassName("org.sqlite.JDBC");
config.setJdbcUrl("jdbc:sqlite:" + databasePath);

config.setMaximumPoolSize(20);
config.setLeakDetectionThreshold(300000);
config.setMaximumPoolSize(MAX_POOL_SIZE);
config.setLeakDetectionThreshold(CONN_LEAK_DETECT_THRESHOLD);
config.setAutoCommit(false);

return new HikariDataSource(config);
Expand All @@ -158,7 +162,7 @@ private void printCreatedOnDebug()
caller = stack[i + 1];
if (!caller.getClassName().equals(this.getClass().getName()))
{
System.out.println("Transaction(" + this.connection.hashCode() +
DebugConstants.debugLog("Transaction(" + this.connection.hashCode() +
") created by " + caller.getClassName() + "#" + caller.getMethodName() + " at " +
caller.getFileName() + ":" + caller.getLineNumber());
break;
Expand Down Expand Up @@ -646,8 +650,10 @@ public void close()
if (!this.connection.isClosed())
this.connection.close();

if (DebugConstants.DB_CONNECTION_TRACE)
System.out.println("Transaction(" + this.connection.hashCode() + ") is closed.");
DebugConstants.debugLog(
"Transaction(" + this.connection.hashCode() + ") is closed.",
DebugConstants.DB_CONNECTION_TRACE
);
}
catch (SQLException ignored)
{
Expand Down Expand Up @@ -695,9 +701,8 @@ public boolean tryAdvance(Consumer<? super ResultRow> action)
{
this.result.getStatement().getConnection().close();
}
catch (SQLException e1)
catch (SQLException ignored)
{
e1.printStackTrace();
}
}
throw new IllegalStateException(e);
Expand Down Expand Up @@ -759,9 +764,9 @@ public void close() throws SQLException
* @param max 最大件数
* @return 変換されたList
*/
public ArrayList<T> mapToList(Function<ResultRow, T> resultMapper, long max)
public List<T> mapToList(Function<? super ResultRow, ? extends T> resultMapper, long max)
{
ArrayList<T> list = new ArrayList<>();
List<T> list = new ArrayList<>();

try
{
Expand All @@ -782,7 +787,7 @@ public ArrayList<T> mapToList(Function<ResultRow, T> resultMapper, long max)
* @param resultMapper マッピング関数
* @return 変換されたList
*/
public ArrayList<T> mapToList(Function<ResultRow, T> resultMapper)
public List<T> mapToList(Function<ResultRow, T> resultMapper)
{
return this.mapToList(resultMapper, -1);
}
Expand Down

0 comments on commit bc0e2db

Please sign in to comment.