You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2016/05/25 19:35:31 UTC

[1/2] jclouds git commit: Allow to link contexts and views together

Repository: jclouds
Updated Branches:
  refs/heads/master b7ed4f34a -> 613290ea8


Allow to link contexts and views together

Some providers need other APIs or portable services to perform certain
operations. This change allows to pass existing Context and View
instances when creating a context, so they are added to the injector of
the context being built. This allows to inject external apis and
providers into an existing provider to leverage the external
functionality.


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/613290ea
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/613290ea
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/613290ea

Branch: refs/heads/master
Commit: 613290ea8d16f3744cd25384a464b63594565495
Parents: d05af22
Author: Ignasi Barrera <na...@apache.org>
Authored: Sat May 21 00:25:31 2016 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Wed May 25 21:14:26 2016 +0200

----------------------------------------------------------------------
 .../java/org/jclouds/config/ContextLinking.java | 78 ++++++++++++++++++++
 .../org/jclouds/config/ContextLinkingTest.java  | 69 +++++++++++++++++
 2 files changed, 147 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/613290ea/core/src/main/java/org/jclouds/config/ContextLinking.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/config/ContextLinking.java b/core/src/main/java/org/jclouds/config/ContextLinking.java
new file mode 100644
index 0000000..51f9a2f
--- /dev/null
+++ b/core/src/main/java/org/jclouds/config/ContextLinking.java
@@ -0,0 +1,78 @@
+/*
+ * 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.jclouds.config;
+
+import org.jclouds.Context;
+import org.jclouds.View;
+
+import com.google.common.base.Function;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.inject.AbstractModule;
+import com.google.inject.Module;
+import com.google.inject.TypeLiteral;
+import com.google.inject.name.Names;
+
+/**
+ * Utility methods to allow {@link Context} and {@link View} linking between
+ * contexts.
+ * <p>
+ * By using this module users can configure a context to be able to inject other
+ * contexts or views by their provider id.
+ */
+public class ContextLinking {
+
+   static final TypeLiteral<Supplier<Context>> CONTEXT_SUPPLIER = new TypeLiteral<Supplier<Context>>() {
+   };
+
+   static final TypeLiteral<Supplier<View>> VIEW_SUPPLIER = new TypeLiteral<Supplier<View>>() {
+   };
+
+   public static Module linkView(final String id, final Supplier<View> view) {
+      return new AbstractModule() {
+         @Override
+         protected void configure() {
+            bind(VIEW_SUPPLIER).annotatedWith(Names.named(id)).toInstance(view);
+            bind(CONTEXT_SUPPLIER).annotatedWith(Names.named(id)).toInstance(Suppliers.compose(ViewToContext, view));
+         }
+      };
+   }
+
+   public static Module linkContext(final String id, final Supplier<Context> context) {
+      return new AbstractModule() {
+         @Override
+         protected void configure() {
+            bind(CONTEXT_SUPPLIER).annotatedWith(Names.named(id)).toInstance(context);
+         }
+      };
+   }
+
+   public static Module linkView(View view) {
+      return linkView(view.unwrap().getId(), Suppliers.ofInstance(view));
+   }
+
+   public static Module linkContext(Context context) {
+      return linkContext(context.getId(), Suppliers.ofInstance(context));
+   }
+
+   private static final Function<View, Context> ViewToContext = new Function<View, Context>() {
+      @Override
+      public Context apply(View input) {
+         return input.unwrap();
+      }
+   };
+}

