Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » data » [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.data;
   18   
   19   import java.io.File;
   20   import java.io.FileNotFoundException;
   21   import java.io.IOException;
   22   import java.io.RandomAccessFile;
   23   
   24   /**
   25    * DataFile
   26    * 
   27    * @version $Revision: 1.1.1.1 $
   28    */
   29   class DataFile {
   30   
   31       private final File file;
   32       private final Integer number;
   33       private int referenceCount;
   34       private RandomAccessFile randomAcessFile;
   35       private Object writerData;
   36       private long length;
   37       private boolean dirty;
   38   
   39       DataFile(File file, int number) {
   40           this.file = file;
   41           this.number = Integer.valueOf(number);
   42           length = file.exists() ? file.length() : 0;
   43       }
   44   
   45       Integer getNumber() {
   46           return number;
   47       }
   48   
   49       synchronized RandomAccessFile getRandomAccessFile() throws FileNotFoundException {
   50           if (randomAcessFile == null) {
   51               randomAcessFile = new RandomAccessFile(file, "rw");
   52           }
   53           return randomAcessFile;
   54       }
   55   
   56       synchronized long getLength() {
   57           return length;
   58       }
   59   
   60       synchronized void incrementLength(int size) {
   61           length += size;
   62       }
   63   
   64       synchronized void purge() throws IOException {
   65           if (randomAcessFile != null) {
   66               randomAcessFile.close();
   67               randomAcessFile = null;
   68           }
   69       }
   70   
   71       synchronized boolean delete() throws IOException {
   72           purge();
   73           return file.delete();
   74       }
   75   
   76       synchronized void close() throws IOException {
   77           if (randomAcessFile != null) {
   78               randomAcessFile.close();
   79           }
   80       }
   81   
   82       synchronized int increment() {
   83           return ++referenceCount;
   84       }
   85   
   86       synchronized int decrement() {
   87           return --referenceCount;
   88       }
   89   
   90       synchronized boolean isUnused() {
   91           return referenceCount <= 0;
   92       }
   93   
   94       public String toString() {
   95           String result = file.getName() + " number = " + number + " , length = " + length + " refCount = " + referenceCount;
   96           return result;
   97       }
   98   
   99       /**
  100        * @return Opaque data that a DataFileWriter may want to associate with the
  101        *         DataFile.
  102        */
  103       public synchronized Object getWriterData() {
  104           return writerData;
  105       }
  106   
  107       /**
  108        * @param writerData - Opaque data that a DataFileWriter may want to
  109        *                associate with the DataFile.
  110        */
  111       public synchronized void setWriterData(Object writerData) {
  112           this.writerData = writerData;
  113           dirty = true;
  114       }
  115   
  116       public synchronized boolean isDirty() {
  117           return dirty;
  118       }
  119   
  120       public synchronized void setDirty(boolean value) {
  121           this.dirty = value;
  122       }
  123   
  124   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » kaha » impl » data » [javadoc | source]