Merge "Remove unnecessary warn log from config subsystem"
[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
20 /**
21 *
22 */
23 public final class SchemaServiceImplSingletonModule extends
24         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractSchemaServiceImplSingletonModule {
25
26     BundleContext bundleContext;
27
28     public SchemaServiceImplSingletonModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
29             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
30         super(identifier, dependencyResolver);
31     }
32
33     public SchemaServiceImplSingletonModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
34             org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
35             SchemaServiceImplSingletonModule oldModule, java.lang.AutoCloseable oldInstance) {
36         super(identifier, dependencyResolver, oldModule, oldInstance);
37     }
38
39     @Override
40     public boolean canReuseInstance(AbstractSchemaServiceImplSingletonModule oldModule) {
41         return true;
42     }
43
44     public BundleContext getBundleContext() {
45         return bundleContext;
46     }
47
48     public void setBundleContext(BundleContext bundleContext) {
49         this.bundleContext = bundleContext;
50     }
51
52     @Override
53     public void validate() {
54         super.validate();
55     }
56
57     @Override
58     public java.lang.AutoCloseable createInstance() {
59         ServiceReference<SchemaService> ref = getBundleContext().getServiceReference(SchemaService.class);
60         if (ref != null) {
61             return new GlobalSchemaServiceProxy(getBundleContext(), ref);
62         }
63
64         GlobalBundleScanningSchemaServiceImpl newInstance = new GlobalBundleScanningSchemaServiceImpl();
65         newInstance.setContext(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(BundleContext bundleContext, 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                 bundleContext.ungetService(reference);
87                 reference = null;
88                 bundleContext = null;
89             }
90         }
91
92         public void addModule(Module arg0) {
93             delegate.addModule(arg0);
94         }
95
96         public SchemaContext getGlobalContext() {
97             return delegate.getGlobalContext();
98         }
99
100         public SchemaContext getSessionContext() {
101             return delegate.getSessionContext();
102         }
103
104         public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener arg0) {
105             return delegate.registerSchemaServiceListener(arg0);
106         }
107
108         public void removeModule(Module arg0) {
109             delegate.removeModule(arg0);
110         }
111
112         @Override
113         public SchemaService getDelegate() {
114             return delegate;
115         }
116
117     }
118 }