View Javadoc

1   /*
2    * Copyright (C) 2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21   */
22  package de.fu_berlin.ties.context.sensor;
23  
24  import de.fu_berlin.ties.ProcessingException;
25  import de.fu_berlin.ties.TiesConfiguration;
26  import de.fu_berlin.ties.util.Util;
27  
28  /***
29   * Abstract implementation of the
30   * {@link de.fu_berlin.ties.context.sensor.Sensor} interface that stores a
31   * configuration and provides a factory method to initialize a set of sensors.
32   *
33   * @author Christian Siefkes
34   * @version $Revision: 1.2 $, $Date: 2004/07/08 08:45:33 $, $Author: siefkes $
35   */
36  public abstract class BaseSensor implements Sensor {
37  
38      /***
39       * Factory method that initializes an array of sensors.
40       *
41       * @param classNames array of fully specified names of classes
42       * implementing the {@link Sensor} interface; each of them must provide
43       * a constructor that accepts a {@link TiesConfiguration} as single argument
44       * @param conf used to configure the sensors
45       * @return an array of newly created sensors
46       * @throws ProcessingException if an error occurred while creating the
47       * classifier
48       */
49      public static Sensor[] createSensors(final String[] classNames,
50              final TiesConfiguration conf) throws ProcessingException {
51          final Sensor[] result = new Sensor[classNames.length];
52  
53          // use the configuration as single constructor parameter
54          final Object[] params = new Object[] {conf};
55  
56          for (int i = 0; i < result.length; i++) {
57              try {
58                  result[i] = (Sensor) Util.createObject(
59                      Class.forName(classNames[i]), params,
60                      TiesConfiguration.class);
61              } catch (ClassNotFoundException cnfe) {
62                  // convert and rethrow exception
63                  throw new ProcessingException(
64                      "Cannot initialize sensor " + classNames[i],
65                      cnfe);
66              } catch (InstantiationException ie) {
67                  // convert and rethrow exception
68                  throw new ProcessingException(
69                      "Cannot initialize sensor " + classNames[i],
70                      ie);
71              }
72          }
73  
74          return result;
75      }
76  
77  
78      /***
79       * The configuration used by this instance.
80       */
81      private final TiesConfiguration config;
82  
83      /***
84       * Creates a new instance.
85       * @param conf the configuration to use
86       */
87      public BaseSensor(final TiesConfiguration conf) {
88          super();
89          config = conf;
90      }
91  
92      /***
93       * Returns the configuration used by this instance.
94       * @return the configuration
95       */
96      public TiesConfiguration getConfig() {
97          return config;
98      }
99  
100 }