You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2006/01/21 15:56:38 UTC
svn commit: r371070 - in /directory/trunks/common/ldap/src:
main/java/org/apache/ldap/common/codec/extended/operations/
test/java/org/apache/ldap/common/codec/extended/operations/
Author: elecharny
Date: Sat Jan 21 06:56:30 2006
New Revision: 371070
URL: http://svn.apache.org/viewcvs?rev=371070&view=rev
Log:
Added the GracefulShutdown codec and test
Added:
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdown.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownConstants.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownContainer.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownDecoder.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownGrammar.java
directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownStatesEnum.java
directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownTest.java
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdown.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdown.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdown.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdown.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.Asn1Object;
+import org.apache.asn1.ber.tlv.Length;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
+import org.apache.asn1.codec.EncoderException;
+
+
+/**
+ * An extended operation to proceed a gracefull shutdown
+ *
+ * <pre>
+ * GracefullShutdown ::= SEQUENCE
+ * {
+ * timeOffline INTEGER DEFAULT 0,
+ * delay [0] INTEGER DEFAULT 0
+ * }
+ * </pre>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class GracefulShutdown extends Asn1Object
+{
+ private int timeOffline;
+ private int delay;
+ private transient int gracefullSequenceLength;
+
+ public GracefulShutdown( int timeOffline, int delay )
+ {
+ this.timeOffline = timeOffline;
+ this.delay = delay;
+ }
+
+ public GracefulShutdown()
+ {
+ this.timeOffline = 0;
+ this.delay = 0;
+ }
+
+ public int getDelay() {
+ return delay;
+ }
+
+
+ public void setDelay(int delay) {
+ this.delay = delay;
+ }
+
+
+ public int getTimeOffline() {
+ return timeOffline;
+ }
+
+
+ public void setTimeOffline(int timeOffline) {
+ this.timeOffline = timeOffline;
+ }
+
+ /**
+ * Compute the GracefullShutdown length
+ * 0x30 L1
+ * |
+ * +--> [0x02 0x0(1-4) [0..720] ]
+ * +--> [0x80 0x0(1-3) [0..86400] ]
+ *
+ * L1 will always be < 11.
+ */
+ public int computeLength()
+ {
+ int gracefullLength = 1 + 1;
+ gracefullSequenceLength = 0;
+
+ if ( timeOffline != 0 )
+ {
+ gracefullSequenceLength += 1 + 1 + Length.getNbBytes( timeOffline );
+ }
+
+ if ( delay != 0 )
+ {
+ gracefullSequenceLength += 1 + 1 + Length.getNbBytes( delay );
+ }
+
+ return gracefullLength + gracefullSequenceLength;
+ }
+
+ /**
+ * Encodes the gracefullShutdown extended request.
+ *
+ * @param buffer The encoded sink
+ * @return A ByteBuffer that contains the encoded PDU
+ * @throws EncoderException If anything goes wrong.
+ */
+ public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+ {
+ // Allocate the bytes buffer.
+ ByteBuffer bb = ByteBuffer.allocate( computeLength() );
+
+ bb.put( UniversalTag.SEQUENCE_TAG );
+ bb.put( Length.getBytes( gracefullSequenceLength ) );
+
+ if ( timeOffline != 0 )
+ {
+ Value.encode( bb, timeOffline );
+ }
+
+ if ( delay != 0 )
+ {
+ bb.put( (byte)GracefulShutdownConstants.GRACEFULL_SHUTDOWN_DELAY_TAG );
+ bb.put( (byte)Length.getNbBytes( delay ) );
+ bb.put( Value.getBytes( delay ) );
+ }
+ return bb;
+ }
+
+ public String toString()
+ {
+ return " TimeOffline : " + timeOffline + "\n Delay : " + delay;
+ }
+}
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownConstants.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownConstants.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownConstants.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownConstants.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+
+/**
+ * GracefullShutdown extended operation constants
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class GracefulShutdownConstants
+{
+ public static final int GRACEFULL_SHUTDOWN_DELAY_TAG = 0x80;
+}
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownContainer.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownContainer.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownContainer.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownContainer.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+
+import org.apache.asn1.ber.AbstractContainer;
+import org.apache.asn1.ber.IAsn1Container;
+import org.apache.asn1.ber.grammar.IGrammar;
+
+
+/**
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulShutdownContainer extends AbstractContainer implements IAsn1Container
+{
+ /** GracefulShutdown */
+ private GracefulShutdown gracefulShutdown;
+
+ /**
+ * Creates a new GracefulShutdownContainer object.
+ * We will store one grammar, it's enough ...
+ */
+ public GracefulShutdownContainer()
+ {
+ super( );
+ currentGrammar = 0;
+ grammars = new IGrammar[GracefulShutdownStatesEnum.NB_GRAMMARS];
+ grammarStack = new IGrammar[1];
+ stateStack = new int[1];
+ nbGrammars = 0;
+
+ grammars[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_GRAMMAR] = GracefulShutdownGrammar.getInstance();
+ grammarStack[currentGrammar] = grammars[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_GRAMMAR];
+ states = GracefulShutdownStatesEnum.getInstance();
+ }
+
+
+ /**
+ * @return Returns the Graceful Shutdown object.
+ */
+ public GracefulShutdown getGracefulShutdown()
+ {
+
+ return gracefulShutdown;
+ }
+
+
+ /**
+ * Set a GracefulShutdown Object into the container. It will be completed
+ * by the ldapDecoder.
+ *
+ * @param control the GracefulShutdown to set.
+ */
+ public void setGracefulShutdown( GracefulShutdown gracefulShutdown )
+ {
+ this.gracefulShutdown = gracefulShutdown;
+ }
+
+
+ public void clean()
+ {
+ super.clean();
+ gracefulShutdown = null;
+ }
+}
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownDecoder.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownDecoder.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownDecoder.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownDecoder.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.Asn1Object;
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * A decoder for GracefulShutdowns.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulShutdownDecoder extends Asn1Decoder
+{
+ private static final Asn1Decoder decoder = new Asn1Decoder();
+
+ public Asn1Object decode(byte[] controlBytes) throws DecoderException
+ {
+ ByteBuffer bb = ByteBuffer.wrap( controlBytes );
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+ decoder.decode( bb, container );
+ return container.getGracefulShutdown();
+ }
+}
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownGrammar.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownGrammar.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownGrammar.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownGrammar.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+import org.apache.asn1.ber.IAsn1Container;
+import org.apache.asn1.ber.grammar.AbstractGrammar;
+import org.apache.asn1.ber.grammar.GrammarAction;
+import org.apache.asn1.ber.grammar.GrammarTransition;
+import org.apache.asn1.ber.grammar.IGrammar;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1.util.IntegerDecoder;
+import org.apache.asn1.util.IntegerDecoderException;
+import org.apache.ldap.common.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+/**
+ * This class implements the Graceful shutdown. All the actions are declared in this
+ * class. As it is a singleton, these declaration are only done once.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulShutdownGrammar extends AbstractGrammar implements IGrammar
+{
+ /** The logger */
+ private static final Logger log = LoggerFactory.getLogger( GracefulShutdownGrammar.class );
+
+ /** The instance of grammar. GracefulShutdownGrammar is a singleton */
+ private static IGrammar instance = new GracefulShutdownGrammar();
+
+ /**
+ * Creates a new GracefulShutdownGrammar object.
+ */
+ private GracefulShutdownGrammar()
+ {
+ name = GracefulShutdownGrammar.class.getName();
+ statesEnum = GracefulShutdownStatesEnum.getInstance();
+
+ // Create the transitions table
+ super.transitions = new GrammarTransition[GracefulShutdownStatesEnum.LAST_GRACEFUL_SHUTDOWN_STATE][256];
+
+ super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_TAG][UniversalTag.SEQUENCE_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_TAG,
+ GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_VALUE, null );
+
+ super.transitions[GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_VALUE][UniversalTag.SEQUENCE_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.GRACEFUL_SHUTDOWN_SEQUENCE_VALUE,
+ GracefulShutdownStatesEnum.TIME_OFFLINE_OR_DELAY_OR_END_TAG,
+ new GrammarAction( "Init GracefulShutdown" )
+ {
+ public void action( IAsn1Container container )
+ {
+ GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container;
+ GracefulShutdown gracefulShutdown = new GracefulShutdown();
+ gracefulShutdownContainer.setGracefulShutdown( gracefulShutdown );
+ gracefulShutdownContainer.grammarEndAllowed( true );
+ }
+ }
+ );
+
+ super.transitions[GracefulShutdownStatesEnum.TIME_OFFLINE_OR_DELAY_OR_END_TAG][UniversalTag.INTEGER_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.TIME_OFFLINE_OR_DELAY_OR_END_TAG,
+ GracefulShutdownStatesEnum.TIME_OFFLINE_VALUE, null );
+
+ super.transitions[GracefulShutdownStatesEnum.TIME_OFFLINE_OR_DELAY_OR_END_TAG][GracefulShutdownConstants.GRACEFULL_SHUTDOWN_DELAY_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.TIME_OFFLINE_OR_DELAY_OR_END_TAG,
+ GracefulShutdownStatesEnum.DELAY_VALUE, null );
+
+ super.transitions[GracefulShutdownStatesEnum.TIME_OFFLINE_VALUE][UniversalTag.INTEGER_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.TIME_OFFLINE_VALUE,
+ GracefulShutdownStatesEnum.DELAY_OR_END_TAG,
+ new GrammarAction( "Set PSearchControl changeTypes" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+ GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container;
+ Value value = gracefulShutdownContainer.getCurrentTLV().getValue();
+
+ try
+ {
+ int timeOffline = IntegerDecoder.parse( value, 0, 720 );
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Time Offline = " + timeOffline );
+ }
+
+ gracefulShutdownContainer.getGracefulShutdown().setTimeOffline( timeOffline );
+ gracefulShutdownContainer.grammarEndAllowed( true );
+ }
+ catch ( IntegerDecoderException e )
+ {
+ String msg = "failed to decode the timeOffline, the value should be between 0 and 720 minutes, it is '" +
+ StringTools.dumpBytes( value.getData() ) + "'";
+ log.error( msg );
+ throw new DecoderException( msg );
+ }
+ }
+ }
+ );
+
+ super.transitions[GracefulShutdownStatesEnum.DELAY_OR_END_TAG][GracefulShutdownConstants.GRACEFULL_SHUTDOWN_DELAY_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.DELAY_OR_END_TAG,
+ GracefulShutdownStatesEnum.DELAY_VALUE, null );
+
+ super.transitions[GracefulShutdownStatesEnum.DELAY_VALUE][GracefulShutdownConstants.GRACEFULL_SHUTDOWN_DELAY_TAG] =
+ new GrammarTransition( GracefulShutdownStatesEnum.DELAY_VALUE,
+ GracefulShutdownStatesEnum.GRAMMAR_END,
+ new GrammarAction( "Set PSearchControl changesOnly" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+ GracefulShutdownContainer gracefulShutdownContainer = ( GracefulShutdownContainer ) container;
+ Value value = gracefulShutdownContainer.getCurrentTLV().getValue();
+
+ try
+ {
+ int delay = IntegerDecoder.parse( value, 0, 86400 );
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "changesOnly = " + delay );
+ }
+
+ gracefulShutdownContainer.getGracefulShutdown().setDelay( delay );
+ gracefulShutdownContainer.grammarEndAllowed( true );
+ }
+ catch ( IntegerDecoderException e )
+ {
+ String msg = "failed to decode the delay, the value should be between 0 and 86400 seconds, it is '" +
+ StringTools.dumpBytes( value.getData() ) + "'";
+ log.error( msg );
+ throw new DecoderException( msg );
+ }
+ }
+ }
+ );
+ }
+
+ /**
+ * This class is a singleton.
+ *
+ * @return An instance on this grammar
+ */
+ public static IGrammar getInstance()
+ {
+ return instance;
+ }
+}
Added: directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownStatesEnum.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownStatesEnum.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownStatesEnum.java (added)
+++ directory/trunks/common/ldap/src/main/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownStatesEnum.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+
+import org.apache.asn1.ber.grammar.IStates;
+import org.apache.asn1.ber.grammar.IGrammar;
+
+
+/**
+ * This class store the GracefulShutdown's grammar constants.
+ * It is also used for debugging purposes.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulShutdownStatesEnum implements IStates
+{
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ //=========================================================================
+ // GracefulShutdown grammar states
+ //=========================================================================
+
+ /** Sequence Tag */
+ public static int GRACEFUL_SHUTDOWN_SEQUENCE_TAG = 0;
+
+ /** Sequence Value */
+ public static int GRACEFUL_SHUTDOWN_SEQUENCE_VALUE = 1;
+
+ /** Time offline Tag */
+ public static int TIME_OFFLINE_OR_DELAY_OR_END_TAG = 2;
+
+ /** Time offline Value */
+ public static int TIME_OFFLINE_VALUE = 3;
+
+ /** Delay Tag */
+ public static int DELAY_OR_END_TAG = 4;
+
+ /** Delay Value */
+ public static int DELAY_VALUE = 5;
+
+ /** terminal state */
+ public static int LAST_GRACEFUL_SHUTDOWN_STATE = 8;
+
+ //=========================================================================
+ // Grammars declaration.
+ //=========================================================================
+ /** GracefulShutdown grammar */
+ public static final int GRACEFUL_SHUTDOWN_GRAMMAR_SWITCH = 0x0100;
+
+ /** GracefulShutdown grammar number */
+ public static final int GRACEFUL_SHUTDOWN_GRAMMAR = 0;
+
+ /** The total number of grammars used */
+ public static final int NB_GRAMMARS = 1;
+
+ //=========================================================================
+ // Grammar switches debug strings
+ //=========================================================================
+ /** A string representation of grammars */
+ private static String[] GrammarSwitchString =
+ new String[]
+ {
+ "GRACEFUL_SHUTDOWN_GRAMMAR_SWITCH"
+ };
+
+ //=========================================================================
+ // States debug strings
+ //=========================================================================
+ /** A string representation of all the states */
+ private static String[] GracefulShutdownString = new String[]
+ {
+ "GRACEFUL_SHUTDOWN_SEQUENCE_TAG",
+ "GRACEFUL_SHUTDOWN_SEQUENCE_VALUE",
+ "TIME_OFFLINE_OR_DELAY_OR_END_TAG",
+ "TIME_OFFLINE_VALUE",
+ "DELAY_OR_END_TAG",
+ "DELAY_VALUE"
+ };
+
+ /** The instance */
+ private static GracefulShutdownStatesEnum instance = new GracefulShutdownStatesEnum();
+
+ //~ Constructors -------------------------------------------------------------------------------
+
+ /**
+ * This is a private constructor. This class is a singleton
+ *
+ */
+ private GracefulShutdownStatesEnum()
+ {
+ }
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Get an instance of this class
+ * @return An instance on this class
+ */
+ public static IStates getInstance()
+ {
+ return instance;
+ }
+
+ /**
+ * Get the grammar name
+ * @param grammar The grammar code
+ * @return The grammar name
+ */
+ public String getGrammarName( int grammar )
+ {
+ switch ( grammar )
+ {
+ case GRACEFUL_SHUTDOWN_GRAMMAR: return "GRACEFUL_SHUTDOWN_GRAMMAR";
+ default: return "UNKNOWN";
+ }
+ }
+
+ /**
+ * Get the grammar name
+ * @param grammar The grammar class
+ * @return The grammar name
+ */
+ public String getGrammarName( IGrammar grammar )
+ {
+ if ( grammar instanceof GracefulShutdownGrammar )
+ {
+ return "GRACEFUL_SHUTDOWN_GRAMMAR";
+ }
+
+ return "UNKNOWN GRAMMAR";
+ }
+
+ /**
+ * Get the string representing the state
+ *
+ * @param grammar The current grammar being used
+ * @param state The state number
+ * @return The String representing the state
+ */
+ public String getState( int grammar, int state )
+ {
+
+ if ( ( state & GRAMMAR_SWITCH_MASK ) != 0 )
+ {
+ return ( state == END_STATE ) ? "END_STATE"
+ : GrammarSwitchString[( ( state & GRAMMAR_SWITCH_MASK ) >> 8 ) - 1];
+ }
+ else
+ {
+
+ switch ( grammar )
+ {
+
+ case GRACEFUL_SHUTDOWN_GRAMMAR :
+ return ( ( state == GRAMMAR_END ) ? "GRACEFUL_SHUTDOWN_END_STATE" : GracefulShutdownString[state] );
+
+ default :
+ return "UNKNOWN";
+ }
+ }
+ }
+}
Added: directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownTest.java?rev=371070&view=auto
==============================================================================
--- directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownTest.java (added)
+++ directory/trunks/common/ldap/src/test/java/org/apache/ldap/common/codec/extended/operations/GracefulShutdownTest.java Sat Jan 21 06:56:30 2006
@@ -0,0 +1,282 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ldap.common.codec.extended.operations;
+
+import java.nio.ByteBuffer;
+
+import javax.naming.NamingException;
+
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.ldap.common.codec.LdapDecoder;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * Test the PSearchControlTest codec
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulShutdownTest extends TestCase {
+ /**
+ * Test the decoding of a GracefulShutdown
+ */
+ public void testDecodeGracefulShutdownSuccess() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x06, // GracefulShutdown ::= SEQUENCE {
+ 0x02, 0x01, 0x01, // timeOffline INTEGER (0..720) DEFAULT 0,
+ (byte)0x80, 0x01, 0x01 // delay INTEGER (0..86400) DEFAULT 0
+ // }
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ GracefulShutdown gracefulShutdown = container.getGracefulShutdown();
+ assertEquals( 1, gracefulShutdown.getTimeOffline() );
+ assertEquals( 1, gracefulShutdown.getDelay() );
+ }
+
+ /**
+ * Test the decoding of a GracefulShutdown with a timeOffline only
+ */
+ public void testDecodeGracefulShutdownTimeOffline() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x03, // GracefulShutdown ::= SEQUENCE {
+ 0x02, 0x01, 0x01 // timeOffline INTEGER (0..720) DEFAULT 0,
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ GracefulShutdown gracefulShutdown = container.getGracefulShutdown();
+ assertEquals( 1, gracefulShutdown.getTimeOffline() );
+ assertEquals( 0, gracefulShutdown.getDelay() );
+ }
+
+ /**
+ * Test the decoding of a GracefulShutdown with a delay only
+ */
+ public void testDecodeGracefulShutdownDelay() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x03, // GracefulShutdown ::= SEQUENCE {
+ (byte)0x80, 0x01, 0x01 // delay INTEGER (0..86400) DEFAULT 0
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ GracefulShutdown gracefulShutdown = container.getGracefulShutdown();
+ assertEquals( 0, gracefulShutdown.getTimeOffline() );
+ assertEquals( 1, gracefulShutdown.getDelay() );
+ }
+
+ /**
+ * Test the decoding of a empty GracefulShutdown
+ */
+ public void testDecodeGracefulShutdownEmpty() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x00 // GracefulShutdown ::= SEQUENCE {
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ GracefulShutdown gracefulShutdown = container.getGracefulShutdown();
+ assertEquals( 0, gracefulShutdown.getTimeOffline() );
+ assertEquals( 0, gracefulShutdown.getDelay() );
+ }
+
+ // Defensive tests
+
+ /**
+ * Test the decoding of a GracefulShutdown with a timeOffline off limit
+ */
+ public void testDecodeGracefulShutdownTimeOfflineOffLimit() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x04, // GracefulShutdown ::= SEQUENCE {
+ 0x02, 0x02, 0x03, (byte)0xE8 // timeOffline INTEGER (0..720) DEFAULT 0,
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a GracefulShutdown with a delay off limit
+ */
+ public void testDecodeGracefulShutdownDelayOffLimit() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x05, // GracefulShutdown ::= SEQUENCE {
+ (byte)0x80, 0x03, 0x01, (byte)0x86, (byte)0xA0 // delay INTEGER (0..86400) DEFAULT 0
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a GracefulShutdown with an empty TimeOffline
+ */
+ public void testDecodeGracefulShutdownTimeOfflineEmpty() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x02, // GracefulShutdown ::= SEQUENCE {
+ 0x02, 0x00 // timeOffline INTEGER (0..720) DEFAULT 0,
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+
+ /**
+ * Test the decoding of a GracefulShutdown with an empty delay
+ */
+ public void testDecodeGracefulShutdownDelayEmpty() throws NamingException
+ {
+ Asn1Decoder decoder = new LdapDecoder();
+ ByteBuffer bb = ByteBuffer.allocate( 0x0b );
+ bb.put( new byte[]
+ {
+ 0x30, 0x02, // GracefulShutdown ::= SEQUENCE {
+ (byte)0x80, 0x00 // delay INTEGER (0..86400) DEFAULT 0
+ } );
+ bb.flip();
+
+ GracefulShutdownContainer container = new GracefulShutdownContainer();
+
+ try
+ {
+ decoder.decode( bb, container );
+ }
+ catch ( DecoderException de )
+ {
+ System.out.println( de.getMessage() );
+ assertTrue( true );
+ return;
+ }
+
+ fail( "We should not reach this point" );
+ }
+}