994a88d815bc13ec76342d00035241094a805588
[mdsal.git] / binding / mdsal-binding-dom-codec-osgi / src / main / java / org / opendaylight / mdsal / binding / dom / codec / osgi / impl / Activator.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.osgi.impl;
9
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;
23
24 public final class Activator implements BundleActivator {
25     private final List<ServiceRegistration<?>> registrations = new ArrayList<>(2);
26
27     private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker;
28     private SimpleBindingRuntimeContextService service;
29
30     @Override
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();
34
35         service = new SimpleBindingRuntimeContextService(context, moduleInfoBackedContext, moduleInfoBackedContext);
36
37         final OsgiModuleInfoRegistry registry = new OsgiModuleInfoRegistry(moduleInfoBackedContext,
38                 moduleInfoBackedContext, service);
39
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();
45
46         service.open();
47         registrations.add(context.registerService(BindingRuntimeContextService.class, service, null));
48         registrations.add(context.registerService(ClassLoadingStrategy.class, moduleInfoBackedContext, null));
49     }
50
51     @Override
52     public void stop(final BundleContext context) {
53         moduleInfoResolvedBundleTracker.close();
54         service.close();
55         registrations.forEach(ServiceRegistration::unregister);
56         registrations.clear();
57     }
58 }