View Javadoc

1   /* $HeadURL::                                                                            $
2    * $Id$
3    *
4    * Copyright (c) 2009-2010 DuraSpace
5    * http://duraspace.org
6    *
7    * In collaboration with Topaz Inc.
8    * http://www.topazproject.org
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  package org.akubraproject.fs;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  
28  import java.net.URI;
29  
30  import java.util.HashSet;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Set;
34  
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import org.akubraproject.Blob;
39  import org.akubraproject.BlobStore;
40  import org.akubraproject.impl.AbstractBlobStoreConnection;
41  import org.akubraproject.impl.StreamManager;
42  
43  /**
44   * Filesystem-backed BlobStoreConnection implementation.
45   *
46   * @author Chris Wilper
47   */
48  class FSBlobStoreConnection extends AbstractBlobStoreConnection {
49    private static final Logger log = LoggerFactory.getLogger(FSBlobStoreConnection.class);
50  
51    private final File baseDir;
52    private final Set<File>     modified;
53  
54    FSBlobStoreConnection(BlobStore blobStore, File baseDir, StreamManager manager, boolean noSync) {
55      super(blobStore, manager);
56      this.baseDir = baseDir;
57      this.modified = noSync ? null : new HashSet<File>();
58    }
59  
60    @Override
61    public Blob getBlob(URI blobId, Map<String, String> hints) throws IOException {
62      ensureOpen();
63  
64      if (blobId == null)
65        throw new UnsupportedOperationException();
66  
67      return new FSBlob(this, baseDir, blobId, streamManager, modified);
68    }
69  
70    @Override
71    public Iterator<URI> listBlobIds(String filterPrefix) {
72      ensureOpen();
73      return new FSBlobIdIterator(baseDir, filterPrefix);
74    }
75  
76    @Override
77    public void sync() throws IOException {
78      ensureOpen();
79  
80      if (modified == null)
81        throw new UnsupportedOperationException("You promised you weren't going to call sync!");
82  
83      for (File f : modified) {
84        try {
85          FileInputStream fis = new FileInputStream(f);
86          try {
87            fis.getFD().sync();
88          } finally {
89            fis.close();
90          }
91        } catch (IOException ioe) {
92          log.warn("Error sync'ing file '" + f + "'", ioe);
93        }
94      }
95  
96      modified.clear();
97    }
98  
99    @Override
100   public void close() {
101     if (modified != null)
102       modified.clear();
103 
104     super.close();
105   }
106 }