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.File;
   20   import java.io.FilenameFilter;
   21   import java.io.IOException;
   22   import java.util.ArrayList;
   23   import java.util.Collections;
   24   import java.util.List;
   25   
   26   import org.apache.activemq.thread.Scheduler;
   27   import org.apache.activemq.util.ByteSequence;
   28   import org.apache.commons.logging.Log;
   29   import org.apache.commons.logging.LogFactory;
   30   
   31   /**
   32    * An AsyncDataManager that works in read only mode against multiple data directories.
   33    * Useful for reading back archived data files.
   34    */
   35   public class ReadOnlyAsyncDataManager extends AsyncDataManager {
   36       
   37       private static final Log LOG = LogFactory.getLog(ReadOnlyAsyncDataManager.class);
   38       private final ArrayList<File> dirs;
   39   
   40       public ReadOnlyAsyncDataManager(final ArrayList<File> dirs) {
   41           this.dirs = dirs;
   42       }
   43   
   44       @SuppressWarnings("unchecked")
   45       public synchronized void start() throws IOException {
   46           if (started) {
   47               return;
   48           }
   49   
   50           started = true;
   51   
   52           accessorPool = new DataFileAccessorPool(this);
   53                   
   54           ArrayList<File> files = new ArrayList<File>();
   55           for (File directory : dirs) {
   56               final File d = directory;
   57               File[] f = d.listFiles(new FilenameFilter() {
   58                   public boolean accept(File dir, String n) {
   59                       return dir.equals(d) && n.startsWith(filePrefix);
   60                   }
   61               });
   62               for (int i = 0; i < f.length; i++) {
   63                   files.add(f[i]);
   64               }
   65           }
   66          
   67           for (File file : files) {
   68               try {
   69                   String n = file.getName();
   70                   String numStr = n.substring(filePrefix.length(), n.length());
   71                   int num = Integer.parseInt(numStr);
   72                   DataFile dataFile = new ReadOnlyDataFile(file, num, preferedFileLength);
   73                   fileMap.put(dataFile.getDataFileId(), dataFile);
   74                   storeSize.addAndGet(dataFile.getLength());
   75               } catch (NumberFormatException e) {
   76                   // Ignore file that do not match the pattern.
   77               }
   78           }
   79   
   80           // Sort the list so that we can link the DataFiles together in the
   81           // right order.
   82           List<DataFile> dataFiles = new ArrayList<DataFile>(fileMap.values());
   83           Collections.sort(dataFiles);
   84           currentWriteFile = null;
   85           for (DataFile df : dataFiles) {
   86               if (currentWriteFile != null) {
   87                   currentWriteFile.linkAfter(df);
   88               }
   89               currentWriteFile = df;
   90               fileByFileMap.put(df.getFile(), df);
   91           }
   92           
   93           // Need to check the current Write File to see if there was a partial
   94           // write to it.
   95           if (currentWriteFile != null) {
   96   
   97               // See if the lastSyncedLocation is valid..
   98               Location l = lastAppendLocation.get();
   99               if (l != null && l.getDataFileId() != currentWriteFile.getDataFileId().intValue()) {
  100                   l = null;
  101               }
  102           }
  103       }
  104       
  105       public synchronized void close() throws IOException {
  106           if (!started) {
  107               return;
  108           }
  109           accessorPool.close();
  110           fileMap.clear();
  111           fileByFileMap.clear();
  112           started = false;
  113       }
  114   
  115       
  116       public Location getFirstLocation() throws IllegalStateException, IOException {
  117           if( currentWriteFile == null ) {
  118               return null;
  119           }
  120           
  121           DataFile first = (DataFile)currentWriteFile.getHeadNode();
  122           Location cur = new Location();
  123           cur.setDataFileId(first.getDataFileId());
  124           cur.setOffset(0);
  125           cur.setSize(0);
  126           return getNextLocation(cur);
  127       }
  128       
  129       @Override
  130       public synchronized boolean delete() throws IOException {
  131           throw new RuntimeException("Cannot delete a ReadOnlyAsyncDataManager");
  132       }    
  133   }

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