Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright: Almende B.V. (2014), Rotterdam, The Netherlands
* License: The Apache Software License, Version 2.0
*/
package com.almende.eve.state.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.almende.eve.state.AbstractState;
import com.almende.eve.state.file.FileStateBuilder.FileStateProvider;
import com.almende.util.TypeUtil;
import com.almende.util.jackson.JOM;
import com.almende.util.jackson.JsonNullAwareDeserializer;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* The Class ConcurrentJsonFileState.
*
* @author jos
* @author ludo
* @class FileState
*
* A persistent state for an Eve Agent, which stores the data on disk.
* Data is stored in the path provided by the configuration file.
*
* The state provides general information for the agent (about itself,
* the environment, and the system configuration), and the agent can
* store its state in the state. The state extends a standard Java
* Map.
*
* All operations on this FileState are thread-safe. It also provides two
* aditional methods: PutIfNotChanged() and PutAllIfNotChanged().
*
* Usage:
* AgentHost factory = AgentHost.getInstance(config);
* ConcurrentFileState state = new
* ConcurrentFileState("agentId",".eveagents");
* state.put("key", "value");
* System.out.println(state.get("key")); // "value"
*/
public class ConcurrentJsonFileState extends AbstractState {
private class Lock {
private boolean locked = false;
}
private static final Logger LOG = Logger.getLogger("ConcurrentFileState");
private String filename = null;
private FileChannel channel = null;
private FileLock lock = null;
private InputStream fis = null;
private OutputStream fos = null;
private ObjectMapper om = null;
private ObjectWriter writer = null;
private static final Map LOCKED = new ConcurrentHashMap();
private Map properties = Collections
.synchronizedMap(new HashMap());
private static final JavaType MAPTYPE = JOM.getTypeFactory()
.constructMapLikeType(
HashMap.class,
String.class,
JsonNode.class);
/**
* Instantiates a new concurrent json file state.
*
* @param agentId
* the agent id
* @param filename
* the filename
* @param service
* the service
* @param params
* the params
*/
public ConcurrentJsonFileState(final String agentId, final String filename,
final FileStateProvider service, final ObjectNode params) {
super(agentId, service, params);
this.filename = filename;
om = JOM.getInstance();
writer = om.writerWithType(new TypeUtil