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.service;
 
 
 import java.util.Map;
 import java.util.Set;
 
A convenience class for building jclouds org.jclouds.compute.ComputeServiceContext objects.
 
 // singleton enum pattern
 public enum ComputeCache implements Function<ClusterSpecComputeServiceContext> {
    
 
   INSTANCE;
   
   private static final Logger LOG = LoggerFactory.getLogger(ComputeCache.class);
 
   @Override
   public ComputeServiceContext apply(ClusterSpec arg0) {
     return .get(new Key(arg0));
   }
   
   // this should prevent recreating the same compute context twice
       new Function<KeyComputeServiceContext>(){
         private final ComputeServiceContextFactory factory =  new ComputeServiceContextFactory();
         private final Set<AbstractModulewiring = ImmutableSet.of(
               new SshjSshClientModule(),
               new SLF4JLoggingModule(), 
               new EnterpriseConfigurationModule(),
               new BindLoginCredentialsPatchForEC2());        
         @Override
         public ComputeServiceContext apply(Key arg0) {
           .debug("creating new ComputeServiceContext {}"arg0);
             arg0.providerarg0.identityarg0.credential,
             arg0.overrides));
           .debug("created new ComputeServiceContext {}"context);
           return context;
         }
     
     }
   );
    
   private static class IgnoreCloseComputeServiceContext extends ForwardingObject implements ComputeServiceContext {
 
     private final ComputeServiceContext context;
 
     public IgnoreCloseComputeServiceContext(final ComputeServiceContext context) {
       this. = context;
       Runtime.getRuntime().addShutdownHook(new Thread() {
          @Override
         public void run() {
           .debug("closing ComputeServiceContext {}"context);
           context.close();
         }
       });
    }
    @Override
    protected ComputeServiceContext delegate() {
       return ;
    }
    @Override
    public ComputeService getComputeService() {
      return delegate().getComputeService();
    }
    @Override
    public <S, A> RestContext<S, A> getProviderSpecificContext() {
      return delegate().getProviderSpecificContext();
    }
    @Override
    public Map<StringCredentialsgetCredentialStore() {
      return delegate().getCredentialStore();
    }
    @Override
    public Map<StringCredentialscredentialStore() {
      return delegate().credentialStore();
    }
    @Override
    public Utils getUtils() {
      return delegate().getUtils();
    }
    @Override
    public Utils utils() {
      return delegate().utils();
    }
    @Override
    public void close() {
      /* Do nothing. The instance is closed by the builder */
    }
  }

  
All APIs that are independently configurable.

See also:
  public static final Iterable<StringCOMPUTE_APIS = ImmutableSet.of("stub""nova""vcloud""elasticstack",
      "eucalyptus""deltacloud""byon");

  
jclouds providers and apis that can be used in ComputeServiceContextFactory
  public static final Iterable<StringCOMPUTE_KEYS = Iterables.concat(
      Iterables.transform(Providers.allCompute(), new Function<ProviderMetadataString>() {
        @Override
        public String apply(ProviderMetadata input) {
          return input.getId();
        }
      }), );

  
configurable properties, scoped to a provider.
  public static final Iterable<StringPROVIDER_PROPERTIES = ImmutableSet.of("endpoint""api""apiversion",
      "iso3166-codes");

  
Key class for the compute context cache
  private static class Key {
    private String provider;
    private String identity;
    private String credential;
    private final String key;
    private final Properties overrides;
    public Key(ClusterSpec spec) {
       = spec.getProvider();
       = spec.getIdentity();
       = spec.getCredential();
       = String.format("%s-%s-%s");
      Configuration jcloudsConfig = spec.getConfigurationForKeysWithPrefix("jclouds");
      
      // jclouds configuration for providers are not prefixed with jclouds.
      for (String key : ) {
        for (String property : ) {
          String prefixedProperty = "jclouds." + key + "." + property;
          if (jcloudsConfig.containsKey(prefixedProperty))
            jcloudsConfig.setProperty(key + "." + property
                jcloudsConfig.getProperty(prefixedProperty));
        }
      }
       = ConfigurationConverter.getProperties(jcloudsConfig);
    }
    @Override
    public boolean equals(Object that) {
      if (that instanceof Key) {
        return Objects.equal(this., ((Key)that).)
          && Objects.equal(, ((Key)that).);
      }
      return false;
    }
    @Override
    public int hashCode() {
      return Objects.hashCode();
    }
  }
  
  //patch until jclouds 1.0-beta-10
  private static class BindLoginCredentialsPatchForEC2 extends AbstractModule {
    @Override
    protected void configure() {
    }
     
  }
New to GrepCode? Check out our FAQ X