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.Asn1Object;
026 import org.apache.directory.shared.asn1.ber.tlv.TLV;
027 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
028 import org.apache.directory.shared.asn1.ber.tlv.Value;
029 import org.apache.directory.shared.asn1.codec.EncoderException;
030 import org.apache.directory.shared.ldap.codec.extended.operations.GracefulAction;
031 import org.apache.directory.shared.ldap.codec.extended.operations.GracefulActionConstants;
032
033
034 /**
035 * An extended operation to proceed a graceful shutdown
036 *
037 * <pre>
038 * GracefulShutdown ::= SEQUENCE
039 * {
040 * timeOffline INTEGER (0..720) DEFAULT 0,
041 * delay [0] INTEGER (0..86400) DEFAULT 0,
042 * }
043 * </pre>
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 * @version $Rev: 910150 $
047 */
048 public class GracefulShutdown extends GracefulAction
049 {
050 /** Length of the sequence */
051 private int gracefulSequenceLength;
052
053 /**
054 * @see Asn1Object#Asn1Object
055 */
056 public GracefulShutdown()
057 {
058 super();
059 }
060
061 /**
062 * Compute the GracefulShutdown length
063 * 0x30 L1
064 * |
065 * +--> [0x02 0x0(1-4) [0..720] ]
066 * +--> [0x80 0x0(1-3) [0..86400] ]
067 *
068 * L1 will always be < 11.
069 */
070 public int computeLength()
071 {
072 int gracefulLength = 1 + 1;
073 gracefulSequenceLength = 0;
074
075 if ( timeOffline != 0 )
076 {
077 gracefulSequenceLength += 1 + 1 + Value.getNbBytes( timeOffline );
078 }
079
080 if ( delay != 0 )
081 {
082 gracefulSequenceLength += 1 + 1 + Value.getNbBytes( delay );
083 }
084
085 return gracefulLength + gracefulSequenceLength;
086 }
087
088
089 /**
090 * Encodes the gracefulShutdown extended operation.
091 *
092 * @return A ByteBuffer that contains the encoded PDU
093 * @throws EncoderException If anything goes wrong.
094 */
095 public ByteBuffer encode() 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 }