001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions copyright 2012-2015 ForgeRock AS
026 */
027package org.opends.server.replication.protocol;
028
029import org.opends.server.replication.common.CSN;
030import org.opends.server.types.Operation;
031import org.opends.server.types.operation.PluginOperation;
032
033/**
034 * This class describe the replication context that is attached
035 * to each Operation using the SYNCHROCONTEXT key.
036 */
037public abstract class OperationContext
038{
039  /** The identifier used to attach the context to operations. */
040  public static final String SYNCHROCONTEXT = "replicationContext";
041
042  /** The CSN of the Operation. */
043  private CSN csn;
044
045  /**
046   * The unique Id of the entry that was modified in the original operation.
047   */
048  private String entryUUID;
049
050  /**
051   * Create a new OperationContext.
052   * @param csn The CSN of the operation.
053   * @param entryUUID The unique Identifier of the modified entry.
054   */
055  protected OperationContext(CSN csn, String entryUUID)
056  {
057    this.csn = csn;
058    this.entryUUID = entryUUID;
059  }
060
061  /**
062   * Gets the CSN of the Operation.
063   *
064   * @return The CSN of the Operation.
065   */
066  public CSN getCSN()
067  {
068    return csn;
069  }
070
071  /**
072   * Get the unique Identifier of the modified entry.
073   *
074   * @return the unique Identifier of the modified entry.
075   */
076  public String getEntryUUID()
077  {
078    return entryUUID;
079  }
080
081  /**
082   * Get the CSN of an operation.
083   *
084   * @param  op The operation.
085   *
086   * @return The CSN of the provided operation, or null if there is
087   *         no CSN associated with the operation.
088   */
089  public static CSN getCSN(Operation op)
090  {
091    OperationContext ctx = (OperationContext)op.getAttachment(SYNCHROCONTEXT);
092    if (ctx == null)
093    {
094      return null;
095    }
096    return ctx.csn;
097  }
098
099  /**
100   * Get the CSN of an operation from the synchronization context
101   * attached to the provided operation.
102   *
103   * @param  op The operation.
104   *
105   * @return The CSN of the provided operation, or null if there is
106   *         no CSN associated with the operation.
107   */
108  public static CSN getCSN(PluginOperation op)
109  {
110    OperationContext ctx = (OperationContext)op.getAttachment(SYNCHROCONTEXT);
111    if (ctx == null)
112    {
113      return null;
114    }
115    return ctx.csn;
116  }
117
118  /** {@inheritDoc} */
119  @Override
120  public boolean equals(Object obj)
121  {
122    if (obj instanceof OperationContext)
123    {
124      OperationContext ctx = (OperationContext) obj;
125      return this.csn.equals(ctx.getCSN())
126          && this.entryUUID.equals(ctx.getEntryUUID());
127    }
128    return false;
129  }
130
131  /** {@inheritDoc} */
132  @Override
133  public int hashCode()
134  {
135    return csn.hashCode() + entryUUID.hashCode();
136  }
137
138
139}