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