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 org.apache.directory.shared.asn1.ber.IAsn1Container; 024 import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar; 025 import org.apache.directory.shared.asn1.ber.grammar.GrammarAction; 026 import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition; 027 import org.apache.directory.shared.asn1.ber.grammar.IGrammar; 028 import org.apache.directory.shared.asn1.ber.grammar.IStates; 029 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag; 030 import org.apache.directory.shared.asn1.ber.tlv.Value; 031 import org.apache.directory.shared.asn1.codec.DecoderException; 032 import org.apache.directory.shared.asn1.util.IntegerDecoder; 033 import org.apache.directory.shared.asn1.util.IntegerDecoderException; 034 import org.apache.directory.shared.i18n.I18n; 035 import org.apache.directory.shared.ldap.codec.extended.operations.GracefulActionConstants; 036 import org.apache.directory.shared.ldap.util.StringTools; 037 import org.slf4j.Logger; 038 import org.slf4j.LoggerFactory; 039 040 041 /** 042 * This class implements the Graceful shutdown. All the actions are declared in 043 * this class. As it is a singleton, these declaration are only done once. The 044 * grammar is : 045 * 046 * <pre> 047 * GracefulShutdwon ::= SEQUENCE { 048 * timeOffline INTEGER (0..720) DEFAULT 0, 049 * delay [0] INTEGER (0..86400) DEFAULT 0 050 * } 051 * </pre> 052 * 053 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 054 * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 055 */ 056 public class GracefulShutdownGrammar extends AbstractGrammar 057 { 058 /** The logger */ 059 static final Logger log = LoggerFactory.getLogger( GracefulShutdownGrammar.class ); 060 061 /** Speedup for logs */ 062 static final boolean IS_DEBUG = log.isDebugEnabled(); 063 064 /** The instance of grammar. GracefulShutdownGrammar is a singleton */ 065 private static IGrammar instance = new GracefulShutdownGrammar(); 066 067 068 /** 069 * Creates a new GracefulShutdownGrammar object. 070 */ 071 private GracefulShutdownGrammar() 072 { 073 name = GracefulShutdownGrammar.class.getName(); 074 statesEnum = GracefulShutdownStatesEnum.getInstance(); 075 076 // Create the transitions table 077 super.transitions = new GrammarTransition[GracefulShutdownStatesEnum.LAST_GRACEFUL_SHUTDOWN_STATE][256]; 078 079 /** 080 * Transition from init state to graceful shutdown 081 * 082 * GracefulShutdown ::= SEQUENCE { 083 * ... 084 * 085 * Creates the GracefulShutdown object 086 */ 087 super.transitions[IStates.INIT_GRAMMAR_STATE][UniversalTag.SEQUENCE_TAG] = 088 new GrammarTransition( IStates.INIT_GRAMMAR_STATE, 089 GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE, 090 UniversalTag.SEQUENCE_TAG, 091 new GrammarAction( "Init GracefulShutdown" ) 092 { 093 public void action( IAsn1Container container ) 094 { 095 GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container; 096 GracefulShutdown gracefulShutdown = new GracefulShutdown(); 097 gracefulShutdownContainer.setGracefulShutdown( gracefulShutdown ); 098 gracefulShutdownContainer.grammarEndAllowed( true ); 099 } 100 } ); 101 102 /** 103 * Transition from graceful shutdown to time offline 104 * 105 * GracefulShutdown ::= SEQUENCE { 106 * timeOffline INTEGER (0..720) DEFAULT 0, 107 * ... 108 * 109 * Set the time offline value into the GracefulShutdown 110 * object. 111 */ 112 super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE][UniversalTag.INTEGER_TAG] = 113 new GrammarTransition( GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE, 114 GracefulShutdownStatesEnum.TIME_OFFLINE_STATE, 115 UniversalTag.INTEGER_TAG, 116 new GrammarAction( "Set Graceful Shutdown time offline" ) 117 { 118 public void action( IAsn1Container container ) throws DecoderException 119 { 120 GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container; 121 Value value = gracefulShutdownContainer.getCurrentTLV().getValue(); 122 123 try 124 { 125 int timeOffline = IntegerDecoder.parse( value, 0, 720 ); 126 127 if ( IS_DEBUG ) 128 { 129 log.debug( "Time Offline = " + timeOffline ); 130 } 131 132 gracefulShutdownContainer.getGracefulShutdown().setTimeOffline( timeOffline ); 133 gracefulShutdownContainer.grammarEndAllowed( true ); 134 } 135 catch ( IntegerDecoderException e ) 136 { 137 String msg = I18n.err( I18n.ERR_04037, StringTools.dumpBytes( value.getData() ) ); 138 log.error( msg ); 139 throw new DecoderException( msg ); 140 } 141 } 142 } ); 143 144 /** 145 * Transition from time offline to delay 146 * 147 * GracefulShutdown ::= SEQUENCE { 148 * ... 149 * delay [0] INTEGER (0..86400) DEFAULT 0 } 150 * 151 * Set the delay value into the GracefulShutdown 152 * object. 153 */ 154 super.transitions[GracefulShutdownStatesEnum.TIME_OFFLINE_STATE][GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG] = 155 new GrammarTransition( GracefulShutdownStatesEnum.TIME_OFFLINE_STATE, 156 GracefulShutdownStatesEnum.DELAY_STATE, 157 GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG, 158 159 new GrammarAction( "Set Graceful Shutdown Delay" ) 160 { 161 public void action( IAsn1Container container ) throws DecoderException 162 { 163 GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container; 164 Value value = gracefulShutdownContainer.getCurrentTLV().getValue(); 165 166 try 167 { 168 int delay = IntegerDecoder.parse( value, 0, 86400 ); 169 170 if ( IS_DEBUG ) 171 { 172 log.debug( "Delay = " + delay ); 173 } 174 175 gracefulShutdownContainer.getGracefulShutdown().setDelay( delay ); 176 gracefulShutdownContainer.grammarEndAllowed( true ); 177 } 178 catch ( IntegerDecoderException e ) 179 { 180 String msg = I18n.err( I18n.ERR_04036, StringTools.dumpBytes( value.getData() ) ); 181 log.error( msg ); 182 throw new DecoderException( msg ); 183 } 184 } 185 } ); 186 187 /** 188 * Transition from graceful shutdown to delay 189 * 190 * GracefulShutdown ::= SEQUENCE { 191 * ... 192 * delay [0] INTEGER (0..86400) DEFAULT 0 } 193 * 194 * Set the delay value into the GracefulShutdown 195 * object. 196 */ 197 super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE] 198 [GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG] = 199 new GrammarTransition( GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE, 200 GracefulShutdownStatesEnum.DELAY_STATE, 201 GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG, 202 203 new GrammarAction( "Set Graceful Shutdown Delay" ) 204 { 205 public void action( IAsn1Container container ) throws DecoderException 206 { 207 GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container; 208 Value value = gracefulShutdownContainer.getCurrentTLV().getValue(); 209 210 try 211 { 212 int delay = IntegerDecoder.parse( value, 0, 86400 ); 213 214 if ( IS_DEBUG ) 215 { 216 log.debug( "Delay = " + delay ); 217 } 218 219 gracefulShutdownContainer.getGracefulShutdown().setDelay( delay ); 220 gracefulShutdownContainer.grammarEndAllowed( true ); 221 } 222 catch ( IntegerDecoderException e ) 223 { 224 String msg = I18n.err( I18n.ERR_04036, StringTools.dumpBytes( value.getData() ) ); 225 log.error( msg ); 226 throw new DecoderException( msg ); 227 } 228 } 229 } ); 230 } 231 232 233 /** 234 * This class is a singleton. 235 * 236 * @return An instance on this grammar 237 */ 238 public static IGrammar getInstance() 239 { 240 return instance; 241 } 242 }