// Path: core/src/main/java/com/threerings/nexus/util/Log.java
// public static Logger log = new JavaLogger("nexus");


import java.util.concurrent.Executor;
import static com.threerings.nexus.util.Log.log;
import java.util.ArrayDeque;
import java.util.Queue;
//
// Nexus Server - server-side support for Nexus distributed application framework
// http://github.com/threerings/nexus/blob/master/LICENSE

package com.threerings.nexus.server;



/**
 * Contains an execution queue for one or more Nexus entities. Distributed events, actions and
 * requests are all executed in order, one at a time, by no more than a single thread (for a given
 * context) for all entities registered with a particular context. In general, there is a one
 * entity to one context relationship, meaning that everything is run in parallel. However, if an
 * object is registered as a child of another entity, that object will share the entity's context.
 */
public class EntityContext
{
    /** Contains a reference to the currently executing entity context. */
    public static final ThreadLocal<EntityContext> current = new ThreadLocal<EntityContext>();

    /**
     * Creates an entity context that will use the supplied executor for executions.
     */
    public EntityContext (Executor exec) {
        _exec = exec;
    }

    /**
     * Queues the supplied operation for execution on this context. If the context is currently
     * being executed, the operation will simply be added to its queue. If it is not being
     * executed, it will additionally be queued up on the supplied executor, so that its pending
     * operations will be executed.
     */
    public synchronized void postOp (final Runnable op) {
        _ops.offer(new Runnable() {
            public void run () {
                try {
                    current.set(EntityContext.this);
                    op.run();
                } catch (Throwable t) {
// Path: core/src/main/java/com/threerings/nexus/util/Log.java
// public static Logger log = new JavaLogger("nexus");


// Path: server/src/main/java/com/threerings/nexus/server/EntityContext.java
import java.util.concurrent.Executor;
import static com.threerings.nexus.util.Log.log;
import java.util.ArrayDeque;
import java.util.Queue;

//
// Nexus Server - server-side support for Nexus distributed application framework
// http://github.com/threerings/nexus/blob/master/LICENSE

package com.threerings.nexus.server;



/**
 * Contains an execution queue for one or more Nexus entities. Distributed events, actions and
 * requests are all executed in order, one at a time, by no more than a single thread (for a given
 * context) for all entities registered with a particular context. In general, there is a one
 * entity to one context relationship, meaning that everything is run in parallel. However, if an
 * object is registered as a child of another entity, that object will share the entity's context.
 */
public class EntityContext
{
    /** Contains a reference to the currently executing entity context. */
    public static final ThreadLocal<EntityContext> current = new ThreadLocal<EntityContext>();

    /**
     * Creates an entity context that will use the supplied executor for executions.
     */
    public EntityContext (Executor exec) {
        _exec = exec;
    }

    /**
     * Queues the supplied operation for execution on this context. If the context is currently
     * being executed, the operation will simply be added to its queue. If it is not being
     * executed, it will additionally be queued up on the supplied executor, so that its pending
     * operations will be executed.
     */
    public synchronized void postOp (final Runnable op) {
        _ops.offer(new Runnable() {
            public void run () {
                try {
                    current.set(EntityContext.this);
                    op.run();
                } catch (Throwable t) {
                    log.warning("Entity operation failed: " + op, t);