Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.whirr.util;
 
 import java.io.File;
 import java.net.URI;
 import java.util.Map;
 import java.util.Set;
 
 
 
 import static org.jclouds.blobstore.options.PutOptions.Builder.multipart;
 
 public class BlobCache {
 
   private static final Logger LOG = LoggerFactory.getLogger(BlobCache.class);
 
   private static Map<ClusterSpecBlobCacheinstances = Maps.newHashMap();
 
   static {
     /* Ensure that all created containers are removed when the JVM stops */
     Runtime.getRuntime().addShutdownHook(new Thread() {
       @Override
       public void run() {
         BlobCache.dropAndCloseAll();
       }
     });
   }
 
   public synchronized static BlobCache getInstance(Function<ClusterSpecComputeServiceContextgetCompute,
       ClusterSpec specthrows IOException {
     if (!.containsKey(spec)) {
       try {
         .put(spec.copy(), new BlobCache(getComputespec));
       } catch (ConfigurationException e) {
         throw new IOException(e);
       }
     }
     return .get(spec);
   }
 
   public synchronized static void dropAndCloseAll() {
     for(BlobCache instance : .values()) {
       instance.dropAndClose();
     }
     .clear();
   }
 
 
   String container = null;
   boolean temporary = true;
 
   Location defaultLocation = null;
 
   private BlobCache(Function<ClusterSpecComputeServiceContextgetCompute,
       ClusterSpec specthrows IOException {
     this. = getCompute;
     this. = BlobStoreContextBuilder.build(spec);
 
     if (spec.getBlobStoreCacheContainer() != null) {
       this. = spec.getBlobStoreCacheContainer();
       this. = false;
    }
  }
  public Location getLocation() {
    return ;
  }
  private void updateDefaultLocation(ClusterSpec specthrows IOException {
    if (spec.getBlobStoreLocationId() != null) {
      /* find the location with the given Id */
      for(Location loc : .getBlobStore().listAssignableLocations()) {
        if (loc.getId().equals(spec.getBlobStoreLocationId())) {
           = loc;
          break;
        }
      }
      if ( == null) {
        .warn("No blob store location found with this ID '{}'. " +
          "Using default location."spec.getBlobStoreLocationId());
      }
    } else if (spec.getLocationId() != null) {
      /* find the closest location to the compute nodes */
      ComputeServiceContext compute = .apply(spec);
      Set<StringcomputeIsoCodes = null;
      for(Location loc : compute.getComputeService().listAssignableLocations()) {
        if (loc.getId().equals(spec.getLocationId())) {
          computeIsoCodes = loc.getIso3166Codes();
          break;
        }
      }
      if (computeIsoCodes == null) {
        .warn("Invalid compute location ID '{}'. " +
          "Using default blob store location."spec.getLocationId());
      } else {
        for (Location loc : .getBlobStore().listAssignableLocations()) {
          if (containsAny(loc.getIso3166Codes(), computeIsoCodes)) {
             = loc;
            break;
          }
        }
      }
    }
  }
  private <T> boolean containsAny(Set<T> set1Set<T> set2) {
    for (T el : set1) {
      if (set2.contains(el)) {
        return true;
      }
    }
    return false;
  }
  public synchronized void putIfAbsent(String localUrithrows URISyntaxExceptionIOException {
    putIfAbsent(new URI(localUri));
  }
  public synchronized void putIfAbsent(URI urithrows IOException {
    try {
      putIfAbsent(new File(uri));
    } catch(FileNotFoundException e) {
      throw new IOException(e);
    }
  }
  public synchronized void putIfAbsent(File filethrows FileNotFoundException {
      allocateContainer();
      BlobStore store = .getBlobStore();
      if (!store.blobExists(file.getName())) {
        .info("Uploading '{}' to '{}' blob cache."file.getName(), );
        Blob blob = .getBlobStore().blobBuilder()
                .name(file.getName())
                .payload(file)
                .contentLength(file.length())
                .build();
        store.putBlob(blobmultipart());
      }
  }
  public synchronized Statement getAsSaveToStatement(String targetString namethrows IOException {
    HttpRequest req = getSignedRequest(name);
    return new SaveHttpResponseTo(targetnamereq.getMethod(), req.getEndpoint(), req.getHeaders());
  }
  public synchronized Statement getAsSaveToStatement(String targetURI urithrows IOException {
    return getAsSaveToStatement(targetnew File(uri).getName());
  }
  public synchronized HttpRequest getSignedRequest(String blobNamethrows IOException {
    checkExistsBlob(blobName);
    return .getSigner().signGetBlob(blobName);
  }
  public String getContainer() {
    return ;
  }
  private void checkExistsBlob(String namethrows IOException {
    if ( == null || !.getBlobStore().blobExists(name)) {
      throw new IOException("Blob not found: " +  + ":" + name);
    }
  }
  private void allocateContainer() {
    if ( == null) {
    }
  }
    String candidate;
    do {
      candidate = RandomStringUtils.randomAlphanumeric(12).toLowerCase();
    .info("Created blob cache container '{}' located in '{}'"candidate);
    return candidate;
  }
  public synchronized void dropAndClose() {
    if ( != null && ) {
      .info("Removing blob cache '{}'");
    }
    .close();
  }
New to GrepCode? Check out our FAQ X