You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2006/03/08 08:31:54 UTC
svn commit: r384142 - in /directory/trunks/shared/ldap/src:
main/java/org/apache/directory/shared/ldap/codec/extended/operations/
test/java/org/apache/directory/shared/ldap/codec/extended/operations/
Author: ersiner
Date: Tue Mar 7 23:31:52 2006
New Revision: 384142
URL: http://svn.apache.org/viewcvs?rev=384142&view=rev
Log:
Added Stored Procedure Codec and a simple test case
Added:
directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedure.java
directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureContainer.java
directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureDecoder.java
directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureGrammar.java
directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureStatesEnum.java
directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureTest.java
Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedure.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedure.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedure.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedure.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * Stored Procedure Extended Operation bean
+ *
+ * <pre>
+ * StoredProcedure ::= SEQUENCE {
+ * language OCTETSTRING,
+ * procedure OCTETSTRING,
+ * parameters SEQUENCE OF Parameter {
+ * Parameter ::= SEQUENCE OF {
+ * type OCTETSTRING,
+ * value OCTETSTRING
+ * }
+ * }
+ * }
+ * </pre>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedure
+{
+ private String language;
+
+ private byte[] procedure;
+
+ private ArrayList parameters;
+
+ private transient StoredProcedureParameter currentParameter;
+
+
+ public String getLanguage()
+ {
+ return language;
+ }
+
+
+ public void setLanguage( String language )
+ {
+ this.language = language;
+ }
+
+
+ public byte[] getProcedure()
+ {
+ return procedure;
+ }
+
+
+ public void setProcedure( byte[] procedure )
+ {
+ this.procedure = procedure;
+ }
+
+
+ public List getParameters()
+ {
+ return parameters;
+ }
+
+
+ public void addParameter( StoredProcedureParameter parameter )
+ {
+ if ( parameters == null )
+ {
+ parameters = new ArrayList();
+ }
+
+ parameters.add( parameter );
+ }
+
+
+ public StoredProcedureParameter getCurrentParameter()
+ {
+ return currentParameter;
+ }
+
+
+ public void setCurrentParameter( StoredProcedureParameter currentParameter )
+ {
+ this.currentParameter = currentParameter;
+ }
+
+ /**
+ * Bean for representing a Stored Procedure Parameter
+ */
+ public static class StoredProcedureParameter
+ {
+ private byte[] type;
+
+ private byte[] value;
+
+
+ public byte[] getType()
+ {
+ return type;
+ }
+
+
+ public void setType( byte[] type )
+ {
+ this.type = type;
+ }
+
+
+ public byte[] getValue()
+ {
+ return value;
+ }
+
+
+ public void setValue( byte[] value )
+ {
+ this.value = value;
+ }
+ }
+
+}
Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureContainer.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureContainer.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureContainer.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureContainer.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+import org.apache.directory.shared.asn1.ber.IAsn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
+
+
+/**
+ * A container for the StoredProcedure codec
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedureContainer extends AbstractContainer implements IAsn1Container
+{
+ // ~ Instance fields
+ // ----------------------------------------------------------------------------
+
+ /** StoredProcedure */
+ private StoredProcedure storedProcedure;
+
+
+ // ~ Constructors
+ // -------------------------------------------------------------------------------
+
+ public StoredProcedureContainer()
+ {
+ super();
+ currentGrammar = 0;
+ grammars = new IGrammar[1];
+ grammarStack = new IGrammar[1];
+ stateStack = new int[1];
+ nbGrammars = 0;
+
+ grammars[StoredProcedureStatesEnum.STORED_PROCEDURE_GRAMMAR] = StoredProcedureGrammar.getInstance();
+
+ grammarStack[currentGrammar] = grammars[StoredProcedureStatesEnum.STORED_PROCEDURE_GRAMMAR];
+
+ states = StoredProcedureStatesEnum.getInstance();
+ }
+
+
+ // ~ Methods
+ // ------------------------------------------------------------------------------------
+ /**
+ * @return Returns the ldapMessage.
+ */
+ public StoredProcedure getStoredProcedure()
+ {
+ return storedProcedure;
+ }
+
+
+ /**
+ * Set a StoredProcedure object into the container. It will be completed by the
+ * ldapDecoder.
+ *
+ * @param ldapMessage
+ * The ldapMessage to set.
+ */
+ public void setStoredProcedure( StoredProcedure storedProcedure )
+ {
+ this.storedProcedure = storedProcedure;
+ }
+
+
+ public void clean()
+ {
+ super.clean();
+
+ storedProcedure = null;
+ }
+}
Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureDecoder.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureDecoder.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureDecoder.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureDecoder.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+
+
+/**
+ * StoredProcedure Decoder
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedureDecoder extends Asn1Decoder
+{
+
+ public StoredProcedureDecoder()
+ {
+ }
+
+}
Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureGrammar.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureGrammar.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureGrammar.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureGrammar.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,315 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import org.apache.directory.shared.asn1.ber.IAsn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.apache.log4j.Logger;
+import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedure.StoredProcedureParameter;
+
+
+/**
+ * ASN.1 BER Grammar for Stored Procedure Extended Operation
+ *
+ * <pre>
+ * StoredProcedure ::= SEQUENCE {
+ * language OCTETSTRING,
+ * procedure OCTETSTRING,
+ * parameters SEQUENCE OF Parameter {
+ * Parameter ::= SEQUENCE OF {
+ * type OCTETSTRING,
+ * value OCTETSTRING
+ * }
+ * }
+ * }
+ * </pre>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedureGrammar extends AbstractGrammar implements IGrammar
+{
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ /** The logger */
+ //private static final Logger log = LoggerFactory.getLogger( StoredProcedureGrammar.class );
+ private static final Logger log = Logger.getLogger( StoredProcedureGrammar.class );
+
+ /** The instance of grammar. StoredProcedureGrammar is a singleton. */
+ private static IGrammar instance = new StoredProcedureGrammar();
+
+
+ //~ Constructors -------------------------------------------------------------------------------
+
+ /**
+ * Creates a new StoredProcedureGrammar object.
+ */
+ private StoredProcedureGrammar()
+ {
+ name = StoredProcedureGrammar.class.getName();
+ statesEnum = StoredProcedureStatesEnum.getInstance();
+
+ // Create the transitions table
+ super.transitions = new GrammarTransition[StoredProcedureStatesEnum.LAST_STORED_PROCEDURE_STATE][256];
+
+ //============================================================================================
+ // StoredProcedure Message
+ //============================================================================================
+ // StoredProcedure ::= SEQUENCE { (Tag)
+ // ...
+ // Nothing to do.
+ super.transitions[StoredProcedureStatesEnum.STORED_PROCEDURE_TAG][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.STORED_PROCEDURE_TAG, StoredProcedureStatesEnum.STORED_PROCEDURE_VALUE, null );
+
+ // StoredProcedure ::= SEQUENCE { (Value)
+ // ...
+ // Nothing to do.
+ super.transitions[StoredProcedureStatesEnum.STORED_PROCEDURE_VALUE][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.STORED_PROCEDURE_VALUE, StoredProcedureStatesEnum.LANGUAGE_TAG, null );
+
+ // language OCTETSTRING, (Tag)
+ // ...
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.LANGUAGE_TAG][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.LANGUAGE_TAG, StoredProcedureStatesEnum.LANGUAGE_VALUE, null );
+
+ // language OCTETSTRING, (Value)
+ // ...
+ // Store the language.
+ super.transitions[StoredProcedureStatesEnum.LANGUAGE_VALUE][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.LANGUAGE_VALUE, StoredProcedureStatesEnum.PROCEDURE_TAG, new GrammarAction(
+ "Stores the language" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+
+ StoredProcedureContainer storedProcedureContainer = ( StoredProcedureContainer ) container;
+
+ TLV tlv = storedProcedureContainer.getCurrentTLV();
+
+ StoredProcedure storedProcedure = null;
+
+ // Store the value.
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ // We can't have a void language !
+ log.error( "The stored procedure language is null" );
+ throw new DecoderException( "The stored procedure language cannot be null" );
+ }
+ else
+ {
+ // Only this field's type is String by default
+ String language = StringTools.utf8ToString( tlv.getValue().getData() );
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "SP language found: " + language );
+ }
+
+ storedProcedure = new StoredProcedure();
+ storedProcedure.setLanguage( language );
+ storedProcedureContainer.setStoredProcedure( storedProcedure );
+ }
+ }
+ } );
+
+ // procedure OCTETSTRING, (Tag)
+ // ...
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.PROCEDURE_TAG][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PROCEDURE_TAG, StoredProcedureStatesEnum.PROCEDURE_VALUE, null );
+
+ // procedure OCTETSTRING, (Value)
+ // ...
+ // Store the procedure.
+ super.transitions[StoredProcedureStatesEnum.PROCEDURE_VALUE][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PROCEDURE_VALUE, StoredProcedureStatesEnum.PARAMETERS_TAG, new GrammarAction(
+ "Stores the procedure" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+
+ StoredProcedureContainer storedProcedureContainer = ( StoredProcedureContainer ) container;
+
+ TLV tlv = storedProcedureContainer.getCurrentTLV();
+
+ StoredProcedure storedProcedure = storedProcedureContainer.getStoredProcedure();
+
+ // Store the value.
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ // We can't have a void procedure !
+ log.error( "The procedure can't be null" );
+ throw new DecoderException( "The procedure can't be null" );
+ }
+ else
+ {
+ byte[] procedure = tlv.getValue().getData();
+
+ storedProcedure.setProcedure( procedure );
+ }
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Procedure found : " + storedProcedure.getProcedure() );
+ }
+ }
+ } );
+
+ // parameters SEQUENCE OF Parameter { (Tag)
+ // ...
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.PARAMETERS_TAG][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETERS_TAG, StoredProcedureStatesEnum.PARAMETERS_VALUE, null );
+
+ // parameters SEQUENCE OF Parameter { (Value)
+ // ...
+ // Nothing to do. The list of parameters will be created with the first parameter.
+ super.transitions[StoredProcedureStatesEnum.PARAMETERS_VALUE][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETERS_VALUE, StoredProcedureStatesEnum.PARAMETER_TYPE_TAG, null );
+
+ // Parameter ::= {
+ // type OCTETSTRING, (Tag)
+ // ...
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.PARAMETER_TYPE_TAG][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETER_TYPE_TAG, StoredProcedureStatesEnum.PARAMETER_TYPE_VALUE, null );
+
+ // Parameter ::= {
+ // type OCTETSTRING, (Value)
+ // ...
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.PARAMETER_TYPE_VALUE][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETER_TYPE_VALUE, StoredProcedureStatesEnum.PARAMETER_VALUE_TAG, new GrammarAction(
+ "Store parameter type" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+ StoredProcedureContainer storedProcedureContainer = ( StoredProcedureContainer ) container;
+
+ TLV tlv = storedProcedureContainer.getCurrentTLV();
+ StoredProcedure storedProcedure = storedProcedureContainer.getStoredProcedure();
+
+ // Store the value.
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ // We can't have a void parameter type !
+ log.error( "The parameter type can't be null" );
+ throw new DecoderException( "The parameter type can't be null" );
+ }
+ else
+ {
+ StoredProcedureParameter parameter = new StoredProcedureParameter();
+
+ byte[] parameterType = tlv.getValue().getData();
+
+ parameter.setType( parameterType );
+
+ // We store the type in the current parameter.
+ storedProcedure.setCurrentParameter( parameter );
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Parameter type found : " + StringTools.dumpBytes( parameterType ) );
+ }
+
+ }
+ }
+ } );
+
+ // Parameter ::= {
+ // ...
+ // value OCTETSTRING (Tag)
+ // }
+ // Nothing to do
+ super.transitions[StoredProcedureStatesEnum.PARAMETER_VALUE_TAG][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETER_VALUE_TAG, StoredProcedureStatesEnum.PARAMETER_VALUE_VALUE, null );
+
+ // Parameter ::= {
+ // ...
+ // value OCTETSTRING (Tag)
+ // }
+ // Store the parameter value
+ super.transitions[StoredProcedureStatesEnum.PARAMETER_VALUE_VALUE][UniversalTag.OCTET_STRING_TAG] = new GrammarTransition(
+ StoredProcedureStatesEnum.PARAMETER_VALUE_VALUE, StoredProcedureStatesEnum.PARAMETER_TYPE_TAG, new GrammarAction(
+ "Store parameter value" )
+ {
+ public void action( IAsn1Container container ) throws DecoderException
+ {
+ StoredProcedureContainer storedProcedureContainer = ( StoredProcedureContainer ) container;
+
+ TLV tlv = storedProcedureContainer.getCurrentTLV();
+ StoredProcedure storedProcedure = storedProcedureContainer.getStoredProcedure();
+
+ // Store the value.
+ if ( tlv.getLength().getLength() == 0 )
+ {
+ // We can't have a void parameter value !
+ log.error( "The parameter value can't be null" );
+ throw new DecoderException( "The parameter value can't be null" );
+ }
+ else
+ {
+ byte[] parameterValue = tlv.getValue().getData();
+
+ if ( parameterValue.length != 0 )
+ {
+ StoredProcedureParameter parameter = storedProcedure.getCurrentParameter();
+ parameter.setValue( parameterValue );
+
+ // We can now add a new Parameter to the pojo
+ storedProcedure.addParameter( parameter );
+
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Parameter value found : " + StringTools.dumpBytes( parameterValue ) );
+ }
+ }
+ else
+ {
+ log.error( "The parameter value is empty. This is not allowed." );
+ throw new DecoderException( "The parameter value is empty. This is not allowed." );
+ }
+ }
+
+ // The only possible END state for the grammar is here
+ container.grammarEndAllowed( true );
+ }
+ } );
+ }
+
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Get the instance of this grammar
+ *
+ * @return An instance on the StoredProcedure Grammar
+ */
+ public static IGrammar getInstance()
+ {
+ return instance;
+ }
+}
Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureStatesEnum.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureStatesEnum.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureStatesEnum.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureStatesEnum.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.IStates;
+
+
+/**
+ * Constants for StoredProcedureGrammar
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedureStatesEnum implements IStates
+{
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ //=========================================================================
+ // StoredProcedure
+ //=========================================================================
+ /** StoredProcedure Tag */
+ public static int STORED_PROCEDURE_TAG = 0;
+
+ /** StoredProcedure Value */
+ public static int STORED_PROCEDURE_VALUE = 1;
+
+ // Language ---------------------------------------------------------------
+ /** Language Tag */
+ public static int LANGUAGE_TAG = 2;
+
+ /** Language Value */
+ public static int LANGUAGE_VALUE = 3;
+
+ // Procedure --------------------------------------------------------------
+ /** Procedure Tag */
+ public static int PROCEDURE_TAG = 4;
+
+ /** Procedure Value */
+ public static int PROCEDURE_VALUE = 5;
+
+ // Parameters -------------------------------------------------------------
+ /** Parameters Tag */
+ public static int PARAMETERS_TAG = 6;
+
+ /** Parameters Value */
+ public static int PARAMETERS_VALUE = 7;
+
+ // Parameter type ---------------------------------------------------------
+ /** Parameter type Tag */
+ public static int PARAMETER_TYPE_TAG = 8;
+
+ /** Parameter type Value */
+ public static int PARAMETER_TYPE_VALUE = 9;
+
+ // Parameters value -------------------------------------------------------
+ /** Parameter value Tag */
+ public static int PARAMETER_VALUE_TAG = 10;
+
+ /** Parameter value Value */
+ public static int PARAMETER_VALUE_VALUE = 11;
+
+ public static int LAST_STORED_PROCEDURE_STATE = 12;
+
+ //=========================================================================
+ // Grammars declaration.
+ //=========================================================================
+ /** Ldap Message Grammar */
+ public static final int STORED_PROCEDURE_GRAMMAR_SWITCH = 0x0100;
+
+ /** LdapMessage grammar number */
+ public static final int STORED_PROCEDURE_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[]
+ { "STORED_PROCEDURE_GRAMMAR_SWITCH", };
+
+ //=========================================================================
+ // States debug strings
+ //=========================================================================
+ /** A string representation of all the states */
+ private static String[] StoredProcedureString = new String[]
+ { "STORED_PROCEDURE_TAG", "STORED_PROCEDURE_VALUE", "LANGUAGE_TAG", "LANGUAGE_VALUE", "PROCEDURE_TAG",
+ "PROCEDURE_VALUE", "PARAMETERS_TAG", "PARAMETERS_VALUE", "PARAMETER_TYPE_TAG", "PARAMETER_TYPE_VALUE",
+ "PARAMETER_VALUE_TAG", "PARAMETER_VALUE_VALUE" };
+
+ /** The instance */
+ private static StoredProcedureStatesEnum instance = new StoredProcedureStatesEnum();
+
+
+ //~ Constructors -------------------------------------------------------------------------------
+
+ /**
+ * This is a private constructor. This class is a singleton
+ *
+ */
+ private StoredProcedureStatesEnum()
+ {
+ }
+
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Get an instance of this class
+ * @return An instance on this class
+ */
+ public static IStates getInstance()
+ {
+ return instance;
+ }
+
+
+ /**
+ * Get the grammar name
+ * @param The grammar code
+ * @return The grammar name
+ */
+ public String getGrammarName( int grammar )
+ {
+ switch ( grammar )
+ {
+ case STORED_PROCEDURE_GRAMMAR:
+ return "STORED_PROCEDURE_GRAMMAR";
+
+ default:
+ return "UNKNOWN";
+ }
+ }
+
+
+ /**
+ * Get the grammar name
+ * @param The grammar class
+ * @return The grammar name
+ */
+ public String getGrammarName( IGrammar grammar )
+ {
+ if ( grammar instanceof StoredProcedureGrammar )
+ {
+ return "STORED_PROCEDURE_GRAMMAR";
+ }
+ else
+ {
+ 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 STORED_PROCEDURE_GRAMMAR:
+ return ( ( state == GRAMMAR_END ) ? "STORED_PROCEDURE_END_STATE" : StoredProcedureString[state] );
+
+ default:
+ return "UNKNOWN";
+ }
+ }
+ }
+}
Added: directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureTest.java?rev=384142&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureTest.java (added)
+++ directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/codec/extended/operations/StoredProcedureTest.java Tue Mar 7 23:31:52 2006
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2006 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.directory.shared.ldap.codec.extended.operations;
+
+
+import java.nio.ByteBuffer;
+
+import javax.naming.NamingException;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.ber.IAsn1Container;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedure.StoredProcedureParameter;
+import org.apache.directory.shared.ldap.util.StringTools;
+
+
+/*
+ * TestCase for a Stored Procedure Extended Operation ASN.1 codec
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StoredProcedureTest extends TestCase
+{
+ public void testDecodeStoredProcedure() throws NamingException
+ {
+ Asn1Decoder storedProcedureDecoder = new StoredProcedureDecoder();
+
+ ByteBuffer stream = ByteBuffer.allocate( 0x3E );
+
+ stream.put( new byte[]
+ {
+ 0x30, 0x3C,
+ 0x04, 0x04, 'J', 'a', 'v', 'a',
+ 0x04, 0x07, 'e', 'x', 'e', 'c', 'u', 't', 'e',
+ 0x30, 0x2B,
+ 0x04, 0x03, 'i', 'n', 't', 0x04, 0x01, 0x01,
+ 0x04, 0x07, 'b', 'o', 'o', 'l', 'e', 'a', 'n', 0x04, 0x04, 't', 'r', 'u', 'e',
+ 0x04, 0x06, 'S', 't', 'r', 'i', 'n', 'g', 0x04, 0x0A, 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', '3'
+ } );
+
+ //String decodedPdu = StringTools.dumpBytes( stream.array() );
+ stream.flip();
+
+ // Allocate a StoredProcedure Container
+ IAsn1Container storedProcedureContainer = new StoredProcedureContainer();
+
+ // Decode a StoredProcedure message
+ try
+ {
+ storedProcedureDecoder.decode( stream, storedProcedureContainer );
+ }
+ catch ( DecoderException de )
+ {
+ de.printStackTrace();
+ Assert.fail( de.getMessage() );
+ }
+
+ StoredProcedure storedProcedure = ( ( StoredProcedureContainer ) storedProcedureContainer ).getStoredProcedure();
+
+ Assert.assertEquals("Java", storedProcedure.getLanguage());
+
+ Assert.assertEquals( "execute", StringTools.utf8ToString( storedProcedure.getProcedure() ) );
+
+ Assert.assertNotNull( storedProcedure.getParameters() );
+ Assert.assertEquals( 3, storedProcedure.getParameters().size() );
+
+ StoredProcedureParameter param = ( StoredProcedureParameter ) storedProcedure.getParameters().get( 0 );
+
+ Assert.assertEquals( "int", StringTools.utf8ToString( param.getType() ) );
+ Assert.assertEquals( 1, param.getValue()[0] );
+
+ param = ( StoredProcedureParameter ) storedProcedure.getParameters().get( 1 );
+
+ Assert.assertEquals( "boolean", StringTools.utf8ToString( param.getType() ) );
+ Assert.assertEquals( "true", StringTools.utf8ToString( param.getValue() ) );
+
+ param = ( StoredProcedureParameter ) storedProcedure.getParameters().get( 2 );
+
+ Assert.assertEquals( "String", StringTools.utf8ToString( param.getType() ) );
+ Assert.assertEquals( "parameter3", StringTools.utf8ToString( param.getValue() ) );
+
+ }
+}