代码段

public interface Operation

{

 public void setSecurityContext(SecurityContext securityContext);
 public void setCurrentNode(AbstractNode currentNode);
 
 public int getParameterCount();
 public String getKeyword();

 public boolean canExecute();
 public void addSwitch(String sw) throws InvalidSwitchException;
 public void addParameter(Object parameter) throws InvalidParameterException;

}

 

public interface Callback {

 public void callback(Object... params);
}

 

public interface PrimaryOperation extends Operation {

 public boolean executeOperation(StringBuilder stdOut) throws NodeCommandException;
 public void addCallback(Callback callback);

 public String help();
}

 

public interface Notification {
 public String getTitle();
 public String getText();
 public String getContainerCss();
 public String getTitleCss();
 public String getTextCss();
 public boolean isExpired();
}

 

public enum Direction
{
    /**
     * Defines outgoing relationships.
     */
    OUTGOING,
    /**
     * Defines incoming relationships.
     */
    INCOMING,
    /**
     * Defines both incoming and outgoing relationships.
     */
    BOTH;

    /**
     * Reverses the direction returning {@link #INCOMING} if this equals
     * {@link #OUTGOING}, {@link #OUTGOING} if this equals {@link #INCOMING} or
     * {@link #BOTH} if this equals {@link #BOTH}.
     * 
     * @return The reversed direction.
     */
    public Direction reverse()
    {
        switch ( this )
        {
            case OUTGOING:
                return INCOMING;
            case INCOMING:
                return OUTGOING;
            case BOTH:
                return BOTH;
            default:
                throw new IllegalStateException( "Unknown Direction "
                    + "enum: " + this );
        }
    }
}

 

 

import org.neo4j.graphdb.RelationshipType;

/**
 * Contains all application-specific relationship types
 */
public enum RelType implements RelationshipType
{
    HAS_CHILD, // IS_CHILD,
    IS_MEMBER_OF_GROUP,
    UNDEFINED, LINK, PAGE_LINK,
    SECURITY,
    USE_TEMPLATE,
    OWNS,
    ROOT_NODE,
    THUMBNAIL,
    NEXT_LIST_ENTRY,
    LAST_LIST_ENTRY,
    IS_AT,

    // application relationships
    DATA,
    SUBMIT,
    ERROR_DESTINATION,
    SUCCESS_DESTINATION,
    CREATE_DESTINATION,

    // type relationships
    TYPE,
    SUBTYPE,

    // web
    CONTAINS
}

public enum SearchOperator {
    AND,
    OR,
    NOT;
}

public abstract class SearchAttribute
{
    public static final String WILDCARD = "*";

    private SearchOperator searchOp = null;
    private List<GraphObject> result = new LinkedList<GraphObject>();

    public void setSearchOperator(final SearchOperator searchOp)
    {
        this.searchOp = searchOp;
    }

    public SearchOperator getSearchOperator()
    {
        return searchOp;
    }

    public void setResult(final List<GraphObject> result)
    {
        this.result = result;
    }

    public List<GraphObject> getResult()
    {
        return result;
    }

    public void addToResult(final GraphObject graphObject)
    {
        result.add(graphObject);
    }

    public abstract Object getAttribute();

    public abstract void setAttribute(Object attribute);

}

 

public class SearchAttributeGroup extends SearchAttribute
{
    private List<SearchAttribute> searchItems = new LinkedList<SearchAttribute>();

    public SearchAttributeGroup(final SearchOperator searchOp)
    {
        setSearchOperator(searchOp);
    }

    public final void setSearchAttributes(final List<SearchAttribute> searchItems)
    {
        this.searchItems = searchItems;
    }

    public List<SearchAttribute> getSearchAttributes()
    {
        return searchItems;
    }

    public void add(final SearchAttribute searchAttribute)
    {
        searchItems.add(searchAttribute);
    }

    @Override
    public Object getAttribute()
    {
        return searchItems;
    }

