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 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> {
   private static final Logger LOG = LoggerFactory.getLogger(ComputeCache.class);
   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());        
         public ComputeServiceContext apply(Key arg0) {
           .debug("creating new ComputeServiceContext {}"arg0);
           .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() {
         public void run() {
           .debug("closing ComputeServiceContext {}"context);
    protected ComputeServiceContext delegate() {
       return ;
    public ComputeService getComputeService() {
      return delegate().getComputeService();
    public <S, A> RestContext<S, A> getProviderSpecificContext() {
      return delegate().getProviderSpecificContext();
    public Map<StringCredentialsgetCredentialStore() {
      return delegate().getCredentialStore();
    public Map<StringCredentialscredentialStore() {
      return delegate().credentialStore();
    public Utils getUtils() {
      return delegate().getUtils();
    public Utils utils() {
      return delegate().utils();
    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",

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>() {
        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",

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
       = ConfigurationConverter.getProperties(jcloudsConfig);
    public boolean equals(Object that) {
      if (that instanceof Key) {
        return Objects.equal(this., ((Key)that).)
          && Objects.equal(, ((Key)that).);
      return false;
    public int hashCode() {
      return Objects.hashCode();
  //patch until jclouds 1.0-beta-10
  private static class BindLoginCredentialsPatchForEC2 extends AbstractModule {
    protected void configure() {
New to GrepCode? Check out our FAQ X