View Javadoc

1   /*
2    * Copyright (C) 2004-2006 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 program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program 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
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties.context.sensor;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import de.fu_berlin.ties.ProcessingException;
28  import de.fu_berlin.ties.TiesConfiguration;
29  import de.fu_berlin.ties.text.TokenizerFactory;
30  import de.fu_berlin.ties.util.Util;
31  
32  /***
33   * Abstract implementation of the
34   * {@link de.fu_berlin.ties.context.sensor.Sensor} interface that stores a
35   * configuration and provides a factory method to initialize a set of sensors.
36   *
37   * @author Christian Siefkes
38   * @version $Revision: 1.7 $, $Date: 2006/10/21 16:04:06 $, $Author: siefkes $
39   */
40  public abstract class BaseSensor implements Sensor {
41  
42      /***
43       * Factory method that initializes an array of sensors.
44       *
45       * @param classNames array of fully specified names of classes
46       * implementing the {@link Sensor} interface; each of them must provide
47       * a constructor that accepts a {@link TiesConfiguration} as single argument
48       * @param conf used to configure the sensors
49       * @return an array of newly created sensors
50       * @throws ProcessingException if an error occurred while creating the
51       * classifier
52       */
53      public static Sensor[] createSensors(final String[] classNames,
54              final TiesConfiguration conf) throws ProcessingException {
55          final Sensor[] result = new Sensor[classNames.length];
56  
57          // use the configuration as single constructor parameter
58          final Object[] params = new Object[] {conf};
59  
60          for (int i = 0; i < result.length; i++) {
61              try {
62                  result[i] = (Sensor) Util.createObject(
63                      Class.forName(classNames[i]), params,
64                      TiesConfiguration.class);
65              } catch (ClassNotFoundException cnfe) {
66                  // convert and rethrow exception
67                  throw new ProcessingException(
68                      "Cannot initialize sensor " + classNames[i],
69                      cnfe);
70              } catch (InstantiationException ie) {
71                  // convert and rethrow exception
72                  throw new ProcessingException(
73                      "Cannot initialize sensor " + classNames[i],
74                      ie);
75              }
76          }
77  
78          return result;
79      }
80  
81  
82      /***
83       * The configuration used by this instance.
84       */
85      private final TiesConfiguration config;
86  
87      /***
88       * Creates a new instance.
89       * @param conf the configuration to use
90       */
91      public BaseSensor(final TiesConfiguration conf) {
92          super();
93          config = conf;
94      }
95  
96      /***
97       * Returns the configuration used by this instance.
98       * @return the configuration
99       */
100     public TiesConfiguration getConfig() {
101         return config;
102     }
103 
104     /***
105      * {@inheritDoc} The default implementation does nothing.
106      */
107     public void initDocument(final File filename,
108             final TokenizerFactory tFactory)
109     throws ProcessingException, IOException {
110         // default implementation does nothing
111     }
112 
113 }