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.Collection;
22  import org.apache.log4j.Logger;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventType;
24  import at.ac.tuwien.infosys.sm4all.copal.api.service.ContextEventTypeRegistry;
25  import at.ac.tuwien.infosys.sm4all.copal.api.util.AlreadyRegisteredException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.util.NotRegisteredException;
27  
28  /**
29   * Implementation of the {@link ContextEventTypeRegistry} interface.
30   * 
31   * @author sanjin
32   */
33  public class ContextEventTypeRegistryImpl extends
34          RegistryService<String, ContextEventType> implements
35          ContextEventTypeRegistry {
36  
37      private static final Logger LOGGER = Logger.getLogger(ContextEventTypeRegistryImpl.class);
38  
39      /**
40       * Creates an instance of the {@link ContextEventTypeRegistry}
41       * implementation.
42       */
43      public ContextEventTypeRegistryImpl() {
44          super(ContextEventTypeRegistry.class.getName());
45      }
46  
47      /**
48       * Returns if this {@link ContextEventTypeRegistry} has been started by an
49       * OSGi framework.
50       * 
51       * @return if this {@link ContextEventTypeRegistry} has been started by an
52       *         OSGi framework.
53       */
54      @Override
55      public boolean isStarted() {
56          return super.isStarted();
57      }
58  
59      @Override
60      public void register(final ContextEventType eventType)
61              throws AlreadyRegisteredException {
62          if (null == eventType) {
63              throw new NullPointerException("Event type cannot be null.");
64          }
65  
66          super.register(eventType);
67  
68          if (LOGGER.isInfoEnabled()) {
69              LOGGER.info(MessageFormat.format(
70                      "Successfully registered event type ''{0}''.",
71                      eventType.getName()));
72          }
73      }
74  
75      @Override
76      public void unregister(final String name) throws NotRegisteredException {
77          if (null == name) {
78              throw new NullPointerException("Name cannot be null.");
79          }
80  
81          super.unregister(name);
82  
83          if (LOGGER.isInfoEnabled()) {
84              LOGGER.info(MessageFormat.format(
85                      "Successfully unregistered event type ''{0}''.", name));
86          }
87      }
88  
89      @Override
90      public ContextEventType[] getAll() {
91          final Collection<ContextEventType> result = getValues();
92  
93          return result.toArray(new ContextEventType[result.size()]);
94      }
95  
96      @Override
97      protected String getKey(final ContextEventType eventType) {
98          if (null == eventType) {
99              throw new NullPointerException("Event type cannot be null.");
100         }
101 
102         return eventType.getName();
103     }
104 
105     @Override
106     protected void unregisterAll() {
107         if (LOGGER.isDebugEnabled()) {
108             LOGGER.debug("Unregistering all event types.");
109         }
110 
111         super.unregisterAll();
112 
113         if (LOGGER.isInfoEnabled()) {
114             LOGGER.info("All event types unregistered!");
115         }
116     }
117 }