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.mitosis.operation;
21  
22  
23  import org.apache.directory.mitosis.common.CSN;
24  import org.apache.directory.server.core.CoreSession;
25  import org.apache.directory.server.core.DirectoryService;
26  import org.apache.directory.server.core.entry.ServerAttribute;
27  import org.apache.directory.server.core.entry.ServerEntry;
28  import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
29  import org.apache.directory.server.core.partition.PartitionNexus;
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.name.LdapDN;
35  
36  import java.util.List;
37  
38  
39  /**
40   * An {@link Operation} that replaces an attribute in an entry.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class ReplaceAttributeOperation extends AttributeOperation
45  {
46      /**
47       * Declares the Serial Version Uid.
48       *
49       * @see <a
50       *      href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always
51       *      Declare Serial Version Uid</a>
52       */
53      private static final long serialVersionUID = -6573196586521610472L;
54  
55  
56      /**
57       * Creates a new operation that replaces the specified attribute. This 
58       * constructor will not be visible out of this package, as it is 
59       * only used for the deserialization process.
60       * 
61       * @param attribute an attribute to replace
62       */
63      /** No qualifier*/ ReplaceAttributeOperation( Registries registries )
64      {
65          super( registries, OperationType.REPLACE_ATTRIBUTE );
66      }
67  
68      
69      /**
70       * Creates a new operation that replaces the specified attribute.
71       * 
72       * @param attribute an attribute to replace
73       * @param csn The operation CSN
74       * @param dn The associated DN
75       */
76      public ReplaceAttributeOperation( Registries registries, CSN csn, LdapDN dn, ServerAttribute attribute )
77      {
78          super( registries, OperationType.REPLACE_ATTRIBUTE, csn, dn, attribute );
79      }
80  
81  
82      /**
83       * Inject the modified attribute into the server.
84       * 
85       * @param nexus the partition which will be modified
86       * @param coreSession the current session
87       */
88      protected void execute1( PartitionNexus nexus, CoreSession coreSession ) throws Exception
89      {
90          DirectoryService ds = coreSession.getDirectoryService();
91          ServerEntry serverEntry = ds.newEntry( LdapDN.EMPTY_LDAPDN );
92          EntryAttribute attribute = getAttribute();
93          serverEntry.put( attribute );
94          List<Modification> items = ModifyOperationContext.createModItems( serverEntry, 
95              ModificationOperation.REPLACE_ATTRIBUTE );
96  
97          nexus.modify( new ModifyOperationContext( coreSession, getDn(), items ) );
98      }
99  
100 
101     /**
102      * @see Object#toString()
103      */
104     public String toString()
105     {
106         return super.toString() + ".replace( " + getAttribute() + " )";
107     }
108 }