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.ads_impl.gracefulShutdown;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.ber.Asn1Decoder;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.util.Asn1Buffer;
029import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
030import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulActionConstants;
033import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequest;
034import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequestImpl;
035import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownResponse;
036import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownResponseImpl;
037import org.apache.directory.api.ldap.model.message.ExtendedRequest;
038
039
040/**
041 * An {@link ExtendedOperationFactory} for creating cancel extended request response 
042 * pairs.
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class GracefulShutdownFactory extends AbstractExtendedOperationFactory
047{
048    /**
049     * Creates a new instance of GracefulShutdownFactory.
050     *
051     * @param codec The codec for this factory.
052     */
053    public GracefulShutdownFactory( LdapApiService codec )
054    {
055        super( codec, GracefulShutdownRequest.EXTENSION_OID );
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public String getOid()
064    {
065        return GracefulShutdownRequest.EXTENSION_OID;
066    }
067
068
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public GracefulShutdownRequest newRequest()
075    {
076        GracefulShutdownRequest gracefulShutdownRequest = new GracefulShutdownRequestImpl();
077
078        return gracefulShutdownRequest;
079    }
080
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public GracefulShutdownRequest newRequest( byte[] encodedValue ) throws DecoderException
087    {
088        GracefulShutdownRequest gracefulShutdownRequest = new GracefulShutdownRequestImpl();
089        decodeValue( gracefulShutdownRequest, encodedValue );
090
091        return gracefulShutdownRequest;
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public GracefulShutdownResponse newResponse()
100    {
101        return new GracefulShutdownResponseImpl();
102    }
103
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
110    {
111        ByteBuffer bb = ByteBuffer.wrap( requestValue );
112        GracefulShutdownRequestContainer container = new GracefulShutdownRequestContainer();
113        container.setGracefulShutdownRequest( ( GracefulShutdownRequest ) extendedRequest ); 
114        Asn1Decoder.decode( bb, container );
115    }
116
117
118    /**
119     * {@inheritDoc}
120     */
121    @Override
122    public void encodeValue( Asn1Buffer buffer, ExtendedRequest extendedRequest )
123    {
124        int start  = buffer.getPos();
125        GracefulShutdownRequest gracefulShutdownRequest = ( GracefulShutdownRequest ) extendedRequest;
126        
127        // The delay, if any
128        if ( gracefulShutdownRequest.getDelay() != 0 )
129        {
130            BerValue.encodeInteger( buffer, 
131                ( byte ) GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG, 
132                gracefulShutdownRequest.getDelay() );
133        }
134        
135        // The timeOffline
136        if ( gracefulShutdownRequest.getTimeOffline() != 0 )
137        {
138            BerValue.encodeInteger( buffer, gracefulShutdownRequest.getTimeOffline() );
139        }
140        
141        // The sequence
142        BerValue.encodeSequence( buffer, start );
143    }
144}