2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.codec.osgi.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.mdsal.binding.dom.codec.osgi.BindingRuntimeContextService;
14 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
15 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
16 import org.opendaylight.yangtools.concepts.ObjectRegistration;
17 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.BundleTracker;
24 public final class Activator implements BundleActivator {
25 private final List<ServiceRegistration<?>> registrations = new ArrayList<>(2);
27 private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker;
28 private SimpleBindingRuntimeContextService service;
31 public void start(final BundleContext context) {
32 // XXX: this will use thread-context class loader, which is probably appropriate
33 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
35 service = new SimpleBindingRuntimeContextService(context, moduleInfoBackedContext, moduleInfoBackedContext);
37 final OsgiModuleInfoRegistry registry = new OsgiModuleInfoRegistry(moduleInfoBackedContext,
38 moduleInfoBackedContext, service);
40 final ModuleInfoBundleTracker moduleInfoTracker = new ModuleInfoBundleTracker(registry);
41 moduleInfoResolvedBundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING
42 | Bundle.STOPPING | Bundle.ACTIVE, moduleInfoTracker);
43 moduleInfoResolvedBundleTracker.open();
44 moduleInfoTracker.finishStart();
47 registrations.add(context.registerService(BindingRuntimeContextService.class, service, null));
48 registrations.add(context.registerService(ClassLoadingStrategy.class, moduleInfoBackedContext, null));
52 public void stop(final BundleContext context) {
53 moduleInfoResolvedBundleTracker.close();
55 registrations.forEach(ServiceRegistration::unregister);
56 registrations.clear();