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