View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.DecoderException;
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
32  import org.apache.directory.api.ldap.codec.api.LdapApiService;
33  import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulActionConstants;
34  import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequest;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * A Decorator for GracefulShutdownRequests.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class GracefulShutdownRequestDecorator extends ExtendedRequestDecorator<GracefulShutdownRequest>
45      implements GracefulShutdownRequest
46  {
47      private static final Logger LOG = LoggerFactory.getLogger( GracefulShutdownRequestDecorator.class );
48  
49      /** Length of the sequence */
50      private int gracefulSequenceLength;
51  
52      private GracefulShutdownRequest gracefulShutdownRequest;
53  
54      /**
55       * Creates a new instance of GracefulShutdownRequestDecorator.
56       *
57       * @param codec
58       * @param decoratedMessage
59       */
60      public GracefulShutdownRequestDecorator( LdapApiService codec, GracefulShutdownRequest decoratedMessage )
61      {
62          super( codec, decoratedMessage );
63          gracefulShutdownRequest = decoratedMessage;
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void setRequestValue( byte[] requestValue )
71      {
72          GracefulShutdownDecoder decoder = new GracefulShutdownDecoder();
73  
74          try
75          {
76              gracefulShutdownRequest = decoder.decode( requestValue );
77  
78              if ( requestValue != null )
79              {
80                  this.requestValue = new byte[requestValue.length];
81                  System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
82              }
83              else
84              {
85                  this.requestValue = null;
86              }
87          }
88          catch ( DecoderException e )
89          {
90              LOG.error( I18n.err( I18n.ERR_04165 ), e );
91              throw new RuntimeException( e );
92          }
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public byte[] getRequestValue()
100     {
101         if ( requestValue == null )
102         {
103             try
104             {
105                 requestValue = encodeInternal().array();
106             }
107             catch ( EncoderException e )
108             {
109                 LOG.error( I18n.err( I18n.ERR_04164 ), e );
110                 throw new RuntimeException( e );
111             }
112         }
113 
114         return requestValue;
115     }
116 
117 
118     /**
119      * {@inheritDoc}
120      */
121     public int getDelay()
122     {
123         return getDecorated().getDelay();
124     }
125 
126 
127     /**
128      * {@inheritDoc}
129      */
130     public void setDelay( int delay )
131     {
132         getDecorated().setDelay( delay );
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public int getTimeOffline()
140     {
141         return getDecorated().getTimeOffline();
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     public void setTimeOffline( int timeOffline )
149     {
150         getDecorated().setTimeOffline( timeOffline );
151     }
152 
153 
154     /**
155      * Compute the GracefulShutdown length 
156      * 
157      * <pre>
158      * 0x30 L1 
159      *   | 
160      *   +--> [0x02 0x0(1-4) [0..720] ] 
161      *   +--> [0x80 0x0(1-3) [0..86400] ] 
162      * </pre>  
163      * L1 will always be &lt 11.
164      */
165     /* no qualifier */ int computeLengthInternal()
166     {
167         int gracefulLength = 1 + 1;
168         gracefulSequenceLength = 0;
169 
170         if ( gracefulShutdownRequest.getTimeOffline() != 0 )
171         {
172             gracefulSequenceLength += 1 + 1 + BerValue.getNbBytes( gracefulShutdownRequest.getTimeOffline() );
173         }
174 
175         if ( gracefulShutdownRequest.getDelay() != 0 )
176         {
177             gracefulSequenceLength += 1 + 1 + BerValue.getNbBytes( gracefulShutdownRequest.getDelay() );
178         }
179 
180         return gracefulLength + gracefulSequenceLength;
181     }
182 
183 
184     /**
185      * Encodes the gracefulShutdown extended operation.
186      * 
187      * @return A ByteBuffer that contains the encoded PDU
188      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
189      */
190     /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
191     {
192         // Allocate the bytes buffer.
193         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
194 
195         bb.put( UniversalTag.SEQUENCE.getValue() );
196         bb.put( TLV.getBytes( gracefulSequenceLength ) );
197 
198         if ( gracefulShutdownRequest.getTimeOffline() != 0 )
199         {
200             BerValue.encode( bb, gracefulShutdownRequest.getTimeOffline() );
201         }
202 
203         if ( gracefulShutdownRequest.getDelay() != 0 )
204         {
205             bb.put( ( byte ) GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG );
206             bb.put( ( byte ) BerValue.getNbBytes( gracefulShutdownRequest.getDelay() ) );
207             bb.put( BerValue.getBytes( gracefulShutdownRequest.getDelay() ) );
208         }
209         return bb;
210     }
211 
212 
213     /**
214      * Return a string representation of the graceful shutdown
215      */
216     public String toString()
217     {
218         StringBuffer sb = new StringBuffer();
219 
220         sb.append( "Graceful Shutdown extended operation" );
221         sb.append( "    TimeOffline : " ).append( gracefulShutdownRequest.getTimeOffline() ).append( '\n' );
222         sb.append( "    Delay : " ).append( gracefulShutdownRequest.getDelay() ).append( '\n' );
223 
224         return sb.toString();
225     }
226 }