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