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.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             // Skip if there's no backend associated with the name.
123             return;
124         }
125 
126         // Create a glue entry.
127         ServerEntry entry = ds.newEntry( name );
128         
129         //// Add RDN attribute. 
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         //// Add objectClass attribute. 
136         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, SchemaConstants.EXTENSIBLE_OBJECT_OC );
137 
138         // And add it to the nexus.
139         nexus.add( new AddOperationContext( coreSession, entry ) );
140     }
141 
142 
143     private EntryUtil()
144     {
145     }
146 }