fd24944018c69b3b9480a7018e0fea1c23986719
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / config / yang / md / sal / dom / impl / SchemaServiceImplSingletonModule.java
1 /*
2  * Copyright (c) 2014 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.yang.md.sal.dom.impl;
9
10 import org.opendaylight.controller.sal.core.api.model.SchemaService;
11 import org.opendaylight.controller.sal.dom.broker.GlobalBundleScanningSchemaServiceImpl;
12 import org.opendaylight.yangtools.concepts.Delegator;
13 import org.opendaylight.yangtools.concepts.ListenerRegistration;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceReference;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public final class SchemaServiceImplSingletonModule extends
23 org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractSchemaServiceImplSingletonModule {
24
25     private static final Logger LOG = LoggerFactory.getLogger(SchemaServiceImplSingletonModule.class);
26
27     BundleContext bundleContext;
28
29     public SchemaServiceImplSingletonModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
30             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
31         super(identifier, dependencyResolver);
32     }
33
34     public SchemaServiceImplSingletonModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
35             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
36             final SchemaServiceImplSingletonModule oldModule, final java.lang.AutoCloseable oldInstance) {
37         super(identifier, dependencyResolver, oldModule, oldInstance);
38     }
39
40     @Override
41     public boolean canReuseInstance(final AbstractSchemaServiceImplSingletonModule oldModule) {
42         return true;
43     }
44
45     public BundleContext getBundleContext() {
46         return bundleContext;
47     }
48
49     public void setBundleContext(final BundleContext bundleContext) {
50         this.bundleContext = bundleContext;
51     }
52
53     @Override
54     public void validate() {
55         super.validate();
56     }
57
58     @Override
59     public java.lang.AutoCloseable createInstance() {
60         ServiceReference<SchemaService> ref = getBundleContext().getServiceReference(SchemaService.class);
61         if (ref != null) {
62             return new GlobalSchemaServiceProxy(getBundleContext(), ref);
63         }
64
65         GlobalBundleScanningSchemaServiceImpl newInstance = new GlobalBundleScanningSchemaServiceImpl(getBundleContext());
66         newInstance.start();
67         return newInstance;
68     }
69
70     public class GlobalSchemaServiceProxy implements AutoCloseable, SchemaService, Delegator<SchemaService> {
71
72         private BundleContext bundleContext;
73         private ServiceReference<SchemaService> reference;
74         private SchemaService delegate;
75
76         public GlobalSchemaServiceProxy(final BundleContext bundleContext, final ServiceReference<SchemaService> ref) {
77             this.bundleContext = bundleContext;
78             this.reference = ref;
79             this.delegate = bundleContext.getService(reference);
80         }
81
82         @Override
83         public void close() throws Exception {
84             if (delegate != null) {
85                 delegate = null;
86
87                 try {
88                     bundleContext.ungetService(reference);
89                 } catch (IllegalStateException e) {
90                     // Indicates the service was already unregistered which can happen normally
91                     // on shutdown.
92                     LOG.debug( "Error unregistering service", e );
93                 }
94
95                 reference = null;
96                 bundleContext = null;
97             }
98         }
99
100         @Override
101         public void addModule(final Module arg0) {
102             delegate.addModule(arg0);
103         }
104
105         @Override
106         public SchemaContext getGlobalContext() {
107             return delegate.getGlobalContext();
108         }
109
110         @Override
111         public SchemaContext getSessionContext() {
112             return delegate.getSessionContext();
113         }
114
115         @Override
116         public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(final SchemaServiceListener arg0) {
117             return delegate.registerSchemaServiceListener(arg0);
118         }
119
120         @Override
121         public void removeModule(final Module arg0) {
122             delegate.removeModule(arg0);
123         }
124
125         @Override
126         public SchemaService getDelegate() {
127             return delegate;
128         }
129
130     }
131 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.