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 */
020 package org.apache.directory.shared.ldap.codec.extended.operations.gracefulShutdown;
021
022
023 import java.nio.ByteBuffer;
024
025 import org.apache.directory.shared.asn1.ber.tlv.TLV;
026 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
027 import org.apache.directory.shared.asn1.ber.tlv.Value;
028 import org.apache.directory.shared.asn1.codec.EncoderException;
029 import org.apache.directory.shared.ldap.codec.extended.operations.GracefulAction;
030 import org.apache.directory.shared.ldap.codec.extended.operations.GracefulActionConstants;
031
032
033 /**
034 * An extended operation to proceed a graceful shutdown
035 *
036 * <pre>
037 * GracefulShutdown ::= SEQUENCE
038 * {
039 * timeOffline INTEGER (0..720) DEFAULT 0,
040 * delay [0] INTEGER (0..86400) DEFAULT 0,
041 * }
042 * </pre>
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 * @version $Rev: 764131 $
046 */
047 public class GracefulShutdown extends GracefulAction
048 {
049 /** Length of the sequence */
050 private int gracefulSequenceLength;
051
052 /**
053 * @see Asn1Object#Asn1Object
054 */
055 public GracefulShutdown()
056 {
057 super();
058 }
059
060 /**
061 * Compute the GracefulShutdown length
062 * 0x30 L1
063 * |
064 * +--> [0x02 0x0(1-4) [0..720] ]
065 * +--> [0x80 0x0(1-3) [0..86400] ]
066 *
067 * L1 will always be < 11.
068 */
069 public int computeLength()
070 {
071 int gracefulLength = 1 + 1;
072 gracefulSequenceLength = 0;
073
074 if ( timeOffline != 0 )
075 {
076 gracefulSequenceLength += 1 + 1 + Value.getNbBytes( timeOffline );
077 }
078
079 if ( delay != 0 )
080 {
081 gracefulSequenceLength += 1 + 1 + Value.getNbBytes( delay );
082 }
083
084 return gracefulLength + gracefulSequenceLength;
085 }
086
087
088 /**
089 * Encodes the gracefulShutdown extended operation.
090 *
091 * @param buffer The encoded sink
092 * @return A ByteBuffer that contains the encoded PDU
093 * @throws EncoderException If anything goes wrong.
094 */
095 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
096 {
097 // Allocate the bytes buffer.
098 ByteBuffer bb = ByteBuffer.allocate( computeLength() );
099
100 bb.put( UniversalTag.SEQUENCE_TAG );
101 bb.put( TLV.getBytes( gracefulSequenceLength ) );
102
103 if ( timeOffline != 0 )
104 {
105 Value.encode( bb, timeOffline );
106 }
107
108 if ( delay != 0 )
109 {
110 bb.put( ( byte ) GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG );
111 bb.put( ( byte ) Value.getNbBytes( delay ) );
112 bb.put( Value.getBytes( delay ) );
113 }
114 return bb;
115 }
116
117
118 /**
119 * Return a string representation of the graceful shutdown
120 */
121 public String toString()
122 {
123 StringBuffer sb = new StringBuffer();
124
125 sb.append( "Graceful Shutdown extended operation" );
126 sb.append( " TimeOffline : " ).append( timeOffline ).append( '\n' );
127 sb.append( " Delay : " ).append( delay ).append( '\n' );
128
129 return sb.toString();
130 }
131 }