protected BlobStore getBlobStore()

in commands/src/main/java/org/jclouds/karaf/commands/blobstore/BlobStoreCommandWithOptions.java [79:149]


   protected BlobStore getBlobStore() throws IOException {
      if ((name == null && provider == null && api == null) && (blobStoreServices != null && blobStoreServices.size() == 1)) {
         return blobStoreServices.get(0);
      }

      BlobStore blobStore = null;
      if (propertiesFile != null) {
         EnvHelper.loadProperties(new File(propertiesFile));
      }
      String providerValue = EnvHelper.getBlobStoreProvider(provider);
      String apiValue = EnvHelper.getBlobStoreApi(api);
      String identityValue = EnvHelper.getBlobStoreIdentity(identity);
      String credentialValue = EnvHelper.getBlobStoreCredential(providerValue, credential);
      String endpointValue = EnvHelper.getBlobStoreEndpoint(endpoint);
      boolean contextNameProvided = !Strings.isNullOrEmpty(name);
      boolean canCreateService = (!Strings.isNullOrEmpty(providerValue) || !Strings.isNullOrEmpty(apiValue))
               && !Strings.isNullOrEmpty(identityValue) && !Strings.isNullOrEmpty(credentialValue);

      String providerOrApiValue = !Strings.isNullOrEmpty(providerValue) ? providerValue : apiValue;

      try {
         blobStore = ServiceHelper.getService(name, providerOrApiValue, blobStoreServices);
      } catch (Throwable t) {
        if (contextNameProvided) {
          throw new RuntimeException("Could not find blobstore service with id:" + name);
        } else if (!canCreateService) {
            StringBuilder sb = new StringBuilder();
            sb.append("Insufficient information to create blobstore service:\n");
            if (providerOrApiValue == null) {
               sb.append("Missing provider or api." +
                     " Please specify the --provider / --api" +
                     " options, set the " + Constants.PROPERTY_PROVIDER +
                     " / " + Constants.PROPERTY_API + " properties" +
                     ", or set the JCLOUDS_BLOBSTORE_PROVIDER /" +
                     " JCLOUDS_BLOBSTORE_API environment variables.\n");
            }
            if (identityValue == null) {
               sb.append("Missing identity." +
                     " Please specify the --identity option" +
                     ", set the " + Constants.PROPERTY_IDENTITY +
                     " property, or set the " +
                     EnvHelper.JCLOUDS_BLOBSTORE_IDENTITY +
                     " environment variable.\n");
            }
            if (credentialValue == null) {
               sb.append("Missing credential." +
                     " Please specify the --credential option" +
                     ", set the " + Constants.PROPERTY_CREDENTIAL +
                     " property, or set the " +
                     EnvHelper.JCLOUDS_BLOBSTORE_CREDENTIAL +
                     " environment variable.\n");
            }
            throw new RuntimeException(sb.toString());
         }
      }
      if (blobStore == null && canCreateService) {
         try {
            ContextBuilder builder = ContextBuilder.newBuilder(providerOrApiValue)
                     .credentials(identityValue, credentialValue)
                     .modules(ImmutableSet.<Module> of(new Log4JLoggingModule()));
            if (!Strings.isNullOrEmpty(endpointValue)) {
               builder = builder.endpoint(endpointValue);
            }
            BlobStoreContext context = builder.build(BlobStoreContext.class);
            blobStore = context.getBlobStore();
         } catch (Exception ex) {
            throw new RuntimeException("Failed to create service: " + ex.getMessage(), ex);
         }
      }
      return blobStore;
   }