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  
58      /**
59       * Creates a new instance of DefaultSchema.
60       *
61       * @param name The schema's name
62       */
63      public DefaultSchema( String name )
64      {
65          this( name, null, null, false );
66      }
67  
68  
69      /**
70       * Creates a new instance of DefaultSchema.
71       *
72       * @param name The schema's name
73       * @param owner the schema's owner
74       */
75      public DefaultSchema( String name, String owner )
76      {
77          this( name, owner, null, false );
78      }
79  
80  
81      /**
82       * Creates a new instance of DefaultSchema.
83       *
84       * @param name The schema's name
85       * @param owner the schema's owner
86       * @param dependencies The list of schemas it depends on 
87       */
88      public DefaultSchema( String name, String owner, String[] dependencies )
89      {
90          this( name, owner, dependencies, false );
91      }
92  
93  
94      /**
95       * Creates a new instance of DefaultSchema.
96       *
97       * @param name The schema's name
98       * @param owner the schema's owner
99       * @param dependencies The list of schemas it depends on
100      * @param disabled Set the status for this schema 
101      */
102     public DefaultSchema( String name, String owner, String[] dependencies, boolean disabled )
103     {
104         if ( name == null )
105         {
106             throw new IllegalArgumentException( I18n.err( I18n.ERR_04266 ) );
107         }
108 
109         this.name = name;
110 
111         if ( owner != null )
112         {
113             this.owner = owner;
114         }
115         else
116         {
117             this.owner = DEFAULT_OWNER;
118         }
119 
120         if ( dependencies != null )
121         {
122             this.dependencies = new String[dependencies.length];
123             System.arraycopy( dependencies, 0, this.dependencies, 0, dependencies.length );
124         }
125         else
126         {
127             this.dependencies = StringConstants.EMPTY_STRINGS;
128         }
129 
130         this.disabled = disabled;
131 
132         content = new HashSet<SchemaObjectWrapper>();
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public String[] getDependencies()
140     {
141         String[] copy = new String[dependencies.length];
142         System.arraycopy( dependencies, 0, copy, 0, dependencies.length );
143         return copy;
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     public void addDependencies( String... dependenciesToAdd )
151     {
152         if ( dependenciesToAdd != null )
153         {
154             int start = 0;
155 
156             if ( dependencies == null )
157             {
158                 dependencies = new String[dependenciesToAdd.length];
159             }
160             else
161             {
162                 String[] tempDependencies = new String[dependencies.length + dependenciesToAdd.length];
163                 System.arraycopy( dependencies, 0, tempDependencies, 0, dependencies.length );
164                 start = dependencies.length;
165                 dependencies = tempDependencies;
166             }
167 
168             System.arraycopy( dependenciesToAdd, 0, dependencies, start, dependenciesToAdd.length );
169         }
170     }
171 
172 
173     /**
174      * {@inheritDoc}
175      */
176     public String getOwner()
177     {
178         return owner;
179     }
180 
181 
182     /**
183      * {@inheritDoc}
184      */
185     public String getSchemaName()
186     {
187         return name;
188     }
189 
190 
191     /**
192      * {@inheritDoc}
193      */
194     public boolean isDisabled()
195     {
196         return disabled;
197     }
198 
199 
200     /**
201      * {@inheritDoc}
202      */
203     public boolean isEnabled()
204     {
205         return !disabled;
206     }
207 
208 
209     /**
210      * {@inheritDoc}
211      */
212     public void disable()
213     {
214         this.disabled = true;
215     }
216 
217 
218     /**
219      * {@inheritDoc}
220      */
221     public void enable()
222     {
223         this.disabled = false;
224     }
225 
226 
227     /**
228      * {@inheritDoc}
229      */
230     public Set<SchemaObjectWrapper> getContent()
231     {
232         return content;
233     }
234 
235 
236     /**
237      * {@inheritDoc}
238      */
239     @Override
240     public String toString()
241     {
242         StringBuilder sb = new StringBuilder( "\tSchema Name: " );
243         sb.append( name );
244         sb.append( "\n\t\tDisabled: " );
245         sb.append( disabled );
246         sb.append( "\n\t\tOwner: " );
247         sb.append( owner );
248         sb.append( "\n\t\tDependencies: " );
249         sb.append( Arrays.toString( dependencies ) );
250 
251         // TODO : print the associated ShcemaObjects
252         return sb.toString();
253     }
254 }