1 18 package org.apache.activemq.kaha.impl.async; 19 20 import java.io.IOException ; 21 22 import org.apache.activemq.kaha.Marshaller; 23 import org.apache.activemq.kaha.StoreLocation; 24 import org.apache.activemq.kaha.impl.data.RedoListener; 25 import org.apache.activemq.util.ByteSequence; 26 import org.apache.activemq.util.DataByteArrayInputStream; 27 import org.apache.activemq.util.DataByteArrayOutputStream; 28 29 34 public final class DataManagerFacade implements org.apache.activemq.kaha.impl.DataManager { 35 36 private static class StoreLocationFacade implements StoreLocation { 37 private final Location location; 38 39 public StoreLocationFacade(Location location) { 40 this.location = location; 41 } 42 43 public int getFile() { 44 return location.getDataFileId(); 45 } 46 47 public long getOffset() { 48 return location.getOffset(); 49 } 50 51 public int getSize() { 52 return location.getSize(); 53 } 54 55 public Location getLocation() { 56 return location; 57 } 58 } 59 60 static private StoreLocation convertToStoreLocation(Location location) { 61 if(location==null) 62 return null; 63 return new StoreLocationFacade(location); 64 } 65 66 static private Location convertFromStoreLocation(StoreLocation location) { 67 68 if(location==null) 69 return null; 70 71 if( location.getClass()== StoreLocationFacade.class ) 72 return ((StoreLocationFacade)location).getLocation(); 73 74 Location l = new Location(); 75 l.setOffset((int) location.getOffset()); 76 l.setSize(location.getSize()); 77 l.setDataFileId(location.getFile()); 78 return l; 79 } 80 81 static final private ByteSequence FORCE_COMMAND = new ByteSequence(new byte[]{'F', 'O', 'R', 'C', 'E'}); 82 83 AsyncDataManager dataManager; 84 private final String name; 85 private Marshaller redoMarshaller; 86 87 88 public DataManagerFacade(AsyncDataManager dataManager, String name) { 89 this.dataManager=dataManager; 90 this.name = name; 91 } 92 93 public Object readItem(Marshaller marshaller, StoreLocation location) throws IOException { 94 ByteSequence sequence = dataManager.read(convertFromStoreLocation(location)); 95 DataByteArrayInputStream dataIn = new DataByteArrayInputStream(sequence); 96 return marshaller.readPayload(dataIn); 97 } 98 99 100 public StoreLocation storeDataItem(Marshaller marshaller, Object payload) throws IOException { 101 final DataByteArrayOutputStream buffer = new DataByteArrayOutputStream(); 102 marshaller.writePayload(payload,buffer); 103 ByteSequence data = buffer.toByteSequence(); 104 return convertToStoreLocation(dataManager.write(data, (byte)1, false)); 105 } 106 107 108 public void force() throws IOException { 109 dataManager.write(FORCE_COMMAND, (byte)2, true); 110 } 111 112 public void updateItem(StoreLocation location, Marshaller marshaller, Object payload) throws IOException { 113 final DataByteArrayOutputStream buffer = new DataByteArrayOutputStream(); 114 marshaller.writePayload(payload,buffer); 115 ByteSequence data = buffer.toByteSequence(); 116 dataManager.update(convertFromStoreLocation(location), data, false); 117 } 118 119 public void close() throws IOException { 120 dataManager.close(); 121 } 122 123 public void consolidateDataFiles() throws IOException { 124 dataManager.consolidateDataFiles(); 125 } 126 127 public boolean delete() throws IOException { 128 return dataManager.delete(); 129 } 130 131 public void addInterestInFile(int file) throws IOException { 132 dataManager.addInterestInFile(file); 133 } 134 public void removeInterestInFile(int file) throws IOException { 135 dataManager.removeInterestInFile(file); 136 } 137 138 public void recoverRedoItems(RedoListener listener) throws IOException { 139 throw new RuntimeException ("Not Implemented.."); 140 } 141 public StoreLocation storeRedoItem(Object payload) throws IOException { 142 throw new RuntimeException ("Not Implemented.."); 143 } 144 145 public Marshaller getRedoMarshaller() { 146 return redoMarshaller; 147 } 148 public void setRedoMarshaller(Marshaller redoMarshaller) { 149 this.redoMarshaller = redoMarshaller; 150 } 151 152 public String getName() { 153 return name; 154 } 155 156 157 } 158 | Popular Tags |