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<SchemaObjectWrapper>();
140        
141        this.schemaLoader = schemaLoader;
142    }
143
144
145    /**
146     * {@inheritDoc}
147     */
148    public String[] getDependencies()
149    {
150        String[] copy = new String[dependencies.length];
151        System.arraycopy( dependencies, 0, copy, 0, dependencies.length );
152        return copy;
153    }
154
155
156    /**
157     * {@inheritDoc}
158     */
159    public void addDependencies( String... dependenciesToAdd )
160    {
161        if ( dependenciesToAdd != null )
162        {
163            int start = 0;
164
165            if ( dependencies == null )
166            {
167                dependencies = new String[dependenciesToAdd.length];
168            }
169            else
170            {
171                String[] tempDependencies = new String[dependencies.length + dependenciesToAdd.length];
172                System.arraycopy( dependencies, 0, tempDependencies, 0, dependencies.length );
173                start = dependencies.length;
174                dependencies = tempDependencies;
175            }
176
177            System.arraycopy( dependenciesToAdd, 0, dependencies, start, dependenciesToAdd.length );
178        }
179    }
180
181
182    /**
183     * {@inheritDoc}
184     */
185    public String getOwner()
186    {
187        return owner;
188    }
189
190
191    /**
192     * {@inheritDoc}
193     */
194    public String getSchemaName()
195    {
196        return name;
197    }
198
199
200    /**
201     * {@inheritDoc}
202     */
203    public boolean isDisabled()
204    {
205        return disabled;
206    }
207
208
209    /**
210     * {@inheritDoc}
211     */
212    public boolean isEnabled()
213    {
214        return !disabled;
215    }
216
217
218    /**
219     * {@inheritDoc}
220     */
221    public void disable()
222    {
223        this.disabled = true;
224    }
225
226
227    /**
228     * {@inheritDoc}
229     */
230    public void enable()
231    {
232        this.disabled = false;
233    }
234
235
236    /**
237     * {@inheritDoc}
238     */
239    public Set<SchemaObjectWrapper> getContent()
240    {
241        return content;
242    }
243    
244    
245    /**
246     * {@inheritDoc}
247     */
248    public SchemaLoader getSchemaLoader()
249    {
250        return schemaLoader;
251    }
252
253
254    /**
255     * {@inheritDoc}
256     */
257    @Override
258    public String toString()
259    {
260        StringBuilder sb = new StringBuilder( "\tSchema Name: " );
261        sb.append( name );
262        sb.append( "\n\t\tDisabled: " );
263        sb.append( disabled );
264        sb.append( "\n\t\tOwner: " );
265        sb.append( owner );
266        sb.append( "\n\t\tDependencies: " );
267        sb.append( Arrays.toString( dependencies ) );
268        sb.append(  "\n\t\tSchemaLoader : " );
269        
270        if ( schemaLoader != null )
271        {
272            sb.append( schemaLoader.getClass().getSimpleName() );
273        }
274
275        // TODO : print the associated ShcemaObjects
276        return sb.toString();
277    }
278}