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.plugin;
028    
029    import org.opends.server.replication.common.ChangeNumber;
030    import org.opends.server.replication.common.ServerState;
031    import org.opends.server.replication.protocol.UpdateMessage;
032    import org.opends.server.types.DN;
033    import org.opends.server.types.DirectoryException;
034    import org.opends.server.types.operation.PluginOperation;
035    
036    /**
037     * This class is use to store an operation currently
038     * in progress and not yet committed in the database.
039     */
040    public class PendingChange implements Comparable<PendingChange>
041    {
042      private ChangeNumber changeNumber;
043      private boolean committed;
044      private UpdateMessage msg;
045      private PluginOperation op;
046      private ServerState dependencyState = null;
047      private DN targetDN = null;
048    
049      /**
050       * Construct a new PendingChange.
051       * @param changeNumber the ChangeNumber of use
052       * @param op the operation to use
053       * @param msg the message to use (can be null for local operations)
054       */
055      public PendingChange(ChangeNumber changeNumber,
056                           PluginOperation op,
057                           UpdateMessage msg)
058      {
059        this.changeNumber = changeNumber;
060        this.committed = false;
061        this.op = op;
062        this.msg = msg;
063      }
064    
065      /**
066       * Check if a Change is already committed to the database.
067       * @return true if change is already committed to the database.
068       */
069      public boolean isCommitted()
070      {
071        return committed;
072      }
073    
074      /**
075       * Set the committed status of a Pending Change.
076       * @param committed status that must be set
077       */
078      public void setCommitted(boolean committed)
079      {
080        this.committed = committed;
081      }
082    
083      /**
084       * Get the ChangeNumber associated to this PendingChange.
085       * @return the ChangeNumber
086       */
087      public ChangeNumber getChangeNumber()
088      {
089        return changeNumber;
090      }
091    
092      /**
093       * Get the message associated to this PendingChange.
094       * @return the message if operation was a replication operation
095       * null if the operation was a local operation
096       */
097      public UpdateMessage getMsg()
098      {
099        return msg;
100      }
101    
102      /**
103       * Set the message associated to the PendingChange.
104       * @param msg the message
105       */
106      public void setMsg(UpdateMessage msg)
107      {
108        this.msg = msg;
109      }
110    
111      /**
112       * Get the operation associated to the PendingChange.
113       * @return the operation
114       */
115      public PluginOperation getOp()
116      {
117        return this.op;
118      }
119    
120      /**
121       * Set the operation asociated to this PendingChange.
122       * @param op The operation associated to this PendingChange.
123       */
124      public void setOp(PluginOperation op)
125      {
126        this.op = op;
127      }
128    
129      /**
130       * Add the given ChangeNumber in the list of dependencies of this
131       * PendingChange.
132       *
133       * @param changeNumber The ChangeNumber to add in the list of dependencies
134       *                     of this PendingChange.
135       */
136      public void addDependency(ChangeNumber changeNumber)
137      {
138        if (dependencyState == null)
139        {
140          dependencyState = new ServerState();
141        }
142        dependencyState.update(changeNumber);
143      }
144    
145      /**
146       * Check if the given ServerState covers the dependencies of this
147       * PendingChange.
148       *
149       * @param state The ServerState for which dependencies must be checked,
150       *
151       * @return A boolean indicating if the given ServerState covers the
152       *         dependencies of this PendingChange.
153       */
154      public boolean dependenciesIsCovered(ServerState state)
155      {
156        return state.cover(dependencyState);
157      }
158    
159      /**
160       * Get the Target DN of this message.
161       *
162       * @return The target DN of this message.
163       */
164      public DN getTargetDN()
165      {
166        synchronized (this)
167        {
168          if (targetDN != null)
169            return targetDN;
170          else
171          {
172            try
173            {
174              targetDN = DN.decode(msg.getDn());
175            }
176            catch (DirectoryException e)
177            {
178            }
179          }
180          return targetDN;
181        }
182      }
183    
184      /**
185       * {@inheritDoc}
186       */
187      public int compareTo(PendingChange o)
188      {
189        return this.getChangeNumber().compareTo(o.getChangeNumber());
190      }
191    }