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     */
020    package org.apache.directory.shared.ldap.schema.registries;
021    
022    
023    import java.util.Arrays;
024    import java.util.HashSet;
025    import java.util.Set;
026    
027    import org.apache.directory.shared.i18n.I18n;
028    import org.apache.directory.shared.ldap.schema.SchemaObjectWrapper;
029    import org.apache.directory.shared.ldap.util.StringTools;
030    
031    
032    
033    /**
034     * The default Schema interface implementation.
035     *
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$
038     */
039    public class DefaultSchema implements Schema
040    {
041        /** The default schema's owner */
042        private static final String DEFAULT_OWNER = "uid=admin,ou=system";
043        
044        /** Tells if this schema is disabled */
045        private boolean disabled;
046        
047        /** Contains the list of schema it depends on */
048        private String[] dependencies;
049        
050        /** The schema owner */
051        private String owner;
052        
053        /** The schema name */
054        private String name;
055        
056        /** The set of SchemaObjects declared in this schema */
057        private Set<SchemaObjectWrapper> content;
058        
059        
060        /**
061         * Creates a new instance of DefaultSchema.
062         *
063         * @param name The schema's name
064         */
065        public DefaultSchema( String name )
066        {
067            this( name, null, null, false );
068        }
069        
070            
071        /**
072         * Creates a new instance of DefaultSchema.
073         *
074         * @param name The schema's name
075         * @param owner the schema's owner
076         */
077        public DefaultSchema( String name, String owner )
078        {
079            this( name, owner, null, false );
080        }
081        
082            
083        /**
084         * Creates a new instance of DefaultSchema.
085         *
086         * @param name The schema's name
087         * @param owner the schema's owner
088         * @param dependencies The list of schemas it depends on 
089         */
090        public DefaultSchema( String name, String owner, String[] dependencies )
091        {
092            this( name, owner, dependencies, false );
093        }
094        
095            
096        /**
097         * Creates a new instance of DefaultSchema.
098         *
099         * @param name The schema's name
100         * @param owner the schema's owner
101         * @param dependencies The list of schemas it depends on
102         * @param disabled Set the status for this schema 
103         */
104        public DefaultSchema( String name, String owner, String[] dependencies, boolean disabled )
105        {
106            if ( name == null )
107            {
108                throw new NullPointerException( I18n.err( I18n.ERR_04266 ) );
109            }
110            
111            this.name = name;
112            
113            if ( owner != null )
114            {
115                this.owner = owner;
116            }
117            else
118            {
119                this.owner = DEFAULT_OWNER;
120            }
121            
122            if ( dependencies != null )
123            {
124                this.dependencies = dependencies;
125            }
126            else
127            {
128                this.dependencies = StringTools.EMPTY_STRINGS;
129            }
130            
131            this.disabled = disabled;
132            
133            content = new HashSet<SchemaObjectWrapper>(); 
134        }
135    
136    
137        /**
138         * {@inheritDoc}
139         */
140        public String[] getDependencies()
141        {
142            String[] copy = new String[dependencies.length];
143            System.arraycopy( dependencies, 0, copy, 0, dependencies.length );
144            return copy;
145        }
146    
147        
148        /**
149         * {@inheritDoc}
150         */
151        public void addDependencies( String... dependencies )
152        {
153            if ( dependencies != null )
154            {
155                this.dependencies = new String[dependencies.length];
156                System.arraycopy( this.dependencies, 0, dependencies, 0, dependencies.length );
157            }
158        }
159    
160        
161        /**
162         * {@inheritDoc}
163         */
164        public String getOwner()
165        {
166            return owner;
167        }
168    
169        
170        /**
171         * {@inheritDoc}
172         */
173        public String getSchemaName()
174        {
175            return name;
176        }
177        
178        
179        /**
180         * {@inheritDoc}
181         */
182        public boolean isDisabled()
183        {
184            return disabled;
185        }
186        
187        
188        /**
189         * {@inheritDoc}
190         */
191        public boolean isEnabled()
192        {
193            return !disabled;
194        }
195        
196        
197        /**
198         * {@inheritDoc}
199         */
200        public void disable()
201        {
202            this.disabled = true;
203        }
204        
205        
206        /**
207         * {@inheritDoc}
208         */
209        public void enable()
210        {
211            this.disabled = false;
212        }
213    
214    
215        /**
216         * {@inheritDoc}
217         */
218        public Set<SchemaObjectWrapper> getContent()
219        {
220            return content;
221        }
222        
223    
224        /**
225         * @see Object#toString()
226         */
227        public String toString()
228        {
229            StringBuilder sb = new StringBuilder( "\tSchema Name: " );
230            sb.append( name );
231            sb.append( "\n\t\tDisabled: " );
232            sb.append( disabled );
233            sb.append( "\n\t\tOwner: " );
234            sb.append( owner );
235            sb.append( "\n\t\tDependencies: " );
236            sb.append( Arrays.toString( dependencies ) );
237            
238            // TODO : print the associated ShcemaObjects
239            return sb.toString();
240        }
241    }