You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by io...@apache.org on 2013/05/21 20:58:21 UTC
[4/4] git commit: Add jclouds-representations project
Add jclouds-representations project
Conflicts:
pom.xml
Project: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/commit/7cac0ef1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/tree/7cac0ef1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/diff/7cac0ef1
Branch: refs/heads/1.6.x
Commit: 7cac0ef1ae061011151761402c2e9685652480d7
Parents: f8c115b
Author: Ioannis Canellos <io...@apache.org>
Authored: Fri Apr 5 00:10:05 2013 +0300
Committer: Ioannis Canellos <io...@apache.org>
Committed: Tue May 21 21:56:30 2013 +0300
----------------------------------------------------------------------
jclouds-management/README.md | 39 +++
jclouds-management/management-blobstore/pom.xml | 69 ++++
.../blobstore/management/BlobStoreManagement.java | 161 +++++++++
.../management/BlobStoreManagementMBean.java | 25 ++
.../management/BlobStoreViewMBeanFactory.java | 49 +++
.../org.jclouds.management.ViewMBeanFactory | 1 +
jclouds-management/management-compute/pom.xml | 70 ++++
.../management/ComputeServiceManagement.java | 117 +++++++
.../management/ComputeServiceManagementMBean.java | 24 ++
.../management/ComputeServiceViewMBeanFactory.java | 49 +++
.../org.jclouds.management.ViewMBeanFactory | 1 +
jclouds-management/management-core/pom.xml | 83 +++++
.../org/jclouds/management/JcloudsManagement.java | 128 +++++++
.../jclouds/management/JcloudsManagementMBean.java | 73 ++++
.../java/org/jclouds/management/ManagedBean.java | 32 ++
.../org/jclouds/management/ManagementContext.java | 91 +++++
.../java/org/jclouds/management/ViewMBean.java | 27 ++
.../org/jclouds/management/ViewMBeanFactories.java | 52 +++
.../org/jclouds/management/ViewMBeanFactory.java | 41 +++
.../management/ViewMBeanFactoryPredicates.java | 51 +++
.../management/config/ManagementLifecycle.java | 119 +++++++
.../management/internal/BaseManagementContext.java | 175 ++++++++++
.../management/internal/ManagementUtils.java | 90 +++++
.../org/jclouds/management/osgi/Activator.java | 102 ++++++
.../osgi/ManagementFactoryBundleListener.java | 123 +++++++
.../osgi/ViewManagementFactoryRegistry.java | 44 +++
.../jclouds/management/ComputeMBeanFactory.java | 56 +++
.../org/jclouds/management/ComputeManagement.java | 35 ++
.../jclouds/management/ComputeManagementMBean.java | 21 ++
.../jclouds/management/StorageMBeanFactory.java | 56 +++
.../org/jclouds/management/StorageManagement.java | 34 ++
.../jclouds/management/StorageManagementMBean.java | 21 ++
.../jclouds/management/ViewMBeanFactoriesTest.java | 62 ++++
.../management/config/ManagementLifecycleTest.java | 58 ++++
.../management/internal/ManagementUtilsTest.java | 89 +++++
.../org.jclouds.management.ViewMBeanFactory | 2 +
jclouds-management/pom.xml | 42 +++
jclouds-representations/README.md | 27 ++
jclouds-representations/pom.xml | 40 +++
.../representations-codec/pom.xml | 88 +++++
.../java/org/jclouds/blobstore/codec/ToBlob.java | 38 ++
.../jclouds/blobstore/codec/ToBlobMetadata.java | 39 +++
.../blobstore/codec/ToContentContentMetadata.java | 37 ++
.../jclouds/blobstore/codec/ToStorageMetadata.java | 37 ++
.../main/java/org/jclouds/codec/ToApiMetadata.java | 65 ++++
.../src/main/java/org/jclouds/codec/ToContext.java | 36 ++
.../main/java/org/jclouds/codec/ToLocation.java | 38 ++
.../main/java/org/jclouds/codec/ToProvider.java | 87 +++++
.../org/jclouds/compute/codec/ToExecResponse.java | 36 ++
.../java/org/jclouds/compute/codec/ToHardware.java | 47 +++
.../java/org/jclouds/compute/codec/ToImage.java | 39 +++
.../jclouds/compute/codec/ToLoginCredentials.java | 37 ++
.../org/jclouds/compute/codec/ToNodeMetadata.java | 41 +++
.../jclouds/compute/codec/ToOperatingSystem.java | 36 ++
.../org/jclouds/compute/codec/ToProcessor.java | 35 ++
.../java/org/jclouds/compute/codec/ToVolume.java | 36 ++
.../blobstore/codec/BlobStoreConversionsTest.java | 69 ++++
.../java/org/jclouds/codec/ToApiMetadataTest.java | 52 +++
.../test/java/org/jclouds/codec/ToContextTest.java | 39 +++
.../compute/codec/ComputeConversionsTest.java | 88 +++++
.../representations-core/pom.xml | 68 ++++
.../jclouds/blobstore/representations/Blob.java | 84 +++++
.../blobstore/representations/BlobMetadata.java | 216 ++++++++++++
.../blobstore/representations/BlobStore.java | 186 ++++++++++
.../blobstore/representations/ContentMetadata.java | 153 +++++++++
.../blobstore/representations/StorageMetadata.java | 181 ++++++++++
.../compute/representations/ComputeService.java | 123 +++++++
.../compute/representations/ExecResponse.java | 95 ++++++
.../jclouds/compute/representations/Hardware.java | 152 +++++++++
.../org/jclouds/compute/representations/Image.java | 162 +++++++++
.../compute/representations/LoginCredentials.java | 129 +++++++
.../compute/representations/NodeMetadata.java | 224 ++++++++++++
.../compute/representations/OperatingSystem.java | 133 ++++++++
.../jclouds/compute/representations/Processor.java | 84 +++++
.../jclouds/compute/representations/Volume.java | 134 ++++++++
.../org/jclouds/representations/ApiMetadata.java | 257 ++++++++++++++
.../java/org/jclouds/representations/Context.java | 96 ++++++
.../java/org/jclouds/representations/Location.java | 126 +++++++
.../jclouds/representations/ProviderMetadata.java | 262 +++++++++++++++
.../jclouds/representations/Representations.java | 35 ++
.../representations/StorageMetadataTest.java | 58 ++++
.../compute/representations/HardwareTest.java | 64 ++++
.../jclouds/compute/representations/ImageTest.java | 62 ++++
.../jclouds/compute/representations/NodeTest.java | 68 ++++
.../jclouds/representations/ApiMetadataTest.java | 70 ++++
.../org/jclouds/representations/ContextTest.java | 60 ++++
.../org/jclouds/representations/LocationTest.java | 63 ++++
.../representations/ProviderMetadataTest.java | 70 ++++
.../src/test/resources/ApiMetadata-stub.json | 34 ++
.../src/test/resources/Context-stub.json | 5 +
.../resources/Location-aws-ec2-eu-west-1a.json | 9 +
.../test/resources/ProviderMetadata-aws-ec2.json | 72 ++++
.../test/resources/blobstore/Blob-aws-s2-test.json | 47 +++
.../blobstore/StorageMetadata-aws-s3-repo.json | 9 +
.../resources/compute/Hardware-stub-large.json | 23 ++
.../test/resources/compute/Image-aws-5e7de437.json | 22 ++
.../src/test/resources/compute/Image-stub-1.json | 22 ++
.../src/test/resources/compute/Node-stub.json | 19 +
pom.xml | 2 +
rackspace-clouddns-uk/pom.xml | 6 +-
rackspace-clouddns-us/pom.xml | 6 +-
rackspace-clouddns/pom.xml | 6 +-
102 files changed, 7084 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/README.md
----------------------------------------------------------------------
diff --git a/jclouds-management/README.md b/jclouds-management/README.md
new file mode 100644
index 0000000..b7e70ba
--- /dev/null
+++ b/jclouds-management/README.md
@@ -0,0 +1,39 @@
+jclouds management
+=======================
+
+This project provides integration of jclouds with JMX.
+
+Jclouds Managed Beans
+---------------------
+
+The project provides 3 kind of MBeans:
+
+* **JcloudsManagementMBean** (single) Exposes Apis, Providers & Contexts. Additionally provides methods for creating Contexts.
+* **ComputeServiceManagementMBean** (per context) Exposes all ComputeService operations via JMX.
+* **BlobstoreManagementMBean** (per context) Exposes all Blobstore operations via JMX.
+
+
+The ManagementContext
+---------------------
+
+The ManagementContext is resoposible for keeping track of the MBeanServer, MBean and Contexts LifeCycle.
+The default implementation is the BaseManagementContext and it provides method for binding and unbinding an MBeanServer and also jclouds ManagedBean.
+
+Below is an example that create the ManagementContext and registers the core jclouds management bean.
+
+ ManagementContext managementContext = new BaseManagementContext();
+ JcloudsManagementMBean jcloudsManagement = new JcloudsManagement();
+ managementContext.manage(jcloudsManagement);
+
+This will expose via JMX the available Apis, Providers and Contexts. Also it will provide managed methods for creating a Context.
+When used inside OSGi the BaseManagementContext is registered as a service, JcloudsManagementMBean will be autoregistered and it will transparently track MBeanServer changes.
+
+The ManagementLifeCycle module
+-------------------------------
+
+The jclouds management project provides a guice module called ManagementLifecycle. This module can be passed to the ContextBuilder in order to expose mbeans for the created contexts.
+
+ ManagmenetContext managementContext = new BaseManagementContext();
+ ContextBuilder.newBuilder(providerOrApi).modules(ImmutableSet.of(new ManagementLifecycle(managementContext)).build();
+
+
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-blobstore/pom.xml
----------------------------------------------------------------------
diff --git a/jclouds-management/management-blobstore/pom.xml b/jclouds-management/management-blobstore/pom.xml
new file mode 100644
index 0000000..2543aed
--- /dev/null
+++ b/jclouds-management/management-blobstore/pom.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>jclouds-management</artifactId>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <version>1.6.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.jclouds.labs.management</groupId>
+ <artifactId>management-blobstore</artifactId>
+ <packaging>bundle</packaging>
+ <name>jclouds :: management :: blobstore</name>
+
+ <properties>
+ <jclouds.osgi.import>
+ *
+ </jclouds.osgi.import>
+ <jclouds.osgi.export>
+ org.jclouds.management.blobstore*;version="${project.version}";-noimport:=true
+ </jclouds.osgi.export>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.jclouds</groupId>
+ <artifactId>jclouds-blobstore</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.jclouds.labs.representations</groupId>
+ <artifactId>representations-codec</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.jclouds.labs.management</groupId>
+ <artifactId>management-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>14.0.1</version>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagement.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagement.java b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagement.java
new file mode 100644
index 0000000..2290efb
--- /dev/null
+++ b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagement.java
@@ -0,0 +1,161 @@
+/*
+ * 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.blobstore.management;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableSet;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.blobstore.codec.ToBlob;
+import org.jclouds.blobstore.codec.ToBlobMetadata;
+import org.jclouds.blobstore.codec.ToStorageMetadata;
+import org.jclouds.blobstore.options.ListContainerOptions;
+import org.jclouds.codec.ToLocation;
+import org.jclouds.management.ViewMBean;
+import org.jclouds.representations.Location;
+import org.jclouds.blobstore.representations.Blob;
+import org.jclouds.blobstore.representations.BlobMetadata;
+import org.jclouds.blobstore.representations.StorageMetadata;
+import org.jclouds.javax.annotation.Nullable;
+
+import java.util.Set;
+
+import static com.google.common.collect.Iterables.transform;
+import static com.google.common.collect.Iterables.tryFind;
+
+public class BlobStoreManagement implements BlobStoreManagementMBean, ViewMBean<BlobStoreContext> {
+
+ private final BlobStore blobStore;
+
+ public BlobStoreManagement(BlobStoreContext context) {
+ this.blobStore = context.getBlobStore();
+ }
+
+ @Override
+ public Set<Location> listAssignableLocations() {
+ return ImmutableSet.<Location>builder()
+ .addAll(transform(blobStore.listAssignableLocations(), ToLocation.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Set<StorageMetadata> list() {
+ return ImmutableSet.<StorageMetadata>builder()
+ .addAll(transform(blobStore.list(), ToStorageMetadata.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Set<StorageMetadata> list(String container) {
+ return ImmutableSet.<StorageMetadata>builder()
+ .addAll(transform(blobStore.list(container), ToStorageMetadata.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Set<StorageMetadata> list(String container, String directory) {
+ return ImmutableSet.<StorageMetadata>builder()
+ .addAll(transform(blobStore.list(container, ListContainerOptions.Builder.inDirectory(directory)), ToStorageMetadata.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public BlobMetadata blobMetadata(String container, String name) {
+ return ToBlobMetadata.INSTANCE.apply(blobStore.blobMetadata(container, name));
+ }
+
+ @Override
+ public Blob getBlob(String container, String name) {
+ return ToBlob.INSTANCE.apply(blobStore.getBlob(container, name));
+ }
+
+ @Override
+ public boolean containerExists(String container) {
+ return blobStore.containerExists(container);
+ }
+
+ @Override
+ public boolean createContainerInLocation(@Nullable String locationId, String container) {
+ Optional<? extends org.jclouds.domain.Location> location = tryFind(blobStore.listAssignableLocations(), new LocationPredicate(locationId));
+
+ if (location.isPresent()) {
+ return blobStore.createContainerInLocation(location.get(), container);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public void clearContainer(String container) {
+ blobStore.clearContainer(container);
+ }
+
+ @Override
+ public void deleteContainer(String container) {
+ blobStore.deleteContainer(container);
+ }
+
+ @Override
+ public boolean directoryExists(String container, String directory) {
+ return blobStore.directoryExists(container, directory);
+ }
+
+ @Override
+ public void createDirectory(String container, String directory) {
+ blobStore.createDirectory(container, directory);
+ }
+
+ @Override
+ public void deleteDirectory(String containerName, String name) {
+ blobStore.deleteDirectory(containerName, name);
+ }
+
+ @Override
+ public boolean blobExists(String container, String name) {
+ return blobStore.blobExists(container, name);
+ }
+
+ @Override
+ public void removeBlob(String container, String name) {
+ blobStore.removeBlob(container, name);
+ }
+
+ @Override
+ public long countBlobs(String container) {
+ return blobStore.countBlobs(container);
+ }
+
+ @Override
+ public String getType() {
+ return "blobstore";
+ }
+
+ private static final class LocationPredicate implements Predicate<org.jclouds.domain.Location> {
+ private final String id;
+
+ private LocationPredicate(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public boolean apply(@Nullable org.jclouds.domain.Location input) {
+ return input.getId().equals(id);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagementMBean.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagementMBean.java b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagementMBean.java
new file mode 100644
index 0000000..edb00bf
--- /dev/null
+++ b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreManagementMBean.java
@@ -0,0 +1,25 @@
+/*
+ * 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.blobstore.management;
+
+import org.jclouds.blobstore.representations.BlobStore;
+
+public interface BlobStoreManagementMBean extends BlobStore {
+
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreViewMBeanFactory.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreViewMBeanFactory.java b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreViewMBeanFactory.java
new file mode 100644
index 0000000..2ce9b13
--- /dev/null
+++ b/jclouds-management/management-blobstore/src/main/java/org/jclouds/blobstore/management/BlobStoreViewMBeanFactory.java
@@ -0,0 +1,49 @@
+/*
+ * 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.blobstore.management;
+
+import com.google.common.reflect.TypeToken;
+import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.management.ViewMBean;
+import org.jclouds.management.ViewMBeanFactory;
+
+public class BlobStoreViewMBeanFactory implements ViewMBeanFactory<BlobStoreContext> {
+
+ private static final TypeToken<BlobStoreContext> TYPE = TypeToken.of(BlobStoreContext.class);
+
+ /**
+ * Creates a {@link org.jclouds.management.ManagedBean} for the Context.
+ *
+ * @param view
+ * @return
+ */
+ @Override
+ public ViewMBean<BlobStoreContext> create(BlobStoreContext view) {
+ return new BlobStoreManagement(view);
+ }
+
+ /**
+ * Returns the {@link com.google.common.reflect.TypeToken} of the {@link org.jclouds.View}.
+ *
+ * @return
+ */
+ @Override
+ public TypeToken<BlobStoreContext> getViewType() {
+ return TYPE;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-blobstore/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
----------------------------------------------------------------------
diff --git a/jclouds-management/management-blobstore/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory b/jclouds-management/management-blobstore/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
new file mode 100644
index 0000000..6e360fb
--- /dev/null
+++ b/jclouds-management/management-blobstore/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
@@ -0,0 +1 @@
+org.jclouds.blobstore.management.BlobStoreViewMBeanFactory
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-compute/pom.xml
----------------------------------------------------------------------
diff --git a/jclouds-management/management-compute/pom.xml b/jclouds-management/management-compute/pom.xml
new file mode 100644
index 0000000..6c5f385
--- /dev/null
+++ b/jclouds-management/management-compute/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>jclouds-management</artifactId>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <version>1.6.1-SNAPSHOT</version>
+ <relativePath>..</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.jclouds.labs.management</groupId>
+ <artifactId>management-compute</artifactId>
+ <packaging>bundle</packaging>
+ <name>jclouds :: management :: compute</name>
+
+ <properties>
+ <jclouds.osgi.import>
+ *
+ </jclouds.osgi.import>
+ <jclouds.osgi.export>
+ org.jclouds.management.compute*;version="${project.version}";-noimport:=true
+ </jclouds.osgi.export>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.jclouds</groupId>
+ <artifactId>jclouds-compute</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.jclouds.labs.representations</groupId>
+ <artifactId>representations-codec</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.jclouds.labs.management</groupId>
+ <artifactId>management-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>14.0.1</version>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagement.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagement.java b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagement.java
new file mode 100644
index 0000000..5139820
--- /dev/null
+++ b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagement.java
@@ -0,0 +1,117 @@
+/*
+ * 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.compute.management;
+
+
+import com.google.common.collect.ImmutableSet;
+import org.jclouds.codec.ToLocation;
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.ComputeServiceContext;
+import org.jclouds.compute.codec.ToExecResponse;
+import org.jclouds.compute.codec.ToHardware;
+import org.jclouds.compute.codec.ToImage;
+import org.jclouds.compute.codec.ToNodeMetadata;
+import org.jclouds.compute.representations.ExecResponse;
+import org.jclouds.compute.representations.NodeMetadata;
+import org.jclouds.management.ViewMBean;
+import org.jclouds.representations.Location;
+import org.jclouds.compute.representations.Hardware;
+import org.jclouds.compute.representations.Image;
+
+import java.util.Set;
+
+import static com.google.common.collect.Iterables.transform;
+
+public class ComputeServiceManagement implements ComputeServiceManagementMBean, ViewMBean<ComputeServiceContext> {
+
+ private final ComputeService service;
+
+ public ComputeServiceManagement(ComputeServiceContext context) {
+ this.service = context.getComputeService();
+ }
+
+ @Override
+ public Set<Hardware> listHardwareProfiles() {
+ return ImmutableSet.<Hardware>builder()
+ .addAll(transform(service.listHardwareProfiles(), ToHardware.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Set<Image> listImages() {
+ return ImmutableSet.<Image>builder()
+ .addAll(transform(service.listImages(), ToImage.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Image getImage(String id) {
+ return ToImage.INSTANCE.apply(service.getImage(id));
+ }
+
+ @Override
+ public Set<NodeMetadata> listNodes() {
+ return ImmutableSet.<NodeMetadata>builder()
+ .addAll(transform( (Set<org.jclouds.compute.domain.NodeMetadata>) service.listNodes(), ToNodeMetadata.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Set<Location> listAssignableLocations() {
+ return ImmutableSet.<Location>builder()
+ .addAll(transform(service.listAssignableLocations(), ToLocation.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public void resumeNode(String id) {
+ service.resumeNode(id);
+ }
+
+ @Override
+ public void suspendNode(String id) {
+ service.suspendNode(id);
+ }
+
+
+ @Override
+ public void destroyNode(String id) {
+ service.destroyNode(id);
+ }
+
+
+ @Override
+ public void rebootNode(String id) {
+ service.destroyNode(id);
+ }
+
+ @Override
+ public NodeMetadata getNode(String id) {
+ return ToNodeMetadata.INSTANCE.apply(service.getNodeMetadata(id));
+ }
+
+ @Override
+ public ExecResponse runScriptOnNode(String id, String runScript) {
+ return ToExecResponse.INSTANCE.apply(service.runScriptOnNode(id, runScript));
+ }
+
+ @Override
+ public String getType() {
+ return "compute";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagementMBean.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagementMBean.java b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagementMBean.java
new file mode 100644
index 0000000..351adda
--- /dev/null
+++ b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceManagementMBean.java
@@ -0,0 +1,24 @@
+/*
+ * 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.compute.management;
+
+import org.jclouds.compute.representations.ComputeService;
+
+public interface ComputeServiceManagementMBean extends ComputeService {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceViewMBeanFactory.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceViewMBeanFactory.java b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceViewMBeanFactory.java
new file mode 100644
index 0000000..43c68eb
--- /dev/null
+++ b/jclouds-management/management-compute/src/main/java/org/jclouds/compute/management/ComputeServiceViewMBeanFactory.java
@@ -0,0 +1,49 @@
+/*
+ * 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.compute.management;
+
+import com.google.common.reflect.TypeToken;
+import org.jclouds.compute.ComputeServiceContext;
+import org.jclouds.management.ViewMBean;
+import org.jclouds.management.ViewMBeanFactory;
+
+
+public class ComputeServiceViewMBeanFactory implements ViewMBeanFactory<ComputeServiceContext> {
+
+ private static final TypeToken<ComputeServiceContext> TYPE = TypeToken.of(ComputeServiceContext.class);
+ /**
+ * Creates a {@link org.jclouds.management.ManagedBean} for the Context.
+ *
+ * @param context
+ * @return
+ */
+ @Override
+ public ViewMBean<ComputeServiceContext> create(ComputeServiceContext context) {
+ return new ComputeServiceManagement(context);
+ }
+
+ /**
+ * Returns the {@link com.google.common.reflect.TypeToken} of the {@link org.jclouds.View}.
+ *
+ * @return
+ */
+ @Override
+ public TypeToken<ComputeServiceContext> getViewType() {
+ return TYPE;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-compute/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
----------------------------------------------------------------------
diff --git a/jclouds-management/management-compute/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory b/jclouds-management/management-compute/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
new file mode 100644
index 0000000..6dde9d4
--- /dev/null
+++ b/jclouds-management/management-compute/src/main/resources/META-INF/services/org.jclouds.management.ViewMBeanFactory
@@ -0,0 +1 @@
+org.jclouds.compute.management.ComputeServiceViewMBeanFactory
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/pom.xml
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/pom.xml b/jclouds-management/management-core/pom.xml
new file mode 100644
index 0000000..36a4c3f
--- /dev/null
+++ b/jclouds-management/management-core/pom.xml
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>jclouds-management</artifactId>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <version>1.6.1-SNAPSHOT</version>
+ <relativePath>..</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.jclouds.labs.management</groupId>
+ <artifactId>management-core</artifactId>
+ <packaging>bundle</packaging>
+ <name>jclouds :: management :: core</name>
+
+ <properties>
+ <jclouds.osgi.import>
+ *
+ </jclouds.osgi.import>
+ <jclouds.osgi.export>
+ org.jclouds.management*;version="${project.version}";-noimport:=true
+ </jclouds.osgi.export>
+ <jclouds.osgi.activator>org.jclouds.management.osgi.Activator</jclouds.osgi.activator>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.jclouds</groupId>
+ <artifactId>jclouds-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs.representations</groupId>
+ <artifactId>representations-codec</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>14.0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>4.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- Testing Dependencies -->
+ <dependency>
+ <groupId>org.apache.jclouds</groupId>
+ <artifactId>jclouds-core</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagement.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagement.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagement.java
new file mode 100644
index 0000000..15e3921
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagement.java
@@ -0,0 +1,128 @@
+/*
+ * 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.management;
+
+import com.google.common.base.Charsets;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.io.Closeables;
+import org.jclouds.ContextBuilder;
+import org.jclouds.apis.Apis;
+import org.jclouds.codec.ToApiMetadata;
+import org.jclouds.codec.ToContext;
+import org.jclouds.codec.ToProvider;
+import org.jclouds.javax.annotation.Nullable;
+import org.jclouds.management.config.ManagementLifecycle;
+import org.jclouds.management.internal.BaseManagementContext;
+import org.jclouds.providers.Providers;
+import org.jclouds.representations.ApiMetadata;
+import org.jclouds.representations.Context;
+import org.jclouds.representations.ProviderMetadata;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import static com.google.common.collect.Iterables.transform;
+
+/**
+ * Core Jclouds MBean for displaying available {@link org.jclouds.representations.ApiMetadata}, {@link org.jclouds.representations.ProviderMetadata} and {@link org.jclouds.representations.Context}s.
+ * Also useful for creating contexts.
+ */
+public class JcloudsManagement implements JcloudsManagementMBean, ManagedBean {
+
+ private final ManagementContext managementContext;
+
+ public JcloudsManagement() {
+ this(BaseManagementContext.INSTANCE);
+ }
+
+ public JcloudsManagement(ManagementContext managementContext) {
+ this.managementContext = managementContext;
+ }
+
+ @Override
+ public Iterable<ApiMetadata> getApis() {
+ return ImmutableSet.<ApiMetadata>builder()
+ .addAll(transform(Apis.all(), ToApiMetadata.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public ApiMetadata findApiById(String id) {
+ return ToApiMetadata.INSTANCE.apply(Apis.withId(id));
+ }
+
+ @Override
+ public Iterable<ProviderMetadata> getProviders() {
+ return ImmutableSet.<ProviderMetadata>builder()
+ .addAll(transform(Providers.all(), ToProvider.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public ProviderMetadata findProviderById(String id) {
+ return ToProvider.INSTANCE.apply(Providers.withId(id));
+ }
+
+ @Override
+ public Iterable<Context> getContexts() {
+ return ImmutableSet.<Context>builder()
+ .addAll(transform(managementContext.listContexts(), ToContext.INSTANCE))
+ .build();
+ }
+
+ @Override
+ public Context createContext(String id, String name, String identity, String credential, String endpoint, String overrides) throws IOException {
+ return ToContext.INSTANCE.apply(
+ ContextBuilder.newBuilder(id).name(name).credentials(identity, credential).endpoint(endpoint)
+ .modules(ImmutableSet.of(new ManagementLifecycle(BaseManagementContext.INSTANCE)))
+ .overrides(stringToProperties(overrides))
+ .build()
+ );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getType() {
+ return "management";
+ }
+
+ /**
+ * Parses the String into a {@link Properties} object.
+ * The String is expected to separated key from valus using the '=' sign and key/value pairs with a new line.
+ * @param input
+ * @return
+ * @throws IOException
+ */
+ private static Properties stringToProperties(@Nullable String input) throws IOException {
+ Properties properties = new Properties();
+ if (!Strings.isNullOrEmpty(input)) {
+ ByteArrayInputStream bis = null;
+ try {
+ bis = new ByteArrayInputStream(input.getBytes(Charsets.UTF_8));
+ properties.load(bis);
+ } finally {
+ Closeables.close(bis, true);
+ }
+ }
+ return properties;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagementMBean.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagementMBean.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagementMBean.java
new file mode 100644
index 0000000..436c6bb
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/JcloudsManagementMBean.java
@@ -0,0 +1,73 @@
+/*
+ * 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.management;
+
+
+import org.jclouds.representations.ApiMetadata;
+import org.jclouds.representations.Context;
+import org.jclouds.representations.ProviderMetadata;
+
+import java.io.IOException;
+
+
+public interface JcloudsManagementMBean {
+
+ /**
+ * Lists all available {@link org.jclouds.representations.ApiMetadata}.
+ * @return
+ */
+ Iterable<ApiMetadata> getApis();
+
+ /**
+ * Find {@link org.jclouds.representations.ApiMetadata} by id.
+ * @return
+ */
+ ApiMetadata findApiById(String id);
+
+
+ /**
+ * Lists all available {@link org.jclouds.representations.ProviderMetadata}
+ * @return
+ */
+ Iterable<ProviderMetadata> getProviders();
+
+
+ /**
+ * Find {@link org.jclouds.representations.ProviderMetadata} by id.
+ * @return
+ */
+ ProviderMetadata findProviderById(String id);
+
+ /**
+ * Lists all {@link org.jclouds.representations.Context} objects.
+ * @return
+ */
+ Iterable<Context> getContexts();
+
+ /**
+ * Creates a {@link org.jclouds.representations.Context}.
+ * @param id
+ * @param name
+ * @param identity
+ * @param credential
+ * @param endpoint
+ * @param overrides The override properties as a list of new line separated key value pairs. Key/Values are separated by the equals sign.
+ * @return
+ */
+ Context createContext(String id, String name, String identity, String credential, String endpoint, String overrides) throws IOException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagedBean.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagedBean.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagedBean.java
new file mode 100644
index 0000000..dba7225
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagedBean.java
@@ -0,0 +1,32 @@
+/*
+ * 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.management;
+
+/**
+ * An interface that describes jclouds managed beans.
+ * It's used for generating the {@link javax.management.ObjectName}.
+ */
+public interface ManagedBean {
+
+ /**
+ * Returns the type of the MBean.
+ * @return
+ */
+ String getType();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagementContext.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagementContext.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagementContext.java
new file mode 100644
index 0000000..53d5290
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ManagementContext.java
@@ -0,0 +1,91 @@
+/*
+ * 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.management;
+
+import org.jclouds.Context;
+import org.jclouds.View;
+
+import javax.management.MBeanServer;
+
+/**
+ * The management context, keeps track of the {@link ManagedBean} objects that have been created.
+ * It is responsible for exporting beans to the {@link javax.management.MBeanServer}, whenever it becomes available.
+ * It also keeps track of {@link View}s created, so that they can be accessed via JMX.
+ */
+public interface ManagementContext {
+
+ /**
+ * Register a {@link ManagedBean} to the MBeanServer.
+ * @param mBean The ManagedBean to add to the context.
+ * @param name The name under which the bean will be exposed.
+ */
+ void manage(ManagedBean mBean, String name);
+
+ /**
+ * Un-registers a {@link ManagedBean} to the MBeanServer.
+ * @param mBean The ManagedBean to remove from the context.
+ * @param name The name under which the bean was exposed.
+ */
+ void unmanage(ManagedBean mBean, String name);
+
+
+ /**
+ * Bind an {@link javax.management.MBeanServer} to the context.
+ * This is mostly useful for dynamic environments where an {@link javax.management.MBeanServer} may come and go.
+ * The context should re-register the {@link ManagedBean} objects that have been added to the context.
+ * @param mBeanServer
+ */
+ void bind(MBeanServer mBeanServer);
+
+ /**
+ * Unbind an {@link javax.management.MBeanServer} to the context.
+ * This is mostly useful for dynamic environments where an {@link javax.management.MBeanServer} may come and go.
+ * The context should unregister the {@link ManagedBean} objects that have been added to the context.
+ * @param mBeanServer
+ */
+ void unbind(MBeanServer mBeanServer);
+
+ /**
+ * Register {@link org.jclouds.View}.
+ * @param view
+ * @param <V>
+ */
+ <V extends View> void register(V view);
+
+ /**
+ * Un-register {@link View}.
+ * @param view
+ * @param <V>
+ */
+ <V extends View> void unregister(V view);
+
+ /**
+ * List all registered {@link Context} objects.
+ * @return
+ */
+ Iterable<? extends Context> listContexts();
+
+
+ /**
+ * Returns {@link Context} by name.
+ * @param name
+ * @param <C>
+ */
+ <C extends Context> C getContext(String name);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBean.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBean.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBean.java
new file mode 100644
index 0000000..cf0a0e2
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBean.java
@@ -0,0 +1,27 @@
+/*
+ * 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.management;
+
+import org.jclouds.View;
+
+/**
+ * A {@link ManagedBean} type for managing {@link View}s.
+ * @param <V>
+ */
+public interface ViewMBean<V extends View> extends ManagedBean {
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactories.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactories.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactories.java
new file mode 100644
index 0000000..1968a7c
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactories.java
@@ -0,0 +1,52 @@
+/*
+ * 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.management;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.reflect.TypeToken;
+import org.jclouds.management.osgi.ViewManagementFactoryRegistry;
+
+import java.util.ServiceLoader;
+
+import static com.google.common.collect.Iterables.filter;
+
+public class ViewMBeanFactories {
+
+ /**
+ * Returns the {@link ViewMBeanFactory} located on the classpath via {@link java.util.ServiceLoader}.
+ * @return all available factories loaded from classpath via {@link java.util.ServiceLoader}
+ */
+ public static Iterable<ViewMBeanFactory> fromServiceLoader() {
+ return ServiceLoader.load(ViewMBeanFactory.class);
+ }
+
+ /**
+ * Returns the {@link ViewMBeanFactory} found via {@link org.jclouds.management.osgi.ViewManagementFactoryRegistry} and {@link java.util.ServiceLoader}.
+ * @return all available factories.
+ */
+ public static Iterable<ViewMBeanFactory> all() {
+ return ImmutableSet.<ViewMBeanFactory>builder()
+ .addAll(fromServiceLoader())
+ .addAll(ViewManagementFactoryRegistry.fromRegistry()).build();
+ }
+
+ public static Iterable<ViewMBeanFactory> forType(TypeToken viewableAs) {
+ return filter(all(), ViewMBeanFactoryPredicates.forType(viewableAs));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactory.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactory.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactory.java
new file mode 100644
index 0000000..a5e68ba
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactory.java
@@ -0,0 +1,41 @@
+/*
+ * 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.management;
+
+import com.google.common.reflect.TypeToken;
+import org.jclouds.View;
+
+/**
+ * A factory for {@ViewManagement}.
+ * @param <V>
+ */
+public interface ViewMBeanFactory<V extends View> {
+
+ /**
+ * Creates a {@link ManagedBean} for the Context.
+ * @param view
+ * @return
+ */
+ ViewMBean<V> create(V view);
+
+ /**
+ * Returns the {@link TypeToken} of the {@link View}.
+ * @return
+ */
+ TypeToken<V> getViewType();
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactoryPredicates.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactoryPredicates.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactoryPredicates.java
new file mode 100644
index 0000000..fb8c5ec
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/ViewMBeanFactoryPredicates.java
@@ -0,0 +1,51 @@
+/*
+ * 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.management;
+
+import com.google.common.base.Predicate;
+import com.google.common.reflect.TypeToken;
+import org.jclouds.View;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+public class ViewMBeanFactoryPredicates {
+
+ public static <V extends View> Predicate<ViewMBeanFactory<V>> forType(final TypeToken<V> type) {
+ checkNotNull(type, "type");
+ return new ViewMBeanFactoryForType<V>(type);
+ }
+
+ private static class ViewMBeanFactoryForType<V extends View> implements Predicate<ViewMBeanFactory<V>> {
+
+ private final TypeToken<V> type;
+
+ private ViewMBeanFactoryForType(TypeToken<V> type) {
+ this.type = type;
+ }
+
+ @Override
+ public boolean apply(ViewMBeanFactory factory) {
+ return factory.getViewType().isAssignableFrom(type);
+ }
+
+ @Override
+ public String toString() {
+ return "forType(" + type + ")";
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/config/ManagementLifecycle.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/config/ManagementLifecycle.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/config/ManagementLifecycle.java
new file mode 100644
index 0000000..adc3be2
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/config/ManagementLifecycle.java
@@ -0,0 +1,119 @@
+/*
+ * 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.management.config;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.common.reflect.TypeToken;
+import com.google.inject.AbstractModule;
+import com.google.inject.TypeLiteral;
+import com.google.inject.matcher.AbstractMatcher;
+import com.google.inject.matcher.Matcher;
+import com.google.inject.matcher.Matchers;
+import com.google.inject.spi.InjectionListener;
+import com.google.inject.spi.TypeEncounter;
+import com.google.inject.spi.TypeListener;
+import org.jclouds.View;
+import org.jclouds.javax.annotation.Nullable;
+import org.jclouds.lifecycle.Closer;
+import org.jclouds.management.ManagementContext;
+import org.jclouds.management.ViewMBean;
+import org.jclouds.management.ViewMBeanFactories;
+import org.jclouds.management.ViewMBeanFactory;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A {@link com.google.inject.Module} for managing the lifecycle of {@link org.jclouds.management.ViewMBean} beans.
+ * The goal of this module is to create {@link org.jclouds.management.ViewMBean} beans that correspond to each {@link View} created/destroyed
+ * and register/un-register them to the {@link ManagementContext}.
+ */
+public class ManagementLifecycle extends AbstractModule {
+
+ private final ManagementContext managementContext;
+
+ public ManagementLifecycle(ManagementContext context) {
+ this.managementContext = context;
+ }
+
+ @Override
+ protected void configure() {
+ bindListener(subClassOf(View.class), new TypeListener() {
+ @Override
+ public <I> void hear(final TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
+ typeEncounter.register(new InjectionListener<I>() {
+ @Override
+ public void afterInjection(Object object) {
+ final View view = (View) object;
+ final Iterable<ViewMBean> viewMamanagementBeans = viewManagementOf(view);
+ final Closer closer = view.unwrap().utils().injector().getInstance(Closer.class);
+ //We get the name from the view and not from the view management object to avoid proxy issues.
+ final String name = view.unwrap().getName();
+ managementContext.register(view);
+
+ //Manage the created management view objects to the context.
+ for (ViewMBean viewMBean : viewMamanagementBeans) {
+ managementContext.manage(viewMBean, name);
+ }
+
+ //Add the the management view objects to the Closer, so that they are unregistered on close.
+ closer.addToClose(new Closeable() {
+ @Override
+ public void close() throws IOException {
+ for (ViewMBean viewMBean : viewMamanagementBeans) {
+ managementContext.unmanage(viewMBean, name);
+ }
+ managementContext.unregister(view);
+ }
+ });
+ }
+ });
+ }
+ });
+ }
+
+ /**
+ * Returns an {@link Iterable} of {@link org.jclouds.management.ViewMBean} for the specified {@link View}.
+ * @param view
+ * @return
+ */
+ private static Iterable<ViewMBean> viewManagementOf(final View view) {
+ TypeToken type = TypeToken.of(view.getClass());
+ return Iterables.transform(ViewMBeanFactories.forType(type), new Function<ViewMBeanFactory, ViewMBean>() {
+ @Override
+ public ViewMBean apply(@Nullable ViewMBeanFactory factory) {
+ return factory.create(view);
+ }
+ });
+ }
+
+ /**
+ * Creates a {@link TypeLiteral} {@link Matcher} for matching subclasses.
+ * This is for use in bindListener.
+ * @param clazz
+ * @return
+ */
+ private static Matcher<TypeLiteral> subClassOf(final Class<?> clazz) {
+ return new AbstractMatcher<TypeLiteral>() {
+ public boolean matches (TypeLiteral typeLiteral){
+ return Matchers.subclassesOf(clazz).matches(typeLiteral.getRawType());
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/BaseManagementContext.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/BaseManagementContext.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/BaseManagementContext.java
new file mode 100644
index 0000000..aad7838
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/BaseManagementContext.java
@@ -0,0 +1,175 @@
+/*
+ * 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.management.internal;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
+import org.jclouds.Context;
+import org.jclouds.View;
+import org.jclouds.javax.annotation.Nullable;
+import org.jclouds.management.ManagedBean;
+import org.jclouds.management.ManagementContext;
+
+import javax.management.MBeanServer;
+import java.lang.management.ManagementFactory;
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+public enum BaseManagementContext implements ManagementContext {
+
+ INSTANCE;
+
+ private final Map<String, View> views = Maps.newHashMap();
+ private final Map<Key, ManagedBean> mbeans = Maps.newHashMap();
+
+ //The MBeanServer can be bind/unbind (especially inside OSGi) so its not always available.
+ //Thus is represented as Optional.
+ private Optional<MBeanServer> mBeanServer = Optional.of(ManagementFactory.getPlatformMBeanServer());
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public synchronized void manage(ManagedBean mBean, String name) {
+ if (mBeanServer.isPresent()) {
+ ManagementUtils.register(mBeanServer.get(), mBean, mBean.getType(), name);
+ }
+ mbeans.put(new Key(mBean, name), mBean);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public synchronized void unmanage(ManagedBean mBean, String name) {
+ if (mBeanServer.isPresent()) {
+ ManagementUtils.unregister(mBeanServer.get(), mBean.getType(), name);
+ }
+ mbeans.remove(new Key(mBean, name));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public synchronized void bind(MBeanServer server) {
+ this.mBeanServer = Optional.of(server);
+ for(Map.Entry<Key, ManagedBean> entry : mbeans.entrySet()) {
+ String name = entry.getKey().getName();
+ ManagedBean mBean = entry.getValue();
+ ManagementUtils.register(server, mBean, mBean.getType(), name);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public synchronized void unbind(MBeanServer server) {
+ for(Map.Entry<Key, ManagedBean> entry : mbeans.entrySet()) {
+ String name = entry.getKey().getName();
+ ManagedBean mBean = entry.getValue();
+ ManagementUtils.unregister(server, mBean.getType(), name);
+ }
+ this.mBeanServer = Optional.absent();
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <V extends View> void register(V view) {
+ views.put(view.unwrap().getName(), view);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public <V extends View> void unregister(V view) {
+ views.remove(view.unwrap().getName());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Iterable<? extends Context> listContexts() {
+ return Iterables.transform(views.values(), new Function<View, Context>() {
+ @Override
+ public Context apply(@Nullable View input) {
+ return input.unwrap();
+ }
+ });
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Context getContext(String name) {
+ return views.get(name).unwrap();
+ }
+
+ private class Key {
+
+ private final String type;
+ private final String name;
+
+ public Key(String type, String name) {
+ this.type = checkNotNull(type, "type");
+ this.name = checkNotNull(name, "name");
+ }
+
+ public Key(ManagedBean mbean, String name) {
+ this(checkNotNull(mbean, "mbean").getType(), name);
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Key key = (Key) o;
+
+ if (name != null ? !name.equals(key.name) : key.name != null) return false;
+ if (type != null ? !type.equals(key.type) : key.type != null) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = type != null ? type.hashCode() : 0;
+ result = 31 * result + (name != null ? name.hashCode() : 0);
+ return result;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/ManagementUtils.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/ManagementUtils.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/ManagementUtils.java
new file mode 100644
index 0000000..37a27cb
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/internal/ManagementUtils.java
@@ -0,0 +1,90 @@
+/*
+ * 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.management.internal;
+
+import com.google.common.base.Throwables;
+import org.jclouds.JcloudsVersion;
+
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.OperationsException;
+
+public final class ManagementUtils {
+
+ private static final JcloudsVersion VERSION = JcloudsVersion.get();
+ private static final String OBJECT_NAME_FORMAT = "org.jclouds:type=%s,name=%s,version=%d.%d";
+
+ private ManagementUtils() {
+ //Utility Class
+ }
+
+ /**
+ * Registers a managed object to the mbean server.
+ *
+ * @param mBeanServer
+ * @param mbean
+ * @param type
+ * @param name
+ */
+ public static void register(MBeanServer mBeanServer, Object mbean, String type, String name) {
+ try {
+ ObjectName objectName = objectNameFor(type, name);
+ if (!mBeanServer.isRegistered(objectName)) {
+ mBeanServer.registerMBean(mbean, objectName);
+ }
+ } catch (OperationsException e) {
+ Throwables.propagate(e);
+ } catch (MBeanRegistrationException e) {
+ Throwables.propagate(e);
+ }
+ }
+
+ /**
+ * Un-registers a managed object to the mbean server.
+ *
+ * @param mBeanServer
+ * @param type
+ * @param name
+ */
+ public static void unregister(MBeanServer mBeanServer, String type, String name) {
+ try {
+ ObjectName objectName = objectNameFor(type, name);
+ if (mBeanServer.isRegistered(objectName)) {
+ mBeanServer.unregisterMBean(objectName);
+ }
+ } catch (OperationsException e) {
+ Throwables.propagate(e);
+ } catch (MBeanRegistrationException e) {
+ Throwables.propagate(e);
+ }
+ }
+
+
+ /**
+ * Creates a jclouds {@link javax.management.ObjectName} for the mbean type.
+ *
+ * @param type
+ * @return
+ * @throws javax.management.MalformedObjectNameException
+ */
+ public static ObjectName objectNameFor(String type, String name) throws MalformedObjectNameException {
+ return new ObjectName(String.format(OBJECT_NAME_FORMAT, type, name, VERSION.majorVersion, VERSION.minorVersion));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/Activator.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/Activator.java
new file mode 100644
index 0000000..abe5532
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/Activator.java
@@ -0,0 +1,102 @@
+/*
+ * 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.management.osgi;
+
+import org.jclouds.management.JcloudsManagement;
+import org.jclouds.management.internal.BaseManagementContext;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+
+import javax.management.MBeanServer;
+
+public class Activator implements BundleActivator {
+
+ private ServiceTracker mbeanServerTracker = null;
+ private ManagementFactoryBundleListener bundleListener = new ManagementFactoryBundleListener();
+ private final JcloudsManagement jcloudsManagement = new JcloudsManagement();
+
+ /**
+ * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
+ * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
+ * <p/>
+ * <p/>
+ * This method must complete and return to its caller in a timely manner.
+ *
+ * @param context
+ * The execution context of the bundle being started.
+ * @throws Exception
+ * If this method throws an exception, this bundle is marked as stopped and the Framework will remove this
+ * bundle's listeners, unregister all services registered by this bundle, and release all services used by
+ * this bundle.
+ */
+ @Override
+ public void start(BundleContext context) throws Exception {
+ bundleListener.start(context);
+ mbeanServerTracker = new ServiceTracker(context, MBeanServer.class.getName(), null) {
+
+ @Override
+ public Object addingService(ServiceReference reference) {
+ Object obj = super.addingService(reference);
+ if (MBeanServer.class.isAssignableFrom(obj.getClass())) {
+ BaseManagementContext.INSTANCE.bind((MBeanServer) obj);
+ BaseManagementContext.INSTANCE.manage(jcloudsManagement, "core");
+ }
+ return obj;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference reference, Object service) {
+ super.modifiedService(reference, service);
+ }
+
+ @Override
+ public void removedService(ServiceReference reference, Object service) {
+ if (MBeanServer.class.isAssignableFrom(service.getClass())) {
+ BaseManagementContext.INSTANCE.unbind((MBeanServer) service);
+ }
+ super.removedService(reference, service);
+ }
+ };
+ mbeanServerTracker.open();
+ }
+
+ /**
+ * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
+ * the bundle. In general, this method should undo the work that the <code>BundleActivator.start</code> method
+ * started. There should be no active threads that were started by this bundle when this bundle returns. A stopped
+ * bundle must not call any Framework objects.
+ * <p/>
+ * <p/>
+ * This method must complete and return to its caller in a timely manner.
+ *
+ * @param context
+ * The execution context of the bundle being stopped.
+ * @throws Exception
+ * If this method throws an exception, the bundle is still marked as stopped, and the Framework will
+ * remove the bundle's listeners, unregister all services registered by the bundle, and release all
+ * services used by the bundle.
+ */
+ @Override
+ public void stop(BundleContext context) throws Exception {
+ bundleListener.stop(context);
+ if (mbeanServerTracker != null) {
+ mbeanServerTracker.close();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs/blob/7cac0ef1/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/ManagementFactoryBundleListener.java
----------------------------------------------------------------------
diff --git a/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/ManagementFactoryBundleListener.java b/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/ManagementFactoryBundleListener.java
new file mode 100644
index 0000000..807c485
--- /dev/null
+++ b/jclouds-management/management-core/src/main/java/org/jclouds/management/osgi/ManagementFactoryBundleListener.java
@@ -0,0 +1,123 @@
+/*
+ * 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.management.osgi;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import org.jclouds.management.ViewMBeanFactory;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+
+import static org.jclouds.osgi.Bundles.instantiateAvailableClasses;
+import static org.jclouds.osgi.Bundles.stringsForResourceInBundle;
+import static org.osgi.framework.BundleEvent.STARTED;
+import static org.osgi.framework.BundleEvent.STOPPING;
+import static org.osgi.framework.BundleEvent.STOPPED;
+
+/**
+ * A {@link org.osgi.framework.BundleListener} that listens for {@link org.osgi.framework.BundleEvent} and searches for
+ * {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} in newly installed Bundles.
+ * This is used as a workaround for OSGi environments where the ServiceLoader cannot cross bundle boundaries.
+ *
+ * @author iocanel
+ */
+public class ManagementFactoryBundleListener implements BundleListener {
+
+ private final Multimap<Long, ViewMBeanFactory> managedViewFactoryMap = ArrayListMultimap.create();
+
+ /**
+ * Starts the listener. Checks the bundles that are already active and registers {@link org.jclouds.providers.ProviderMetadata} and
+ * {@link org.jclouds.apis.ApiMetadata} found. Registers the itself as a {@link org.osgi.framework.BundleListener}.
+ *
+ * @param bundleContext
+ */
+ public synchronized void start(BundleContext bundleContext) {
+ bundleContext.addBundleListener(this);
+ for (Bundle bundle : bundleContext.getBundles()) {
+ if (bundle.getState() == Bundle.ACTIVE) {
+ addBundle(bundle);
+ }
+ }
+ bundleContext.addBundleListener(this);
+ }
+
+ /**
+ * Stops the listener. Removes itself from the {@link org.osgi.framework.BundleListener}s. Clears metadata maps and listeners lists.
+ *
+ * @param bundleContext
+ */
+ public void stop(BundleContext bundleContext) {
+ bundleContext.removeBundleListener(this);
+ managedViewFactoryMap.clear();
+ }
+
+ @Override
+ public synchronized void bundleChanged(BundleEvent event) {
+ switch (event.getType()) {
+ case STARTED:
+ addBundle(event.getBundle());
+ break;
+ case STOPPING:
+ case STOPPED:
+ removeBundle(event.getBundle());
+ break;
+ }
+ }
+
+ /**
+ * Searches for {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} inside the {@link org.osgi.framework.Bundle}. If metadata are found
+ * they are registered in the {@link org.jclouds.osgi.ProviderRegistry} and {@link org.jclouds.osgi.ApiRegistry}. Also the {@link org.jclouds.osgi.ProviderListener} and
+ * {@link org.jclouds.osgi.ApiListener} are notified.
+ *
+ * @param bundle
+ */
+ private synchronized void addBundle(Bundle bundle) {
+ for (ViewMBeanFactory viewMBeanFactory : listManagedViewFactories(bundle)) {
+ if (viewMBeanFactory != null) {
+ ViewManagementFactoryRegistry.registerFactory(viewMBeanFactory);
+ managedViewFactoryMap.put(bundle.getBundleId(), viewMBeanFactory);
+ }
+ }
+ }
+
+ /**
+ * Searches for {@link org.jclouds.providers.ProviderMetadata} and {@link org.jclouds.apis.ApiMetadata} registered under the {@link org.osgi.framework.Bundle} id. If metadata
+ * are found they are removed the {@link org.jclouds.osgi.ProviderRegistry} and {@link org.jclouds.osgi.ApiRegistry}. Also the {@link org.jclouds.osgi.ProviderListener}
+ * and {@link org.jclouds.osgi.ApiListener} are notified.
+ *
+ * @param bundle
+ */
+ private synchronized void removeBundle(Bundle bundle) {
+ for (ViewMBeanFactory viewMBeanFactory : managedViewFactoryMap.removeAll(bundle.getBundleId())) {
+ ViewManagementFactoryRegistry.registerFactory(viewMBeanFactory);
+ }
+
+ }
+
+ /**
+ * Creates an instance of {@link org.jclouds.management.ViewMBeanFactory} from the {@link org.osgi.framework.Bundle}.
+ *
+ * @param bundle
+ * @return
+ */
+ public Iterable<ViewMBeanFactory> listManagedViewFactories(Bundle bundle) {
+ Iterable<String> classNames = stringsForResourceInBundle("/META-INF/services/" + ViewMBeanFactory.class.getName(), bundle);
+ return instantiateAvailableClasses(bundle, classNames, ViewMBeanFactory.class);
+ }
+}