From d0ff9153d2955941de95fcd05fd5475c442f334d Mon Sep 17 00:00:00 2001 From: ChessNineeee <709030194@qq.com> Date: Wed, 7 Apr 2021 14:00:34 +0800 Subject: [PATCH] Persist Implementation --- .../minsecurity/identity/persist/Persist.java | 91 +++++++++++++++++++ .../identity/persist/sqlite/db/Db.java | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/main/java/minsecurity/identity/persist/Persist.java diff --git a/src/main/java/minsecurity/identity/persist/Persist.java b/src/main/java/minsecurity/identity/persist/Persist.java new file mode 100644 index 0000000..c0eea37 --- /dev/null +++ b/src/main/java/minsecurity/identity/persist/Persist.java @@ -0,0 +1,91 @@ +package minsecurity.identity.persist; + +import minsecurity.Common; +import minsecurity.identity.Identity; +import minsecurity.identity.persist.sqlite.Sqlite; +import minsecurity.identity.persist.sqlite.db.Db; + +import java.util.List; + +public class Persist { + private static int persistPlugin = Common.SQLITE; // 默认使用Sqlite + + /** + * 存储一个Identity,生成identity结构体和存储分开 + * @param identity + * @throws Exception + */ + public static void persistIdentity(Identity identity) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + Db.persistIdentity(identity); + } + } + + /** + * 通过Identity的名称,删除一个Identity对象 + * @param name + * @throws Exception + */ + public static void deleteIdentityByNameFromStorage(String name) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + Db.deleteIdentityByName(name); + } + } + + /** + * 通过Identity的名称,获得一个Identity对象 + * @param name + * @param passwd + * @return + * @throws Exception + */ + public static Identity getIdentityByNameFromStorage(String name, String passwd) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + return Db.getIdentityByNameFromStorage(name); + } + return null; + } + + /** + * 获得所有的身份实例 + * @param passwd + * @return + * @throws Exception + */ + public static List getAllIdentityFromStorage(String passwd) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + return Db.getAllIdentityFromStorage(); + } + return null; + } + + /** + * 通过Identity的名称,设置默认的Identity + * @param name + * @throws Exception + */ + public static void setDefaultIdentityByNameInStorage(String name) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + Db.SetDefaultIdentityByNameInStorage(name); + } + } + + /** + * 获得当前系统默认使用的Identity对象 + * @param passwd + * @return + * @throws Exception + */ + public static Identity getDefaultIdentityFromStorage(String passwd) throws Exception{ + switch (persistPlugin){ + case Common.SQLITE: + return Db.getDefaultIdentityFromStorage(); + } + return null; + } +} diff --git a/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java b/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java index d028be6..403d54a 100644 --- a/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java +++ b/src/main/java/minsecurity/identity/persist/sqlite/db/Db.java @@ -132,7 +132,7 @@ public final class Db { stmt.close(); } - public static List getAllIdentityFromStorage(String name) throws Exception { + public static List getAllIdentityFromStorage() throws Exception { Connection c = Sqlite.getInstance().getConn(); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from identityinfo"); @@ -243,7 +243,7 @@ public final class Db { c.close(); } - public Identity getDefaultIdentityFromStorage() throws Exception{ + public static Identity getDefaultIdentityFromStorage() throws Exception{ Connection c = Sqlite.getInstance().getConn(); PreparedStatement pstmt = c.prepareStatement("SELECT * from identityinfo where is_default= ? LIMIT 1"); pstmt.setInt(1, 1);