001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.directory.server.core.journal;
020    
021    import org.apache.directory.server.core.DirectoryService;
022    import org.apache.directory.server.core.LdapPrincipal;
023    import org.apache.directory.shared.ldap.ldif.LdifEntry;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    /**
028     * The default journal implementation. It stores the operation and the
029     * associated status (acked or nacked) in a file which will be used to
030     * restore the server if it crashes.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev$, $Date$
034     */
035    public class DefaultJournal implements Journal
036    {
037        /** The class logger */
038        private static final Logger LOG = LoggerFactory.getLogger( DefaultJournal.class );
039    
040        /** Tells if the service is activated or not */ 
041        private boolean enabled;
042    
043        /** An instance of the Journal store */
044        private JournalStore store;
045    
046        /** 
047         * A parameter indicating the number of operations stored in a journal
048         * before it is rotated. If set to 0, no rotation is done
049         */ 
050        private int rotation;
051        
052        /**
053         * {@inheritDoc}
054         */
055        public void destroy() throws Exception
056        {
057            LOG.debug( "Stopping the journal" );
058            
059            // We have to release the file, otherwise Windows won't be able
060            // to stop the server
061            if ( store != null )
062            {
063                store.destroy();
064            }
065        }
066    
067    
068        /**
069         * {@inheritDoc}
070         */
071        public JournalStore getJournalStore()
072        {
073            return store;
074        }
075    
076    
077        /**
078         * {@inheritDoc}
079         */
080        public void init( DirectoryService directoryService ) throws Exception
081        {
082            LOG.debug( "Starting the journal" );
083    
084            if( store == null )
085            {
086                store = new DefaultJournalStore();
087            }
088    
089            store.init( directoryService );
090    
091            LOG.debug( "The Journal service has been initialized" );
092        }
093    
094    
095        /**
096         * {@inheritDoc}
097         */
098        public boolean isEnabled()
099        {
100            return enabled;
101        }
102    
103    
104        /**
105         * {@inheritDoc}
106         */
107        public void log( LdapPrincipal principal, long revision, LdifEntry entry ) throws Exception
108        {
109            store.log( principal, revision, entry );
110        }
111    
112    
113        /**
114         * {@inheritDoc}
115         */
116        public void ack( long revision )
117        {
118            store.ack( revision );
119        }
120    
121    
122        /**
123         * {@inheritDoc}
124         */
125        public void nack( long revision )
126        {
127            store.nack( revision );
128        }
129    
130    
131        /**
132         * @return the rotation
133         */
134        public int getRotation()
135        {
136            return rotation;
137        }
138    
139    
140        /**
141         * @param rotation the rotation to set
142         */
143        public void setRotation( int rotation )
144        {
145            this.rotation = rotation;
146        }
147    
148        
149        public void setEnabled( boolean enabled )
150        {
151            this.enabled = enabled; 
152        }
153    
154    
155        public void setJournalStore( JournalStore store )
156        {
157            this.store = store;
158        }
159    }