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 java.util.LinkedList;
23  import java.util.List;
24  import org.apache.log4j.Logger;
25  import at.ac.tuwien.infosys.sm4all.copal.api.publisher.ContextPublisher;
26  import at.ac.tuwien.infosys.sm4all.copal.api.service.ContextPublisherRegistry;
27  import at.ac.tuwien.infosys.sm4all.copal.api.util.AlreadyRegisteredException;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.NotRegisteredException;
29  
30  /**
31   * Implementation of the {@link ContextPublisherRegistry} interface.
32   * 
33   * @author fei
34   * @author sanjin
35   */
36  public class ContextPublisherRegistryImpl extends
37          RegistryService<String, ContextPublisher> implements
38          ContextPublisherRegistry {
39  
40      private static final Logger LOGGER = Logger.getLogger(ContextPublisherRegistryImpl.class);
41  
42      /**
43       * Creates an instance of the {@link ContextPublisherRegistry}
44       * implementation.
45       */
46      public ContextPublisherRegistryImpl() {
47          super(ContextPublisherRegistry.class.getName());
48      }
49  
50      /**
51       * Returns if this {@link ContextPublisherRegistry} has been started by an
52       * OSGi framework.
53       * 
54       * @return if this {@link ContextPublisherRegistry} has been started by an
55       *         OSGi framework.
56       */
57      @Override
58      public boolean isStarted() {
59          return super.isStarted();
60      }
61  
62      @Override
63      public void register(final ContextPublisher publisher)
64              throws AlreadyRegisteredException {
65          if (null == publisher) {
66              throw new NullPointerException("Publisher cannot be null.");
67          }
68  
69          super.register(publisher);
70  
71          if (LOGGER.isInfoEnabled()) {
72              LOGGER.info(MessageFormat.format(
73                      "Successfully registered publisher ''{0}''.",
74                      publisher.getSourceID()));
75          }
76      }
77  
78      @Override
79      public void unregister(final String sourceID) throws NotRegisteredException {
80          if (null == sourceID) {
81              throw new NullPointerException("Source ID cannot be null.");
82          }
83  
84          super.unregister(sourceID);
85  
86          if (LOGGER.isInfoEnabled()) {
87              LOGGER.info(MessageFormat.format(
88                      "Successfully unregistered publisher ''{0}''.", sourceID));
89          }
90      }
91  
92      @Override
93      public ContextPublisher[] getAll() {
94          final Collection<ContextPublisher> result = getValues();
95  
96          return result.toArray(new ContextPublisher[result.size()]);
97      }
98  
99      @Override
100     public ContextPublisher[] getAll(final String eventType) {
101         final List<ContextPublisher> result = new LinkedList<ContextPublisher>();
102 
103         for (final ContextPublisher publisher : getValues()) {
104             if (contains(publisher.getPublishedTypes(), eventType)) {
105                 result.add(publisher);
106             }
107         }
108 
109         return result.toArray(new ContextPublisher[result.size()]);
110     }
111 
112     @Override
113     protected String getKey(final ContextPublisher publisher) {
114         if (null == publisher) {
115             throw new NullPointerException("Publisher cannot be null.");
116         }
117 
118         return publisher.getSourceID();
119     }
120 
121     @Override
122     protected void unregisterAll() {
123         if (LOGGER.isDebugEnabled()) {
124             LOGGER.debug("Unregistering all context publishers.");
125         }
126 
127         super.unregisterAll();
128 
129         if (LOGGER.isInfoEnabled()) {
130             LOGGER.info("All context publishers unregistered!");
131         }
132     }
133 
134     private static <T> boolean contains(final T[] array, final T object) {
135         boolean result = false;
136 
137         for (final T element : array) {
138             if (object.equals(element)) {
139                 result = true;
140                 break;
141             }
142         }
143 
144         return result;
145     }
146 }