1 18 package org.apache.activemq.kaha.impl.async; 19 20 import java.io.IOException ; 21 22 import org.apache.activeio.journal.InvalidRecordLocationException; 23 import org.apache.activeio.journal.Journal; 24 import org.apache.activeio.journal.JournalEventListener; 25 import org.apache.activeio.journal.RecordLocation; 26 import org.apache.activeio.packet.ByteArrayPacket; 27 import org.apache.activeio.packet.Packet; 28 import org.apache.activemq.util.ByteSequence; 29 30 35 public final class JournalFacade implements Journal { 36 37 38 public static class RecordLocationFacade implements RecordLocation { 39 private final Location location; 40 41 public RecordLocationFacade(Location location) { 42 this.location = location; 43 } 44 45 public Location getLocation() { 46 return location; 47 } 48 49 public int compareTo(Object o) { 50 RecordLocationFacade rlf = (RecordLocationFacade)o; 51 int rc = location.compareTo(rlf.location); 52 return rc; 53 } 54 } 55 56 static private RecordLocation convertToRecordLocation(Location location) { 57 if(location==null) 58 return null; 59 return new RecordLocationFacade(location); 60 } 61 62 static private Location convertFromRecordLocation(RecordLocation location) { 63 64 if(location==null) 65 return null; 66 67 return ((RecordLocationFacade)location).getLocation(); 68 } 69 70 AsyncDataManager dataManager; 71 72 public JournalFacade(AsyncDataManager dataManager) { 73 this.dataManager = dataManager; 74 } 75 76 public void close() throws IOException { 77 dataManager.close(); 78 } 79 80 public RecordLocation getMark() throws IllegalStateException { 81 return convertToRecordLocation(dataManager.getMark()); 82 } 83 84 public RecordLocation getNextRecordLocation(RecordLocation location) throws InvalidRecordLocationException, IOException , IllegalStateException { 85 return convertToRecordLocation(dataManager.getNextLocation(convertFromRecordLocation(location))); 86 } 87 88 public Packet read(RecordLocation location) throws InvalidRecordLocationException, IOException , IllegalStateException { 89 ByteSequence rc = dataManager.read(convertFromRecordLocation(location)); 90 if( rc == null ) 91 return null; 92 return new ByteArrayPacket(rc.getData(), rc.getOffset(), rc.getLength()); 93 } 94 95 public void setJournalEventListener(JournalEventListener listener) throws IllegalStateException { 96 } 97 98 public void setMark(RecordLocation location, boolean sync) throws InvalidRecordLocationException, IOException , IllegalStateException { 99 dataManager.setMark(convertFromRecordLocation(location), sync); 100 } 101 102 public RecordLocation write(Packet packet, boolean sync) throws IOException , IllegalStateException { 103 org.apache.activeio.packet.ByteSequence data = packet.asByteSequence(); 104 ByteSequence sequence = new ByteSequence(data.getData(), data.getOffset(), data.getLength()); 105 return convertToRecordLocation(dataManager.write(sequence, sync)); 106 } 107 108 } 109 | Popular Tags |