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.server.core.schema;
21  
22  
23  import org.apache.directory.server.constants.MetaSchemaConstants;
24  import org.apache.directory.server.core.entry.ServerEntry;
25  import org.apache.directory.server.schema.bootstrap.Schema;
26  import org.apache.directory.server.schema.registries.Registries;
27  import org.apache.directory.shared.ldap.entry.EntryAttribute;
28  import org.apache.directory.shared.ldap.entry.Modification;
29  import org.apache.directory.shared.ldap.entry.ModificationOperation;
30  import org.apache.directory.shared.ldap.exception.LdapNamingException;
31  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
32  import org.apache.directory.shared.ldap.name.LdapDN;
33  import org.apache.directory.shared.ldap.schema.AttributeType;
34  import org.apache.directory.shared.ldap.schema.SchemaObject;
35  
36  //import javax.naming.NamingException;
37  import java.util.HashSet;
38  import java.util.List;
39  import java.util.Set;
40  
41  
42  /**
43   * An abstract schema change handler with some reused functionality.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   * @version $Rev$
47   */
48  public abstract class AbstractSchemaChangeHandler implements SchemaChangeHandler
49  {
50      protected final Registries targetRegistries;
51      protected final PartitionSchemaLoader loader;
52      protected final AttributeType m_oidAT;
53      protected final SchemaEntityFactory factory;
54  
55      
56      protected AbstractSchemaChangeHandler( Registries targetRegistries, PartitionSchemaLoader loader ) throws Exception
57      {
58          this.targetRegistries = targetRegistries;
59          this.loader = loader;
60          this.m_oidAT = targetRegistries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_OID_AT );
61          this.factory = new SchemaEntityFactory( targetRegistries );
62      }
63      
64      
65      protected void checkOidIsUnique( ServerEntry entry ) throws Exception
66      {
67          String oid = getOid( entry );
68  
69          if ( targetRegistries.getOidRegistry().hasOid( oid ) )
70          {
71              throw new LdapNamingException( "Oid " + oid + " for new schema entity is not unique.",
72                  ResultCodeEnum.OTHER );
73          }
74      }
75  
76  
77      protected void checkOidIsUnique( SchemaObject schemaObject ) throws Exception
78      {
79          String oid = schemaObject.getOid();
80  
81          if ( targetRegistries.getOidRegistry().hasOid( oid ) )
82          {
83              throw new LdapNamingException( "Oid " + oid + " for new schema entity is not unique.",
84                  ResultCodeEnum.OTHER );
85          }
86      }
87  
88  
89      protected void checkOidIsUnique( String oid ) throws Exception
90      {
91          if ( targetRegistries.getOidRegistry().hasOid( oid ) )
92          {
93              throw new LdapNamingException( "Oid " + oid + " for new schema entity is not unique.",
94                  ResultCodeEnum.OTHER );
95          }
96      }
97      
98      
99      protected abstract void modify( LdapDN name, ServerEntry entry, ServerEntry targetEntry, boolean cascade ) 
100         throws Exception;
101     
102     
103     public final void modify( LdapDN name, ModificationOperation modOp, ServerEntry mods, ServerEntry entry, ServerEntry targetEntry, 
104         boolean cascade ) throws Exception
105     {
106         modify( name, entry, targetEntry, cascade );
107     }
108 
109 
110     public final void modify( LdapDN name, List<Modification> mods, ServerEntry entry,
111         ServerEntry targetEntry, boolean cascade ) throws Exception
112     {
113         modify( name, entry, targetEntry, cascade );
114     }
115 
116     
117     protected Set<String> getOids( Set<ServerEntry> results ) throws Exception
118     {
119         Set<String> oids = new HashSet<String>( results.size() );
120         
121         for ( ServerEntry result : results )
122         {
123             LdapDN dn = result.getDn();
124             dn.normalize( this.targetRegistries.getAttributeTypeRegistry().getNormalizerMapping() );
125             oids.add( ( String ) dn.getRdn().getValue() );
126         }
127         
128         return oids;
129     }
130     
131     
132     protected String getOid( ServerEntry entry ) throws Exception
133     {
134         EntryAttribute oid = entry.get( m_oidAT );
135         
136         if ( oid == null )
137         {
138             return null;
139         }
140         
141         return oid.getString();
142     }
143     
144     
145     protected String getSchemaName( LdapDN name ) throws Exception
146     {
147         return MetaSchemaUtils.getSchemaName( name );
148     }
149     
150     
151     protected Schema getSchema( LdapDN name ) throws Exception
152     {
153         return loader.getSchema( MetaSchemaUtils.getSchemaName( name ) );
154     }
155     
156     
157     protected void unregisterOids( String oid ) throws Exception
158     {
159         targetRegistries.getOidRegistry().unregister( oid );
160     }
161     
162     
163     protected void registerOids( SchemaObject obj ) throws Exception
164     {
165         String[] names = obj.getNamesRef();
166         
167         if ( names != null )
168         {
169             for ( String name:names )
170             {
171                 targetRegistries.getOidRegistry().register( name, obj.getOid() );
172             }
173         }
174         
175         targetRegistries.getOidRegistry().register( obj.getOid(), obj.getOid() );
176     }
177 }