package de.msquadrat.blobwizard;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.NotImplementedException;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;

import io.dropwizard.lifecycle.Managed;

public class BlobStoreManager implements Managed {
    private static final Logger LOGGER = LoggerFactory.getLogger(BlobStoreManager.class);
    
    final Map<String, Store> stores = new HashMap<>();
    
    
    public BlobStoreManager(ServerConfiguration config) {
        for (Map.Entry<String, ServerConfiguration.Store> store : config.getStores().entrySet()) {
            stores.put(store.getKey(), new Store(store.getValue()));
        }
    }
    
    @Override
    public void start() throws Exception {
        for (Store store : stores.values()) {
            store.start();
        }
    }

    @Override
    public void stop() throws Exception {
        for (Store store : stores.values()) {
            store.start();
        }
    }
    
    public Store get(String name) {
        return stores.get(name);
    }

    
    public class Store implements Managed {
        private final BlobStoreContext context;

        public Store(ServerConfiguration.Store config) {
            Properties overrides = new Properties();
            for (Map.Entry<String, String> option : config.getOptions().entrySet()) {
                String name = "jclouds." + option.getKey();
                overrides.put(name, option.getValue());
            }

            Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());
            
            ContextBuilder builder = ContextBuilder.newBuilder(config.getApi())
                    .modules(modules)
                    .overrides(overrides);
            if (!Strings.isNullOrEmpty(config.getIdentity())) {
                builder.credentials(config.getIdentity(), config.getCredential());
            }
            context = builder.buildView(BlobStoreContext.class);
        }
        
        @Override
        public void start() throws Exception {
        }

        @Override
        public void stop() throws Exception {
            context.close();
        }
        
        @Deprecated
        private Optional<Object> notImplemented(String method, String container, String path) {
            LOGGER.info("{} {}:{}", method, container, path);
            throw new NotImplementedException(method);
        }
        
        public void put(String container, String path, Object data) {
            notImplemented("PUT", container, path);
        }
        
        public Optional<Object> get(String container, String path) {
            return notImplemented("GET", container, path);
        }
        
        public void delete(String container, String path) {
            notImplemented("DELETE", container, path);
        }
    }
}

@@ -713,16 +713,24 @@
    %0A    
+private 
 final Ma
@@ -768,24 +768,24 @@
 ashMap%3C%3E();%0A
-
     %0A    %0A  
@@ -955,33 +955,65 @@
     
-stores.put(store.getKey()
+String name = store.getKey();%0A            stores.put(name
 , ne
@@ -1020,16 +1020,22 @@
 w Store(
+name, 
 store.ge
@@ -1485,24 +1485,59 @@
 s Managed %7B%0A
+        private final String name;%0A
         priv
@@ -1594,16 +1594,29 @@
 c Store(
+String name, 
 ServerCo
@@ -1635,32 +1635,75 @@
 Store config) %7B%0A
+            this.name = name;%0A            %0A
             Prop
@@ -1848,20 +1848,19 @@
  String 
-name
+key
  = %22jclo
@@ -1914,20 +1914,19 @@
 des.put(
-name
+key
 , option
@@ -2520,32 +2520,83 @@
 ows Exception %7B%0A
+            LOGGER.info(%22Started store %7B%7D%22, name);%0A
         %7D%0A%0A     
@@ -2679,24 +2679,75 @@
 xt.close();%0A
+            LOGGER.info(%22Stopped store %7B%7D%22, name);%0A
         %7D%0A  
@@ -2900,16 +2900,19 @@
 (%22%7B%7D
+.%7B%7D
  %7B%7D:%7B%7D%22,
  met
@@ -2907,16 +2907,22 @@
  %7B%7D:%7B%7D%22,
+ name,
  method,
@@ -3259,18 +3259,18 @@
 mented(%22
-GE
+PU
 T%22, cont

Use named Stores