14138ee9b688f8e6c153772265aa9e0bba842ab5
[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.List;
12 import org.opendaylight.mdsal.binding.dom.codec.osgi.BindingRuntimeContextService;
13 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
14 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
15 import org.osgi.framework.BundleActivator;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceRegistration;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class Activator implements BundleActivator {
22     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
23
24     private final List<ServiceRegistration<?>> registrations = new ArrayList<>(2);
25
26     private ModuleInfoBundleTracker bundleTracker = null;
27     private SimpleBindingRuntimeContextService service = null;
28
29     @Override
30     public void start(final BundleContext context) {
31         LOG.info("Binding-DOM codec starting");
32
33         // XXX: this will use thread-context class loader, which is probably appropriate
34         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
35
36         service = new SimpleBindingRuntimeContextService(context, moduleInfoBackedContext, moduleInfoBackedContext);
37
38         final OsgiModuleInfoRegistry registry = new OsgiModuleInfoRegistry(moduleInfoBackedContext,
39                 moduleInfoBackedContext, service);
40
41         LOG.debug("Starting Binding-DOM codec bundle tracker");
42         bundleTracker = new ModuleInfoBundleTracker(context, registry);
43         bundleTracker.open();
44
45         LOG.debug("Starting Binding-DOM runtime context service");
46         service.open();
47
48         LOG.debug("Registering Binding-DOM codec services");
49         registrations.add(context.registerService(BindingRuntimeContextService.class, service, null));
50         registrations.add(context.registerService(ClassLoadingStrategy.class, moduleInfoBackedContext, null));
51
52         LOG.info("Binding-DOM codec started");
53     }
54
55     @Override
56     public void stop(final BundleContext context) {
57         LOG.info("Binding-DOM codec stopping");
58
59         LOG.debug("Unregistering Binding-DOM codec services");
60         registrations.forEach(ServiceRegistration::unregister);
61         registrations.clear();
62
63         LOG.debug("Stopping Binding-DOM codec bundle tracker");
64         bundleTracker.close();
65
66         LOG.debug("Stoping Binding-DOM runtime context service");
67         service.close();
68
69         LOG.info("Binding-DOM codec stopped");
70     }
71 }