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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.replication.protocol;
028
029 import org.opends.server.replication.common.ChangeNumber;
030 import org.opends.server.types.Operation;
031 import 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 */
037 public abstract class OperationContext
038 {
039 /**
040 * The identifier used to attach the context to operations.
041 */
042 public static final String SYNCHROCONTEXT = "replicationContext";
043
044 /**
045 * The change Number of the Operation.
046 */
047 private ChangeNumber changeNumber;
048
049 /**
050 * The unique Id of the entry that was modified in the original operation.
051 */
052 private String entryUid;
053
054 /**
055 * Create a new OperationContext.
056 * @param changeNumber The change number of the operation.
057 * @param uid The unique Identifier of the modified entry.
058 */
059 protected OperationContext(ChangeNumber changeNumber, String uid)
060 {
061 this.changeNumber = changeNumber;
062 this.entryUid = uid;
063 }
064
065 /**
066 * Gets The change number of the Operation.
067 *
068 * @return The change number of the Operation.
069 */
070 public ChangeNumber getChangeNumber()
071 {
072 return changeNumber;
073 }
074
075 /**
076 * Get the unique Identifier of the modified entry.
077 *
078 * @return the unique Identifier of the modified entry.
079 */
080 public String getEntryUid()
081 {
082 return entryUid;
083 }
084
085 /**
086 * Get the change number of an operation.
087 *
088 * @param op The operation.
089 *
090 * @return The change number of the provided operation, or null if there is
091 * no change number associated with the operation.
092 */
093 public static ChangeNumber getChangeNumber(Operation op)
094 {
095 OperationContext ctx = (OperationContext)op.getAttachment(SYNCHROCONTEXT);
096 if (ctx == null)
097 {
098 return null;
099 }
100 return ctx.changeNumber;
101 }
102
103 /**
104 * Get the change number of an operation.
105 *
106 * @param op The operation.
107 *
108 * @return The change number of the provided operation, or null if there is
109 * no change number associated with the operation.
110 */
111 public static ChangeNumber getChangeNumber(PluginOperation op)
112 {
113 OperationContext ctx = (OperationContext)op.getAttachment(SYNCHROCONTEXT);
114 if (ctx == null)
115 {
116 return null;
117 }
118 return ctx.changeNumber;
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 @Override
125 public boolean equals(Object obj)
126 {
127 if (obj instanceof OperationContext)
128 {
129 OperationContext ctx = (OperationContext) obj;
130 return ((this.changeNumber.equals(ctx.getChangeNumber()) &&
131 (this.entryUid.equals(ctx.getEntryUid()))));
132 }
133 else
134 return false;
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 @Override
141 public int hashCode()
142 {
143 return changeNumber.hashCode() + entryUid.hashCode();
144 }
145
146
147 }