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.server.core.CoreSession;
24 import org.apache.directory.server.core.partition.PartitionNexus;
25 import org.apache.directory.server.schema.registries.Registries;
26 import org.apache.directory.shared.ldap.entry.EntryAttribute;
27 import org.apache.directory.shared.ldap.name.LdapDN;
28 import org.apache.directory.mitosis.common.CSN;
29 import org.apache.directory.mitosis.operation.support.EntryUtil;
30 import org.apache.directory.mitosis.store.ReplicationStore;
31
32
33 /**
34 * An {@link Operation} that adds an attribute to an entry.
35 *
36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37 */
38 public abstract class AttributeOperation extends Operation
39 {
40 /** The attribute's DN */
41 protected LdapDN dn;
42
43 /** The attribute */
44 protected EntryAttribute attribute;
45
46
47 /**
48 * Create a new instance of AttributeOperation. This constructor should not
49 * be visible out of this package, as it's only used for deserialization.
50 *
51 * @param registries The server registries
52 * @param operationType The operation type
53 */
54 /* No qualifier*/ AttributeOperation( Registries registries, OperationType operationType )
55 {
56 super( registries, operationType );
57 }
58
59
60 /**
61 * Create a new operation that affects an entry with the specified name.
62 *
63 * @param registries the server registries
64 * @param operationType The operation's type
65 * @param csn the operation's CSN
66 * @param dn the normalized name of an entry
67 * @param attribute an attribute to modify
68 */
69 public AttributeOperation( Registries registries, OperationType operationType, CSN csn,
70 LdapDN dn, EntryAttribute attribute )
71 {
72 super( registries, operationType, csn );
73
74 assert dn != null;
75 assert attribute != null;
76
77 this.dn = dn;
78 this.attribute = attribute.clone();
79 }
80
81
82 /**
83 * @return the name of an entry this operation will affect.
84 */
85 public LdapDN getDn()
86 {
87 return ( LdapDN ) dn.clone();
88 }
89
90
91 /**
92 * Check that we can apply the modification, and create the associated entry, if
93 * it does not exists locally.
94 *
95 * @param nexus The partition to update
96 * @param store the replication storage
97 * @param coreSession the current session
98 */
99 protected final void execute0( PartitionNexus nexus, ReplicationStore store, CoreSession coreSession )
100 throws Exception
101 {
102 if ( ! EntryUtil.isEntryUpdatable( coreSession, dn, getCSN() ) )
103 {
104 return;
105 }
106
107 EntryUtil.createGlueEntries( coreSession, dn, true );
108
109 execute1( nexus, coreSession );
110 }
111
112
113 /**
114 * Apply the requested modification locally
115 *
116 * @param nexus The partition on which the operation is applied
117 * @param coreSession the current session
118 * @throws Exception
119 */
120 protected abstract void execute1( PartitionNexus nexus, CoreSession coreSession ) throws Exception;
121
122
123 /**
124 * @return Returns the attribute to modify
125 */
126 public EntryAttribute getAttribute()
127 {
128 return attribute;
129 }
130
131 /**
132 * @see Object#toString()
133 */
134 public String toString()
135 {
136 return super.toString() + ": [" + dn + ']';
137 }
138 }