001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *  http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.directory.shared.ldap.model.entry;
020
021
022import java.io.IOException;
023import java.io.ObjectInput;
024import java.io.ObjectOutput;
025import java.util.Iterator;
026import java.util.List;
027import java.util.Set;
028
029import org.apache.directory.shared.ldap.model.exception.LdapException;
030import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
031import org.apache.directory.shared.ldap.model.name.Dn;
032import org.apache.directory.shared.ldap.model.schema.AttributeType;
033import org.apache.directory.shared.util.exception.NotImplementedException;
034
035
036/**
037 * A default implementation of a ServerEntry which should suite most
038 * use cases.
039 * 
040 * This class is final, it should not be extended.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class ImmutableEntry implements Entry
045{
046    /** Used for serialization */
047    private static final long serialVersionUID = 2L;
048
049    /** The wrapped Entry for this entry */
050    private Entry entry;
051
052    //-------------------------------------------------------------------------
053    // Constructors
054    //-------------------------------------------------------------------------
055    /**
056     * Creates a new instance of DefaultEntry. 
057     * <p>
058     * This entry <b>must</b> be initialized before being used !
059     */
060    public ImmutableEntry( Entry entry )
061    {
062        this.entry = entry;
063    }
064
065
066    //-------------------------------------------------------------------------
067    // Entry methods
068    //-------------------------------------------------------------------------
069    /**
070     * {@inheritDoc}
071     */
072    public void add( AttributeType attributeType, byte[]... values ) throws LdapException
073    {
074        new Exception().printStackTrace();
075        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
076    }
077
078
079    /**
080     * {@inheritDoc}
081     */
082    public void add( AttributeType attributeType, String... values ) throws LdapException
083    {
084        new Exception().printStackTrace();
085        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
086    }
087
088
089    /**
090     * {@inheritDoc}
091     */
092    public void add( AttributeType attributeType, Value<?>... values ) throws LdapException
093    {
094        new Exception().printStackTrace();
095        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public void add( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
103    {
104        new Exception().printStackTrace();
105        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    public void add( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
113    {
114        new Exception().printStackTrace();
115        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    public void add( String upId, AttributeType attributeType, String... values ) throws LdapException
123    {
124        new Exception().printStackTrace();
125        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    public void add( Attribute... attributes ) throws LdapException
133    { 
134        new Exception().printStackTrace();
135        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    public void add( String upId, byte[]... values ) throws LdapException
143    {
144        new Exception().printStackTrace();
145        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
146    }
147
148
149    /**
150     * {@inheritDoc}
151     */
152    public void add( String upId, String... values ) throws LdapException
153    {
154        new Exception().printStackTrace();
155        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    public void add( String upId, Value<?>... values ) throws LdapException
163    {
164        new Exception().printStackTrace();
165        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
166    }
167
168
169    /**
170     * Clone an entry. All the element are duplicated, so a modification on
171     * the original object won't affect the cloned object, as a modification
172     * on the cloned object has no impact on the original object
173     */
174    public Entry clone()
175    {
176        return entry.clone();
177    }
178
179
180    /**
181     * {@inheritDoc}
182     */
183    public boolean contains( Attribute... attributes )
184    {
185        return entry.contains( attributes );
186    }
187
188
189    /**
190     * {@inheritDoc}
191     */
192    public boolean containsAttribute( String... attributes )
193    {
194        return entry.containsAttribute( attributes );
195    }
196
197
198    /**
199     * {@inheritDoc}
200     */
201    public boolean containsAttribute( AttributeType attributeType )
202    {
203        return entry.containsAttribute( attributeType );
204    }
205
206
207    /**
208     * {@inheritDoc}
209     */
210    public boolean contains( AttributeType attributeType, byte[]... values )
211    {
212        return entry.contains( attributeType, values );
213    }
214
215
216    /**
217     * {@inheritDoc}
218     */
219    public boolean contains( AttributeType attributeType, String... values )
220    {
221        return entry.contains( attributeType, values );
222    }
223
224
225    /**
226     * {@inheritDoc}
227     */
228    public boolean contains( AttributeType attributeType, Value<?>... values )
229    {
230        return entry.contains( attributeType, values );
231    }
232
233
234    /**
235     * {@inheritDoc}
236     */
237    public boolean contains( String upId, byte[]... values )
238    {
239        return entry.contains( upId, values );
240    }
241
242
243    /**
244     * {@inheritDoc}
245     */
246    public boolean contains( String upId, String... values )
247    {
248        return entry.contains( upId, values );
249    }
250
251
252    /**
253     * {@inheritDoc}
254     */
255    public boolean contains( String upId, Value<?>... values )
256    {
257        return entry.contains( upId, values );
258    }
259
260
261    /**
262     * {@inheritDoc}
263     */
264    public Attribute get( String alias )
265    {
266        return entry.get( alias );
267    }
268
269
270    /**
271     * {@inheritDoc}
272     */
273    public Attribute get( AttributeType attributeType )
274    {
275        return entry.get( attributeType );
276    }
277
278
279    /**
280     * {@inheritDoc}
281     */
282    public Set<AttributeType> getAttributeTypes()
283    {
284        return entry.getAttributeTypes();
285    }
286
287
288    /**
289     * {@inheritDoc}
290     */
291    public Attribute put( String upId, byte[]... values )
292    {
293        new Exception().printStackTrace();
294        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
295    }
296
297
298    /**
299     * {@inheritDoc}
300     */
301    public Attribute put( String upId, String... values )
302    {
303        new Exception().printStackTrace();
304        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
305    }
306
307
308    /**
309     * {@inheritDoc}
310     */
311    public Attribute put( String upId, Value<?>... values )
312    {
313        new Exception().printStackTrace();
314        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
315    }
316
317
318    /**
319     * {@inheritDoc}
320     */
321    public List<Attribute> put( Attribute... attributes ) throws LdapException
322    {
323        new Exception().printStackTrace();
324        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
325    }
326
327
328    /**
329     * {@inheritDoc}
330     */
331    public Attribute put( AttributeType attributeType, byte[]... values ) throws LdapException
332    {
333        new Exception().printStackTrace();
334        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
335    }
336
337
338    /**
339     * {@inheritDoc}
340     */
341    public Attribute put( AttributeType attributeType, String... values ) throws LdapException
342    {
343        new Exception().printStackTrace();
344        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
345    }
346
347
348    /**
349     * {@inheritDoc}
350     */
351    public Attribute put( AttributeType attributeType, Value<?>... values ) throws LdapException
352    {
353        new Exception().printStackTrace();
354        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
355    }
356
357
358    /**
359     * {@inheritDoc}
360     */
361    public Attribute put( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
362    {
363        new Exception().printStackTrace();
364        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
365    }
366
367
368    /**
369     * {@inheritDoc}
370     */
371    public Attribute put( String upId, AttributeType attributeType, String... values ) throws LdapException
372    {
373        new Exception().printStackTrace();
374        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
375    }
376
377
378    /**
379     * {@inheritDoc}
380     */
381    public Attribute put( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
382    {
383        new Exception().printStackTrace();
384        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
385    }
386
387
388    /**
389     * {@inheritDoc}
390     */
391    public List<Attribute> remove( Attribute... attributes ) throws LdapException
392    {
393        new Exception().printStackTrace();
394        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
395    }
396
397
398    /**
399     * {@inheritDoc}
400     */
401    public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
402    {
403        new Exception().printStackTrace();
404        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
405    }
406
407
408    /**
409     * {@inheritDoc}
410     */
411    public boolean remove( AttributeType attributeType, String... values ) throws LdapException
412    {
413        new Exception().printStackTrace();
414        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
415    }
416
417
418    /**
419     * {@inheritDoc}
420     */
421    public boolean remove( AttributeType attributeType, Value<?>... values ) throws LdapException
422    {
423        new Exception().printStackTrace();
424        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
425    }
426
427
428    /**
429     * <p>
430     * Removes the attribute with the specified AttributeTypes. 
431     * </p>
432     * <p>
433     * The removed attribute are returned by this method. 
434     * </p>
435     * <p>
436     * If there is no attribute with the specified AttributeTypes,
437     * the return value is <code>null</code>.
438     * </p>
439     *
440     * @param attributes the AttributeTypes to be removed
441     * @return the removed attributes, if any, as a list; otherwise <code>null</code>
442     */
443    public List<Attribute> removeAttributes( AttributeType... attributes )
444    {
445        new Exception().printStackTrace();
446        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
447    }
448
449
450    /**
451     * {@inheritDoc}
452     */
453    public List<Attribute> removeAttributes( String... attributes )
454    {
455        new Exception().printStackTrace();
456        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
457    }
458
459
460    /**
461     * <p>
462     * Removes the specified binary values from an attribute.
463     * </p>
464     * <p>
465     * If at least one value is removed, this method returns <code>true</code>.
466     * </p>
467     * <p>
468     * If there is no more value after having removed the values, the attribute
469     * will be removed too.
470     * </p>
471     * <p>
472     * If the attribute does not exist, nothing is done and the method returns 
473     * <code>false</code>
474     * </p> 
475     *
476     * @param upId The attribute ID  
477     * @param values the values to be removed
478     * @return <code>true</code> if at least a value is removed, <code>false</code>
479     * if not all the values have been removed or if the attribute does not exist. 
480     */
481    public boolean remove( String upId, byte[]... values ) throws LdapException
482    {
483        new Exception().printStackTrace();
484        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
485    }
486
487
488    /**
489     * <p>
490     * Removes the specified String values from an attribute.
491     * </p>
492     * <p>
493     * If at least one value is removed, this method returns <code>true</code>.
494     * </p>
495     * <p>
496     * If there is no more value after having removed the values, the attribute
497     * will be removed too.
498     * </p>
499     * <p>
500     * If the attribute does not exist, nothing is done and the method returns 
501     * <code>false</code>
502     * </p> 
503     *
504     * @param upId The attribute ID  
505     * @param values the attributes to be removed
506     * @return <code>true</code> if at least a value is removed, <code>false</code>
507     * if not all the values have been removed or if the attribute does not exist. 
508     */
509    public boolean remove( String upId, String... values ) throws LdapException
510    {
511        new Exception().printStackTrace();
512        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
513    }
514
515
516    /**
517     * <p>
518     * Removes the specified values from an attribute.
519     * </p>
520     * <p>
521     * If at least one value is removed, this method returns <code>true</code>.
522     * </p>
523     * <p>
524     * If there is no more value after having removed the values, the attribute
525     * will be removed too.
526     * </p>
527     * <p>
528     * If the attribute does not exist, nothing is done and the method returns 
529     * <code>false</code>
530     * </p> 
531     *
532     * @param upId The attribute ID  
533     * @param values the attributes to be removed
534     * @return <code>true</code> if at least a value is removed, <code>false</code>
535     * if not all the values have been removed or if the attribute does not exist. 
536     */
537    public boolean remove( String upId, Value<?>... values ) throws LdapException
538    {
539        new Exception().printStackTrace();
540        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
541    }
542
543
544    /**
545     * Get this entry's Dn.
546     *
547     * @return The entry's Dn
548     */
549    public Dn getDn()
550    {
551        return entry.getDn();
552    }
553
554
555    /**
556     * {@inheritDoc}
557     */
558    public void setDn( Dn dn )
559    {
560        new Exception().printStackTrace();
561        throw new NotImplementedException( "Cannot rename the entry " + entry.getDn() + " is immutable." );
562    }
563
564
565    /**
566     * {@inheritDoc}
567     */
568    public void setDn( String dn ) throws LdapInvalidDnException
569    {
570        new Exception().printStackTrace();
571        throw new NotImplementedException( "Cannot rename the entry " + entry.getDn() + " is immutable." );
572    }
573
574
575    /**
576     * Remove all the attributes for this entry. The Dn is not reset
577     */
578    public void clear()
579    {
580        new Exception().printStackTrace();
581        throw new NotImplementedException( "Cannot clear the entry " + entry.getDn() + " is immutable." );
582    }
583
584
585    /**
586     * Returns an enumeration containing the zero or more attributes in the
587     * collection. The behavior of the enumeration is not specified if the
588     * attribute collection is changed.
589     *
590     * @return an enumeration of all contained attributes
591     */
592    public Iterator<Attribute> iterator()
593    {
594        return entry.iterator();
595    }
596
597
598    /**
599     * Returns the number of attributes.
600     *
601     * @return the number of attributes
602     */
603    public int size()
604    {
605        return entry.size();
606    }
607
608
609    /**
610     * This is the place where we serialize entries, and all theirs
611     * elements.
612     * <br/>
613     * The structure used to store the entry is the following :
614     * <ul>
615     *   <li>
616     *     <b>[Dn]</b> : If it's null, stores an empty Dn
617     *   </li>
618     *   <li>
619     *     <b>[attributes number]</b> : the number of attributes.
620     *   </li>
621     *   <li>
622     *     <b>[attribute]*</b> : each attribute, if we have some
623     *   </li>
624     * </ul>
625     * 
626     * {@inheritDoc} 
627     */
628    public void writeExternal( ObjectOutput out ) throws IOException
629    {
630        entry.writeExternal( out );
631    }
632
633
634    /**
635     * {@inheritDoc}
636     */
637    public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
638    {
639        new Exception().printStackTrace();
640        throw new NotImplementedException( "Cannot read the entry " + entry.getDn() + " is immutable." );
641    }
642
643
644    /**
645     * Serialize an Entry.
646     * 
647     * The structure is the following :
648     * <b>[a byte]</b> : if the Dn is empty 0 will be written else 1
649     * <b>[Rdn]</b> : The entry's Rdn.
650     * <b>[numberAttr]</b> : the bumber of attributes. Can be 0 
651     * <b>[attribute's oid]*</b> : The attribute's OID to get back 
652     * the attributeType on deserialization
653     * <b>[Attribute]*</b> The attribute
654     * 
655     * @param out the buffer in which the data will be serialized
656     * @throws IOException if the serialization failed
657     */
658    public void serialize( ObjectOutput out ) throws IOException
659    {
660        new Exception().printStackTrace();
661        throw new NotImplementedException( "Cannot serialize the entry " + entry.getDn() + " is immutable." );
662    }
663
664
665    /**
666     * Deserialize an entry. 
667     * 
668     * @param in The buffer containing the serialized serverEntry
669     * @throws IOException if there was a problem when deserializing
670     * @throws ClassNotFoundException if we can't deserialize an expected object
671     */
672    public void deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
673    {
674        new Exception().printStackTrace();
675        throw new NotImplementedException( "Cannot deserialize the entry " + entry.getDn() + " is immutable." );
676    }
677
678
679    /**
680     * Get the hash code of this ClientEntry. The Attributes will be sorted
681     * before the comparison can be done.
682     *
683     * @see java.lang.Object#hashCode()
684     * @return the instance's hash code 
685     */
686    public int hashCode()
687    {
688        return entry.hashCode();
689    }
690
691
692    /**
693     * {@inheritDoc}
694     */
695    public boolean hasObjectClass( String... objectClasses )
696    {
697        return entry.hasObjectClass( objectClasses );
698    }
699
700
701    /**
702     * {@inheritDoc}
703     */
704    public boolean hasObjectClass( Attribute... objectClasses )
705    {
706        return entry.hasObjectClass( objectClasses );
707    }
708
709
710    /**
711     * {@inheritDoc}
712     */
713    public boolean isSchemaAware()
714    {
715        return entry.isSchemaAware();
716    }
717    
718    
719    /**
720     * @see Object#equals(Object)
721     */
722    public boolean equals( Object o )
723    {
724        return entry.equals( o );
725    }
726
727
728    /**
729     * @see Object#toString()
730     */
731    public String toString()
732    {
733        return entry.toString();
734    }
735}