View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.core.internal;
19  
20  import java.text.MessageFormat;
21  import java.util.concurrent.atomic.AtomicBoolean;
22  import org.apache.log4j.Level;
23  import org.apache.log4j.Logger;
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.osgi.framework.ServiceRegistration;
27  import at.ac.tuwien.infosys.sm4all.copal.api.util.BaseRegistry;
28  
29  /**
30   * The abstract class which can be used as base class for any registry that also
31   * wants to be registered as an OSGi service.
32   * 
33   * @param <K> the type of keys maintained by this registry.
34   * @param <V> the type of values maintained by this registry.
35   * @author sanjin
36   */
37  public abstract class RegistryService<K, V> extends BaseRegistry<K, V>
38          implements BundleActivator {
39  
40      private static final Logger LOGGER = Logger.getLogger(RegistryService.class);
41  
42      private final AtomicBoolean started = new AtomicBoolean(false);
43      private final String serviceName;
44      private ServiceRegistration registration;
45  
46      /**
47       * Creates an instance of the {@link RegistryService} that uses specified
48       * name as name when registering the OSGi service.
49       * 
50       * @param serviceName the name of this {@link RegistryService}.
51       */
52      protected RegistryService(final String serviceName) {
53          super();
54  
55          this.serviceName = serviceName;
56      }
57  
58      /**
59       * Returns if this {@link RegistryService} has been started by an OSGi
60       * framework.
61       * 
62       * @return if this {@link RegistryService} has been started by an OSGi
63       *         framework.
64       */
65      protected boolean isStarted() {
66          return this.started.get();
67      }
68  
69      @Override
70      public void start(final BundleContext bundleContext) {
71          if (!this.started.getAndSet(true)) {
72              this.registration = bundleContext.registerService(this.serviceName,
73                      this, null);
74              if (null == this.registration) {
75                  if (LOGGER.isEnabledFor(Level.ERROR)) {
76                      LOGGER.error(MessageFormat.format(
77                              "Failed to register service {0}!", this.serviceName));
78                  }
79              } else {
80                  if (LOGGER.isInfoEnabled()) {
81                      LOGGER.info(MessageFormat.format(
82                              "Successfully registered {0} service.",
83                              this.serviceName));
84                  }
85              }
86          }
87      }
88  
89      @Override
90      public void stop(final BundleContext bundleContext) {
91          if (this.started.getAndSet(false)) {
92              this.registration.unregister();
93              this.registration = null;
94  
95              unregisterAll();
96              detachAll();
97  
98              if (LOGGER.isInfoEnabled()) {
99                  LOGGER.info(MessageFormat.format(
100                         "Successfully unregistered {0} service.",
101                         this.serviceName));
102             }
103         }
104     }
105 
106     @Override
107     public void attach(final RegistryObserver<V> observer) {
108         if (this.started.get()) {
109             super.attach(observer);
110         }
111     }
112 
113     @Override
114     public void detach(final RegistryObserver<V> observer) {
115         if (this.started.get()) {
116             super.detach(observer);
117         }
118     }
119 }