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 */
019package org.apache.directory.server.core.journal;
020
021
022import java.io.IOException;
023
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.ldif.LdifEntry;
026import org.apache.directory.server.core.api.DirectoryService;
027import org.apache.directory.server.core.api.LdapPrincipal;
028import org.apache.directory.server.core.api.journal.Journal;
029import org.apache.directory.server.core.api.journal.JournalStore;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * The default journal implementation. It stores the operation and the
036 * associated status (acked or nacked) in a file which will be used to
037 * restore the server if it crashes.
038 * 
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class DefaultJournal implements Journal
042{
043    /** The class logger */
044    private static final Logger LOG = LoggerFactory.getLogger( DefaultJournal.class );
045
046    /** Tells if the service is activated or not */
047    private boolean enabled;
048
049    /** An instance of the Journal store */
050    private JournalStore store;
051
052    /** 
053     * A parameter indicating the number of operations stored in a journal
054     * before it is rotated. If set to 0, no rotation is done
055     */
056    private int rotation;
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public void destroy() throws LdapException
064    {
065        LOG.debug( "Stopping the journal" );
066
067        // We have to release the file, otherwise Windows won't be able
068        // to stop the server
069        if ( store != null )
070        {
071            try
072            {
073                store.destroy();
074            }
075            catch ( IOException ioe )
076            {
077                throw new LdapException( ioe.getMessage(), ioe );
078            }
079        }
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public JournalStore getJournalStore()
088    {
089        return store;
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void init( DirectoryService directoryService ) throws LdapException
098    {
099        LOG.debug( "Starting the journal" );
100
101        if ( store == null )
102        {
103            store = new DefaultJournalStore();
104        }
105
106        try
107        {
108            store.init( directoryService );
109        }
110        catch ( IOException ioe )
111        {
112            throw new LdapException( ioe.getMessage(), ioe );
113        }
114
115        LOG.debug( "The Journal service has been initialized" );
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public boolean isEnabled()
124    {
125        return enabled;
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public void log( LdapPrincipal principal, long revision, LdifEntry entry ) throws LdapException
134    {
135        store.log( principal, revision, entry );
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public void ack( long revision )
144    {
145        store.ack( revision );
146    }
147
148
149    /**
150     * {@inheritDoc}
151     */
152    @Override
153    public void nack( long revision )
154    {
155        store.nack( revision );
156    }
157
158
159    /**
160     * @return the rotation
161     */
162    @Override
163    public int getRotation()
164    {
165        return rotation;
166    }
167
168
169    /**
170     * @param rotation the rotation to set
171     */
172    @Override
173    public void setRotation( int rotation )
174    {
175        this.rotation = rotation;
176    }
177
178
179    @Override
180    public void setEnabled( boolean enabled )
181    {
182        this.enabled = enabled;
183    }
184
185
186    @Override
187    public void setJournalStore( JournalStore store )
188    {
189        this.store = store;
190    }
191}