http://git-wip-us.apache.org/repos/asf/jclouds/blob/613290ea/core/src/test/java/org/jclouds/config/ContextLinkingTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/config/ContextLinkingTest.java b/core/src/test/java/org/jclouds/config/ContextLinkingTest.java
new file mode 100644
index 0000000..7d2f2a2
--- /dev/null
+++ b/core/src/test/java/org/jclouds/config/ContextLinkingTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.jclouds.config;
+
+import static org.jclouds.config.ContextLinking.CONTEXT_SUPPLIER;
+import static org.jclouds.config.ContextLinking.VIEW_SUPPLIER;
+import static org.jclouds.config.ContextLinking.linkContext;
+import static org.jclouds.config.ContextLinking.linkView;
+import static org.jclouds.providers.AnonymousProviderMetadata.forApiOnEndpoint;
+import static org.testng.Assert.assertNotNull;
+
+import java.io.Closeable;
+
+import org.jclouds.Context;
+import org.jclouds.ContextBuilder;
+import org.jclouds.http.IntegrationTestClient;
+import org.jclouds.internal.BaseView;
+import org.testng.annotations.Test;
+
+import com.google.common.reflect.TypeToken;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.name.Names;
+
+@Test(groups = "unit", testName = "ContextLinkingTest")
+public class ContextLinkingTest {
+
+   @Test
+   public void testLinkedViewBindsViewAndContextSuppliers() {
+      Injector injector = Guice.createInjector(linkView(new DummyView(contextFor(IntegrationTestClient.class))));
+
+      assertNotNull(injector.getExistingBinding(Key.get(CONTEXT_SUPPLIER, Names.named("IntegrationTestClient"))));
+      assertNotNull(injector.getExistingBinding(Key.get(VIEW_SUPPLIER, Names.named("IntegrationTestClient"))));
+   }
+
+   @Test
+   public void testLinkedContextBindsContextSupplier() {
+      Injector injector = Guice.createInjector(linkContext(contextFor(IntegrationTestClient.class)));
+
+      assertNotNull(injector.getExistingBinding(Key.get(CONTEXT_SUPPLIER, Names.named("IntegrationTestClient"))));
+   }
+
+   private static class DummyView extends BaseView {
+      protected DummyView(Context context) {
+         super(context, new TypeToken<Context>() {
+            private static final long serialVersionUID = 1L;
+         });
+      }
+   }
+
+   private static Context contextFor(Class<? extends Closeable> apiClass) {
+      return ContextBuilder.newBuilder(forApiOnEndpoint(apiClass, "http://localhost")).build();
+   }
+}


[2/2] jclouds git commit: Allow to override the Image and Security extension bindings

Posted by na...@apache.org.
Allow to override the Image and Security extension bindings

Some providers, such as Azure ARM, might need additional
resources present in order to be able to use the extension.
This change allows to override its binding (Guice 4 by default does not
allow to override provider bindings, so we need to define them as final
ones and provide an alternate method for subclasses that need to
override the binding).


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/d05af224
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/d05af224
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/d05af224

Branch: refs/heads/master
Commit: d05af2246b2252782b87f53332fc0a8a7313bbe1
Parents: b7ed4f3
Author: Ignasi Barrera <na...@apache.org>
Authored: Sat May 21 00:30:31 2016 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Wed May 25 21:14:26 2016 +0200

----------------------------------------------------------------------
 .../config/BaseComputeServiceContextModule.java       | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/d05af224/compute/src/main/java/org/jclouds/compute/config/BaseComputeServiceContextModule.java
----------------------------------------------------------------------
diff --git a/compute/src/main/java/org/jclouds/compute/config/BaseComputeServiceContextModule.java b/compute/src/main/java/org/jclouds/compute/config/BaseComputeServiceContextModule.java
index ab5fb0f..3e83617 100644
--- a/compute/src/main/java/org/jclouds/compute/config/BaseComputeServiceContextModule.java
+++ b/compute/src/main/java/org/jclouds/compute/config/BaseComputeServiceContextModule.java
@@ -304,16 +304,22 @@ public abstract class BaseComputeServiceContextModule extends AbstractModule {
    @Provides
    @Singleton
    public final Optional<ImageExtension> guiceProvideImageExtension(Injector i) {
-      Binding<ImageExtension> binding = i.getExistingBinding(Key.get(ImageExtension.class));
-      return binding == null ? Optional.<ImageExtension> absent() : Optional.of(binding.getProvider().get());
+      return provideImageExtension(i);
    }
 
    @Provides
    @Singleton
    protected final Optional<SecurityGroupExtension> guiceProvideSecurityGroupExtension(Injector i) {
+      return provideSecurityGroupExtension(i);
+   }
+
+   protected Optional<ImageExtension> provideImageExtension(Injector i) {
+      Binding<ImageExtension> binding = i.getExistingBinding(Key.get(ImageExtension.class));
+      return binding == null ? Optional.<ImageExtension> absent() : Optional.of(binding.getProvider().get());
+   }
+
+   protected Optional<SecurityGroupExtension> provideSecurityGroupExtension(Injector i) {
       Binding<SecurityGroupExtension> binding = i.getExistingBinding(Key.get(SecurityGroupExtension.class));
       return binding == null ? Optional.<SecurityGroupExtension> absent() : Optional.of(binding.getProvider().get());
    }
-   
-
 }