Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » async » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.kaha.impl.async;
   18   
   19   import java.io.IOException;
   20   
   21   import org.apache.activeio.journal.InvalidRecordLocationException;
   22   import org.apache.activeio.journal.Journal;
   23   import org.apache.activeio.journal.JournalEventListener;
   24   import org.apache.activeio.journal.RecordLocation;
   25   import org.apache.activeio.packet.ByteArrayPacket;
   26   import org.apache.activeio.packet.Packet;
   27   import org.apache.activemq.util.ByteSequence;
   28   
   29   /**
   30    * Provides a Journal Facade to the DataManager.
   31    * 
   32    * @version $Revision: 1.1.1.1 $
   33    */
   34   public final class JournalFacade implements Journal {
   35   
   36       private final AsyncDataManager dataManager;
   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       public JournalFacade(AsyncDataManager dataManager) {
   57           this.dataManager = dataManager;
   58       }
   59   
   60       private static RecordLocation convertToRecordLocation(Location location) {
   61           if (location == null) {
   62               return null;
   63           }
   64           return new RecordLocationFacade(location);
   65       }
   66   
   67       private static Location convertFromRecordLocation(RecordLocation location) {
   68   
   69           if (location == null) {
   70               return null;
   71           }
   72   
   73           return ((RecordLocationFacade)location).getLocation();
   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           }
   93           return new ByteArrayPacket(rc.getData(), rc.getOffset(), rc.getLength());
   94       }
   95   
   96       public void setJournalEventListener(JournalEventListener listener) throws IllegalStateException {
   97       }
   98   
   99       public void setMark(RecordLocation location, boolean sync) throws InvalidRecordLocationException, IOException, IllegalStateException {
  100           dataManager.setMark(convertFromRecordLocation(location), sync);
  101       }
  102   
  103       public RecordLocation write(Packet packet, boolean sync) throws IOException, IllegalStateException {
  104           org.apache.activeio.packet.ByteSequence data = packet.asByteSequence();
  105           ByteSequence sequence = new ByteSequence(data.getData(), data.getOffset(), data.getLength());
  106           return convertToRecordLocation(dataManager.write(sequence, sync));
  107       }
  108       
  109       public RecordLocation write(Packet packet, Runnable onComplete) throws IOException, IllegalStateException {
  110           org.apache.activeio.packet.ByteSequence data = packet.asByteSequence();
  111           ByteSequence sequence = new ByteSequence(data.getData(), data.getOffset(), data.getLength());
  112           return convertToRecordLocation(dataManager.write(sequence, onComplete));
  113       }
  114   
  115   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » async » [javadoc | source]