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.server.config.beans;
021
022
023import java.util.List;
024
025
026/**
027 * The base class containing all the configuration hierarchy. This hierarchy
028 * starts with the DirectoryService elements.
029 *
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032public class ConfigBean
033{
034    /** The DirectoryService beans */
035    private List<AdsBaseBean> directoryServiceBeans;
036
037
038    /**
039     * Create a new ConfigBean instance
040     */
041    public ConfigBean()
042    {
043    }
044
045
046    /**
047     * Add underlying DirectoryServiceBean
048     * @param directoryServiceBeans The DirectoryServiceBeans
049     */
050    public void addDirectoryService( DirectoryServiceBean... directoryServiceBeans )
051    {
052        for ( DirectoryServiceBean directoryServiceBean : directoryServiceBeans )
053        {
054            this.directoryServiceBeans.add( directoryServiceBean );
055        }
056    }
057
058
059    /**
060     * @return the directoryServiceBeans
061     */
062    public List<AdsBaseBean> getDirectoryServiceBeans()
063    {
064        return directoryServiceBeans;
065    }
066
067
068    /**
069     * @return the first directoryServiceBean found into the configuration
070     */
071    public DirectoryServiceBean getDirectoryServiceBean()
072    {
073        if ( ( directoryServiceBeans == null ) || ( directoryServiceBeans.size() == 0 ) )
074        {
075            return null;
076        }
077
078        for ( AdsBaseBean bean : directoryServiceBeans )
079        {
080            if ( bean instanceof DirectoryServiceBean )
081            {
082                return ( DirectoryServiceBean ) bean;
083            }
084        }
085
086        return null;
087    }
088
089
090    /**
091     * @param directoryServiceId The DirectoryService ID we want to get
092     * @return the found directoryServiceBean
093     */
094    public DirectoryServiceBean getDirectoryServiceBean( String directoryServiceId )
095    {
096        if ( ( directoryServiceBeans == null ) || ( directoryServiceBeans.size() == 0 ) )
097        {
098            return null;
099        }
100
101        for ( AdsBaseBean bean : directoryServiceBeans )
102        {
103            if ( bean instanceof DirectoryServiceBean )
104            {
105                if ( ( ( DirectoryServiceBean ) bean ).getDirectoryServiceId().equals( directoryServiceId ) )
106                {
107                    return ( DirectoryServiceBean ) bean;
108                }
109            }
110        }
111
112        return null;
113    }
114
115
116    /**
117     * @param directoryServiceBeans the directoryServiceBeans to set
118     */
119    public void setDirectoryServiceBeans( List<AdsBaseBean> directoryServiceBeans )
120    {
121        this.directoryServiceBeans = directoryServiceBeans;
122    }
123
124
125    /**
126     * {@inheritDoc}
127     */
128    public String toString()
129    {
130        StringBuilder sb = new StringBuilder();
131
132        if ( directoryServiceBeans != null )
133        {
134            for ( AdsBaseBean directoryService : directoryServiceBeans )
135            {
136                sb.append( directoryService ).append( "\n\n" );
137            }
138        }
139
140        return sb.toString();
141    }
142}