    @Override
    public void setAttribute(Object attribute)
    {
        this.searchItems = (List<SearchAttribute>) attribute;
    }
}

 

public enum SearchValue

{
    EMPTY,
    NOT_EMPTY,
    EQUAL,
    NOT_EQUAL,
    GREATER_THAN,
    LESSER_THAN,
    GREATER_OR_EQUAL,
    LESSER_OR_EQUAL;
}

 

public interface PropertyContainer
{
    public GraphDatabaseService getGraphDatabase();
    public boolean hasProperty( String key );
    public Object getProperty( String key );
    public Object getProperty( String key, Object defaultValue );
    public void setProperty( String key, Object value );
    public Object removeProperty( String key );
    public Iterable<String> getPropertyKeys();
    public Iterable<Object> getPropertyValues();
}

 

public interface NodeIterator {

    int getPosition();

    boolean setPosition(int position);

    NodePointer getNodePointer();
}

 

public interface Value<T> {

 public void set(T value);
 public T get();

 

public interface RelationshipType
{

    public String name();
}

 

public interface Service {

 public void injectArguments(Command command);

 public void initialize(Map<String, Object> context);

 public void shutdown();

 public String getName();

 public boolean isRunning();

}

 

public interface Authenticator {

 public void examineRequest(SecurityContext securityContext, HttpServletRequest request) throws FrameworkException;
 public User doLogin(SecurityContext securityContext, HttpServletRequest request, String userName, String password) throws AuthenticationException;

 public void doLogout(SecurityContext securityContext, HttpServletRequest request);

 public User getUser(SecurityContext securityContext, HttpServletRequest request);
}

 

public interface VetoableGraphObjectListener {

 public boolean begin(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer);
 public boolean commit(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer);
 public boolean rollback(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer);

 public boolean propertyModified(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject, String key, Object oldValue, Object newValue);

 public boolean graphObjectCreated(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject);
 public boolean graphObjectModified(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject);
 public boolean graphObjectDeleted(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject, Map<String, Object> properties);

 public boolean wasVisited(List<GraphObject> traversedNodes, long transactionKey, ErrorBuffer errorBuffer, SecurityContext securityContext);
}

 class TemporaryValue<T> {

	private long timeToLive = 0L;
	private long timestamp = 0L;
	private T value = null;

	public TemporaryValue(T value, long timeToLive) {

		this.timestamp = System.currentTimeMillis() + timeToLive;
		this.timeToLive = timeToLive;
		this.value = value;
	}

	public boolean isExpired() {
		return(System.currentTimeMillis() > timestamp);
	}

	public void refreshStoredValue(T value) {

		this.value = value;
		this.timestamp = System.currentTimeMillis() + timeToLive;
	}

	public long getTimeToLive() {

		return(timeToLive);
	}

	public long getCreateTimestamp() {

		return(timestamp - timeToLive);
	}

	public T getStoredValue() {

		if(isExpired()) {
			value = null;
		}
		
		return(value);
	}
}

 

 

public class SynchronizationController implements VetoableGraphObjectListener {

	private static final Logger logger = Logger.getLogger(SynchronizationController.class.getName());

	private Map<Long, WebSocketMessage> messageMap = new LinkedHashMap<Long, WebSocketMessage>();
	private Set<StructrWebSocket> clients          = null;
	private Gson gson                              = null;
	
	public SynchronizationController(Gson gson) {
		this.clients = new LinkedHashSet<StructrWebSocket>();
		this.gson = gson;
	}

	public void registerClient(StructrWebSocket client) {
		clients.add(client);
	}

	public void unregisterClient(StructrWebSocket client) {
		clients.remove(client);
	}

