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.extras.extended.gracefulDisconnect;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
025import org.apache.directory.api.ldap.model.message.Referral;
026import org.apache.directory.api.ldap.model.message.ReferralImpl;
027import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
028
029
030/**
031 * An unsolicited notification, extended response, intended for notifying
032 * clients of up coming disconnection due to intended service windows. Unlike the
033 * {@link org.apache.directory.api.ldap.model.message.extended.NoticeOfDisconnect} this response contains additional information about
034 * the amount of time the server will be offline and exactly when it intends to
035 * shutdown.
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class GracefulDisconnectResponseImpl extends ExtendedResponseImpl implements GracefulDisconnectResponse
040{
041    /** Offline time after disconnection */
042    private int timeOffline;
043
044    /** Delay before disconnection */
045    private int delay;
046
047    /** String based LDAP URL that may be followed for replicated namingContexts */
048    private Referral replicatedContexts = new ReferralImpl();
049
050
051    /**
052     * Instantiates a new graceful disconnect.
053     */
054    public GracefulDisconnectResponseImpl()
055    {
056        super( 0, EXTENSION_OID );
057    }
058
059
060    /**
061     * Instantiates a new graceful disconnect.
062     *
063     * @param timeOffline the offline time after disconnect, in minutes
064     * @param delay the delay before disconnect, in seconds
065     */
066    public GracefulDisconnectResponseImpl( int timeOffline, int delay )
067    {
068        super( 0, EXTENSION_OID );
069        responseName = EXTENSION_OID;
070        this.timeOffline = timeOffline;
071        this.delay = delay;
072
073        StringBuffer buf = new StringBuffer();
074        buf.append( "The server will disconnect and will be unavailable for " ).append( timeOffline );
075        buf.append( " minutes in " ).append( delay ).append( " seconds." );
076
077        ldapResult.setDiagnosticMessage( buf.toString() );
078        ldapResult.setMatchedDn( null );
079        ldapResult.setResultCode( ResultCodeEnum.UNAVAILABLE );
080    }
081
082
083    /**
084     * Gets the OID uniquely identifying this extended response (a.k.a. its
085     * name).
086     * 
087     * @return the OID of the extended response type.
088     */
089    public String getResponseName()
090    {
091        return EXTENSION_OID;
092    }
093
094
095    /**
096     * Sets the OID uniquely identifying this extended response (a.k.a. its
097     * name).
098     * 
099     * @param oid the OID of the extended response type.
100     */
101    public void setResponseName( String oid )
102    {
103        throw new UnsupportedOperationException( I18n.err( I18n.ERR_04168, EXTENSION_OID ) );
104    }
105
106
107    // -----------------------------------------------------------------------
108    // Parameters of the Extended Response Value
109    // -----------------------------------------------------------------------
110    /**
111     * {@inheritDoc}
112     */
113    public int getDelay()
114    {
115        return delay;
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    public void setDelay( int delay )
123    {
124        this.delay = delay;
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    public int getTimeOffline()
132    {
133        return timeOffline;
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public void setTimeOffline( int timeOffline )
141    {
142        this.timeOffline = timeOffline;
143    }
144
145
146    public Referral getReplicatedContexts()
147    {
148        return replicatedContexts;
149    }
150    
151
152
153    /**
154     * {@inheritDoc}
155     */
156    public void addReplicatedContexts( String replicatedContext )
157    {
158        replicatedContexts.addLdapUrl( replicatedContext );
159    }
160}