[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-06 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/brooklyn-server/pull/672


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-05 Thread ahgittin
Github user ahgittin commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r120078356
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---
@@ -209,54 +213,114 @@ public Boolean call() {
 }
 
 public Map getManagedBundles() {
-return ImmutableMap.copyOf(managedBundles);
+synchronized (managedBundles) {
+return ImmutableMap.copyOf(managedBundles);
+}
+}
+
+public String getManagedBundleId(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundlesByNam.get(vn);
+}
 }
 
-public Bundle installUploadedBundle(ManagedBundle bundleMetadata, 
InputStream zipIn, boolean loadCatalogBom) {
-try {
-Bundle alreadyBundle = 
checkBundleInstalledThrowIfInconsistent(bundleMetadata, false);
-if (alreadyBundle!=null) {
-return alreadyBundle;
-}
+public ManagedBundle getManagedBundle(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundles.get(managedBundlesByNam.get(vn));
+}
+}
 
-File zipF = 
Os.newTempFile("brooklyn-bundle-transient-"+bundleMetadata, "zip");
-FileOutputStream fos = new FileOutputStream(zipF);
-Streams.copy(zipIn, fos);
-zipIn.close();
-fos.close();
-
-Bundle bundleInstalled = 
framework.getBundleContext().installBundle(bundleMetadata.getOsgiUniqueUrl(), 
-new FileInputStream(zipF));
-checkCorrectlyInstalled(bundleMetadata, bundleInstalled);
-if (!bundleMetadata.isNameResolved()) {
-
((BasicManagedBundle)bundleMetadata).setSymbolicName(bundleInstalled.getSymbolicName());
-
((BasicManagedBundle)bundleMetadata).setVersion(bundleInstalled.getVersion().toString());
-}
-
((BasicManagedBundle)bundleMetadata).setTempLocalFileWhenJustUploaded(zipF);
-
-synchronized (managedBundles) {
-managedBundles.put(bundleMetadata.getId(), bundleMetadata);
-}
-
mgmt.getRebindManager().getChangeListener().onChanged(bundleMetadata);
-
-// starting here  flags wiring issues earlier
-// but may break some things running from the IDE
-bundleInstalled.start();
+/** For bundles which are installed by a URL, see whether a bundle has 
been installed from that URL */
+public ManagedBundle getManagedBundleFromUrl(String url) {
+synchronized (managedBundles) {
+String id = managedBundlesByUrl.get(url);
+if (id==null) return null;
+return managedBundles.get(id);
+}
+}
+
+/** See {@link OsgiArchiveInstaller#install()}, using default values */
+public ReferenceWithError 
install(InputStream zipIn) {
+return new OsgiArchiveInstaller(this, null, zipIn).install();
+}
 
-if (loadCatalogBom) {
-loadCatalogBom(bundleInstalled);
+/** See {@link OsgiArchiveInstaller#install()}, but deferring the 
start and catalog load */
+public ReferenceWithError 
installDeferredStart(@Nullable ManagedBundle knownBundleMetadata, @Nullable 
InputStream zipIn) {
+OsgiArchiveInstaller installer = new OsgiArchiveInstaller(this, 
knownBundleMetadata, zipIn);
+installer.setDeferredStart(true);
+
+return installer.install();
+}
+
+/** See {@link OsgiArchiveInstaller#install()} - this exposes custom 
options */
+@Beta
+public ReferenceWithError 
install(@Nullable ManagedBundle knownBundleMetadata, @Nullable InputStream 
zipIn,
+boolean start, boolean loadCatalogBom, boolean 
forceUpdateOfNonSnapshots) {
+
+OsgiArchiveInstaller installer = new OsgiArchiveInstaller(this, 
knownBundleMetadata, zipIn);
+installer.setStart(start);
+installer.setLoadCatalogBom(loadCatalogBom);
+installer.setForce(forceUpdateOfNonSnapshots);
+
+return installer.install();
+}
+
+/**
+ * Removes this bundle from Brooklyn management, 
+ * removes all catalog items it defined,
+ * and then uninstalls the bundle from OSGi.
+ * 
+ * No checking is done whether anything is using the bundle;
+ * behaviour of such things is not guaranteed. They will work for many 
things
+ * but attempts to load new classes may fail.
+ * 
   

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-05 Thread ahgittin
Github user ahgittin commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r120078260
  
--- Diff: 
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
 ---
@@ -158,6 +149,39 @@ public Response createFromYaml(String yaml) {
 }
 }
 
+public static class BundleInstallationRestResult {
+// as Osgi result, but without bundle, and with maps of catalog 
items installed
+
+String message;
+ManagedBundle metadata;
+OsgiBundleInstallationResult.ResultCode code;
+
+Map types;
+
+public String getMessage() {
+return message;
+}
+
+@SuppressWarnings("deprecation")
+public static BundleInstallationRestResult 
of(OsgiBundleInstallationResult in, ManagementContext mgmt, 
BrooklynRestResourceUtils brooklynU, UriInfo ui) {
+BundleInstallationRestResult result = new 
BundleInstallationRestResult();
+result.message = in.getMessage();
+result.metadata = in.getMetadata();
+result.code = in.getCode();
+if (in.getCatalogItemsInstalled()!=null) {
+result.types = MutableMap.of();
+for (String id: in.getCatalogItemsInstalled()) {
+// TODO prefer to use RegisteredType, but we need 
transformer for those in REST
+//RegisteredType ci = mgmt.getTypeRegistry().get(id);
+
+CatalogItem ci = 
CatalogUtils.getCatalogItemOptionalVersion(mgmt, id);
+CatalogTransformer.catalogItemSummary(brooklynU, ci, 
ui.getBaseUriBuilder());
--- End diff --

absolutely -- good spot!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-05 Thread ahgittin
Github user ahgittin commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r120077947
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-05 Thread ahgittin
Github user ahgittin commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r120077518
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-06-05 Thread ahgittin
Github user ahgittin commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r120077094
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-26 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r118721807
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-19 Thread aledsage
Github user aledsage commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r117490368
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115944663
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115980431
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---
@@ -209,54 +213,114 @@ public Boolean call() {
 }
 
 public Map getManagedBundles() {
-return ImmutableMap.copyOf(managedBundles);
+synchronized (managedBundles) {
+return ImmutableMap.copyOf(managedBundles);
+}
+}
+
+public String getManagedBundleId(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundlesByNam.get(vn);
+}
 }
 
-public Bundle installUploadedBundle(ManagedBundle bundleMetadata, 
InputStream zipIn, boolean loadCatalogBom) {
-try {
-Bundle alreadyBundle = 
checkBundleInstalledThrowIfInconsistent(bundleMetadata, false);
-if (alreadyBundle!=null) {
-return alreadyBundle;
-}
+public ManagedBundle getManagedBundle(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundles.get(managedBundlesByNam.get(vn));
+}
+}
 
-File zipF = 
Os.newTempFile("brooklyn-bundle-transient-"+bundleMetadata, "zip");
-FileOutputStream fos = new FileOutputStream(zipF);
-Streams.copy(zipIn, fos);
-zipIn.close();
-fos.close();
-
-Bundle bundleInstalled = 
framework.getBundleContext().installBundle(bundleMetadata.getOsgiUniqueUrl(), 
-new FileInputStream(zipF));
-checkCorrectlyInstalled(bundleMetadata, bundleInstalled);
-if (!bundleMetadata.isNameResolved()) {
-
((BasicManagedBundle)bundleMetadata).setSymbolicName(bundleInstalled.getSymbolicName());
-
((BasicManagedBundle)bundleMetadata).setVersion(bundleInstalled.getVersion().toString());
-}
-
((BasicManagedBundle)bundleMetadata).setTempLocalFileWhenJustUploaded(zipF);
-
-synchronized (managedBundles) {
-managedBundles.put(bundleMetadata.getId(), bundleMetadata);
-}
-
mgmt.getRebindManager().getChangeListener().onChanged(bundleMetadata);
-
-// starting here  flags wiring issues earlier
-// but may break some things running from the IDE
-bundleInstalled.start();
+/** For bundles which are installed by a URL, see whether a bundle has 
been installed from that URL */
+public ManagedBundle getManagedBundleFromUrl(String url) {
+synchronized (managedBundles) {
+String id = managedBundlesByUrl.get(url);
+if (id==null) return null;
+return managedBundles.get(id);
+}
+}
+
+/** See {@link OsgiArchiveInstaller#install()}, using default values */
+public ReferenceWithError 
install(InputStream zipIn) {
+return new OsgiArchiveInstaller(this, null, zipIn).install();
+}
 
-if (loadCatalogBom) {
-loadCatalogBom(bundleInstalled);
+/** See {@link OsgiArchiveInstaller#install()}, but deferring the 
start and catalog load */
+public ReferenceWithError 
installDeferredStart(@Nullable ManagedBundle knownBundleMetadata, @Nullable 
InputStream zipIn) {
+OsgiArchiveInstaller installer = new OsgiArchiveInstaller(this, 
knownBundleMetadata, zipIn);
+installer.setDeferredStart(true);
+
+return installer.install();
+}
+
+/** See {@link OsgiArchiveInstaller#install()} - this exposes custom 
options */
+@Beta
+public ReferenceWithError 
install(@Nullable ManagedBundle knownBundleMetadata, @Nullable InputStream 
zipIn,
+boolean start, boolean loadCatalogBom, boolean 
forceUpdateOfNonSnapshots) {
+
+OsgiArchiveInstaller installer = new OsgiArchiveInstaller(this, 
knownBundleMetadata, zipIn);
+installer.setStart(start);
+installer.setLoadCatalogBom(loadCatalogBom);
+installer.setForce(forceUpdateOfNonSnapshots);
+
+return installer.install();
+}
+
+/**
+ * Removes this bundle from Brooklyn management, 
+ * removes all catalog items it defined,
+ * and then uninstalls the bundle from OSGi.
+ * 
+ * No checking is done whether anything is using the bundle;
+ * behaviour of such things is not guaranteed. They will work for many 
things
+ * but attempts to load new classes may fail.
+ * 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115987215
  
--- Diff: 
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
 ---
@@ -158,6 +149,39 @@ public Response createFromYaml(String yaml) {
 }
 }
 
+public static class BundleInstallationRestResult {
+// as Osgi result, but without bundle, and with maps of catalog 
items installed
+
+String message;
+ManagedBundle metadata;
+OsgiBundleInstallationResult.ResultCode code;
+
+Map types;
+
+public String getMessage() {
+return message;
+}
+
+@SuppressWarnings("deprecation")
+public static BundleInstallationRestResult 
of(OsgiBundleInstallationResult in, ManagementContext mgmt, 
BrooklynRestResourceUtils brooklynU, UriInfo ui) {
+BundleInstallationRestResult result = new 
BundleInstallationRestResult();
+result.message = in.getMessage();
+result.metadata = in.getMetadata();
+result.code = in.getCode();
+if (in.getCatalogItemsInstalled()!=null) {
+result.types = MutableMap.of();
+for (String id: in.getCatalogItemsInstalled()) {
+// TODO prefer to use RegisteredType, but we need 
transformer for those in REST
+//RegisteredType ci = mgmt.getTypeRegistry().get(id);
+
+CatalogItem ci = 
CatalogUtils.getCatalogItemOptionalVersion(mgmt, id);
+CatalogTransformer.catalogItemSummary(brooklynU, ci, 
ui.getBaseUriBuilder());
--- End diff --

Shouldn't this be
```
CatalogItemSummary summary = 
CatalogTransformer.catalogItemSummary(brooklynU, ci, ui.getBaseUriBuilder());
result.types().put(id, summary);
```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115983244
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/typereg/BasicRegisteredType.java ---
@@ -39,6 +39,7 @@
 final RegisteredTypeKind kind;
 final String symbolicName;
 final String version;
+String containingBundle;
--- End diff --

Would it not be better to make this a `VersionedName`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115984092
  
--- Diff: 
api/src/main/java/org/apache/brooklyn/api/catalog/CatalogItem.java ---
@@ -123,6 +123,8 @@ public String toString() {
 @Nullable public String getIconUrl();
 
 public String getSymbolicName();
+
+public String getContainingBundle();
--- End diff --

Why not make this a `VersionedName`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115939182
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---
@@ -209,54 +213,114 @@ public Boolean call() {
 }
 
 public Map getManagedBundles() {
-return ImmutableMap.copyOf(managedBundles);
+synchronized (managedBundles) {
+return ImmutableMap.copyOf(managedBundles);
+}
+}
+
+public String getManagedBundleId(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundlesByNam.get(vn);
+}
 }
 
-public Bundle installUploadedBundle(ManagedBundle bundleMetadata, 
InputStream zipIn, boolean loadCatalogBom) {
-try {
-Bundle alreadyBundle = 
checkBundleInstalledThrowIfInconsistent(bundleMetadata, false);
-if (alreadyBundle!=null) {
-return alreadyBundle;
-}
+public ManagedBundle getManagedBundle(VersionedName vn) {
+synchronized (managedBundles) {
+return managedBundles.get(managedBundlesByNam.get(vn));
+}
+}
 
-File zipF = 
Os.newTempFile("brooklyn-bundle-transient-"+bundleMetadata, "zip");
-FileOutputStream fos = new FileOutputStream(zipF);
-Streams.copy(zipIn, fos);
-zipIn.close();
-fos.close();
-
-Bundle bundleInstalled = 
framework.getBundleContext().installBundle(bundleMetadata.getOsgiUniqueUrl(), 
-new FileInputStream(zipF));
-checkCorrectlyInstalled(bundleMetadata, bundleInstalled);
-if (!bundleMetadata.isNameResolved()) {
-
((BasicManagedBundle)bundleMetadata).setSymbolicName(bundleInstalled.getSymbolicName());
-
((BasicManagedBundle)bundleMetadata).setVersion(bundleInstalled.getVersion().toString());
-}
-
((BasicManagedBundle)bundleMetadata).setTempLocalFileWhenJustUploaded(zipF);
-
-synchronized (managedBundles) {
-managedBundles.put(bundleMetadata.getId(), bundleMetadata);
-}
-
mgmt.getRebindManager().getChangeListener().onChanged(bundleMetadata);
-
-// starting here  flags wiring issues earlier
-// but may break some things running from the IDE
-bundleInstalled.start();
+/** For bundles which are installed by a URL, see whether a bundle has 
been installed from that URL */
+public ManagedBundle getManagedBundleFromUrl(String url) {
+synchronized (managedBundles) {
+String id = managedBundlesByUrl.get(url);
+if (id==null) return null;
+return managedBundles.get(id);
+}
+}
+
+/** See {@link OsgiArchiveInstaller#install()}, using default values */
+public ReferenceWithError 
install(InputStream zipIn) {
+return new OsgiArchiveInstaller(this, null, zipIn).install();
+}
 
-if (loadCatalogBom) {
-loadCatalogBom(bundleInstalled);
+/** See {@link OsgiArchiveInstaller#install()}, but deferring the 
start and catalog load */
+public ReferenceWithError 
installDeferredStart(@Nullable ManagedBundle knownBundleMetadata, @Nullable 
InputStream zipIn) {
+OsgiArchiveInstaller installer = new OsgiArchiveInstaller(this, 
knownBundleMetadata, zipIn);
+installer.setDeferredStart(true);
+
+return installer.install();
+}
+
+/** See {@link OsgiArchiveInstaller#install()} - this exposes custom 
options */
+@Beta
+public ReferenceWithError 
install(@Nullable ManagedBundle knownBundleMetadata, @Nullable InputStream 
zipIn,
--- End diff --

It's interesting that the only place this is called is in test code; in one 
sense this maybe isn't needed!  But good to have it for completeness's sake.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115977432
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115947429
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115976553
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java ---
@@ -93,17 +95,19 @@
 
 /* see `Osgis` class for info on starting framework etc */
 
-protected final ManagementContext mgmt;
-protected final OsgiClassPrefixer osgiClassPrefixer;
-protected Framework framework;
-protected boolean reuseFramework;
+final ManagementContext mgmt;
+final OsgiClassPrefixer osgiClassPrefixer;
+Framework framework;
+
+private boolean reuseFramework;
 private Set bundlesAtStartup;
-protected File osgiCacheDir;
-protected Map managedBundles = MutableMap.of();
-protected AtomicInteger numberOfReusableFrameworksCreated = new 
AtomicInteger();
-
+private File osgiCacheDir;
+Map managedBundles = MutableMap.of();
+Map managedBundlesByNam = MutableMap.of();
--- End diff --

typo `managedBundlesByNam`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115976832
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-11 Thread geomacy
Github user geomacy commented on a diff in the pull request:

https://github.com/apache/brooklyn-server/pull/672#discussion_r115790699
  
--- Diff: 
core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiArchiveInstaller.java 
---
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.mgmt.ha;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.brooklyn.api.catalog.CatalogItem;
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
+import 
org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BasicManagedBundle;
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.core.osgi.BundleMaker;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.ReferenceWithError;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.os.Os;
+import org.apache.brooklyn.util.osgi.VersionedName;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.text.Strings;
+import org.apache.brooklyn.util.text.VersionComparator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Objects;
+
+// package-private so we can move this one if/when we move OsgiManager
+class OsgiArchiveInstaller {
+
+private static final Logger log = 
LoggerFactory.getLogger(OsgiArchiveInstaller.class);
+
+final private OsgiManager osgiManager;
+private ManagedBundle suppliedKnownBundleMetadata;
+private InputStream zipIn;
+
+private boolean start = true;
+private boolean loadCatalogBom = true;
+private boolean force = false;
+private boolean deferredStart = false;
+
+private File zipFile;
+private Manifest discoveredManifest;
+private VersionedName discoveredBomVersionedName;
+OsgiBundleInstallationResult result;
+
+private ManagedBundle inferredMetadata;
+private final boolean inputStreamSupplied;
+
+OsgiArchiveInstaller(OsgiManager osgiManager, ManagedBundle 
knownBundleMetadata, InputStream zipIn) {
+this.osgiManager = osgiManager;
+this.suppliedKnownBundleMetadata = knownBundleMetadata;
+this.zipIn = zipIn;
+inputStreamSupplied = zipIn!=null;
+}
+
+public void setStart(boolean start) {
+this.start = start;
+}
+
+public void setLoadCatalogBom(boolean loadCatalogBom) {
+this.loadCatalogBom = loadCatalogBom;
+}
+
+public void setForce(boolean force) {
+this.force = force;
+}
+
+public void setDeferredStart(boolean deferredStart) {
+this.deferredStart = deferredStart;
+}
+
+private ManagementContextInternal mgmt() {
+return (ManagementContextInternal) osgiManager.mgmt;
+}
+
+private synchronized void init() {
+if (result!=null) {
+if (zipFile!=null || zipIn==null) return;
+throw new IllegalStateException("This installer instance has 
already been used and the input stream discarded");
+}
+result = new OsgiBundleInstallationResult();
+inferredMetadata = 

[GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot

2017-05-08 Thread ahgittin
GitHub user ahgittin opened a pull request:

https://github.com/apache/brooklyn-server/pull/672

Bundle uninstall and snapshot

Supports bundle install and auto-reinstall for snapshot POSTs.

Doesn't do anything for auto-upgrade but if you POST a new snapshot ZIP 
then redeploy, the newer version gets used.  Also cleanly uninstalls catalog 
items.

You can merge this pull request into a Git repository by running:

$ git pull https://github.com/ahgittin/brooklyn-server 
bundle-uninstall-and-snapshot

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/brooklyn-server/pull/672.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #672






---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---