001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.schema.registries;
021
022
023import java.util.Arrays;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper;
029import org.apache.directory.api.util.StringConstants;
030
031
032/**
033 * The default Schema interface implementation.
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class DefaultSchema implements Schema
038{
039    /** The default schema's owner */
040    protected static final String DEFAULT_OWNER = "uid=admin,ou=system";
041
042    /** Tells if this schema is disabled */
043    protected boolean disabled;
044
045    /** Contains the list of schema it depends on */
046    protected String[] dependencies;
047
048    /** The schema owner */
049    protected String owner;
050
051    /** The schema name */
052    protected String name;
053
054    /** The set of SchemaObjects declared in this schema */
055    protected Set<SchemaObjectWrapper> content;
056    
057    /** The SchemaLoader used to load this schema */
058    protected SchemaLoader schemaLoader;
059
060
061    /**
062     * Creates a new instance of DefaultSchema.
063     *
064     * @param schemaLoader The ShcemaLoader to use
065     * @param name The schema's name
066     */
067    public DefaultSchema( SchemaLoader schemaLoader, String name )
068    {
069        this( schemaLoader, name, null, null, false );
070    }
071
072
073    /**
074     * Creates a new instance of DefaultSchema.
075     *
076     * @param schemaLoader The ShcemaLoader to use
077     * @param name The schema's name
078     * @param owner the schema's owner
079     */
080    public DefaultSchema( SchemaLoader schemaLoader, String name, String owner )
081    {
082        this( schemaLoader, name, owner, null, false );
083    }
084
085
086    /**
087     * Creates a new instance of DefaultSchema.
088     *
089     * @param schemaLoader The ShcemaLoader to use
090     * @param name The schema's name
091     * @param owner the schema's owner
092     * @param dependencies The list of schemas it depends on 
093     */
094    public DefaultSchema( SchemaLoader schemaLoader, String name, String owner, String[] dependencies )
095    {
096        this( schemaLoader, name, owner, dependencies, false );
097    }
098
099
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}