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.EncoderException; 027import org.apache.directory.api.asn1.ber.tlv.BerValue; 028import org.apache.directory.api.asn1.ber.tlv.TLV; 029import org.apache.directory.api.asn1.ber.tlv.UniversalTag; 030import org.apache.directory.api.i18n.I18n; 031import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator; 032import org.apache.directory.api.ldap.codec.api.LdapApiService; 033import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulActionConstants; 034import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequest; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * A Decorator for GracefulShutdownRequests. 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044public class GracefulShutdownRequestDecorator extends ExtendedRequestDecorator<GracefulShutdownRequest> 045 implements GracefulShutdownRequest 046{ 047 private static final Logger LOG = LoggerFactory.getLogger( GracefulShutdownRequestDecorator.class ); 048 049 /** Length of the sequence */ 050 private int gracefulSequenceLength; 051 052 private GracefulShutdownRequest gracefulShutdownRequest; 053 054 /** 055 * Creates a new instance of GracefulShutdownRequestDecorator. 056 * 057 * @param codec 058 * @param decoratedMessage 059 */ 060 public GracefulShutdownRequestDecorator( LdapApiService codec, GracefulShutdownRequest decoratedMessage ) 061 { 062 super( codec, decoratedMessage ); 063 gracefulShutdownRequest = decoratedMessage; 064 } 065 066 067 /** 068 * {@inheritDoc} 069 */ 070 public void setRequestValue( byte[] requestValue ) 071 { 072 GracefulShutdownDecoder decoder = new GracefulShutdownDecoder(); 073 074 try 075 { 076 gracefulShutdownRequest = decoder.decode( requestValue ); 077 078 if ( requestValue != null ) 079 { 080 this.requestValue = new byte[requestValue.length]; 081 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length ); 082 } 083 else 084 { 085 this.requestValue = null; 086 } 087 } 088 catch ( DecoderException e ) 089 { 090 LOG.error( I18n.err( I18n.ERR_04165 ), e ); 091 throw new RuntimeException( e ); 092 } 093 } 094 095 096 /** 097 * {@inheritDoc} 098 */ 099 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 < 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}