|
| 1 | +/* |
| 2 | + * Source++, the open-source live coding platform. |
| 3 | + * Copyright (C) 2022 CodeBrig, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published |
| 7 | + * by the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package spp.probe.services.common.serialize |
| 19 | + |
| 20 | +import com.google.gson.Gson |
| 21 | +import com.google.gson.TypeAdapter |
| 22 | +import com.google.gson.TypeAdapterFactory |
| 23 | +import com.google.gson.internal.bind.JsogRegistry |
| 24 | +import com.google.gson.reflect.TypeToken |
| 25 | +import com.google.gson.stream.JsonReader |
| 26 | +import com.google.gson.stream.JsonWriter |
| 27 | +import spp.probe.services.common.ModelSerializer |
| 28 | +import java.io.IOException |
| 29 | +import java.lang.instrument.Instrumentation |
| 30 | + |
| 31 | +class CappedTypeAdapterFactory(val maxDepth: Int) : TypeAdapterFactory { |
| 32 | + |
| 33 | + override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? { |
| 34 | + return if (instrumentation == null || maxMemorySize == -1L) null else object : TypeAdapter<T>() { |
| 35 | + |
| 36 | + @Throws(IOException::class) |
| 37 | + override fun write(jsonWriter: JsonWriter, value: T?) { |
| 38 | + if (value == null) { |
| 39 | + jsonWriter.nullValue() |
| 40 | + return |
| 41 | + } else if (value is Class<*>) { |
| 42 | + jsonWriter.value(value.name) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + JsogRegistry.get().userData.putIfAbsent("depth", 0) |
| 47 | + if ((JsogRegistry.get().userData["depth"] as Int) >= maxDepth) { |
| 48 | + jsonWriter.beginObject() |
| 49 | + jsonWriter.name("@skip") |
| 50 | + jsonWriter.value("MAX_DEPTH_EXCEEDED") |
| 51 | + jsonWriter.name("@class") |
| 52 | + jsonWriter.value(value.javaClass.name) |
| 53 | + jsonWriter.name("@id") |
| 54 | + jsonWriter.value(Integer.toHexString(System.identityHashCode(value))) |
| 55 | + jsonWriter.endObject() |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + val objSize = instrumentation!!.getObjectSize(value) |
| 60 | + if (objSize <= maxMemorySize) { |
| 61 | + JsogRegistry.get().userData["depth"] = (JsogRegistry.get().userData["depth"] as Int) + 1 |
| 62 | + try { |
| 63 | + ModelSerializer.INSTANCE.extendedGson.getDelegateAdapter( |
| 64 | + this@CappedTypeAdapterFactory, type |
| 65 | + ).write(jsonWriter, value) |
| 66 | + } catch (e: Exception) { |
| 67 | + jsonWriter.beginObject() |
| 68 | + jsonWriter.name("@skip") |
| 69 | + jsonWriter.value("EXCEPTION_OCCURRED") |
| 70 | + jsonWriter.name("@class") |
| 71 | + jsonWriter.value(value.javaClass.name) |
| 72 | + jsonWriter.name("@size") |
| 73 | + jsonWriter.value(objSize.toString()) |
| 74 | + jsonWriter.name("@cause") |
| 75 | + jsonWriter.value(e.message) |
| 76 | + jsonWriter.name("@id") |
| 77 | + jsonWriter.value(Integer.toHexString(System.identityHashCode(value))) |
| 78 | + jsonWriter.endObject() |
| 79 | + } |
| 80 | + JsogRegistry.get().userData["depth"] = (JsogRegistry.get().userData["depth"] as Int) - 1 |
| 81 | + } else { |
| 82 | + jsonWriter.beginObject() |
| 83 | + jsonWriter.name("@skip") |
| 84 | + jsonWriter.value("MAX_SIZE_EXCEEDED") |
| 85 | + jsonWriter.name("@class") |
| 86 | + jsonWriter.value(value.javaClass.name) |
| 87 | + jsonWriter.name("@size") |
| 88 | + jsonWriter.value(objSize.toString()) |
| 89 | + jsonWriter.name("@id") |
| 90 | + jsonWriter.value(Integer.toHexString(System.identityHashCode(value))) |
| 91 | + jsonWriter.endObject() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + override fun read(jsonReader: JsonReader): T? = null |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + companion object { |
| 100 | + private var instrumentation: Instrumentation? = null |
| 101 | + private var maxMemorySize: Long = -1 |
| 102 | + |
| 103 | + @JvmStatic |
| 104 | + fun setInstrumentation(instrumentation: Instrumentation) { |
| 105 | + Companion.instrumentation = instrumentation |
| 106 | + } |
| 107 | + |
| 108 | + @JvmStatic |
| 109 | + fun setMaxMemorySize(maxMemorySize: Long) { |
| 110 | + Companion.maxMemorySize = maxMemorySize |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments