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.gracefulShutdown;
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.ResultCodeEnum;
026
027
028/**
029 * The response sent back from the server when a {@link GracefulShutdownRequestImpl}
030 * extended operation is sent. Delivery of this response may block until all
031 * connected clients are sent a GracefulDisconnect unsolicited notification.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class GracefulShutdownResponseImpl extends ExtendedResponseImpl implements GracefulShutdownResponse
036{
037    /**
038     * Instantiates a new graceful shutdown response.
039     *
040     * @param messageId the message id
041     * @param rcode the result code
042     */
043    public GracefulShutdownResponseImpl( int messageId, ResultCodeEnum rcode )
044    {
045        super( messageId, EXTENSION_OID );
046
047        switch ( rcode )
048        {
049            case SUCCESS:
050            case OPERATIONS_ERROR:
051            case INSUFFICIENT_ACCESS_RIGHTS:
052                break;
053
054            default:
055                throw new IllegalArgumentException( I18n.err( I18n.ERR_04166, ResultCodeEnum.SUCCESS,
056                    ResultCodeEnum.OPERATIONS_ERROR, ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
057        }
058
059        super.getLdapResult().setMatchedDn( null );
060        super.getLdapResult().setResultCode( rcode );
061    }
062
063
064    /**
065     * Instantiates a new graceful shutdown response.
066     *
067     * @param messageId the message id
068     */
069    public GracefulShutdownResponseImpl( int messageId )
070    {
071        super( messageId, EXTENSION_OID );
072        super.getLdapResult().setMatchedDn( null );
073        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
074    }
075
076
077    /**
078     * Instantiates a new graceful shutdown response.
079     */
080    public GracefulShutdownResponseImpl()
081    {
082        super( EXTENSION_OID );
083        super.getLdapResult().setMatchedDn( null );
084        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
085    }
086
087
088    // ------------------------------------------------------------------------
089    // ExtendedResponse Interface Method Implementations
090    // ------------------------------------------------------------------------
091
092    /**
093     * Gets the OID uniquely identifying this extended response (a.k.a. its
094     * name).
095     * 
096     * @return the OID of the extended response type.
097     */
098    @Override
099    public String getResponseName()
100    {
101        return EXTENSION_OID;
102    }
103
104
105    /**
106     * Sets the OID uniquely identifying this extended response (a.k.a. its
107     * name).
108     * 
109     * @param oid
110     *            the OID of the extended response type.
111     */
112    @Override
113    public void setResponseName( String oid )
114    {
115        throw new UnsupportedOperationException( I18n.err( I18n.ERR_04168, EXTENSION_OID ) );
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public int hashCode()
124    {
125        int hash = 37;
126        // Seems simple but look at the equals() method ...
127        hash = hash * 17 + getClass().getName().hashCode();
128
129        return hash;
130    }
131
132
133    /**
134     * {@inheritDoc}
135     */
136    @Override
137    public boolean equals( Object obj )
138    {
139        if ( obj == this )
140        {
141            return true;
142        }
143
144        return obj instanceof GracefulShutdownResponseImpl;
145    }
146}