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 name The schema's name
065     */
066    public DefaultSchema( SchemaLoader schemaLoader, String name )
067    {
068        this( schemaLoader, name, null, null, false );
069    }
070
071
072    /**
073     * Creates a new instance of DefaultSchema.
074     *
075     * @param name The schema's name
076     * @param owner the schema's owner
077     */
078    public DefaultSchema( SchemaLoader schemaLoader, String name, String owner )
079    {
080        this( schemaLoader, name, owner, null, false );
081    }
082
083
084    /**
085     * Creates a new instance of DefaultSchema.
086     *
087     * @param name The schema's name
088     * @param owner the schema's owner
089     * @param dependencies The list of schemas it depends on 
090     */
091    public DefaultSchema( SchemaLoader schemaLoader, String name, String owner, String[] dependencies )
092    {
093        this( schemaLoader, name, owner, dependencies, false );
094    }
095
096
097    /**
098     * Creates a new instance of DefaultSchema.
099     *
100     * @param name The schema's name
101     * @param owner the schema's owner
102     * @param dependencies The list of schemas it depends on
103     * @param disabled Set the status for this schema 
104     */
105    public DefaultSchema( SchemaLoader schemaLoader, String name, String owner, String[] dependencies, boolean disabled )
106    {
107        if ( name == null )
108        {
109            throw new IllegalArgumentException( I18n.err( I18n.ERR_04266 ) );
110        }
111
112        this.name = name;
113
114        if ( owner != null )
115        {
116            this.owner = owner;
117        }
118        else
119        {
120            this.owner = DEFAULT_OWNER;
121        }
122
123        if ( dependencies != null )
124        {
125            this.dependencies = new String[dependencies.length];
126            System.arraycopy( dependencies, 0, this.dependencies, 0, dependencies.length );
127        }
128        else
129        {
130            this.dependencies = StringConstants.EMPTY_STRINGS;
131        }
132
133        this.disabled = disabled;
134
135        content = new HashSet<SchemaObjectWrapper>();
136        
137        this.schemaLoader = schemaLoader;
138    }
139
140
141    /**
142     * {@inheritDoc}
143     */
144    public String[] getDependencies()
145    {
146        String[] copy = new String[dependencies.length];
147        System.arraycopy( dependencies, 0, copy, 0, dependencies.length );
148        return copy;
149    }
150
151
152    /**
153     * {@inheritDoc}
154     */
155    public void addDependencies( String... dependenciesToAdd )
156    {
157        if ( dependenciesToAdd != null )
158        {
159            int start = 0;
160
161            if ( dependencies == null )
162            {
163                dependencies = new String[dependenciesToAdd.length];
164            }
165            else
166            {
167                String[] tempDependencies = new String[dependencies.length + dependenciesToAdd.length];
168                System.arraycopy( dependencies, 0, tempDependencies, 0, dependencies.length );
169                start = dependencies.length;
170                dependencies = tempDependencies;
171            }
172
173            System.arraycopy( dependenciesToAdd, 0, dependencies, start, dependenciesToAdd.length );
174        }
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    public String getOwner()
182    {
183        return owner;
184    }
185
186
187    /**
188     * {@inheritDoc}
189     */
190    public String getSchemaName()
191    {
192        return name;
193    }
194
195
196    /**
197     * {@inheritDoc}
198     */
199    public boolean isDisabled()
200    {
201        return disabled;
202    }
203
204
205    /**
206     * {@inheritDoc}
207     */
208    public boolean isEnabled()
209    {
210        return !disabled;
211    }
212
213
214    /**
215     * {@inheritDoc}
216     */
217    public void disable()
218    {
219        this.disabled = true;
220    }
221
222
223    /**
224     * {@inheritDoc}
225     */
226    public void enable()
227    {
228        this.disabled = false;
229    }
230
231
232    /**
233     * {@inheritDoc}
234     */
235    public Set<SchemaObjectWrapper> getContent()
236    {
237        return content;
238    }
239    
240    
241    /**
242     * {@inheritDoc}
243     */
244    public SchemaLoader getSchemaLoader()
245    {
246        return schemaLoader;
247    }
248
249
250    /**
251     * {@inheritDoc}
252     */
253    @Override
254    public String toString()
255    {
256        StringBuilder sb = new StringBuilder( "\tSchema Name: " );
257        sb.append( name );
258        sb.append( "\n\t\tDisabled: " );
259        sb.append( disabled );
260        sb.append( "\n\t\tOwner: " );
261        sb.append( owner );
262        sb.append( "\n\t\tDependencies: " );
263        sb.append( Arrays.toString( dependencies ) );
264        sb.append(  "\n\t\tSchemaLoader : " );
265        
266        if ( schemaLoader != null )
267        {
268            sb.append( schemaLoader.getClass().getSimpleName() );
269        }
270
271        // TODO : print the associated ShcemaObjects
272        return sb.toString();
273    }
274}