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.api.ldap.model.schema.registries;
21  
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper;
29  import org.apache.directory.api.util.StringConstants;
30  
31  
32  /**
33   * The default Schema interface implementation.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class DefaultSchema implements Schema
38  {
39      /** The default schema's owner */
40      protected static final String DEFAULT_OWNER = "uid=admin,ou=system";
41  
42      /** Tells if this schema is disabled */
43      protected boolean disabled;
44  
45      /** Contains the list of schema it depends on */
46      protected String[] dependencies;
47  
48      /** The schema owner */
49      protected String owner;
50  
51      /** The schema name */
52      protected String name;
53  
54      /** The set of SchemaObjects declared in this schema */
55      protected Set<SchemaObjectWrapper> content;
56      
57      /** The SchemaLoader used to load this schema */
58      protected SchemaLoader schemaLoader;
59  
60  
61      /**
62       * Creates a new instance of DefaultSchema.
63       *
64       * @param schemaLoader The ShcemaLoader to use
65       * @param name The schema's name
66       */
67      public DefaultSchema( SchemaLoader schemaLoader, String name )
68      {
69          this( schemaLoader, name, null, null, false );
70      }
71  
72  
73      /**
74       * Creates a new instance of DefaultSchema.
75       *
76       * @param schemaLoader The ShcemaLoader to use
77       * @param name The schema's name
78       * @param owner the schema's owner
79       */
80      public DefaultSchema( SchemaLoader schemaLoader, String name, String owner )
81      {
82          this( schemaLoader, name, owner, null, false );
83      }
84  
85  
86      /**
87       * Creates a new instance of DefaultSchema.
88       *
89       * @param schemaLoader The ShcemaLoader to use
90       * @param name The schema's name
91       * @param owner the schema's owner
92       * @param dependencies The list of schemas it depends on 
93       */
94      public DefaultSchema( SchemaLoader schemaLoader, String name, String owner, String[] dependencies )
95      {
96          this( schemaLoader, name, owner, dependencies, false );
97      }
98  
99  
100     /**
101      * Creates a new instance of DefaultSchema.
102      *
103      * @param schemaLoader The ShcemaLoader to use
104      * @param name The schema's name
105      * @param owner the schema's owner
106      * @param dependencies The list of schemas it depends on
107      * @param disabled Set the status for this schema 
108      */
109     public DefaultSchema( SchemaLoader schemaLoader, String name, String owner, String[] dependencies, boolean disabled )
110     {
111         if ( name == null )
112         {
113             throw new IllegalArgumentException( I18n.err( I18n.ERR_04266 ) );
114         }
115 
116         this.name = name;
117 
118         if ( owner != null )
119         {
120             this.owner = owner;
121         }
122         else
123         {
124             this.owner = DEFAULT_OWNER;
125         }
126 
127         if ( dependencies != null )
128         {
129             this.dependencies = new String[dependencies.length];
130             System.arraycopy( dependencies, 0, this.dependencies, 0, dependencies.length );
131         }
132         else
133         {
134             this.dependencies = StringConstants.EMPTY_STRINGS;
135         }
136 
137         this.disabled = disabled;
138 
139         content = new HashSet<>();
140         
141         this.schemaLoader = schemaLoader;
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public String[] getDependencies()
150     {
151         String[] copy = new String[dependencies.length];
152         System.arraycopy( dependencies, 0, copy, 0, dependencies.length );
153         return copy;
154     }
155 
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public void addDependencies( String... dependenciesToAdd )
162     {
163         if ( dependenciesToAdd != null )
164         {
165             int start = 0;
166 
167             if ( dependencies == null )
168             {
169                 dependencies = new String[dependenciesToAdd.length];
170             }
171             else
172             {
173                 String[] tempDependencies = new String[dependencies.length + dependenciesToAdd.length];
174                 System.arraycopy( dependencies, 0, tempDependencies, 0, dependencies.length );
175                 start = dependencies.length;
176                 dependencies = tempDependencies;
177             }
178 
179             System.arraycopy( dependenciesToAdd, 0, dependencies, start, dependenciesToAdd.length );
180         }
181     }
182 
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public String getOwner()
189     {
190         return owner;
191     }
192 
193 
194     /**
195      * {@inheritDoc}
196      */
197     @Override
198     public String getSchemaName()
199     {
200         return name;
201     }
202 
203 
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public boolean isDisabled()
209     {
210         return disabled;
211     }
212 
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public boolean isEnabled()
219     {
220         return !disabled;
221     }
222 
223 
224     /**
225      * {@inheritDoc}
226      */
227     @Override
228     public void disable()
229     {
230         this.disabled = true;
231     }
232 
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public void enable()
239     {
240         this.disabled = false;
241     }
242 
243 
244     /**
245      * {@inheritDoc}
246      */
247     @Override
248     public Set<SchemaObjectWrapper> getContent()
249     {
250         return content;
251     }
252     
253     
254     /**
255      * {@inheritDoc}
256      */
257     @Override
258     public SchemaLoader getSchemaLoader()
259     {
260         return schemaLoader;
261     }
262 
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     public String toString()
269     {
270         StringBuilder sb = new StringBuilder( "\tSchema Name: " );
271         sb.append( name );
272         sb.append( "\n\t\tDisabled: " );
273         sb.append( disabled );
274         sb.append( "\n\t\tOwner: " );
275         sb.append( owner );
276         sb.append( "\n\t\tDependencies: " );
277         sb.append( Arrays.toString( dependencies ) );
278         sb.append(  "\n\t\tSchemaLoader : " );
279         
280         if ( schemaLoader != null )
281         {
282             sb.append( schemaLoader.getClass().getSimpleName() );
283         }
284 
285         // TODO : print the associated ShcemaObjects
286         return sb.toString();
287     }
288 }