View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.extras.controls.syncrepl_impl;
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.Grammar;
26  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.BooleanDecoder;
30  import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException;
31  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32  import org.apache.directory.api.i18n.I18n;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * 
40   * Implementation of SyncDoneValueControl. All the actions are declared in
41   * this class. As it is a singleton, these declaration are only done once.
42   *
43   *  The decoded grammar is as follows :
44   *  
45   *  syncDoneValue ::= SEQUENCE 
46   *  {
47   *       cookie          syncCookie OPTIONAL,
48   *       refreshDeletes  BOOLEAN DEFAULT FALSE
49   *  }
50   *  
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  public final class SyncDoneValueGrammar extends AbstractGrammar<SyncDoneValueContainer>
54  {
55  
56      /** the logger */
57      private static final Logger LOG = LoggerFactory.getLogger( SyncDoneValueGrammar.class );
58  
59      /** speedup for logger */
60      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
61  
62      /** SyncDoneValueControlGrammar singleton instance */
63      private static final SyncDoneValueGrammar INSTANCE = new SyncDoneValueGrammar();
64  
65  
66      /**
67       * 
68       * Creates a new instance of SyncDoneValueControlGrammar.
69       *
70       */
71      @SuppressWarnings("unchecked")
72      private SyncDoneValueGrammar()
73      {
74          setName( SyncDoneValueGrammar.class.getName() );
75  
76          super.transitions = new GrammarTransition[SyncDoneValueStatesEnum.LAST_SYNC_DONE_VALUE_STATE.ordinal()][256];
77  
78          /** 
79           * Transition from initial state to SyncDoneValue sequence
80           * SyncDoneValue ::= SEQUENCE {
81           *     ...
82           *     
83           * Initialize the syncDoneValue object
84           */
85          super.transitions[SyncDoneValueStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition<SyncDoneValueContainer>(
86              SyncDoneValueStatesEnum.START_STATE, SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
87              UniversalTag.SEQUENCE.getValue(),
88              new GrammarAction<SyncDoneValueContainer>( "Initialization" )
89              {
90                  public void action( SyncDoneValueContainer container ) throws DecoderException
91                  {
92                      // As all the values are optional or defaulted, we can end here
93                      container.setGrammarEndAllowed( true );
94                  }
95              } );
96  
97          /**
98           * transition from start to cookie
99           * {
100          *    cookie          syncCookie OPTIONAL
101          *    ....
102          * }
103          */
104         super.transitions[SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE.ordinal()][UniversalTag.OCTET_STRING
105             .getValue()] =
106             new GrammarTransition<SyncDoneValueContainer>( SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
107                 SyncDoneValueStatesEnum.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
108                 new GrammarAction<SyncDoneValueContainer>( "Set SyncDoneValueControl cookie" )
109                 {
110                     public void action( SyncDoneValueContainer container ) throws DecoderException
111                     {
112                         BerValue value = container.getCurrentTLV().getValue();
113 
114                         byte[] cookie = value.getData();
115 
116                         if ( IS_DEBUG )
117                         {
118                             LOG.debug( "cookie = {}", Strings.dumpBytes( cookie ) );
119                         }
120 
121                         container.getSyncDoneValueControl().setCookie( cookie );
122 
123                         container.setGrammarEndAllowed( true );
124                     }
125                 } );
126 
127         GrammarAction<SyncDoneValueContainer> refreshDeletesTagAction =
128             new GrammarAction<SyncDoneValueContainer>( "set SyncDoneValueControl refreshDeletes flag" )
129             {
130                 public void action( SyncDoneValueContainer container ) throws DecoderException
131                 {
132                     BerValue value = container.getCurrentTLV().getValue();
133 
134                     try
135                     {
136                         boolean refreshDeletes = BooleanDecoder.parse( value );
137 
138                         if ( IS_DEBUG )
139                         {
140                             LOG.debug( "refreshDeletes = {}", refreshDeletes );
141                         }
142 
143                         container.getSyncDoneValueControl().setRefreshDeletes( refreshDeletes );
144 
145                         // the END transition for grammar
146                         container.setGrammarEndAllowed( true );
147                     }
148                     catch ( BooleanDecoderException be )
149                     {
150                         String msg = I18n.err( I18n.ERR_04024 );
151                         LOG.error( msg, be );
152                         throw new DecoderException( msg, be );
153                     }
154 
155                 }
156             };
157         /**
158          * transition from cookie to refreshDeletes
159          * {
160          *    ....
161          *    refreshDeletes BOOLEAN DEFAULT FALSE
162          * }
163          */
164         super.transitions[SyncDoneValueStatesEnum.COOKIE_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
165             new GrammarTransition<SyncDoneValueContainer>(
166                 SyncDoneValueStatesEnum.COOKIE_STATE, SyncDoneValueStatesEnum.REFRESH_DELETES_STATE,
167                 UniversalTag.BOOLEAN.getValue(), refreshDeletesTagAction );
168 
169         /**
170          * transition from SEQUENCE to refreshDeletes
171          * {
172          *    ....
173          *    refreshDeletes BOOLEAN DEFAULT FALSE
174          * }
175          */
176         super.transitions[SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE.ordinal()][UniversalTag.BOOLEAN
177             .getValue()] =
178             new GrammarTransition<SyncDoneValueContainer>( SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
179                 SyncDoneValueStatesEnum.REFRESH_DELETES_STATE, UniversalTag.BOOLEAN.getValue(), refreshDeletesTagAction );
180     }
181 
182 
183     /**
184      * @return the singleton instance of the SyncDoneValueControlGrammar
185      */
186     public static Grammar<SyncDoneValueContainer> getInstance()
187     {
188         return INSTANCE;
189     }
190 }