1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mitosis.operation.support;
21
22
23 import javax.naming.NameNotFoundException;
24
25 import org.apache.directory.server.core.CoreSession;
26 import org.apache.directory.server.core.DirectoryService;
27 import org.apache.directory.server.core.entry.ServerEntry;
28 import org.apache.directory.server.core.interceptor.context.AddOperationContext;
29 import org.apache.directory.server.core.interceptor.context.EntryOperationContext;
30 import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
31 import org.apache.directory.server.core.partition.PartitionNexus;
32 import org.apache.directory.shared.ldap.constants.SchemaConstants;
33 import org.apache.directory.shared.ldap.entry.EntryAttribute;
34 import org.apache.directory.shared.ldap.name.LdapDN;
35 import org.apache.directory.shared.ldap.util.NamespaceTools;
36 import org.apache.directory.shared.ldap.util.StringTools;
37 import org.apache.directory.mitosis.common.CSN;
38 import org.apache.directory.mitosis.common.Constants;
39 import org.apache.directory.mitosis.common.DefaultCSN;
40
41
42 public class EntryUtil
43 {
44 @SuppressWarnings("unchecked")
45 public static boolean isEntryUpdatable( CoreSession coreSession, LdapDN name, CSN newCSN )
46 throws Exception
47 {
48 PartitionNexus nexus = coreSession.getDirectoryService().getPartitionNexus();
49 LookupOperationContext lookupContext = new LookupOperationContext( coreSession, name );
50 ServerEntry entry = nexus.lookup( lookupContext );
51
52 if ( entry == null )
53 {
54 return true;
55 }
56
57 EntryAttribute entryCSNAttr = entry.get( Constants.ENTRY_CSN );
58
59 if ( entryCSNAttr == null )
60 {
61 return true;
62 }
63 else
64 {
65 CSN oldCSN = null;
66
67 try
68 {
69 Object val = entryCSNAttr.get();
70
71 if ( val instanceof byte[] )
72 {
73 oldCSN = new DefaultCSN( StringTools.utf8ToString( (byte[])val ) );
74 }
75 else
76 {
77 oldCSN = new DefaultCSN( (String)val );
78 }
79 }
80 catch ( IllegalArgumentException e )
81 {
82 return true;
83 }
84
85 return oldCSN.compareTo( newCSN ) < 0;
86 }
87 }
88
89
90 public static void createGlueEntries( CoreSession coreSession, LdapDN name, boolean includeLeaf )
91 throws Exception
92 {
93 assert name.size() > 0;
94
95 for ( int i = name.size() - 1; i > 0; i-- )
96 {
97 createGlueEntry( coreSession, ( LdapDN ) name.getSuffix( i ) );
98 }
99
100 if ( includeLeaf )
101 {
102 createGlueEntry( coreSession, name );
103 }
104 }
105
106
107 private static void createGlueEntry( CoreSession coreSession, LdapDN name )
108 throws Exception
109 {
110 DirectoryService ds = coreSession.getDirectoryService();
111 PartitionNexus nexus = ds.getPartitionNexus();
112
113 try
114 {
115 if ( nexus.hasEntry( new EntryOperationContext( coreSession, name ) ) )
116 {
117 return;
118 }
119 }
120 catch ( NameNotFoundException e )
121 {
122
123 return;
124 }
125
126
127 ServerEntry entry = ds.newEntry( name );
128
129
130 String rdn = name.get( name.size() - 1 );
131 String rdnAttribute = NamespaceTools.getRdnAttribute( rdn );
132 String rdnValue = NamespaceTools.getRdnValue( rdn );
133 entry.put( rdnAttribute, rdnValue );
134
135
136 entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, SchemaConstants.EXTENSIBLE_OBJECT_OC );
137
138
139 nexus.add( new AddOperationContext( coreSession, entry ) );
140 }
141
142
143 private EntryUtil()
144 {
145 }
146 }