HowlJournal xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.journal.howl;
    19
    20  import java.io.IOException;
    21  import java.io.InterruptedIOException;
    22
    23  import org.codehaus.activemq.journal.InvalidRecordLocationException;
    24  import org.codehaus.activemq.journal.Journal;
    25  import org.codehaus.activemq.journal.JournalEventListener;
    26  import org.codehaus.activemq.journal.RecordLocation;
    27  import org.objectweb.howl.log.Configuration;
    28  import org.objectweb.howl.log.InvalidFileSetException;
    29  import org.objectweb.howl.log.InvalidLogBufferException;
    30  import org.objectweb.howl.log.InvalidLogKeyException;
    31  import org.objectweb.howl.log.LogConfigurationException;
    32  import org.objectweb.howl.log.LogEventListener;
    33  import org.objectweb.howl.log.Logger;
    34
    35  /***
    36   * An implementation of the Journal interface using a HOWL logger.  This is is a thin
    37   * wrapper around a HOWL logger.
    38   *
    39   * This implementation can be used to write records but not to retreive them
    40   * yet. Once the HOWL logger implements the methods needed to retreive
    41   * previously stored records, this class can be completed.
    42   *
    43   * @version $Revision: 1.1 $
    44   */
    45  public class HowlJournal implements Journal {
    46
    47  	private final Logger logger;
    48
    49  	private RecordLocation lastMark;
    50
    51  	public HowlJournal(Configuration configuration)
    52  			throws InvalidFileSetException, LogConfigurationException,
    53  			InvalidLogBufferException, ClassNotFoundException, IOException,
    54  			InterruptedException {
    55  		this.logger = new Logger(configuration);
    56  		this.logger.open();
    57  	}
    58
    59  	/***
    60  	 * @see org.codehaus.activemq.journal.Journal#write(byte[], boolean)
    61  	 */
    62  	public RecordLocation write(byte[] data, boolean sync) throws IOException {
    63  		try {
    64  			return new LongRecordLocation(logger.put(data, sync));
    65  		} catch (InterruptedException e) {
    66  			throw (InterruptedIOException) new InterruptedIOException()
    67  					.initCause(e);
    68  		} catch (IOException e) {
    69  			throw e;
    70  		} catch (Exception e) {
    71  			throw (IOException) new IOException("Journal write failed: " + e)
    72  					.initCause(e);
    73  		}
    74  	}
    75
    76  	/***
    77  	 * @see org.codehaus.activemq.journal.Journal#setMark(org.codehaus.activemq.journal.RecordLocation, boolean)
    78  	 */
    79  	public void setMark(RecordLocation recordLocator, boolean force)
    80  			throws InvalidRecordLocationException, IOException {
    81  		try {
    82  			if (recordLocator == null
    83  					|| recordLocator.getClass() != LongRecordLocation.class)
    84  				throw new InvalidRecordLocationException();
    85
    86  			long location = ((LongRecordLocation) recordLocator)
    87  					.getLongLocation();
    88  			logger.mark(location, force);
    89  			lastMark = recordLocator;
    90
    91  		} catch (InterruptedException e) {
    92  			throw (InterruptedIOException) new InterruptedIOException()
    93  					.initCause(e);
    94  		} catch (IOException e) {
    95  			throw e;
    96  		} catch (InvalidLogKeyException e) {
    97  			throw new InvalidRecordLocationException(e.getMessage(), e);
    98  		} catch (Exception e) {
    99  			throw (IOException) new IOException("Journal write failed: " + e)
    100 					.initCause(e);
    101 		}
    102 	}
    103
    104 	/***
    105 	 * @see org.codehaus.activemq.journal.Journal#getMark()
    106 	 */
    107 	public RecordLocation getMark() {
    108 		// TODO: this does not work if the journal has
    109 		// just been opened. Need to get the HOWL logger
    110 		// to tell us what the last mark was.
    111 		return lastMark;
    112 	}
    113
    114 	/***
    115 	 * @see org.codehaus.activemq.journal.Journal#close()
    116 	 */
    117 	public void close() throws IOException {
    118 		try {
    119 			logger.close();
    120 		} catch (IOException e) {
    121 			throw e;
    122 		} catch (InterruptedException e) {
    123 			throw (InterruptedIOException) new InterruptedIOException()
    124 					.initCause(e);
    125 		} catch (Exception e) {
    126 			throw (IOException) new IOException("Journal close failed: " + e)
    127 					.initCause(e);
    128 		}
    129 	}
    130
    131 	/***
    132 	 * @see org.codehaus.activemq.journal.Journal#setJournalEventListener(org.codehaus.activemq.journal.JournalEventListener)
    133 	 */
    134 	public void setJournalEventListener(final JournalEventListener eventListener) {
    135 		logger.setLogEventListener(new LogEventListener() {
    136 			public void logOverflowNotification(long key) {
    137 				eventListener.overflowNotification(new LongRecordLocation(key));
    138 			}
    139 		});
    140 	}
    141
    142 	/***
    143 	 * @see org.codehaus.activemq.journal.Journal#getNextRecordLocation(org.codehaus.activemq.journal.RecordLocation)
    144 	 */
    145 	public RecordLocation getNextRecordLocation(RecordLocation lastLocation)
    146 			throws InvalidRecordLocationException {
    147 		//TODO: Need to figure out how to get HOWL to provided this.
    148 		return null;
    149 	}
    150
    151 	/***
    152 	 * @see org.codehaus.activemq.journal.Journal#read(org.codehaus.activemq.journal.RecordLocation)
    153 	 */
    154 	public byte[] read(RecordLocation location)
    155 			throws InvalidRecordLocationException, IOException {
    156 		//TODO: Need to figure out how to get HOWL to provided this.
    157 		return null;
    158 	}
    159
    160 }