Package org.codehaus.activemq.journal.howl

Source Code of org.codehaus.activemq.journal.howl.HowlJournal

/**
*
* Copyright 2004 Hiram Chirino
*
* 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.codehaus.activemq.journal.howl;

import java.io.IOException;
import java.io.InterruptedIOException;

import org.codehaus.activemq.journal.InvalidRecordLocationException;
import org.codehaus.activemq.journal.Journal;
import org.codehaus.activemq.journal.JournalEventListener;
import org.codehaus.activemq.journal.RecordLocation;
import org.objectweb.howl.log.Configuration;
import org.objectweb.howl.log.InvalidFileSetException;
import org.objectweb.howl.log.InvalidLogBufferException;
import org.objectweb.howl.log.InvalidLogKeyException;
import org.objectweb.howl.log.LogConfigurationException;
import org.objectweb.howl.log.LogEventListener;
import org.objectweb.howl.log.Logger;

/**
* An implementation of the Journal interface using a HOWL logger.  This is is a thin
* wrapper around a HOWL logger.
*
* This implementation can be used to write records but not to retreive them
* yet. Once the HOWL logger implements the methods needed to retreive
* previously stored records, this class can be completed.
*
* @version $Revision: 1.1 $
*/
public class HowlJournal implements Journal {

  private final Logger logger;

  private RecordLocation lastMark;

  public HowlJournal(Configuration configuration)
      throws InvalidFileSetException, LogConfigurationException,
      InvalidLogBufferException, ClassNotFoundException, IOException,
      InterruptedException {
    this.logger = new Logger(configuration);
    this.logger.open();
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#write(byte[], boolean)
   */
  public RecordLocation write(byte[] data, boolean sync) throws IOException {
    try {
      return new LongRecordLocation(logger.put(data, sync));
    } catch (InterruptedException e) {
      throw (InterruptedIOException) new InterruptedIOException()
          .initCause(e);
    } catch (IOException e) {
      throw e;
    } catch (Exception e) {
      throw (IOException) new IOException("Journal write failed: " + e)
          .initCause(e);
    }
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#setMark(org.codehaus.activemq.journal.RecordLocation, boolean)
   */
  public void setMark(RecordLocation recordLocator, boolean force)
      throws InvalidRecordLocationException, IOException {
    try {
      if (recordLocator == null
          || recordLocator.getClass() != LongRecordLocation.class)
        throw new InvalidRecordLocationException();

      long location = ((LongRecordLocation) recordLocator)
          .getLongLocation();
      logger.mark(location, force);
      lastMark = recordLocator;

    } catch (InterruptedException e) {
      throw (InterruptedIOException) new InterruptedIOException()
          .initCause(e);
    } catch (IOException e) {
      throw e;
    } catch (InvalidLogKeyException e) {
      throw new InvalidRecordLocationException(e.getMessage(), e);
    } catch (Exception e) {
      throw (IOException) new IOException("Journal write failed: " + e)
          .initCause(e);
    }
  }
 
  /**
   * @see org.codehaus.activemq.journal.Journal#getMark()
   */
  public RecordLocation getMark() {
    // TODO: this does not work if the journal has
    // just been opened. Need to get the HOWL logger
    // to tell us what the last mark was.
    return lastMark;
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#close()
   */
  public void close() throws IOException {
    try {
      logger.close();
    } catch (IOException e) {
      throw e;
    } catch (InterruptedException e) {
      throw (InterruptedIOException) new InterruptedIOException()
          .initCause(e);
    } catch (Exception e) {
      throw (IOException) new IOException("Journal close failed: " + e)
          .initCause(e);
    }
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#setJournalEventListener(org.codehaus.activemq.journal.JournalEventListener)
   */
  public void setJournalEventListener(final JournalEventListener eventListener) {
    logger.setLogEventListener(new LogEventListener() {
      public void logOverflowNotification(long key) {
        eventListener.overflowNotification(new LongRecordLocation(key));
      }
    });
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#getNextRecordLocation(org.codehaus.activemq.journal.RecordLocation)
   */
  public RecordLocation getNextRecordLocation(RecordLocation lastLocation)
      throws InvalidRecordLocationException {
    //TODO: Need to figure out how to get HOWL to provided this.
    return null;
  }

  /**
   * @see org.codehaus.activemq.journal.Journal#read(org.codehaus.activemq.journal.RecordLocation)
   */
  public byte[] read(RecordLocation location)
      throws InvalidRecordLocationException, IOException {
    //TODO: Need to figure out how to get HOWL to provided this.
    return null;
  }

}
TOP

Related Classes of org.codehaus.activemq.journal.howl.HowlJournal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.