|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.seata.rm.datasource.undo.parser; |
| 18 | + |
| 19 | +import org.apache.fory.Fory; |
| 20 | +import org.apache.fory.ThreadLocalFory; |
| 21 | +import org.apache.fory.ThreadSafeFory; |
| 22 | +import org.apache.fory.config.CompatibleMode; |
| 23 | +import org.apache.fory.config.Language; |
| 24 | +import org.apache.seata.common.executor.Initialize; |
| 25 | +import org.apache.seata.common.loader.LoadLevel; |
| 26 | +import org.apache.seata.rm.datasource.undo.BranchUndoLog; |
| 27 | +import org.apache.seata.rm.datasource.undo.UndoLogParser; |
| 28 | + |
| 29 | +@LoadLevel(name = ForyUndoLogParser.NAME) |
| 30 | +public class ForyUndoLogParser implements UndoLogParser, Initialize { |
| 31 | + public static final String NAME = "fory"; |
| 32 | + |
| 33 | + private static final ThreadSafeFory FORY = new ThreadLocalFory(classLoader -> Fory.builder() |
| 34 | + .withLanguage(Language.JAVA) |
| 35 | + // In JAVA mode, classes cannot be registered by tag, and the different registration order between the |
| 36 | + // server and the client will cause deserialization failure |
| 37 | + // In XLANG cross-language mode has problems with Java class serialization, such as enum classes |
| 38 | + // [https://github.com/apache/fory/issues/1644]. |
| 39 | + .requireClassRegistration(false) |
| 40 | + // enable reference tracking for shared/circular reference. |
| 41 | + .withRefTracking(true) |
| 42 | + .withClassLoader(classLoader) |
| 43 | + .withCompatibleMode(CompatibleMode.COMPATIBLE) |
| 44 | + .build()); |
| 45 | + |
| 46 | + @Override |
| 47 | + public void init() {} |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getName() { |
| 51 | + return NAME; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public byte[] getDefaultContent() { |
| 56 | + return encode(new BranchUndoLog()); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public byte[] encode(BranchUndoLog branchUndoLog) { |
| 61 | + return FORY.serializeJavaObject(branchUndoLog); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public BranchUndoLog decode(byte[] bytes) { |
| 66 | + return FORY.deserializeJavaObject(bytes, BranchUndoLog.class); |
| 67 | + } |
| 68 | +} |
0 commit comments