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.activemq.kaha.Marshaller;
   22   import org.apache.activemq.kaha.StoreLocation;
   23   import org.apache.activemq.kaha.impl.data.RedoListener;
   24   import org.apache.activemq.util.ByteSequence;
   25   import org.apache.activemq.util.DataByteArrayInputStream;
   26   import org.apache.activemq.util.DataByteArrayOutputStream;
   27   
   28   /**
   29    * Provides a Kaha DataManager Facade to the DataManager.
   30    * 
   31    * @version $Revision: 1.1.1.1 $
   32    */
   33   public final class DataManagerFacade implements org.apache.activemq.kaha.impl.DataManager {
   34   
   35       private static final ByteSequence FORCE_COMMAND = new ByteSequence(new byte[] {'F', 'O', 'R', 'C', 'E'});
   36   
   37       private AsyncDataManager dataManager;
   38       private final String name;
   39       private Marshaller redoMarshaller;
   40   
   41       private static class StoreLocationFacade implements StoreLocation {
   42           private final Location location;
   43   
   44           public StoreLocationFacade(Location location) {
   45               this.location = location;
   46           }
   47   
   48           public int getFile() {
   49               return location.getDataFileId();
   50           }
   51   
   52           public long getOffset() {
   53               return location.getOffset();
   54           }
   55   
   56           public int getSize() {
   57               return location.getSize();
   58           }
   59   
   60           public Location getLocation() {
   61               return location;
   62           }
   63       }
   64   
   65       public DataManagerFacade(AsyncDataManager dataManager, String name) {
   66           this.dataManager = dataManager;
   67           this.name = name;
   68       }
   69   
   70       private static StoreLocation convertToStoreLocation(Location location) {
   71           if (location == null) {
   72               return null;
   73           }
   74           return new StoreLocationFacade(location);
   75       }
   76   
   77       private static Location convertFromStoreLocation(StoreLocation location) {
   78   
   79           if (location == null) {
   80               return null;
   81           }
   82   
   83           if (location.getClass() == StoreLocationFacade.class) {
   84               return ((StoreLocationFacade)location).getLocation();
   85           }
   86   
   87           Location l = new Location();
   88           l.setOffset((int)location.getOffset());
   89           l.setSize(location.getSize());
   90           l.setDataFileId(location.getFile());
   91           return l;
   92       }
   93   
   94   
   95       public Object readItem(Marshaller marshaller, StoreLocation location) throws IOException {
   96           ByteSequence sequence = dataManager.read(convertFromStoreLocation(location));
   97           DataByteArrayInputStream dataIn = new DataByteArrayInputStream(sequence);
   98           return marshaller.readPayload(dataIn);
   99       }
  100   
  101       public StoreLocation storeDataItem(Marshaller marshaller, Object payload) throws IOException {
  102           final DataByteArrayOutputStream buffer = new DataByteArrayOutputStream();
  103           marshaller.writePayload(payload, buffer);
  104           ByteSequence data = buffer.toByteSequence();
  105           return convertToStoreLocation(dataManager.write(data, (byte)1, false));
  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   
  135       public void removeInterestInFile(int file) throws IOException {
  136           dataManager.removeInterestInFile(file);
  137       }
  138   
  139       public void recoverRedoItems(RedoListener listener) throws IOException {
  140           throw new RuntimeException("Not Implemented..");
  141       }
  142   
  143       public StoreLocation storeRedoItem(Object payload) throws IOException {
  144           throw new RuntimeException("Not Implemented..");
  145       }
  146   
  147       public Marshaller getRedoMarshaller() {
  148           return redoMarshaller;
  149       }
  150   
  151       public void setRedoMarshaller(Marshaller redoMarshaller) {
  152           this.redoMarshaller = redoMarshaller;
  153       }
  154   
  155       public String getName() {
  156           return name;
  157       }
  158   
  159   }

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