brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From geomacy <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #672: Bundle uninstall and snapshot
Date Fri, 26 May 2017 15:01:38 GMT
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 = suppliedKnownBundleMetadata==null ? new BasicManagedBundle()
: suppliedKnownBundleMetadata;
    +    }
    +    
    +    private synchronized void makeLocalZipFileFromInputStreamOrUrl() {
    +        if (zipIn==null) {
    +            Maybe<Bundle> installedBundle = Maybe.absent();
    +            if (suppliedKnownBundleMetadata!=null) {
    +                // if no input stream, look for a URL and/or a matching bundle
    +                if (!suppliedKnownBundleMetadata.isNameResolved()) {
    +                    ManagedBundle mbFromUrl = osgiManager.getManagedBundleFromUrl(suppliedKnownBundleMetadata.getUrl());
    +                    if (mbFromUrl!=null) {
    +                        // user supplied just a URL (eg brooklyn.libraries), but we recognise
it,
    +                        // so don't try to reload it, just record the info we know about
it to retrieve the bundle
    +                        ((BasicManagedBundle)suppliedKnownBundleMetadata).setSymbolicName(mbFromUrl.getSymbolicName());
    +                        ((BasicManagedBundle)suppliedKnownBundleMetadata).setVersion(mbFromUrl.getVersion());
    +                    }
    +                }
    +                if (installedBundle.isAbsent() && suppliedKnownBundleMetadata.getOsgiUniqueUrl()!=null)
{
    +                    installedBundle = Osgis.bundleFinder(osgiManager.framework).requiringFromUrl(suppliedKnownBundleMetadata.getOsgiUniqueUrl()).find();
    +                }
    +                if (installedBundle.isAbsent() && suppliedKnownBundleMetadata.getUrl()!=null)
{
    +                    installedBundle = Osgis.bundleFinder(osgiManager.framework).requiringFromUrl(suppliedKnownBundleMetadata.getUrl()).find();
    +                }
    +                if (installedBundle.isAbsent() && suppliedKnownBundleMetadata.isNameResolved())
{
    +                    installedBundle = Osgis.bundleFinder(osgiManager.framework).symbolicName(suppliedKnownBundleMetadata.getSymbolicName()).version(suppliedKnownBundleMetadata.getVersion()).find();
    +                }
    +                if (suppliedKnownBundleMetadata.getUrl()!=null) {
    +                    if (installedBundle.isAbsent() || force) {
    +                        // reload 
    +                        zipIn = ResourceUtils.create(mgmt()).getResourceFromUrl(suppliedKnownBundleMetadata.getUrl());
    +                    }
    +                }
    +            }
    +            
    +            if (zipIn==null) {
    +                if (installedBundle.isPresent()) {
    +                    // no way to install (no url), or no need to install (not forced);
just ignore it
    +                    result.metadata = osgiManager.getManagedBundle(new VersionedName(installedBundle.get()));
    +                    if (result.metadata==null) {
    +                        // low-level installed bundle
    +                        result.metadata = new BasicManagedBundle(installedBundle.get().getSymbolicName(),
installedBundle.get().getVersion().toString(),
    +                            suppliedKnownBundleMetadata!=null ? suppliedKnownBundleMetadata.getUrl()
: null);
    +                    }
    +                    result.setIgnoringAlreadyInstalled();
    +                    return;
    +                }
    +                result.metadata = suppliedKnownBundleMetadata;
    +                throw new IllegalArgumentException("No input stream available and no
URL could be found; nothing to install");
    +            }
    +            result.bundle = installedBundle.orNull();
    +        }
    +        
    +        zipFile = Os.newTempFile("brooklyn-bundle-transient-"+suppliedKnownBundleMetadata,
"zip");
    +        try {
    +            FileOutputStream fos = new FileOutputStream(zipFile);
    +            Streams.copy(zipIn, fos);
    +            zipIn.close();
    +            fos.close();
    +        } catch (Exception e) {
    +            throw Exceptions.propagate(e);
    +        } finally {
    +            zipIn = null;
    +        }
    +    }
    +
    +    private void discoverManifestFromCatalogBom(boolean isCatalogBomRequired) {
    +        discoveredManifest = new BundleMaker(mgmt()).getManifest(zipFile);
    +        ZipFile zf = null;
    +        try {
    +            try {
    +                zf = new ZipFile(zipFile);
    +            } catch (IOException e) {
    +                throw new IllegalArgumentException("Invalid ZIP/JAR archive: "+e);
    +            }
    +            ZipEntry bom = zf.getEntry("catalog.bom");
    +            if (bom==null) {
    +                bom = zf.getEntry("/catalog.bom");
    +            }
    +            if (bom==null) {
    +                if (isCatalogBomRequired) {
    +                    throw new IllegalArgumentException("Archive must contain a catalog.bom
file in the root");
    +                } else {
    +                    return;
    +                }
    +            }
    +            String bomS;
    +            try {
    +                bomS = Streams.readFullyString(zf.getInputStream(bom));
    +            } catch (IOException e) {
    +                throw new IllegalArgumentException("Error reading catalog.bom from ZIP/JAR
archive: "+e);
    +            }
    +            discoveredBomVersionedName = BasicBrooklynCatalog.getVersionedName( BasicBrooklynCatalog.getCatalogMetadata(bomS),
false );
    +        } finally {
    +            Streams.closeQuietly(zf);
    +        }
    +    }
    +    
    +    private void updateManifestFromAllSourceInformation() {
    +        if (discoveredBomVersionedName!=null) {
    +            matchSetOrFail("catalog.bom in archive", discoveredBomVersionedName.getSymbolicName(),
discoveredBomVersionedName.getVersion().toString());
    +        }
    +        
    +        boolean manifestNeedsUpdating = false;
    +        if (discoveredManifest==null) {
    +            discoveredManifest = new Manifest();
    +            manifestNeedsUpdating = true;
    +        }
    +        if (!matchSetOrFail("MANIFEST.MF in archive", discoveredManifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME),
    +                discoveredManifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION)
)) {
    +            manifestNeedsUpdating = true;                
    +            discoveredManifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME,
inferredMetadata.getSymbolicName());
    +            discoveredManifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION,
inferredMetadata.getVersion());
    +        }
    +        if (Strings.isBlank(inferredMetadata.getSymbolicName())) {
    +            throw new IllegalArgumentException("Missing bundle symbolic name in BOM or
MANIFEST");
    +        }
    +        if (Strings.isBlank(inferredMetadata.getVersion())) {
    +            throw new IllegalArgumentException("Missing bundle version in BOM or MANIFEST");
    +        }
    +        if (discoveredManifest.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION)==null)
{
    +            discoveredManifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(),
"1.0");
    --- End diff --
    
    ah good point thanks @aledsage 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message