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    
021    package org.apache.directory.server.ldap.replication;
022    
023    
024    import javax.naming.InvalidNameException;
025    
026    import org.apache.directory.shared.ldap.codec.util.LdapURLEncodingException;
027    import org.apache.directory.shared.ldap.name.LdapDN;
028    import org.apache.directory.shared.ldap.util.LdapURL;
029    import org.apache.directory.shared.ldap.util.StringTools;
030    
031    /**
032     * A configuration for a replica peer. We may have many replications relation
033     * set for a server, each one of them being described with this structure. 
034     *
035     * @org.apache.xbean.XBean
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$, $Date$
038     */
039    public class ReplicaPeerConfiguration
040    {
041        /** A flag used when the replication use the RefreshOnly system */
042        private boolean refreshOnly;
043        
044        /** The time to wait between two consecutive RefreshOnly replication*/
045        private long interval;
046        
047        /** Default interval is 5 minutes */
048        private static final long DEFAULT_INTERVAL = 1000L*5L*60L;
049        
050        /** The default host */
051        private static final String DEFAULT_HOST = "localhost";
052        
053        /** The default ssl port */
054        private static final int DEFAULT_PORT = 10389;
055        
056        /** The default port */
057        private static final int DEFAULT_SSL_PORT = 10636;
058        
059        /** The producer we want to replicate */
060        private LdapURL producer;
061        
062        /** The principal to use to connect to the producer */
063        private LdapDN principalDN;
064        
065        /** The principal's password */
066        private String password;
067        
068        /** The producer's host */
069        private String host;
070        
071        /** The producer's port */
072        private int port;
073        
074        /** The base DN used for replication */
075        private LdapDN baseDN;
076        
077        /** A flag to tell the server to use an SSL connection */
078        private boolean useSSL;
079    
080        
081        /**
082         * 
083         * Creates a new instance of ConsumerConfiguration.
084         *
085         */
086        public ReplicaPeerConfiguration()
087        {
088            interval = DEFAULT_INTERVAL;
089        }
090        
091        /**
092         * Set the type of replication wanted. If false, it will default
093         * to RefreshAndPersist.
094         * @param refreshOnly true if the refreshOnly replication is requested
095         */
096        public void setRefreshOnly( boolean refreshOnly )
097        {
098            this.refreshOnly = refreshOnly;
099        }
100    
101        /**
102         * @return the refreshOnly flag
103         */
104        public boolean isRefreshOnly()
105        {
106            return refreshOnly;
107        }
108    
109        /**
110         * Set the delay between two RefreshOnly replication. Its given in seconds.
111         * @param interval the interval to set
112         */
113        public void setInterval( long interval )
114        {
115            // Convert to milliseconds
116            this.interval = interval*1000L;
117        }
118    
119        /**
120         * @return the interval
121         */
122        public long getInterval()
123        {
124            return interval;
125        }
126    
127        /**
128         * @return the baseDN
129         */
130        public LdapDN getBaseDN()
131        {
132            return baseDN;
133        }
134    
135        /**
136         * @param principalDN the principalDN to set
137         */
138        public void setPrincipalDN( String principalDN ) throws InvalidNameException
139        {
140            this.principalDN = new LdapDN( principalDN );
141        }
142    
143    
144        /**
145         * @return the principalDN
146         */
147        public LdapDN getPrincipalDN()
148        {
149            return principalDN;
150        }
151    
152        
153        /**
154         * @param password the password to set
155         */
156        public void setPassword( String password )
157        {
158            this.password = password;
159        }
160    
161        /**
162         * @return the password
163         */
164        public String getPassword()
165        {
166            return password;
167        }
168    
169        /**
170         * @param producer the producer to set
171         */
172        public void setProducer( String producer ) throws LdapURLEncodingException
173        {
174            this.producer = new LdapURL( producer );
175            
176            // Update the other fields
177            baseDN = this.producer.getDn();
178            useSSL = "ldaps".equalsIgnoreCase( this.producer.getScheme() );
179            host = this.producer.getHost();
180            
181            if ( StringTools.isEmpty( host ) )
182            {
183                host = DEFAULT_HOST;
184            }
185            
186            port = this.producer.getPort();
187            
188            if ( port == -1 )
189            {
190                if ( useSSL )
191                {
192                    port = DEFAULT_SSL_PORT;
193                }
194                else
195                {
196                    port = DEFAULT_PORT;
197                }
198            }
199        }
200    
201        
202        /**
203         * @return the producer
204         */
205        public LdapURL getProducer()
206        {
207            return producer;
208        }
209    
210    
211        /**
212         * @return true if the connection with the producer is done using SSL
213         */
214        public boolean isUseSSL()
215        {
216            return useSSL;
217        }
218    
219    
220        /**
221         * @return the producer's host
222         */
223        public String getHost()
224        {
225            return host;
226        }
227    
228    
229        /**
230         * @return the producer's port
231         */
232        public int getPort()
233        {
234            return port;
235        }
236    }