From e95557c6fd1f87ee85ace3c17d34b7a0d061700a Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 13 Mar 2019 16:13:21 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9A=D0=BE=D0=BC=D0=BF=D0=BE?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 3 ++ .../launchserver/components/Component.java | 35 ++++++++++++++++++ .../config/AuthHandlerAdapter.java | 6 +++ .../config/AuthProviderAdapter.java | 6 +++ .../launchserver/config/ComponentAdapter.java | 37 +++++++++++++++++++ .../config/HWIDHandlerAdapter.java | 6 +++ .../config/PermissionsHandlerAdapter.java | 6 +++ .../config/TextureProviderAdapter.java | 6 +++ 8 files changed, 105 insertions(+) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/components/Component.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index c12dbc13..2e65c755 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -18,6 +18,7 @@ import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.*; +import ru.gravit.launchserver.components.Component; import ru.gravit.utils.command.CommandHandler; import ru.gravit.utils.command.JLineCommandHandler; import ru.gravit.utils.command.StdCommandHandler; @@ -532,6 +533,7 @@ public static void initGson() { Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); + Launcher.gsonBuilder.registerTypeAdapter(Component.class, new ComponentAdapter()); Launcher.gson = Launcher.gsonBuilder.create(); //Human readable @@ -542,6 +544,7 @@ public static void initGson() { LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); + LaunchServer.gsonBuilder.registerTypeAdapter(Component.class, new ComponentAdapter()); LaunchServer.gson = LaunchServer.gsonBuilder.create(); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/components/Component.java b/LaunchServer/src/main/java/ru/gravit/launchserver/components/Component.java new file mode 100644 index 00000000..a29aa529 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/components/Component.java @@ -0,0 +1,35 @@ +package ru.gravit.launchserver.components; + +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +public class Component { + private static final Map> COMPONENTS = new ConcurrentHashMap<>(4); + private static boolean registredComp = false; + + public static void registerHandler(String name, Class adapter) { + VerifyHelper.verifyIDName(name); + VerifyHelper.putIfAbsent(COMPONENTS, name, Objects.requireNonNull(adapter, "adapter"), + String.format("Auth handler has been already registered: '%s'", name)); + } + + public static Class getHandlerClass(String name) { + return COMPONENTS.get(name); + } + + public static String getHandlerName(Class clazz) { + for (Map.Entry> e : COMPONENTS.entrySet()) { + if (e.getValue().equals(clazz)) return e.getKey(); + } + return null; + } + + public static void registerComponents() { + if (!registredComp) { + registredComp = true; + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java index 608b36ae..f796e921 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java @@ -2,6 +2,7 @@ import com.google.gson.*; import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -12,6 +13,11 @@ public class AuthHandlerAdapter implements JsonSerializer, JsonDese public AuthHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = AuthHandler.getHandlerClass(typename); + if(cls == null) + { + LogHelper.error("AuthHandler %s not found", typename); + return null; + } return (AuthHandler) context.deserialize(json, cls); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java index a45c6e92..f4e74c95 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java @@ -2,6 +2,7 @@ import com.google.gson.*; import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -12,6 +13,11 @@ public class AuthProviderAdapter implements JsonSerializer, JsonDe public AuthProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = AuthProvider.getProviderClass(typename); + if(cls == null) + { + LogHelper.error("AuthProvider %s not found", typename); + return null; + } return (AuthProvider) context.deserialize(json, cls); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java new file mode 100644 index 00000000..f33d44ad --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java @@ -0,0 +1,37 @@ +package ru.gravit.launchserver.config; + +import com.google.gson.*; +import ru.gravit.launchserver.auth.handler.AuthHandler; +import ru.gravit.launchserver.components.Component; +import ru.gravit.utils.helper.LogHelper; + +import java.lang.reflect.Type; + +public class ComponentAdapter implements JsonSerializer, JsonDeserializer { + private static final String PROP_NAME = "component"; + + @Override + public Component deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = Component.getHandlerClass(typename); + if(cls == null) + { + LogHelper.error("Component %s not found", typename); + return null; + } + + + return (Component) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(Component src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject jo = context.serialize(src).getAsJsonObject(); + + @SuppressWarnings("unchecked") + String classPath = Component.getHandlerName((Class) src.getClass()); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java index bcc887b7..3a2d994c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java @@ -2,6 +2,7 @@ import com.google.gson.*; import ru.gravit.launchserver.auth.hwid.HWIDHandler; +import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -12,6 +13,11 @@ public class HWIDHandlerAdapter implements JsonSerializer, JsonDese public HWIDHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = HWIDHandler.getHandlerClass(typename); + if(cls == null) + { + LogHelper.error("HWIDHandler %s not found", typename); + return null; + } return (HWIDHandler) context.deserialize(json, cls); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java index 83dd21e9..bc2e21ae 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java @@ -2,6 +2,7 @@ import com.google.gson.*; import ru.gravit.launchserver.auth.permissions.PermissionsHandler; +import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -12,6 +13,11 @@ public class PermissionsHandlerAdapter implements JsonSerializer cls = PermissionsHandler.getHandlerClass(typename); + if(cls == null) + { + LogHelper.error("PermissionsHandler %s not found", typename); + return null; + } return (PermissionsHandler) context.deserialize(json, cls); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java index e79041c8..75aba651 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java @@ -2,6 +2,7 @@ import com.google.gson.*; import ru.gravit.launchserver.texture.TextureProvider; +import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; @@ -12,6 +13,11 @@ public class TextureProviderAdapter implements JsonSerializer, public TextureProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = TextureProvider.getProviderClass(typename); + if(cls == null) + { + LogHelper.error("TextureProvider %s not found", typename); + return null; + } return (TextureProvider) context.deserialize(json, cls);