// Path: src/main/java/net/kimleo/rec/common/exception/ResourceAccessException.java
// public class ResourceAccessException extends RuntimeException {
//     public ResourceAccessException(String message, Throwable ex) {
//         super(message, ex);
//     }
// }
// 
// Path: src/main/java/net/kimleo/rec/v2/execution/ExecutionContext.java
// public interface ExecutionContext {
//     void commit();
//     void persist(Throwable causedBy);
// 
//     ExecutionContext restart();
// 
//     boolean isNative();
//     boolean isCloud();
// 
//     Context jsContext();
// }
// 
// Path: src/main/java/net/kimleo/rec/v2/utils/Persistence.java
// public class Persistence {
//     public static void saveObjectToFile(Object serializable, String file) throws IOException {
//         ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
//         stream.writeObject(serializable);
//     }
// 
//     public static Object loadObjectFromFile(String file) throws IOException, ClassNotFoundException {
//         ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// 
//         return in.readObject();
//     }
// }


import net.kimleo.rec.common.exception.ResourceAccessException;
import net.kimleo.rec.v2.execution.ExecutionContext;
import net.kimleo.rec.v2.utils.Persistence;
import org.mozilla.javascript.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import static java.lang.String.format;
package net.kimleo.rec.v2.execution.impl;




public final class NativeExecutionContext implements ExecutionContext, Serializable {
    public static final long serialVersionUID = 1453356753764L;
    private int count = 0;
    private final int baseCount;

    private static final Logger LOGGER = LoggerFactory.getLogger(NativeExecutionContext.class);
    private String scriptPath;
    private transient Context jsContext;
    private transient boolean enableRetry;
    private String retryFile = null;

    public NativeExecutionContext(int baseCount) {
        this.baseCount = baseCount;
        LOGGER.info(format("Initialize execution context based on count %d", baseCount));
    }

    public int state() {
        return baseCount;
    }

    @Override
    public void commit() {
        count ++;
    }

    public void persist(Throwable causedBy) {
        try {
            String filename = String.valueOf(new Date().getTime()).concat(".retry");
            Persistence.saveObjectToFile(this, filename);
            LOGGER.info(format("Successfully save context to [%s]", filename));
        } catch (IOException e) {
            LOGGER.error(format("Unable to save execution context and the count is %d", count));
// Path: src/main/java/net/kimleo/rec/common/exception/ResourceAccessException.java
// public class ResourceAccessException extends RuntimeException {
//     public ResourceAccessException(String message, Throwable ex) {
//         super(message, ex);
//     }
// }
// 
// Path: src/main/java/net/kimleo/rec/v2/execution/ExecutionContext.java
// public interface ExecutionContext {
//     void commit();
//     void persist(Throwable causedBy);
// 
//     ExecutionContext restart();
// 
//     boolean isNative();
//     boolean isCloud();
// 
//     Context jsContext();
// }
// 
// Path: src/main/java/net/kimleo/rec/v2/utils/Persistence.java
// public class Persistence {
//     public static void saveObjectToFile(Object serializable, String file) throws IOException {
//         ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
//         stream.writeObject(serializable);
//     }
// 
//     public static Object loadObjectFromFile(String file) throws IOException, ClassNotFoundException {
//         ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// 
//         return in.readObject();
//     }
// }


// Path: src/main/java/net/kimleo/rec/v2/execution/impl/NativeExecutionContext.java
import net.kimleo.rec.common.exception.ResourceAccessException;
import net.kimleo.rec.v2.execution.ExecutionContext;
import net.kimleo.rec.v2.utils.Persistence;
import org.mozilla.javascript.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import static java.lang.String.format;

package net.kimleo.rec.v2.execution.impl;




public final class NativeExecutionContext implements ExecutionContext, Serializable {
    public static final long serialVersionUID = 1453356753764L;
    private int count = 0;
    private final int baseCount;

    private static final Logger LOGGER = LoggerFactory.getLogger(NativeExecutionContext.class);
    private String scriptPath;
    private transient Context jsContext;
    private transient boolean enableRetry;
    private String retryFile = null;

    public NativeExecutionContext(int baseCount) {
        this.baseCount = baseCount;
        LOGGER.info(format("Initialize execution context based on count %d", baseCount));
    }

    public int state() {
        return baseCount;
    }

    @Override
    public void commit() {
        count ++;
    }

    public void persist(Throwable causedBy) {
        try {
            String filename = String.valueOf(new Date().getTime()).concat(".retry");
            Persistence.saveObjectToFile(this, filename);
            LOGGER.info(format("Successfully save context to [%s]", filename));
        } catch (IOException e) {
            LOGGER.error(format("Unable to save execution context and the count is %d", count));
            throw new ResourceAccessException("Cannot persist execution context", e);