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   *    https://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      /** SyncDoneValueControlGrammar singleton instance */
60      private static final SyncDoneValueGrammar INSTANCE = new SyncDoneValueGrammar();
61  
62  
63      /**
64       * 
65       * Creates a new instance of SyncDoneValueControlGrammar.
66       *
67       */
68      @SuppressWarnings("unchecked")
69      private SyncDoneValueGrammar()
70      {
71          setName( SyncDoneValueGrammar.class.getName() );
72  
73          super.transitions = new GrammarTransition[SyncDoneValueStatesEnum.LAST_SYNC_DONE_VALUE_STATE.ordinal()][256];
74  
75          /** 
76           * Transition from initial state to SyncDoneValue sequence
77           * SyncDoneValue ::= SEQUENCE {
78           *     ...
79           *     
80           * Initialize the syncDoneValue object
81           */
82          super.transitions[SyncDoneValueStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = new GrammarTransition<SyncDoneValueContainer>(
83              SyncDoneValueStatesEnum.START_STATE, SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
84              UniversalTag.SEQUENCE.getValue(),
85              new GrammarAction<SyncDoneValueContainer>( "Initialization" )
86              {
87                  public void action( SyncDoneValueContainer container )
88                  {
89                      // As all the values are optional or defaulted, we can end here
90                      container.setGrammarEndAllowed( true );
91                  }
92              } );
93  
94          /**
95           * transition from start to cookie
96           * {
97           *    cookie          syncCookie OPTIONAL
98           *    ....
99           * }
100          */
101         super.transitions[SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE.ordinal()][UniversalTag.OCTET_STRING
102             .getValue()] =
103             new GrammarTransition<SyncDoneValueContainer>( SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
104                 SyncDoneValueStatesEnum.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
105                 new GrammarAction<SyncDoneValueContainer>( "Set SyncDoneValueControl cookie" )
106                 {
107                     public void action( SyncDoneValueContainer container )
108                     {
109                         BerValue value = container.getCurrentTLV().getValue();
110 
111                         byte[] cookie = value.getData();
112 
113                         if ( LOG.isDebugEnabled() )
114                         {
115                             LOG.debug( I18n.msg( I18n.MSG_08000_COOKIE, Strings.dumpBytes( cookie ) ) );
116                         }
117 
118                         container.getSyncDoneValue().setCookie( cookie );
119 
120                         container.setGrammarEndAllowed( true );
121                     }
122                 } );
123 
124         GrammarAction<SyncDoneValueContainer> refreshDeletesTagAction =
125             new GrammarAction<SyncDoneValueContainer>( "set SyncDoneValueControl refreshDeletes flag" )
126             {
127                 public void action( SyncDoneValueContainer container ) throws DecoderException
128                 {
129                     BerValue value = container.getCurrentTLV().getValue();
130 
131                     try
132                     {
133                         boolean refreshDeletes = BooleanDecoder.parse( value );
134 
135                         if ( LOG.isDebugEnabled() )
136                         {
137                             LOG.debug( I18n.msg( I18n.MSG_08001_REFRESH_DELETES, refreshDeletes ) );
138                         }
139 
140                         container.getSyncDoneValue().setRefreshDeletes( refreshDeletes );
141 
142                         // the END transition for grammar
143                         container.setGrammarEndAllowed( true );
144                     }
145                     catch ( BooleanDecoderException be )
146                     {
147                         String msg = I18n.err( I18n.ERR_08001_CANNOT_DECODE_REFRESH_DELETES );
148                         LOG.error( msg, be );
149                         throw new DecoderException( msg, be );
150                     }
151 
152                 }
153             };
154         /**
155          * transition from cookie to refreshDeletes
156          * {
157          *    ....
158          *    refreshDeletes BOOLEAN DEFAULT FALSE
159          * }
160          */
161         super.transitions[SyncDoneValueStatesEnum.COOKIE_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
162             new GrammarTransition<SyncDoneValueContainer>(
163                 SyncDoneValueStatesEnum.COOKIE_STATE, SyncDoneValueStatesEnum.REFRESH_DELETES_STATE,
164                 UniversalTag.BOOLEAN.getValue(), refreshDeletesTagAction );
165 
166         /**
167          * transition from SEQUENCE to refreshDeletes
168          * {
169          *    ....
170          *    refreshDeletes BOOLEAN DEFAULT FALSE
171          * }
172          */
173         super.transitions[SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE.ordinal()][UniversalTag.BOOLEAN
174             .getValue()] =
175             new GrammarTransition<SyncDoneValueContainer>( SyncDoneValueStatesEnum.SYNC_DONE_VALUE_SEQUENCE_STATE,
176                 SyncDoneValueStatesEnum.REFRESH_DELETES_STATE, UniversalTag.BOOLEAN.getValue(), refreshDeletesTagAction );
177     }
178 
179 
180     /**
181      * @return the singleton instance of the SyncDoneValueControlGrammar
182      */
183     public static Grammar<SyncDoneValueContainer> getInstance()
184     {
185         return INSTANCE;
186     }
187 }