View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.entry;
21  
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import javax.naming.NamingException;
27  //import javax.naming.directory.DirContext;
28  
29  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
30  import org.apache.directory.server.schema.registries.Registries;
31  import org.apache.directory.shared.ldap.entry.EntryAttribute;
32  import org.apache.directory.shared.ldap.entry.Modification;
33  import org.apache.directory.shared.ldap.entry.ModificationOperation;
34  import org.apache.directory.shared.ldap.entry.client.ClientModification;
35  import org.apache.directory.shared.ldap.schema.AttributeType;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * An internal implementation for a ModificationItem. The name has been
41   * chosen so that it does not conflict with @see ModificationItem
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$, $Date$
45   */
46  public class ServerModification implements Modification
47  {
48      public static final long serialVersionUID = 1L;
49      
50      /** logger for reporting errors that might not be handled properly upstream */
51      private static final Logger LOG = LoggerFactory.getLogger( ServerModification.class );
52  
53      /** The modification operation */
54      private ModificationOperation operation;
55      
56      /** The attribute which contains the modification */
57      private EntryAttribute attribute;
58   
59      
60      //-------------------------------------------------------------------------
61      // Constructors
62      //-------------------------------------------------------------------------
63      /**
64       * Create a new instance of a ServerModification.
65       */
66      public ServerModification()
67      {
68      }
69      
70      
71      /**
72       * Create a new instance of a ServerModification.
73       * 
74       * @param operation the Modification operation (one of add, replace or remove)
75       * @param attribute the modified attribute
76       */
77      public ServerModification( ModificationOperation operation, EntryAttribute attribute )
78      {
79          this.operation = operation;
80          this.attribute = attribute;
81      }
82      
83      
84      public ServerModification( Registries registries, Modification modification )
85      {
86          operation = modification.getOperation();
87          
88          EntryAttribute modAttribute = modification.getAttribute();
89          
90          try
91          {
92              AttributeType at = null;
93              
94              if ( modAttribute instanceof ServerAttribute )
95              {
96                  at = ((ServerAttribute)modAttribute).getAttributeType();
97              }
98              else
99              {
100                 at = registries.getAttributeTypeRegistry().lookup( modAttribute.getId() );
101             }
102             
103             attribute = new DefaultServerAttribute( at, modAttribute );
104         }
105         catch ( NamingException ne )
106         {
107             // The attributeType is incorrect. Log, but do nothing otherwise.
108             LOG.error( "The attribute '" + modAttribute.getId() + "' is incorrect" );
109         }
110     }
111     
112     
113     //-------------------------------------------------------------------------
114     // API
115     //-------------------------------------------------------------------------
116     /**
117      *  @return the operation
118      */
119     public ModificationOperation getOperation()
120     {
121         return operation;
122     }
123     
124     
125     /**
126      * Store the modification operation
127      *
128      * @param operation The DirContext value to assign
129      */
130     public void setOperation( int operation )
131     {
132         this.operation = ModificationOperation.getOperation( operation );
133     }
134 
135     
136     /**
137      * Store the modification operation
138      *
139      * @param operation The DirContext value to assign
140      */
141     public void setOperation( ModificationOperation operation )
142     {
143         this.operation = operation;
144     }
145         
146     
147     /**
148      * @return the attribute containing the modifications
149      */
150     public EntryAttribute getAttribute()
151     {
152         return attribute;
153     }
154     
155     
156     /**
157      * Set the attribute's modification
158      *
159      * @param attribute The modified attribute 
160      */
161     public void setAttribute( EntryAttribute attribute )
162     {
163         this.attribute = (ServerAttribute)attribute;
164     }
165     
166 
167     /**
168      * Convert the current ServerModification to a ClientModification instance 
169      *
170      * @return a new ClientModification instance
171      */
172     public Modification toClientModification()
173     {
174         ModificationOperation newOperation = operation;
175         EntryAttribute newAttribute = ((ServerAttribute)attribute).toClientAttribute();
176         Modification newModification = new ClientModification( newOperation, newAttribute );
177         
178         return newModification;
179     }
180     
181     //-------------------------------------------------------------------------
182     // Overloaded Object class methods
183     //-------------------------------------------------------------------------
184     /**
185      * Compute the modification @see Object#hashCode
186      * @return the instance's hash code 
187      */
188     public int hashCode()
189     {
190         int h = 37;
191         
192         h += h*17 + operation.getValue();
193         h += h*17 + attribute.hashCode();
194         
195         return h;
196     }
197     
198     
199     /**
200      * @see Object#equals(Object)
201      */
202     public boolean equals( Object that )
203     {
204         // Shortcut
205         if ( this == that )
206         {
207             return true;
208         }
209         
210         if ( ! ( that instanceof ServerModification ) )
211         {
212             return false;
213         }
214         
215         ServerModification modification = (ServerModification)that;
216         
217         if ( operation != modification.getOperation() )
218         {
219             return false;
220         }
221         
222         if ( attribute == null )
223         {
224             return modification.getAttribute() == null;
225         }
226         
227         return attribute.equals( modification.getAttribute() );
228     }
229     
230     
231     /**
232      * Create a clone instance
233      */
234     public ServerModification clone()
235     {
236         try
237         {
238             ServerModification clone = (ServerModification)super.clone();
239             
240             clone.attribute = (ServerAttribute)this.attribute.clone();
241             return clone;
242         }
243         catch ( CloneNotSupportedException cnse )
244         {
245             return null;
246         }
247     }
248     
249     
250     /**
251      * @see java.io.Externalizable#writeExternal(ObjectOutput)
252      * 
253      * We can't use this method for a ServerModification.
254      */
255     public void writeExternal( ObjectOutput out ) throws IOException
256     {
257         throw new IllegalStateException( "Cannot use standard serialization for a ServerEntry" );
258     }
259     
260     
261     /**
262      * @see java.io.Externalizable#readExternal(ObjectInput)
263      * 
264      * We can't use this method for a ServerModification.
265      */
266     public void readExternal( ObjectInput in ) throws IOException
267     {
268         throw new IllegalStateException( "Cannot use standard serialization for a ServerEntry" );
269     }
270     
271     
272     /**
273      * Deserialize a ServerModification
274      * 
275      * @param in The buffer containing the serialized value
276      * @param atRegistry The AttributeType registry
277      * @throws IOException If we weren't able to deserialize the data
278      * @throws ClassNotFoundException if we weren't able to construct a Modification instance
279      * @throws NamingException If we didn't found the AttributeType in the registries
280      */
281     public void deserialize( ObjectInput in, AttributeTypeRegistry atRegistry ) throws IOException, ClassNotFoundException, NamingException
282     {
283         // Read the operation
284         int op = in.readInt();
285         
286         operation = ModificationOperation.getOperation( op );
287         
288         // Read the attribute OID
289         String oid = in.readUTF();
290         
291         // Lookup for tha associated AttributeType
292         AttributeType attributeType = atRegistry.lookup( oid );
293         
294         attribute = new DefaultServerAttribute( attributeType );
295         
296         // Read the attribute
297         ((DefaultServerAttribute)attribute).deserialize( in );
298     }
299     
300     
301     /**
302      * Serialize a ServerModification.
303      */
304     public void serialize( ObjectOutput out ) throws IOException
305     {
306         if ( attribute == null )
307         {
308             throw new IOException( "Cannot serialize a Modification with no attribute" );
309         }
310         
311         // Write the operation
312         out.writeInt( operation.getValue() );
313         
314         AttributeType at = ((DefaultServerAttribute)attribute).getAttributeType();
315         
316         // Write the attribute's oid
317         out.writeUTF( at.getOid() );
318         
319         // Write the attribute
320         ((DefaultServerAttribute)attribute).serialize( out );
321     }
322     
323     
324     /**
325      * @see Object#toString()
326      */
327     public String toString()
328     {
329         StringBuilder sb = new StringBuilder();
330         
331         sb.append( "Modification: " ).
332             append( operation ).
333             append( "\n" ).
334             append( ", attribute : " ).
335             append( attribute );
336         
337         return sb.toString();
338     }
339 }