brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request: Populate catalog from bundle bom
Date Fri, 25 Mar 2016 13:11:00 GMT
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/80#discussion_r57442437
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java
---
    @@ -0,0 +1,185 @@
    +/*
    + * 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.catalog.internal;
    +
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.Iterables;
    +import org.apache.brooklyn.api.catalog.CatalogItem;
    +import org.apache.brooklyn.api.mgmt.ManagementContext;
    +import org.apache.brooklyn.util.collections.MutableList;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
    +import org.apache.brooklyn.util.stream.Streams;
    +import org.apache.brooklyn.util.yaml.Yamls;
    +import org.osgi.framework.Bundle;
    +import org.osgi.framework.BundleContext;
    +import org.osgi.framework.BundleEvent;
    +import org.osgi.framework.FrameworkUtil;
    +import org.osgi.framework.ServiceReference;
    +import org.osgi.util.tracker.BundleTracker;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.yaml.snakeyaml.Yaml;
    +
    +import java.io.IOException;
    +import java.net.URL;
    +import java.util.List;
    +import java.util.Map;
    +
    +public class CatalogBomScanner {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(CatalogBomScanner.class);
    +    private static final String CATALOG_BOM_URL = "catalog.bom";
    +    private static final String BROOKLYN_CATALOG = "brooklyn.catalog";
    +    private static final String BROOKLYN_LIBRARIES = "brooklyn.libraries";
    +
    +    private CatalogPopulator catalogTracker;
    +
    +    public void bind(ServiceReference<ManagementContext> managementContext) throws
Exception {
    +        LOG.debug("Binding management context");
    +        catalogTracker = new CatalogPopulator(managementContext);
    +    }
    +
    +    public void unbind(ServiceReference<ManagementContext> managementContext) throws
Exception {
    +        LOG.debug("Unbinding management context");
    +        if (null != catalogTracker) {
    +            CatalogPopulator temp = catalogTracker;
    +            catalogTracker = null;
    +            temp.close();
    +        }
    +    }
    +
    +    private String[] bundleIds(Bundle bundle) {
    +        return new String[] {
    +            String.valueOf(bundle.getBundleId()), String.valueOf(bundle.getState()),
bundle.getSymbolicName()
    +        };
    +    }
    +
    +
    +    public class CatalogPopulator extends BundleTracker<Long> {
    +
    +        private ServiceReference<ManagementContext> mgmtContextReference;
    +        private ManagementContext managementContext;
    +
    +        public CatalogPopulator(ServiceReference<ManagementContext> serviceReference)
{
    +            super(serviceReference.getBundle().getBundleContext(), Bundle.ACTIVE, null);
    +            this.mgmtContextReference = serviceReference;
    +            open();
    +        }
    +
    +        @Override
    +        public void open() {
    +            managementContext = mgmtContextReference.getBundle().getBundleContext().getService(mgmtContextReference);
    +            super.open();
    +        }
    +
    +        @Override
    +        public void close() {
    +            super.close();
    +            managementContext = null;
    +            mgmtContextReference.getBundle().getBundleContext().ungetService(mgmtContextReference);
    +        }
    +
    +        public ManagementContext getManagementContext() {
    +            return managementContext;
    +        }
    +
    +        @Override
    +        public Long addingBundle(Bundle bundle, BundleEvent bundleEvent) {
    +
    +            final BundleContext bundleContext = FrameworkUtil.getBundle(CatalogBomScanner.class).getBundleContext();
    +            if (bundleContext == null) {
    +                LOG.info("Bundle context not yet established for bundle {} {} {}", bundleIds(bundle));
    +                return null;
    +            }
    +
    +            scanForCatalog(bundle);
    +
    +            return bundle.getBundleId();
    +        }
    +
    +        @Override
    +        public void modifiedBundle(Bundle bundle, BundleEvent event, Long bundleId) {
    +            sanityCheck(bundle, bundleId);
    +            LOG.info("Modified bundle {} {} {}", bundleIds(bundle));
    +        }
    +
    +        @Override
    +        public void removedBundle(Bundle bundle, BundleEvent bundleEvent, Long bundleId)
{
    +            sanityCheck(bundle, bundleId);
    +            LOG.info("Unloading catalog BOM from {} {} {}", bundleIds(bundle));
    +        }
    +
    +        private void scanForCatalog(Bundle bundle) {
    +            LOG.info("Scanning for catalog items in bundle {} {} {}", bundleIds(bundle));
    +            final URL bom = bundle.getResource(CATALOG_BOM_URL);
    +
    +            if (null != bom) {
    +                LOG.info("Found catalog BOM in {} {} {}", bundleIds(bundle));
    +                String bomText = readBom(bom);
    +                String bomWithLibraryPath = addLibraryDetails(bundle, bomText);
    +                for (CatalogItem<?, ?> item : getManagementContext().getCatalog().addItems(bomWithLibraryPath))
{
    +                    LOG.debug("Added to catalog: {}", item.getSymbolicName());
    +                }
    +            }
    +        }
    +
    +        private String addLibraryDetails(Bundle bundle, String bomText) {
    +            final Map<String, Object> bom = (Map<String, Object>)Iterables.getOnlyElement(Yamls.parseAll(bomText));
    +            final Object catalog = bom.get(BROOKLYN_CATALOG);
    +            if (null != catalog && catalog instanceof Map<?, ?>) {
    --- End diff --
    
    Log if not the case to easily catch typos?


---
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