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.shared.ldap.extras.extended.ads_impl.gracefulShutdown;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.asn1.EncoderException;
025import org.apache.directory.shared.i18n.I18n;
026import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
027import org.apache.directory.shared.ldap.codec.api.LdapApiService;
028import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownRequest;
029import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownResponse;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A Decorator for GracefulShutdownRequests.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class GracefulShutdownRequestDecorator extends ExtendedRequestDecorator<GracefulShutdownRequest,GracefulShutdownResponse> 
040    implements GracefulShutdownRequest
041{
042    private static final Logger LOG = LoggerFactory.getLogger( GracefulShutdownRequestDecorator.class );
043    
044
045    /**
046     * Creates a new instance of GracefulShutdownRequestDecorator.
047     *
048     * @param codec
049     * @param decoratedMessage
050     */
051    public GracefulShutdownRequestDecorator( LdapApiService codec, GracefulShutdownRequest decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054    }
055    
056    
057    /**
058     * {@inheritDoc}
059     */
060    public void setRequestValue( byte[] requestValue )
061    {
062        GracefulShutdownDecoder decoder = new GracefulShutdownDecoder();
063
064        try
065        {
066            GracefulShutdown gs = (GracefulShutdown) decoder.decode( requestValue );
067
068            if ( requestValue != null )
069            {
070                this.requestValue = new byte[requestValue.length];
071                System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
072            }
073            else
074            {
075                this.requestValue = null;
076            }
077
078            setTimeOffline( gs.getTimeOffline() );
079            setDelay( gs.getDelay() );
080        }
081        catch ( DecoderException e )
082        {
083            LOG.error( I18n.err( I18n.ERR_04165 ), e );
084            throw new RuntimeException( e );
085        }
086    }
087
088
089    /**
090     * {@inheritDoc}
091     */
092    public byte[] getRequestValue()
093    {
094        if ( requestValue == null )
095        {
096            try
097            {
098                GracefulShutdown gs = new GracefulShutdown();
099                gs.setDelay( getDecorated().getDelay() );
100                gs.setTimeOffline( getDecorated().getTimeOffline() );
101                requestValue = gs.encode().array();
102            }
103            catch ( EncoderException e )
104            {
105                LOG.error( I18n.err( I18n.ERR_04164 ), e );
106                throw new RuntimeException( e );
107            }
108        }
109
110        return requestValue;
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    public int getDelay()
118    {
119        return getDecorated().getDelay();
120    }
121
122
123    /**
124     * {@inheritDoc}
125     */
126    public void setDelay( int delay )
127    {
128        getDecorated().setDelay( delay );
129    }
130
131
132    /**
133     * {@inheritDoc}
134     */
135    public int getTimeOffline()
136    {
137        return getDecorated().getTimeOffline();
138    }
139
140
141    /**
142     * {@inheritDoc}
143     */
144    public void setTimeOffline( int timeOffline )
145    {
146        getDecorated().setTimeOffline( timeOffline );
147    }
148}