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
058    /**
059     * Creates a new instance of DefaultSchema.
060     *
061     * @param name The schema's name
062     */
063    public DefaultSchema( String name )
064    {
065        this( name, null, null, false );
066    }
067
068
069    /**
070     * Creates a new instance of DefaultSchema.
071     *
072     * @param name The schema's name
073     * @param owner the schema's owner
074     */
075    public DefaultSchema( String name, String owner )
076    {
077        this( name, owner, null, false );
078    }
079
080
081    /**
082     * Creates a new instance of DefaultSchema.
083     *
084     * @param name The schema's name
085     * @param owner the schema's owner
086     * @param dependencies The list of schemas it depends on 
087     */
088    public DefaultSchema( String name, String owner, String[] dependencies )
089    {
090        this( name, owner, dependencies, false );
091    }
092
093
094    /**
095     * Creates a new instance of DefaultSchema.
096     *
097     * @param name The schema's name
098     * @param owner the schema's owner
099     * @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}