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