d449e28dfe9b8a1268dfd40240932e7df6018b03
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dependencyresolver / DestroyedModule.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.config.manager.impl.dependencyresolver;
9
10 import org.opendaylight.controller.config.api.ModuleIdentifier;
11 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
12 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
13 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Transfer object representing already committed module that needs to be
20  * destroyed. Implements comparable in order to preserve order in which modules
21  * were created. Module instances should be closed in order defined by the
22  * compareTo method.
23  */
24 public class DestroyedModule implements AutoCloseable,
25         Comparable<DestroyedModule>, Identifiable<ModuleIdentifier> {
26     private static final Logger LOG = LoggerFactory
27             .getLogger(DestroyedModule.class);
28
29     private final ModuleIdentifier identifier;
30     private final AutoCloseable instance;
31     private final ModuleJMXRegistrator oldJMXRegistrator;
32     private final OsgiRegistration osgiRegistration;
33     private final int orderingIdx;
34     private RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
35
36     public DestroyedModule(final ModuleIdentifier identifier, final AutoCloseable instance,
37                            final ModuleJMXRegistrator oldJMXRegistrator,
38                            final OsgiRegistration osgiRegistration, final int orderingIdx,
39                            final RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator) {
40         this.identifier = identifier;
41         this.instance = instance;
42         this.oldJMXRegistrator = oldJMXRegistrator;
43         this.osgiRegistration = osgiRegistration;
44         this.orderingIdx = orderingIdx;
45         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
46     }
47
48     @Override
49     public void close() {
50         LOG.trace("Destroying {}", identifier);
51         try {
52             instance.close();
53         } catch (final Exception e) {
54             LOG.error("Error while closing instance of {}", identifier, e);
55         }
56         try {
57             oldJMXRegistrator.close();
58         } catch (final Exception e) {
59             LOG.error("Error while closing jmx registrator of {}", identifier, e);
60         }
61         try {
62             if (runtimeBeanRegistrator != null) {
63                 runtimeBeanRegistrator.close();
64             }
65         } catch (final Exception e) {
66             LOG.error("Error while closing runtime bean jmx registrator of {}", identifier, e);
67         }
68         try {
69             osgiRegistration.close();
70         } catch (final Exception e) {
71             LOG.error("Error while closing osgi registration of {}", identifier, e);
72         }
73     }
74
75     @Override
76     public int compareTo(final DestroyedModule o) {
77         return Integer.compare(orderingIdx, o.orderingIdx);
78     }
79
80     @Override
81     public ModuleIdentifier getIdentifier() {
82         return identifier;
83     }
84 }