	// ----- private methods -----
	private void broadcast(final WebSocketMessage webSocketData) {

		logger.log(Level.INFO, "Broadcasting message to {0} clients..", clients.size());

		// session must be valid to be received by the client
		webSocketData.setSessionValid(true);

		// create message
		String message = gson.toJson(webSocketData, WebSocketMessage.class);
		logger.log(Level.INFO, "############################################################ SENDING \n{0}", message);

		for (StructrWebSocket socket : clients) {

			Connection socketConnection = socket.getConnection();
			if ((socketConnection != null) && socket.isAuthenticated()) {

				try {
					socketConnection.sendMessage(message);

				} catch (Throwable t) {
					logger.log(Level.WARNING, "Error sending message to client.", t);
				}
			}
		}
	}

	// ----- interface VetoableGraphObjectListener -----
	@Override
	public boolean begin(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer) {
		messageMap.put(transactionKey, new WebSocketMessage());
		return false;
	}

	@Override
	public boolean commit(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer) {

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {
			broadcast(message);

		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}

		messageMap.remove(transactionKey);
		return false;
	}

	@Override
	public boolean rollback(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer) {

		// roll back transaction
		messageMap.remove(transactionKey);
		return false;
	}

	@Override
	public boolean propertyModified(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject, String key, Object oldValue, Object newValue) {

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {
			// message.setData(key, newValue != null ? newValue.toString() : "null");
			message.getModifiedProperties().add(key);
		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}
		return false;
	}

	public boolean relationshipCreated(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, AbstractRelationship relationship) {

		AbstractNode startNode = relationship.getStartNode();
		AbstractNode endNode = relationship.getEndNode();

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {

			message.setCommand("ADD");
			message.setGraphObject(relationship);
			message.setId(startNode.getStringProperty("uuid"));
			message.setData("id", endNode.getStringProperty("uuid"));

		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}

		logger.log(Level.INFO, "{0} -> {1}", new Object[] { startNode.getId(), endNode.getId() } );
		return false;
	}

	public boolean relationshipDeleted(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, AbstractRelationship relationship) {

		AbstractNode startNode = relationship.getStartNode();
		AbstractNode endNode = relationship.getEndNode();

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {

			message.setCommand("REMOVE");
			message.setGraphObject(relationship);
			message.setId(startNode.getStringProperty("uuid"));
			message.setData("id", endNode.getStringProperty("uuid"));


		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}

		logger.log(Level.INFO, "{0} -> {1}", new Object[] { startNode.getId(), endNode.getId() } );
		return false;
	}

	@Override
	public boolean graphObjectCreated(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject) {

		if(graphObject instanceof AbstractRelationship) {

			return relationshipCreated(securityContext, transactionKey, errorBuffer, (AbstractRelationship)graphObject);

		} else {

			WebSocketMessage message = messageMap.get(transactionKey);
			if(message != null) {

				message.setCommand("CREATE");
				message.setGraphObject(graphObject);

				List<GraphObject> list = new LinkedList<GraphObject>();
				list.add(graphObject);
				message.setResult(list);

			} else {
				logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
			}
			return false;
		}
	}

	@Override
	public boolean graphObjectModified(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject graphObject) {

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {
			String uuid = graphObject.getProperty("uuid").toString();
			message.setId(uuid);
			message.setCommand("UPDATE");
			message.setGraphObject(graphObject);

		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}

		return false;
	}

	@Override
	public boolean graphObjectDeleted(SecurityContext securityContext, long transactionKey, ErrorBuffer errorBuffer, GraphObject obj, Map<String, Object> properties) {

		WebSocketMessage message = messageMap.get(transactionKey);
		if(message != null) {
			String uuid = properties.get("uuid").toString();
			message.setId(uuid);
			message.setCommand("DELETE");
		} else {
			logger.log(Level.WARNING, "No message found for transaction key {0}", transactionKey);
		}
		return false;
	}

	@Override
	public boolean wasVisited(List<GraphObject> traversedNodes, long transactionKey, ErrorBuffer errorBuffer, SecurityContext securityContext) {
		return false;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值