1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
26 import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
29 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
30 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
33 import org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulActionConstants;
34 import org.apache.directory.api.ldap.extras.extended.gracefulShutdown.GracefulShutdownRequestImpl;
35 import org.apache.directory.api.util.Strings;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public final class GracefulShutdownGrammar extends AbstractGrammar<GracefulShutdownContainer>
55 {
56
57 static final Logger LOG = LoggerFactory.getLogger( GracefulShutdownGrammar.class );
58
59
60 static final boolean IS_DEBUG = LOG.isDebugEnabled();
61
62
63 private static GracefulShutdownGrammar instance = new GracefulShutdownGrammar();
64
65
66
67
68
69 @SuppressWarnings("unchecked")
70 private GracefulShutdownGrammar()
71 {
72 setName( GracefulShutdownGrammar.class.getName() );
73
74
75 super.transitions = new GrammarTransition[GracefulShutdownStatesEnum.LAST_GRACEFUL_SHUTDOWN_STATE.ordinal()][256];
76
77
78
79
80
81
82
83
84
85 super.transitions[GracefulShutdownStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
86 new GrammarTransition<GracefulShutdownContainer>( GracefulShutdownStatesEnum.START_STATE,
87 GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE,
88 UniversalTag.SEQUENCE.getValue(),
89 new GrammarAction<GracefulShutdownContainer>( "Init GracefulShutdown" )
90 {
91 public void action( GracefulShutdownContainer container )
92 {
93 GracefulShutdownRequestDecorator gracefulShutdownRequest = new GracefulShutdownRequestDecorator(
94 LdapApiServiceFactory.getSingleton(),
95 new GracefulShutdownRequestImpl() );
96 container.setGracefulShutdownRequest( gracefulShutdownRequest );
97 container.setGrammarEndAllowed( true );
98 }
99 } );
100
101
102
103
104
105
106
107
108
109
110
111 super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER
112 .getValue()] =
113 new GrammarTransition<GracefulShutdownContainer>(
114 GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE,
115 GracefulShutdownStatesEnum.TIME_OFFLINE_STATE,
116 UniversalTag.INTEGER.getValue(),
117 new GrammarAction<GracefulShutdownContainer>( "Set Graceful Shutdown time offline" )
118 {
119 public void action( GracefulShutdownContainer container ) throws DecoderException
120 {
121 BerValue value = container.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 container.getGracefulShutdownRequest().setTimeOffline( timeOffline );
133 container.setGrammarEndAllowed( true );
134 }
135 catch ( IntegerDecoderException e )
136 {
137 String msg = I18n.err( I18n.ERR_04037, Strings.dumpBytes( value.getData() ) );
138 LOG.error( msg );
139 throw new DecoderException( msg );
140 }
141 }
142 } );
143
144
145
146
147
148
149
150
151
152
153
154 super.transitions[GracefulShutdownStatesEnum.TIME_OFFLINE_STATE.ordinal()][GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG] =
155 new GrammarTransition<GracefulShutdownContainer>( GracefulShutdownStatesEnum.TIME_OFFLINE_STATE,
156 GracefulShutdownStatesEnum.DELAY_STATE,
157 GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG,
158
159 new GrammarAction<GracefulShutdownContainer>( "Set Graceful Shutdown Delay" )
160 {
161 public void action( GracefulShutdownContainer container ) throws DecoderException
162 {
163 BerValue value = container.getCurrentTLV().getValue();
164
165 try
166 {
167 int delay = IntegerDecoder.parse( value, 0, 86400 );
168
169 if ( IS_DEBUG )
170 {
171 LOG.debug( "Delay = " + delay );
172 }
173
174 container.getGracefulShutdownRequest().setDelay( delay );
175 container.setGrammarEndAllowed( true );
176 }
177 catch ( IntegerDecoderException e )
178 {
179 String msg = I18n.err( I18n.ERR_04036, Strings.dumpBytes( value.getData() ) );
180 LOG.error( msg );
181 throw new DecoderException( msg );
182 }
183 }
184 } );
185
186
187
188
189
190
191
192
193
194
195
196 super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE.ordinal()][GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG] =
197 new GrammarTransition<GracefulShutdownContainer>(
198 GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_STATE,
199 GracefulShutdownStatesEnum.DELAY_STATE,
200 GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG,
201
202 new GrammarAction<GracefulShutdownContainer>( "Set Graceful Shutdown Delay" )
203 {
204 public void action( GracefulShutdownContainer container ) throws DecoderException
205 {
206 GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container;
207 BerValue value = gracefulShutdownContainer.getCurrentTLV().getValue();
208
209 try
210 {
211 int delay = IntegerDecoder.parse( value, 0, 86400 );
212
213 if ( IS_DEBUG )
214 {
215 LOG.debug( "Delay = " + delay );
216 }
217
218 gracefulShutdownContainer.getGracefulShutdownRequest().setDelay( delay );
219 gracefulShutdownContainer.setGrammarEndAllowed( true );
220 }
221 catch ( IntegerDecoderException e )
222 {
223 String msg = I18n.err( I18n.ERR_04036, Strings.dumpBytes( value.getData() ) );
224 LOG.error( msg );
225 throw new DecoderException( msg );
226 }
227 }
228 } );
229 }
230
231
232
233
234
235
236
237 public static GracefulShutdownGrammar getInstance()
238 {
239 return instance;
240 }
241 }