导航菜单

页面标题

页面副标题

Sandridge Go v2025.2.170190457 - LocalIdManager.java 源代码

正在查看: Sandridge Go v2025.2.170190457 应用的 LocalIdManager.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.parse;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import org.json.JSONException;
import org.json.JSONObject;

class LocalIdManager {
    private final File diskPath;
    private final Random random = new Random();

    private static class MapEntry {
        String objectId;
        int retainCount;

        private MapEntry() {
        }
    }

    LocalIdManager(File file) {
        this.diskPath = new File(file, "LocalId");
    }

    private synchronized MapEntry getMapEntry(String str) {
        MapEntry mapEntry;
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        try {
            JSONObject readFileToJSONObject = ParseFileUtils.readFileToJSONObject(new File(this.diskPath, str));
            mapEntry = new MapEntry();
            mapEntry.retainCount = readFileToJSONObject.optInt("retainCount", 0);
            mapEntry.objectId = readFileToJSONObject.optString(ParseObject.KEY_OBJECT_ID, null);
        } catch (IOException | JSONException unused) {
            return new MapEntry();
        }
        return mapEntry;
    }

    private boolean isLocalId(String str) {
        if (!str.startsWith("local_")) {
            return false;
        }
        for (int i = 6; i < str.length(); i++) {
            char charAt = str.charAt(i);
            if ((charAt < '0' || charAt > '9') && (charAt < 'a' || charAt > 'f')) {
                return false;
            }
        }
        return true;
    }

    private synchronized void putMapEntry(String str, MapEntry mapEntry) {
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("retainCount", mapEntry.retainCount);
            String str2 = mapEntry.objectId;
            if (str2 != null) {
                jSONObject.put(ParseObject.KEY_OBJECT_ID, str2);
            }
            File file = new File(this.diskPath, str);
            if (!this.diskPath.exists()) {
                this.diskPath.mkdirs();
            }
            try {
                ParseFileUtils.writeJSONObjectToFile(file, jSONObject);
            } catch (IOException unused) {
            }
        } catch (JSONException e) {
            throw new IllegalStateException("Error creating local id map entry.", e);
        }
    }

    private synchronized void removeMapEntry(String str) {
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        ParseFileUtils.deleteQuietly(new File(this.diskPath, str));
    }

    synchronized boolean clear() throws IOException {
        String[] list = this.diskPath.list();
        if (list == null) {
            return false;
        }
        if (list.length == 0) {
            return false;
        }
        for (String str : list) {
            if (!new File(this.diskPath, str).delete()) {
                throw new IOException("Unable to delete file " + str + " in localId cache.");
            }
        }
        return true;
    }

    synchronized String createLocalId() {
        String str;
        str = "local_" + Long.toHexString(this.random.nextLong());
        if (!isLocalId(str)) {
            throw new IllegalStateException("Generated an invalid local id: \"" + str + "\". This should never happen. Open a bug at https://github.com/parse-community/parse-server");
        }
        return str;
    }

    synchronized String getObjectId(String str) {
        return getMapEntry(str).objectId;
    }

    synchronized void releaseLocalIdOnDisk(String str) {
        try {
            MapEntry mapEntry = getMapEntry(str);
            int i = mapEntry.retainCount - 1;
            mapEntry.retainCount = i;
            if (i > 0) {
                putMapEntry(str, mapEntry);
            } else {
                removeMapEntry(str);
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    synchronized void retainLocalIdOnDisk(String str) {
        MapEntry mapEntry = getMapEntry(str);
        mapEntry.retainCount++;
        putMapEntry(str, mapEntry);
    }

    synchronized void setObjectId(String str, String str2) {
        try {
            MapEntry mapEntry = getMapEntry(str);
            if (mapEntry.retainCount > 0) {
                if (mapEntry.objectId != null) {
                    throw new IllegalStateException("Tried to set an objectId for a localId that already has one.");
                }
                mapEntry.objectId = str2;
                putMapEntry(str, mapEntry);
            }
        } catch (Throwable th) {
            throw th;
        }
    }
}