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.api.helpers;
19  
20  import java.text.MessageFormat;
21  import java.util.Arrays;
22  import java.util.concurrent.locks.ReadWriteLock;
23  import java.util.concurrent.locks.ReentrantReadWriteLock;
24  import org.apache.log4j.Logger;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventType;
26  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextEventTypeRegistry;
27  
28  /**
29   * This class is an OSGi Activator used to register and unregister
30   * {@link ContextEventType}s defined in specified {@link Context} as
31   * {@link ContextEventTypeRegistry} service, on which it depends, change its
32   * availability.
33   */
34  public class ContextActivator extends AbstractGenericActivator {
35  
36      private static final ContextEventType[] EMPTY_EVENT_TYPE_ARRAY = new ContextEventType[0];
37      private static final Logger LOGGER = Logger.getLogger(ContextActivator.class);
38  
39      private final ReadWriteLock lock = new ReentrantReadWriteLock();
40      private final Context context;
41      private ContextEventTypeRegistry eventTypeRegistry;
42      private ContextEventType[] eventTypes;
43  
44      /**
45       * Create instance of OSGI Activator which registers and unregistered
46       * {@link ContextEventType}s defined in specified {@link Context}.
47       * 
48       * @param context the context.
49       */
50      public ContextActivator(final Context context) {
51          super(ContextEventTypeRegistry.class.getName());
52          this.context = context;
53          this.eventTypes = EMPTY_EVENT_TYPE_ARRAY;
54      }
55  
56      /**
57       * @return all successfully registered {@link ContextEventType}s defined in
58       *         specified {@link Context}.
59       */
60      public ContextEventType[] getRegisteredEventTypes() {
61          final ContextEventType[] result;
62  
63          this.lock.readLock().lock();
64          try {
65              result = Arrays.copyOf(this.eventTypes, this.eventTypes.length);
66          } finally {
67              this.lock.readLock().unlock();
68          }
69  
70          return result;
71      }
72  
73      @Override
74      protected void start() {
75          this.lock.writeLock().lock();
76          try {
77              this.eventTypeRegistry = getDependency(ContextEventTypeRegistry.class.getName());
78              this.eventTypes = this.context.registerEventTypes(this.eventTypeRegistry);
79          } finally {
80              this.lock.writeLock().unlock();
81          }
82      }
83  
84      @Override
85      protected void stop() {
86          this.lock.writeLock().lock();
87          try {
88              if (this.eventTypeRegistry != null) {
89                  for (final ContextEventType eventType : this.eventTypes) {
90                      final String eventTypeName = eventType.getName();
91                      if (!this.eventTypeRegistry.unregister(eventTypeName))
92                          LOGGER.warn(MessageFormat.format(
93                                  "Could not unregister event type ''{0}''! Ignoring.",
94                                  eventTypeName));
95                  }
96                  this.eventTypes = EMPTY_EVENT_TYPE_ARRAY;
97                  this.eventTypeRegistry = null;
98              }
99          } finally {
100             this.lock.writeLock().unlock();
101         }
102     }
103 }